Prechádzať zdrojové kódy

self.trend_rate < self.params.open * dec!(-10)

skyffire 9 mesiacov pred
rodič
commit
5f77fed898
1 zmenil súbory, kde vykonal 35 pridanie a 24 odobranie
  1. 35 24
      strategy/src/predictor.rs

+ 35 - 24
strategy/src/predictor.rs

@@ -19,7 +19,7 @@ pub struct Predictor {
     pub depth_vec: Vec<Depth>,                                                  // 深度队列
     pub record_vec: VecDeque<Record>,                                           // 蜡烛队列
     pub spread_vec: VecDeque<Decimal>,                                          // 价差队列
-    pub trade_price_short_vec: FixedTimeRangeDeque<Decimal>,
+    pub mid_price_short_vec: FixedTimeRangeDeque<Decimal>,
     pub trend_rate: Decimal,
 
     pub prices: Vec<Vec<VecDeque<Decimal>>>,                                    // [[[做市所], [参考所0]], ...]
@@ -27,6 +27,8 @@ pub struct Predictor {
     pub bs: Vec<Decimal>,
 
     pub mid_price: Decimal,                                                     // 中间价
+    pub ask_price: Decimal,                                                     // 中间价
+    pub bid_price: Decimal,                                                     // 中间价
     pub fair_price: Decimal,
     pub last_price: Decimal,                                                    // 最后成交价
 
@@ -147,9 +149,11 @@ impl Predictor {
 
             record_vec: VecDeque::new(),
 
-            trade_price_short_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
+            mid_price_short_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
             spread_vec: VecDeque::new(),
             mid_price: Default::default(),
+            ask_price: Default::default(),
+            bid_price: Default::default(),
             fair_price: Default::default(),
             last_price: Default::default(),
             optimal_ask_price: Self::DONT_VIEW,
@@ -188,9 +192,30 @@ impl Predictor {
         self.last_index = Decimal::from(index);
 
         if index == 233  {
-            let ask_price = depth.asks[0].price;
-            let bid_price = depth.bids[0].price;
-            self.mid_price = (ask_price + bid_price) / Decimal::TWO;
+            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;
+
+            self.mid_price_short_vec.push_back(self.mid_price);
+
+            self.trend_rate = if self.mid_price_short_vec.len() > 2 {
+                let max = self.mid_price_short_vec.deque.iter().max().unwrap();
+                let min = self.mid_price_short_vec.deque.iter().min().unwrap();
+
+                let last = self.mid_price_short_vec.deque.back().unwrap();
+
+                // 在下跌
+                if max - last > last - min {
+                    (last - max) / max - Decimal::ONE
+                }
+                // 在上涨
+                else {
+                    (last - min) / min - Decimal::ONE
+                }
+            } else {
+                Decimal::ZERO
+            };
+            self.trend_rate.rescale(6);
 
             // 拟合k与b
             for (mid_index, mp) in self.mid_price_vec.iter().enumerate() {
@@ -239,25 +264,11 @@ impl Predictor {
         self.processor(depth.time, false).await;
     }
 
-    pub async fn on_trade(&mut self, trade: &Trade, index: usize) {
+    pub async fn on_trade(&mut self, trade: &Trade, _index: usize) {
         // index == 233代表做市所
         // index == 0,1,2,3...代表参考所
 
         self.last_price = trade.price;
-
-        if index == 233 {
-            self.trade_price_short_vec.push_back(trade.price);
-
-            self.trend_rate = if self.trade_price_short_vec.len() > 2 {
-                let last = self.trade_price_short_vec.deque.back().unwrap();
-                let first = self.trade_price_short_vec.deque.front().unwrap();
-
-                (last - first) / first - Decimal::ONE
-            } else {
-                Decimal::ZERO
-            };
-            self.trend_rate.rescale(6);
-        }
     }
 
     pub async fn on_ticker(&mut self, _ticker: &Ticker) {}
@@ -374,17 +385,17 @@ impl Predictor {
             self.optimal_bid_price = max(self.mid_price, self.fair_price);
             self.optimal_ask_price = Self::DONT_VIEW;
         } else if self.inventory.is_zero() {
-            if self.fair_price > self.mid_price * (Decimal::ONE + self.params.open) && self.trend_rate < self.params.open * dec!(-5) {
+            if self.fair_price > self.mid_price * (Decimal::ONE + self.params.open) && self.trend_rate < self.params.open * dec!(-10) {
                 self.bid_delta = dec!(0);
                 self.ask_delta = dec!(-2);
 
-                self.optimal_bid_price = self.mid_price * dec!(1.001);
+                self.optimal_bid_price = self.ask_price * dec!(1.001);
                 self.optimal_ask_price = Self::DONT_VIEW;
-            } else if self.fair_price < self.mid_price * (Decimal::ONE - self.params.open) && self.trend_rate > self.params.open * dec!(5) {
+            } else if self.fair_price < self.mid_price * (Decimal::ONE - self.params.open) && self.trend_rate > self.params.open * dec!(10) {
                 self.ask_delta = dec!(0);
                 self.bid_delta = dec!(-2);
 
-                self.optimal_ask_price = self.mid_price * dec!(0.999);
+                self.optimal_ask_price = self.bid_price * dec!(0.999);
                 self.optimal_bid_price = Self::DONT_VIEW;
             }
         }