Forráskód Böngészése

优化逻辑,修复数据延迟bug。

skyfffire 11 hónapja
szülő
commit
f3b17c1e97
2 módosított fájl, 58 hozzáadás és 23 törlés
  1. 1 1
      global/src/cci.rs
  2. 57 22
      strategy/src/avellaneda_stoikov.rs

+ 1 - 1
global/src/cci.rs

@@ -16,7 +16,7 @@ pub struct CentralControlInfo {
 
 impl CentralControlInfo {
     // 时间窗口大小(微秒)
-    const MAX_TIME_RANGE_MICROS: i64 = 1 * 60_000_000;
+    const MAX_TIME_RANGE_MICROS: i64 = 5 * 60_000_000;
 
     pub fn new() -> Self {
         Self {

+ 57 - 22
strategy/src/avellaneda_stoikov.rs

@@ -500,34 +500,69 @@ impl AvellanedaStoikov {
             smm = (sma + smb) / Decimal::TWO;
         }
 
+
+        let cci_arc = self.cci_arc.clone();
         let now = Decimal::from_i64(Utc::now().timestamp_millis()).unwrap();
+        let mid_price = self.last_price;
+        let ask_price = self.ask_price;
+        let bid_price = self.bid_price;
+        let last_price = self.last_price;
+
+        let spread = smm;
+        let spread_max = self.mid_price;
+        let spread_min = self.ref_price;
+        let optimal_ask_price = self.optimal_ask_price;
+        let optimal_bid_price = self.optimal_bid_price;
+
+        let inventory = self.inventory;
+        let sigma_square = self.error_rate;
+        let gamma = now - self.last_update_time;
+        let kappa = self.last_index;
+
+        let flow_ratio = Decimal::ZERO;
+        let ref_price = self.ref_price;
+
+        // 数据量太多导致的,减少一些吧
+        tokio::spawn(async move {
+            let mut cci = cci_arc.lock().await;
+            let need_append = if cci.predictor_state_vec.len() == 0 {
+                true
+            } else {
+                let state = cci.predictor_state_vec.deque.iter().last().unwrap();
+
+                now - state.update_time > Decimal::ONE_HUNDRED
+            };
+            if !need_append {
+                return;
+            }
+
+            cci.predictor_state_vec.push_back(PredictorState {
+                update_time: now,
+
+                mid_price,
+                ask_price,
+                bid_price,
+                last_price,
+
+                spread,
+                spread_max,
+                spread_min,
+                optimal_ask_price,
+                optimal_bid_price,
+
+                inventory,
+                sigma_square,
+                gamma,
+                kappa,
 
-        let mut cci = self.cci_arc.lock().await;
-        cci.predictor_state_vec.push_back(PredictorState {
-            update_time: now,
-
-            mid_price: self.last_price,
-            ask_price: self.ask_price,
-            bid_price: self.bid_price,
-            last_price: self.last_price,
-            spread: smm,
-            spread_max: self.mid_price,
-            spread_min: self.ref_price,
-            optimal_ask_price: self.optimal_ask_price,
-            optimal_bid_price: self.optimal_bid_price,
-
-            inventory: self.inventory,
-            sigma_square: self.error_rate,
-            gamma: now - self.last_update_time,
-            kappa: self.last_index,
-
-            flow_ratio: Decimal::ZERO,
-            ref_price: self.ref_price,
+                flow_ratio,
+                ref_price,
+            });
         });
     }
 
     // #[instrument(skip(self, ref_ticker_map), level="TRACE")]
     pub fn get_ref_price(&mut self, _ref_ticker_map: &BTreeMap<String, Ticker>) -> Vec<Vec<Decimal>> {
-        return vec![];
+        vec![]
     }
 }