فهرست منبع

参数调整完毕,实现具体逻辑

skyfffire 2 هفته پیش
والد
کامیت
8fd1abc172
3فایلهای تغییر یافته به همراه30 افزوده شده و 18 حذف شده
  1. 11 0
      src/exchange/extended_rest_client.rs
  2. 6 1
      src/main.rs
  3. 13 17
      src/strategy.rs

+ 11 - 0
src/exchange/extended_rest_client.rs

@@ -161,6 +161,17 @@ impl ExtendedRestClient {
         ).await
     }
 
+    pub async fn get_order(&mut self, id: String) -> Response {
+        let params = json!({});
+
+        self.request("GET",
+                     "/api/v1",
+                     format!("/user/orders/{}", id).as_str(),
+                     true,
+                     params,
+        ).await
+    }
+
     pub async fn cancel_order(&mut self, id: String) -> Response {
         let params = json!({});
 

+ 6 - 1
src/main.rs

@@ -122,8 +122,13 @@ pub async fn run_extended_subscriptions(running: Arc<AtomicBool>) -> Result<()>
             let dm_clone = Arc::clone(&dm);
             let sm_clone = Arc::clone(&sm);
             async move {
-                let mut dm_guard = dm_clone.lock().await;
+                // 数据不新鲜直接跳过,300ms为阈值
+                if response.reach_time - response.received_time > 300 {
+                    return
+                }
                 
+                let mut dm_guard = dm_clone.lock().await;
+
                 // 记录消息延迟
                 dm_guard.record_latency(response.received_time, response.reach_time);
 

+ 13 - 17
src/strategy.rs

@@ -1,6 +1,7 @@
 use anyhow::Result;
 use rust_decimal::Decimal;
 use std::time::{Duration, Instant};
+use rust_decimal_macros::dec;
 use tokio::time::sleep;
 use tracing::{info, warn};
 use crate::data_manager::DataManager;
@@ -26,23 +27,22 @@ pub enum StrategyState {
 #[allow(dead_code)]
 pub struct Strategy {
     state: StrategyState,
-    order_quantity: Decimal,        // 写死的订单数量
-    min_order_interval_ms: u128,    // 最小下单间隔(毫秒)
+    order_quantity: Decimal,            // 写死的订单数量
+    filled_quantity: Decimal,           // 成交数量
+    min_order_interval_ms: u128,        // 最小下单间隔(毫秒)
 }
 
 impl Strategy {
     pub fn new() -> Strategy {
         Strategy {
             state: StrategyState::Idle,
-            order_quantity: Decimal::from(100), // 示例数量
-            min_order_interval_ms: 1000,
+            order_quantity: dec!(0.001),
+            filled_quantity: Decimal::ZERO,
+            min_order_interval_ms: 200,
         }
     }
 
     pub async fn do_strategy(&mut self, dm: &DataManager) -> Result<()> {
-        info!("Current state: {:?}", self.state);
-        info!("Best ask: {}, Best bid: {}", dm.best_ask, dm.best_bid);
-
         match self.state.clone() {
             StrategyState::Idle => {
                 if let Err(e) = self.handle_idle_state(dm).await {
@@ -156,10 +156,10 @@ impl Strategy {
         }
 
         // 撤单后暂停几秒再检查
-        sleep(Duration::from_millis(2000)).await;
+        sleep(Duration::from_millis(3000)).await;
 
         // 撤单后检查是否有成交
-        match self.check_order_partially_filled(&order_id).await {
+        match self.check_order_filled(&order_id).await {
             Ok(true) => {
                 info!("撤单时发现有成交,准备执行市价单");
                 self.state = StrategyState::ExecutingMarketOrder { last_order_time };
@@ -186,7 +186,7 @@ impl Strategy {
         info!("执行市价卖单");
 
         // 尝试下市价单
-        let order_id = match self.place_market_sell_order(self.order_quantity).await {
+        let order_id = match self.place_market_sell_order(self.filled_quantity).await {
             Ok(id) => {
                 info!("市价卖单下单成功,订单ID: {}", id);
                 id
@@ -197,6 +197,9 @@ impl Strategy {
             }
         };
 
+        // 下单后暂停几秒再检查
+        sleep(Duration::from_millis(3000)).await;
+
         // 等待市价单成交(市价单通常立即成交)
         match self.check_order_filled(&order_id).await {
             Ok(true) => {
@@ -254,11 +257,4 @@ impl Strategy {
         // TODO: 实现具体的查询逻辑
         Ok(false)
     }
-
-    /// 检查订单是否有部分成交
-    async fn check_order_partially_filled(&self, order_id: &str) -> Result<bool> {
-        info!("检查订单是否有部分成交: {}", order_id);
-        // TODO: 实现具体的查询逻辑
-        Ok(false)
-    }
 }