瀏覽代碼

数据归一化和预定价格的处理。

skyfffire 11 月之前
父節點
當前提交
a7e96bdc9e
共有 4 個文件被更改,包括 58 次插入48 次删除
  1. 2 2
      strategy/src/core.rs
  2. 36 34
      strategy/src/predictor.rs
  3. 4 4
      strategy/src/strategy.rs
  4. 16 8
      strategy/src/utils.rs

+ 2 - 2
strategy/src/core.rs

@@ -1077,10 +1077,10 @@ impl Core {
         }
 
         // 定价异常风控
-        if self.ready == 1 && (self.avellaneda_stoikov.ref_price - self.avellaneda_stoikov.mid_price).abs() / self.avellaneda_stoikov.mid_price > dec!(0.03) {
+        if self.ready == 1 && (self.avellaneda_stoikov.fair_price - self.avellaneda_stoikov.mid_price).abs() / self.avellaneda_stoikov.mid_price > dec!(0.03) {
             let exit_msg = format!("{} 定价偏离过大,怀疑定价异常,退出。", self.params.account_name);
             warn!(exit_msg);
-            warn!(?self.avellaneda_stoikov.ref_price, ?self.avellaneda_stoikov.mid_price);
+            warn!(?self.avellaneda_stoikov.fair_price, ?self.avellaneda_stoikov.mid_price);
             self.exit_msg = exit_msg;
             self.stop().await;
         }

+ 36 - 34
strategy/src/predictor.rs

@@ -17,7 +17,6 @@ use crate::utils;
 #[derive(Debug)]
 pub struct Predictor {
     pub depth_vec: Vec<Depth>,                                                  // 深度队列
-    pub fair_price_vec: Vec<Decimal>,                                           // 预定价格队列
     pub volume_vec: Vec<Decimal>,                                               // 深度队列
     pub trade_long_vec: FixedTimeRangeDeque<Trade>,                             // 交易队列
     pub trade_short_vec: FixedTimeRangeDeque<Trade>,                            // 交易队列
@@ -53,8 +52,11 @@ pub struct Predictor {
     pub bid_delta: Decimal,                                                     // δb
     pub base_delta: Decimal,                                                    // 基础挂单距离
     pub ratio_edge: Decimal,                                                    // 资金流修正的挂单距离
-    pub ref_price: Decimal,                                                     // 预定价格
-    pub order_ref_price: Decimal,                                               // 下单时的预定价格
+
+    pub fair_price_vec: Vec<Decimal>,                                           // 预定价格队列
+    pub fair_price: Decimal,                                                    // 预定价格
+    pub fair_price_when_ordering: Decimal,                                      // 下单时的预定价格
+    pub price_times_avg: Decimal,                                               // 公平所与做市所的价格倍率的平均值
 
     pub cci_arc: Arc<Mutex<CentralControlInfo>>,                                // 中控信息
     pub debugs: Vec<Vec<Decimal>>,                                              // debug数据等
@@ -154,9 +156,10 @@ impl Predictor {
 
             ratio_edge: Default::default(),
             kappa: Default::default(),
-            ref_price: Default::default(),
-            order_ref_price: Default::default(),
+            fair_price: Default::default(),
+            fair_price_when_ordering: Default::default(),
 
+            price_times_avg: Default::default(),
             cci_arc,
 
             debugs: vec![],
@@ -365,7 +368,7 @@ impl Predictor {
 
         if prev_inventory != self.inventory && prev_inventory.is_zero() {
             self.prev_trade_time = Utc::now().timestamp_micros();
-            self.close_price = self.order_ref_price;
+            self.close_price = self.fair_price_when_ordering;
         }
 
         self.update_level().await;
@@ -373,7 +376,7 @@ impl Predictor {
     }
 
     pub fn update_sigma_square(&mut self) {
-        self.sigma_square = self.ref_price * self.params.open;
+        self.sigma_square = self.fair_price * self.params.open;
         self.sigma_square.rescale(10);
     }
 
@@ -417,35 +420,31 @@ impl Predictor {
         };
         self.fair_price_vec[index].rescale(self.mid_price.scale());
         self.volume_vec[index] = a1.size + b1.size;
-    }
 
-    pub fn update_ref_price(&mut self) {
+        // 合成公平价格
         if !self.fair_price_vec[0].is_zero() && !self.fair_price_vec[1].is_zero() {
-            // let v0_rate = self.volume_vec[0] / (self.volume_vec[0] + self.volume_vec[1]);
-            // let v1_rate = self.volume_vec[1] / (self.volume_vec[0] + self.volume_vec[1]);
-
-            let sma = self.depth_vec[1].asks[0].price;
-            let smb = self.depth_vec[1].bids[0].price;
-
-            let mp0 = self.mid_price;
-            let mp1 = (sma + smb) / Decimal::TWO;
-
-            let price_diff = mp0 - mp1;
+            self.price_times_avg = if self.price_times_avg.is_zero() {
+                self.fair_price_vec[1] / self.fair_price_vec[0]
+            } else {
+                self.price_times_avg * dec!(0.9999) + dec!(0.0001) * self.fair_price_vec[1] / self.fair_price_vec[0]
+            };
 
-            self.ref_price = self.fair_price_vec[0] * dec!(0.2) + self.fair_price_vec[1] * dec!(0.8) + price_diff / Decimal::TWO;
-            // self.ref_price = (self.fair_price_vec[0] + self.fair_price_vec[1]) / Decimal::TWO;
+            // 进行价格归一化处理,公平所的价格有可能是带前缀的
+            let fair_price_part0 = self.fair_price_vec[0] * dec!(0.2);
+            let fair_price_part1 = (self.fair_price_vec[1] / self.price_times_avg) * dec!(0.8);
+            self.fair_price = fair_price_part0 + fair_price_part1;
         }
     }
 
     pub fn update_delta(&mut self) {
-        if self.ref_price.is_zero() {
+        if self.fair_price.is_zero() {
             return;
         }
 
-        let is_open_long = self.ref_price > self.mid_price;
-        let is_open_short = self.ref_price < self.mid_price;
-        let is_close_long = self.inventory > Decimal::ZERO && (self.ref_price < self.mid_price || self.pos_avg_price < self.mid_price);
-        let is_close_short = self.inventory < Decimal::ZERO && (self.ref_price > self.mid_price || self.pos_avg_price > self.mid_price);
+        let is_open_long = self.fair_price > self.mid_price;
+        let is_open_short = self.fair_price < self.mid_price;
+        let is_close_long = self.inventory > Decimal::ZERO && (self.fair_price < self.mid_price || self.pos_avg_price < self.mid_price);
+        let is_close_short = self.inventory < Decimal::ZERO && (self.fair_price > self.mid_price || self.pos_avg_price > self.mid_price);
 
         self.bid_delta = Decimal::NEGATIVE_ONE;
         self.ask_delta = Decimal::NEGATIVE_ONE;
@@ -463,13 +462,13 @@ impl Predictor {
 
     pub fn update_optimal_ask_and_bid(&mut self) {
         self.optimal_ask_price = if self.ask_delta == Decimal::NEGATIVE_ONE {
-            self.ref_price + self.mid_price * self.params.open * Decimal::PI
+            self.fair_price + self.mid_price * self.params.open * Decimal::PI
         } else {
             max(self.ask_price + self.ask_delta, self.bid_price)
         };
 
         self.optimal_bid_price = if self.bid_delta == Decimal::NEGATIVE_ONE {
-            self.ref_price - self.mid_price * self.params.open * Decimal::PI
+            self.fair_price - self.mid_price * self.params.open * Decimal::PI
         } else {
             min(self.bid_price - self.bid_delta, self.ask_price)
         };
@@ -526,7 +525,7 @@ impl Predictor {
            return;
         }
 
-        if self.ref_price == Decimal::ZERO {
+        if self.fair_price == Decimal::ZERO {
             return;
         }
 
@@ -560,7 +559,6 @@ impl Predictor {
         self.update_sigma_square();
         self.update_gamma();
         self.update_kappa();
-        self.update_ref_price();
         self.update_delta();
         self.update_optimal_ask_and_bid();
 
@@ -584,9 +582,13 @@ impl Predictor {
         let bid_price = self.bid_price;
         let last_price = self.last_price;
 
-        let spread = self.spread;
-        let spread_max = self.spread_max;
-        let spread_min = self.spread_max * Decimal::NEGATIVE_ONE;
+        // let spread = self.spread;
+        // let spread_max = self.spread_max;
+        // let spread_min = self.spread_max * Decimal::NEGATIVE_ONE;
+        let spread = self.price_times_avg;
+        let spread_max = self.fair_price_vec[1] / self.fair_price_vec[0];
+        let spread_min = self.fair_price / self.mid_price;
+
         let optimal_ask_price = self.optimal_ask_price;
         let optimal_bid_price = self.optimal_bid_price;
 
@@ -596,7 +598,7 @@ impl Predictor {
         let kappa = self.pos_amount;
 
         let flow_ratio = Decimal::ZERO;
-        let ref_price = self.ref_price;
+        let ref_price = self.fair_price;
 
         let need_append = now - self.prev_insert_time > Decimal::ONE_HUNDRED;
         if !need_append {

+ 4 - 4
strategy/src/strategy.rs

@@ -1089,7 +1089,7 @@ impl Strategy {
             // 下单价值判定
             let amount_value = amount * target_buy_price;
             if amount_value >= self.min_amount_value || predictor.inventory < Decimal::ZERO {
-                predictor.order_ref_price = predictor.ref_price.clone();
+                predictor.fair_price_when_ordering = predictor.fair_price.clone();
 
                 let client_id = utils::generate_client_id(Some(self.broker_id.clone()));
                 let order = vec![
@@ -1132,7 +1132,7 @@ impl Strategy {
             // 下单价值不能太大,也不能太小
             let amount_value = amount * target_sell_price;
             if amount_value >= self.min_amount_value || predictor.inventory > Decimal::ZERO {
-                predictor.order_ref_price = predictor.ref_price.clone();
+                predictor.fair_price_when_ordering = predictor.fair_price.clone();
 
                 let client_id = utils::generate_client_id(Some(self.broker_id.clone()));
                 let order = vec![
@@ -1222,7 +1222,7 @@ impl Strategy {
 
         self.ref_ap = predictor.optimal_ask_price;
         self.ref_bp = predictor.optimal_bid_price;
-        self.ref_price = predictor.ref_price;
+        self.ref_price = predictor.fair_price;
         self.mp = predictor.mid_price;
 
         // 修复相关价格
@@ -1230,7 +1230,7 @@ impl Strategy {
 
         self._cancel_open(&mut command, local_orders);              // 撤单命令处理
         self._post_close(&mut command, local_orders, predictor);    // 平仓单命令处理
-        self._post_open(&mut command, local_orders, predictor);     // 限价单命令处理
+        // self._post_open(&mut command, local_orders, predictor);     // 限价单命令处理
         self._check_local_orders(&mut command, local_orders);       // 固定时间检查超时订单
         self._update_in_cancel(&mut command, local_orders);         // 更新撤单队列,是一个filter
         self._check_request_limit(&mut command);                    // 限制频率,移除不合规则之订单,是一个filter

+ 16 - 8
strategy/src/utils.rs

@@ -380,19 +380,22 @@ pub fn build_html_file(data_c: &Vec<VecDeque<Decimal>>) -> String {
             name: "ask_price",
             type: "line",
             showSymbol: false,
-            data: data[2],
+            // data: data[2],
+            data: [],
           },
           {
             name: "bid_price",
             type: "line",
             showSymbol: false,
-            data: data[3],
+            // data: data[3],
+            data: [],
           },
           {
             name: "optimal_ask_price",
             type: "line",
             showSymbol: false,
-            data: data[8],
+            // data: data[8],
+            data: [],
             lineStyle: {
               type: "dashed",
             },
@@ -401,7 +404,8 @@ pub fn build_html_file(data_c: &Vec<VecDeque<Decimal>>) -> String {
             name: "optimal_bid_price",
             type: "line",
             showSymbol: false,
-            data: data[9],
+            // data: data[9],
+            data: [],
             lineStyle: {
               type: "dashed",
             },
@@ -491,10 +495,14 @@ pub fn build_html_file(data_c: &Vec<VecDeque<Decimal>>) -> String {
               data: inventoryList,
             },
           },
-          { data: data[2] },
-          { data: data[3] },
-          { data: data[8] },
-          { data: data[9] },
+          // { data: data[2] },
+          // { data: data[3] },
+          // { data: data[8] },
+          // { data: data[9] },
+          { data: [] },
+          { data: [] },
+          { data: [] },
+          { data: [] },
           { data: data[15] },
           { data: data[5] },
           { data: data[6] },