Przeglądaj źródła

使用trade.price生成mid,而不是depth.price

skyffire 9 miesięcy temu
rodzic
commit
80a3ea5179
2 zmienionych plików z 31 dodań i 45 usunięć
  1. 29 43
      strategy/src/predictor.rs
  2. 2 2
      strategy/src/strategy.rs

+ 29 - 43
strategy/src/predictor.rs

@@ -29,8 +29,6 @@ pub struct Predictor {
 
     pub mid_price: Decimal,                                                     // 中间价
     pub fair_price: Decimal,
-    pub ask_price: Decimal,                                                     // 卖一价
-    pub bid_price: Decimal,                                                     // 买一价
     pub last_price: Decimal,                                                    // 最后成交价
 
     pub optimal_ask_price: Decimal,                                             // 卖出挂单价
@@ -141,8 +139,6 @@ impl Predictor {
             spread_vec: VecDeque::new(),
             mid_price: Default::default(),
             fair_price: Default::default(),
-            ask_price: Default::default(),
-            bid_price: Default::default(),
             last_price: Default::default(),
             optimal_ask_price: Default::default(),
             optimal_bid_price: Default::default(),
@@ -180,9 +176,27 @@ impl Predictor {
         self.last_index = Decimal::from(index);
 
         if index == 233  {
-            self.ask_price = depth.asks[0].price;
-            self.bid_price = depth.bids[0].price;
-            self.mid_price = (self.ask_price + self.bid_price) / Decimal::TWO;
+        } else {
+            self.depth_vec[index] = depth.clone();
+        }
+
+        if self.mid_price.is_zero() {
+            return;
+        }
+
+        self.processor(depth.time, false).await;
+    }
+
+    pub async fn on_trade(&mut self, trade: &Trade, index: usize) {
+        // index == 233代表做市所
+        // index == 0,1,2,3...代表参考所
+
+        self.last_price = trade.price;
+
+        self.trade_price_long_vec.push_back(trade.price);
+
+        if index == 233 {
+            self.mid_price = trade.price;
 
             // 拟合k与b
             for (mid_index, mp) in self.mid_price_vec.iter().enumerate() {
@@ -213,31 +227,14 @@ impl Predictor {
                     }
                 }
             }
-        } else {
-            self.update_fair_price(depth, index).await;
-
-            self.depth_vec[index] = depth.clone();
-        }
-
-        if self.mid_price.is_zero() {
-            return;
-        }
-
-        self.processor(depth.time, false).await;
-    }
-
-    pub async fn on_trade(&mut self, trade: &Trade, index: usize) {
-        // index == 233代表做市所
-        // index == 0代表参考所
-
-        self.last_price = trade.price;
-
-        self.trade_price_long_vec.push_back(trade.price);
 
-        if index == 233 {
             self.trade_233_vec.push_back(trade.clone());
-        } else if index == 0 {
-            self.trade_0_vec.push_back(trade.clone());
+        } else {
+            self.update_fair_price(trade, index).await;
+
+            if index == 0 {
+                self.trade_0_vec.push_back(trade.clone());
+            }
         }
 
         // self.processor().await;
@@ -288,20 +285,17 @@ impl Predictor {
         }
     }
 
-    pub async fn update_fair_price(&mut self, depth: &Depth, index: usize) {
+    pub async fn update_fair_price(&mut self, trade: &Trade, index: usize) {
         if self.mid_price.is_zero() {
             return;
         }
 
-        let a1 = &depth.asks[0];
-        let b1 = &depth.bids[0];
-
         // https://quant.stackexchange.com/questions/50651/how-to-understand-micro-price-aka-weighted-mid-price
         // let total = a1.value + b1.value;
         // let fair_price = (a1.price + b1.price) / Decimal::TWO;
 
         // self.fair_price_vec[index] = a1.price * b1.value / total + b1.price * a1.value / total;
-        let mut mp = (a1.price + b1.price) / Decimal::TWO;
+        let mut mp = trade.price;
         mp.rescale(self.mid_price.scale());
         self.mid_price_vec[index] = mp;
 
@@ -393,14 +387,6 @@ impl Predictor {
             }
         }
 
-        if self.ask_price.is_zero() {
-            return;
-        }
-
-        if self.bid_price.is_zero() {
-            return;
-        }
-
         if self.balance.is_zero() {
             return;
         }

+ 2 - 2
strategy/src/strategy.rs

@@ -245,8 +245,8 @@ impl Strategy {
             self.pos.short_avg = local_position.short_avg;
         }
         // 价格值处理
-        self.bp = predictor.bid_price;
-        self.ap = predictor.ask_price;
+        self.bp = predictor.mid_price;
+        self.ap = predictor.mid_price;
         self.mp = (self.bp + self.ap) * dec!(0.5);
         // 中间价的ema值处理
         if self.mp_ema.eq(&Decimal::ZERO) {