Ver código fonte

3.0.3_fix_position_check

skyffire 1 ano atrás
pai
commit
220d8fcb5a
4 arquivos alterados com 24 adições e 17 exclusões
  1. 2 0
      .gitignore
  2. 1 1
      Cargo.toml
  3. 1 0
      src/main.rs
  4. 20 16
      strategy/src/core.rs

+ 2 - 0
.gitignore

@@ -7,3 +7,5 @@ config.toml*
 *.log.*
 /logs*
 /test_account.toml
+
+config.json

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "as-rust"
-version = "3.0.0"
+version = "3.0.3"
 edition = "2021"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

+ 1 - 0
src/main.rs

@@ -45,6 +45,7 @@ async fn main() {
     let params = read_params();
     // 日志级别配置
     let _guard = log_level_init(params.log_level.clone(), params.port.clone(), params.account_name.clone());
+    info!("--------------------------------程序开始执行-----------------------------");
     info!("配置读取成功:{:?}。", params);
     // 主进程控制
     let running = Arc::new(AtomicBool::new(true));

+ 20 - 16
strategy/src/core.rs

@@ -1148,14 +1148,14 @@ impl Core {
 
     // #[instrument(skip(self, target_hold_coin), level="TRACE")]
     pub async fn check_position(&mut self, target_hold_coin: Decimal) -> bool {
-        let mut is_clear = false;
 
         info!("------------------------------------------------------------------------------------------------------------");
         info!("步骤一:检查挂单:");
+        let mut is_order_clear = false;
         match self.platform_rest.cancel_orders_all().await {
             Ok(val) => {
                 let length = val.len();
-                is_clear = length == 0;
+                is_order_clear = length == 0;
 
                 info!("已清空所有挂单({}条)", length);
 
@@ -1169,7 +1169,7 @@ impl Core {
                 match self.platform_rest.cancel_orders().await {
                     Ok(val) => {
                         let length = val.len();
-                        is_clear = length == 0;
+                        is_order_clear = length == 0;
 
                         info!("清空所有挂单({}条):{:?}", length, val);
                     }
@@ -1183,17 +1183,18 @@ impl Core {
         info!("");
 
         info!("步骤二:检查仓位:");
+        let is_position_clear;
         if self.exchange.contains("spot") { // 现货
-            is_clear = is_clear && (self.check_position_spot(target_hold_coin.clone()).await == 0);
+            is_position_clear = self.check_position_spot(target_hold_coin.clone()).await == 0;
             info!("检查遗漏仓位(现货),目标持仓:{}USDT", target_hold_coin);
         } else { // 合约
-            is_clear = is_clear && (self.check_position_swap().await == 0);
+            is_position_clear = self.check_position_swap().await == 0;
             info!("遗漏仓位检查完毕(合约)!");
         }
         info!("------------------------------------------------------------------------------------------------------------");
         info!("");
 
-        return is_clear;
+        return is_order_clear && is_position_clear;
     }
 
     // #[instrument(skip(self, target_hold_coin), level="TRACE")]
@@ -1447,15 +1448,7 @@ impl Core {
         info!("-------------------------启动退出流程----------------------------");
         info!("");
 
-        // 循环清空仓位,如若彻底清空,才进行退出。
-        let mut clear_count = 1;
-        while !self.check_position(Decimal::ZERO).await {
-            sleep(Duration::from_secs(1)).await;
-
-            clear_count += 1;
-            info!("清理指令发送完毕,启动第{}次检查。", clear_count);
-            info!("");
-        }
+        self.clear_position_and_orders(Decimal::ZERO).await;
 
         info!("订单、仓位清除完毕,为避免api失效导致遗漏仓位,建议人工复查。");
         info!("停机原因:{}。", self.exit_msg);
@@ -1562,7 +1555,7 @@ impl Core {
         }
 
         // 清空挂单和仓位
-        self.check_position(self.hold_coin).await;
+        self.clear_position_and_orders(self.hold_coin).await;
         /*
         ###### 交易前准备就绪 可以开始交易 ######
         self.loop.create_task(self.rest.go())
@@ -1573,6 +1566,17 @@ impl Core {
         */
         return true;
     }
+
+    pub async fn clear_position_and_orders(&mut self, target_hold_coin: Decimal) {
+        let mut clear_count = 1;
+        while !self.check_position(target_hold_coin).await {
+            sleep(Duration::from_secs(1)).await;
+
+            clear_count += 1;
+            info!("清理指令发送完毕,启动第{}次检查。", clear_count);
+            info!("");
+        }
+    }
 }
 
 // #[instrument(skip(core_arc), level="TRACE")]