Sfoglia il codice sorgente

取消一些不必要变量

skyfffire 2 settimane fa
parent
commit
8bf3f19fc7
1 ha cambiato i file con 33 aggiunte e 36 eliminazioni
  1. 33 36
      src/strategy.rs

+ 33 - 36
src/strategy.rs

@@ -18,15 +18,12 @@ pub enum StrategyState {
     WaitingLimitOrder {             // 等待限价单成交
         order_id: String,
         price: Decimal,
-        last_order_time: Instant,
     },
     CancellingOrder {               // 撤单中
         order_id: String,
         price: Decimal,
-        last_order_time: Instant,
     },
     ExecutingMarketOrder {          // 执行市价单
-        last_order_time: Instant,
     },
 }
 
@@ -35,9 +32,14 @@ pub struct Strategy {
     state: StrategyState,
     order_quantity: Decimal,                        // 写死的订单数量
     filled_quantity: Decimal,                       // 成交数量
+    
+    rest_client: Arc<Mutex<ExtendedRestClient>>,    // rest客户端
+    
     min_order_interval_ms: u128,                    // 最小下单间隔(毫秒)
     min_query_order_interval_ms: u128,              // 最小查单间隔(毫秒)
-    rest_client: Arc<Mutex<ExtendedRestClient>>,    // rest客户端
+    
+    last_order_time: Instant,
+    last_query_order_time: Instant,
 }
 
 impl Strategy {
@@ -46,9 +48,14 @@ impl Strategy {
             state: StrategyState::Idle,
             order_quantity: dec!(0.0001),
             filled_quantity: Decimal::ZERO,
+            
+            rest_client: client_am,
+            
             min_order_interval_ms: 200,
             min_query_order_interval_ms: 200,
-            rest_client: client_am,
+            
+            last_order_time: Instant::now(),
+            last_query_order_time: Instant::now(),
         }
     }
 
@@ -59,18 +66,18 @@ impl Strategy {
                     warn!("空闲状态处理失败: {}", e);
                 }
             }
-            StrategyState::WaitingLimitOrder { order_id, price, last_order_time } => {
-                if let Err(e) = self.handle_waiting_limit_order(dm, order_id, price, last_order_time).await {
+            StrategyState::WaitingLimitOrder { order_id, price } => {
+                if let Err(e) = self.handle_waiting_limit_order(dm, order_id, price).await {
                     warn!("等待限价单状态处理失败: {}", e);
                 }
             }
-            StrategyState::CancellingOrder { order_id, price, last_order_time } => {
-                if let Err(e) = self.handle_cancelling_order(dm, price, order_id, last_order_time).await {
+            StrategyState::CancellingOrder { order_id, price } => {
+                if let Err(e) = self.handle_cancelling_order(dm, price, order_id).await {
                     warn!("撤单状态处理失败: {}", e);
                 }
             }
-            StrategyState::ExecutingMarketOrder { last_order_time } => {
-                if let Err(e) = self.handle_executing_market_order(dm, last_order_time).await {
+            StrategyState::ExecutingMarketOrder {  } => {
+                if let Err(e) = self.handle_executing_market_order(dm).await {
                     warn!("市价单执行状态处理失败: {}", e);
                 }
             }
@@ -84,12 +91,9 @@ impl Strategy {
         info!("============================ 进入空闲状态,准备下买单 ===========================");
 
         // 检查是否满足下单时间间隔
-        if let Some(last_time) = self.get_last_order_time() {
-            let elapsed = last_time.elapsed().as_millis();
-            if elapsed < self.min_order_interval_ms {
-                info!("距离上次下单时间不足{}ms,等待中...", self.min_order_interval_ms);
-                return Ok(());
-            }
+        let elapsed = self.last_order_time.elapsed().as_millis();
+        if elapsed < self.min_order_interval_ms {
+            return Ok(());
         }
 
         // 尝试下单,只有成功才转换状态
@@ -98,9 +102,12 @@ impl Strategy {
                 info!("限价买单下单成功,订单ID: {}", order_id);
                 self.state = StrategyState::WaitingLimitOrder {
                     order_id,
-                    price: dm.best_bid,
-                    last_order_time: Instant::now(),
+                    price: dm.best_bid
                 };
+                
+                self.last_order_time = Instant::now();
+                self.last_query_order_time = self.last_order_time;
+                
                 Ok(())
             }
             Err(e) => {
@@ -116,20 +123,20 @@ impl Strategy {
         dm: &DataManager,
         order_id: String,
         price: Decimal,
-        last_order_time: Instant,
     ) -> Result<()> {
         // info!("等待限价单成交,订单ID: {}, 价格: {}", order_id, price);
         // 下单之后过一会再检查订单
-        let elapsed = last_order_time.elapsed().as_millis();
+        let elapsed = self.last_query_order_time.elapsed().as_millis();
         if elapsed < self.min_query_order_interval_ms {
             return Ok(());
         }
+        self.last_query_order_time = Instant::now();
 
         // 检查订单是否已成交
         match self.check_order_filled(&order_id).await {
             Ok(true) => {
                 info!("限价单已成交,准备执行市价单");
-                self.state = StrategyState::ExecutingMarketOrder { last_order_time };
+                self.state = StrategyState::ExecutingMarketOrder { };
                 return Ok(());
             }
             Ok(false) => {
@@ -147,7 +154,6 @@ impl Strategy {
             self.state = StrategyState::CancellingOrder {
                 order_id,
                 price,
-                last_order_time,
             };
         }
 
@@ -160,7 +166,6 @@ impl Strategy {
         _dm: &DataManager,
         _price: Decimal,
         order_id: String,
-        last_order_time: Instant,
     ) -> Result<()> {
         info!("撤单中,订单ID: {}", order_id);
 
@@ -177,7 +182,7 @@ impl Strategy {
         match self.check_order_partially_filled(&order_id).await {
             Ok(true) => {
                 info!("撤单后发现有成交,准备执行市价单");
-                self.state = StrategyState::ExecutingMarketOrder { last_order_time };
+                self.state = StrategyState::ExecutingMarketOrder { };
                 Ok(())
             }
             Ok(false) => {
@@ -196,7 +201,6 @@ impl Strategy {
     async fn handle_executing_market_order(
         &mut self,
         dm: &DataManager,
-        _last_order_time: Instant,
     ) -> Result<()> {
         info!("执行市价卖单");
 
@@ -204,6 +208,9 @@ impl Strategy {
         let order_id = match self.place_market_sell_order(dm.best_bid, self.filled_quantity).await {
             Ok(id) => {
                 info!("市价卖单下单成功,订单ID: {}", id);
+                
+                self.last_order_time = Instant::now();
+                
                 id
             }
             Err(e) => {
@@ -236,16 +243,6 @@ impl Strategy {
         }
     }
 
-    // 获取上次下单时间
-    fn get_last_order_time(&self) -> Option<Instant> {
-        match &self.state {
-            StrategyState::WaitingLimitOrder { last_order_time, .. } => Some(*last_order_time),
-            StrategyState::CancellingOrder { last_order_time, .. } => Some(*last_order_time),
-            StrategyState::ExecutingMarketOrder { last_order_time } => Some(*last_order_time),
-            _ => None,
-        }
-    }
-
     // ==================== 抽象方法(待实现)====================
 
     /// 下限价买单