Jelajahi Sumber

初始余额的获取修正。

skyfffire 2 tahun lalu
induk
melakukan
d74f80d4ce
1 mengubah file dengan 55 tambahan dan 16 penghapusan
  1. 55 16
      strategy/src/quant.rs

+ 55 - 16
strategy/src/quant.rs

@@ -804,14 +804,14 @@ impl Quant {
         self.local_cash = data.balance * self.used_pct
     }
 
-    pub async fn update_equity_rest(&mut self) {
+    pub async fn update_equity_rest_swap(&mut self) {
         match self.platform_rest.get_account().await {
             Ok(val) => {
                 /*
-               更新保证金信息
-               合约一直更新
-               现货只有当出现异常时更新
-           */
+                   更新保证金信息
+                   合约一直更新
+                   现货只有当出现异常时更新
+               */
                 if self.exchange.contains("spot") {
                     return;
                 }
@@ -823,6 +823,34 @@ impl Quant {
         }
     }
 
+    pub async fn update_equity_rest_spot(&mut self) {
+        match self.platform_rest.get_spot_account().await {
+            Ok(mut val) => {
+                // 如果返回的数组里没有交易货币,则补充交易货币
+                if !val.iter().any(|a| a.coin.to_uppercase().eq(&self.base.to_uppercase())) {
+                    let mut base_coin_account = Account::new();
+                    base_coin_account.coin = self.base.to_uppercase();
+                    val.push(base_coin_account);
+                }
+
+                for account in val {
+                    // 交易货币
+                    if self.base.to_uppercase() == account.coin {
+                        self.local_coin = account.balance;
+                    }
+                    // 本位货币
+                    if self.quote.to_uppercase() == account.coin {
+                        self.local_cash = account.balance;
+                    }
+                }
+
+            },
+            Err(err) => {
+                error!("获取仓位信息异常: {}", err);
+            }
+        }
+    }
+
     pub async fn check_risk(&mut self) {
         // 参数检查的风控
         if self.strategy.start_cash == Decimal::ZERO {
@@ -1061,7 +1089,15 @@ impl Quant {
     pub async fn check_position_spot(&mut self){
         info!("检查遗漏仓位!");
         match self.platform_rest.get_spot_account().await {
-            Ok(val) => {
+            Ok(mut val) => {
+                // 如果返回的数组里没有交易货币,则补充交易货币
+                if !val.iter().any(|a| a.coin.to_uppercase().eq(&self.base.to_uppercase())) {
+                    let mut base_coin_account = Account::new();
+                    base_coin_account.coin = self.base.to_uppercase();
+                    val.push(base_coin_account);
+                }
+
+                // 仓位补货、卖货
                 for account in val {
                     let coin_name = account.coin.to_uppercase();
                     if check_coin(&self.exchange, &coin_name){
@@ -1088,7 +1124,6 @@ impl Quant {
                     }
                     let coin_value = account.balance * mp;
                     let diff = (_hold_coin - coin_value)* Decimal::from_str("0.99").unwrap();
-                    info!("需要调整现货仓位 {} usdt", diff);
                     let side;
                     let price= Decimal::ZERO;
                     let amount;
@@ -1096,21 +1131,22 @@ impl Quant {
                         side = "kd";
                         // price = mp*1.001;
                         amount = diff/mp;
-                    } else if diff < Decimal::from(-20) {
+                    } else if diff < Decimal::from(-10) {
                         side = "kk";
                         // price = mp*0.999;
                         amount = -diff/mp;
                     } else {
                         continue;
                     }
+                    info!("{}, 需要调整现货仓位 {} usdt", symbol, diff);
                     // 价格0,市价单
                     match self.platform_rest.take_order_symbol(symbol.clone(), Decimal::ONE, "t-123", side, price, amount).await{
                         Ok(v)=>{
-                            info!("{} 清仓下单,{:?}", symbol, v);
+                            info!("side: {}, {} 下单,{:?}", side, symbol, v);
                             // 执行完当前币对  结束循环
                             continue;
                         },Err(ex)=>{
-                            error!("{} 清仓下单异常:{}", symbol, ex);
+                            error!("side: {}, {} {}", side, symbol, ex);
                             // 执行完当前币对  结束循环
                             continue;
                         }
@@ -1246,12 +1282,16 @@ impl Quant {
         let ticker = self.platform_rest.get_ticker().await.expect("获取价格信息异常!");
         let mp = (ticker.buy + ticker.sell) / Decimal::TWO;
         // 获取账户信息
-        self.update_equity_rest().await;
+        if self.exchange.contains("spot") {
+            self.update_equity_rest_spot().await;
+        } else {
+            self.update_equity_rest_swap().await;
+        }
         // 初始资金
         let start_cash = self.local_cash.clone();
         let start_coin = self.local_coin.clone();
         if start_cash.is_zero() && start_coin.is_zero() {
-            self.exit_msg = format!("{}{}{}{}", "初始为零 cash: ", start_cash, " coin: ", start_coin);
+            self.exit_msg = format!("{}{}{}{}", "初始余额为零 cash: ", start_cash, " coin: ", start_coin);
             // 停止程序
             self.stop().await;
             return false;
@@ -1320,11 +1360,10 @@ impl Quant {
         self.local_coin = start_coin;
 
         // 买入平台币
-        if self.exchange.contains("spot"){ // 现货
+        if self.exchange.contains("spot") { // 现货
             self.buy_token().await;
         }
 
-
         // 清空挂单和仓位
         self.check_position().await;
         /*
@@ -1446,7 +1485,7 @@ pub fn on_timer(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()> {
 pub fn check_coin(exchanges :&String, coin_name: &String) -> bool{
     let mut result = false;
     match exchanges.as_str() {
-        "bitget_usdt_spot" => {
+        "bitget_spot" => {
             result = ["BGB", "USDT"].contains(&coin_name.as_str());
         }
         _ => {}
@@ -1457,7 +1496,7 @@ pub fn check_coin(exchanges :&String, coin_name: &String) -> bool{
 //获取平台币
 pub fn get_exchange_token(exchanges :&String) -> TokenParam{
     return match exchanges.as_str() {
-        "bitget_usdt_spot" => {
+        "bitget_spot" => {
             TokenParam{
                 token: "BGB".to_string(),
                 // 30u