Browse Source

有利价格taker进去(粗糙版本)

skyfffire 11 tháng trước cách đây
mục cha
commit
efdd68cf80
2 tập tin đã thay đổi với 23 bổ sung16 xóa
  1. 13 6
      strategy/src/predictor.rs
  2. 10 10
      strategy/src/strategy.rs

+ 13 - 6
strategy/src/predictor.rs

@@ -437,6 +437,9 @@ impl Predictor {
     }
 
     pub fn update_delta(&mut self) {
+        // -2表示不想成交
+        // -1表示市价成交
+
         if self.fair_price.is_zero() {
             return;
         }
@@ -446,28 +449,32 @@ impl Predictor {
         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;
+        self.bid_delta = dec!(-2);
+        self.ask_delta = dec!(-2);
 
         if is_close_long {
             self.ask_delta = self.mid_price * self.params.close;
         } else if is_close_short {
             self.bid_delta = self.mid_price * self.params.close;
         } else if is_open_long {
-            self.bid_delta = Decimal::ZERO;
+            self.bid_delta = dec!(-1);
         } else if is_open_short {
-            self.ask_delta = Decimal::ZERO;
+            self.ask_delta = dec!(-1);
         }
     }
 
     pub fn update_optimal_ask_and_bid(&mut self) {
-        self.optimal_ask_price = if self.ask_delta == Decimal::NEGATIVE_ONE {
+        self.optimal_ask_price = if self.ask_delta == dec!(-1) {
+            self.bid_price
+        } else if self.ask_delta == dec!(-2) {
             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.optimal_bid_price = if self.bid_delta == dec!(-1) {
+            self.ask_price
+        } else if self.bid_delta == dec!(-2) {
             self.fair_price - self.mid_price * self.params.open * Decimal::PI
         } else {
             min(self.bid_price - self.bid_delta, self.ask_price)

+ 10 - 10
strategy/src/strategy.rs

@@ -892,7 +892,7 @@ impl Strategy {
         // }
         // // debug!(?command);
 
-        if predictor.inventory > Decimal::ZERO && predictor.ask_delta != Decimal::NEGATIVE_ONE {
+        if predictor.inventory > Decimal::ZERO && predictor.ask_delta != dec!(-2) {
             if pd_order_num == 0 {
                 let mut price = (short_lower + short_upper) * dec!(0.5);
                 // 不限制大小
@@ -912,7 +912,7 @@ impl Strategy {
                 // debug!(?command);
             }
         }
-        if predictor.inventory < Decimal::ZERO && predictor.bid_delta != Decimal::NEGATIVE_ONE {
+        if predictor.inventory < Decimal::ZERO && predictor.bid_delta != dec!(-2) {
             if pk_order_num == 0 {
                 let mut price = (long_upper + long_lower) * dec!(0.5);
                 // 不限制大小
@@ -936,7 +936,7 @@ impl Strategy {
 
     // 生成取消订单的指令
     // #[instrument(skip(self, command), level="TRACE")]
-    pub fn _cancel_open(&self, command: &mut OrderCommand, local_orders: &HashMap<String, OrderInfo>, predictor: &mut Predictor) {
+    pub fn _cancel_open(&self, command: &mut OrderCommand, local_orders: &HashMap<String, OrderInfo>, _predictor: &mut Predictor) {
         // debug!(?command);
         // 挂单范围
         // let long_upper = self.open_dist[0];
@@ -953,9 +953,9 @@ impl Strategy {
             if order.side == "kd".to_string() {
                 // 在价格范围内时不处理
                 // if order.price <= long_upper && order.price >= long_lower {
-                // if self.local_time - order.local_time <= 500 {
+                if self.local_time - order.local_time <= 500 {
                 // if (self.local_time - order.local_time <= 500) && (order.price <= long_upper && order.price >= long_lower) {
-                if predictor.bid_price == order.price {
+                // if predictor.bid_price == order.price {
                     continue
                 }
                 command.cancel.insert(key, value);
@@ -964,8 +964,8 @@ impl Strategy {
                 // 在价格范围内时不处理
                 // if (self.local_time - order.local_time <= 500) && (order.price >= short_lower && order.price <= short_upper) {
                 // if order.price >= short_lower && order.price <= short_upper {
-                // if self.local_time - order.local_time <= 500 {
-                if predictor.ask_price == order.price {
+                if self.local_time - order.local_time <= 500 {
+                // if predictor.ask_price == order.price {
                     continue
                 }
                 // debug!(?key, ?order.price, ?short_lower, ?short_upper);
@@ -1068,7 +1068,7 @@ impl Strategy {
         // let one_hand_long_value = dec!(0.99) * (self.max_long_value / self.grid);
         // let one_hand_short_value = dec!(0.99) * (self.max_short_value / self.grid);
         // 挂多单
-        if self.post_side >= 0 && buy_value == Decimal::ZERO && predictor.inventory.is_zero() && predictor.bid_delta != Decimal::NEGATIVE_ONE {
+        if self.post_side >= 0 && buy_value == Decimal::ZERO && predictor.inventory.is_zero() && predictor.bid_delta != dec!(-2) {
             let mut target_buy_price = predictor.optimal_bid_price;
             // target_buy_price = utils::clip(target_buy_price, self.bp * dec!(0.97), self.ap * dec!(1.0005));
             target_buy_price = utils::fix_price(target_buy_price, self.tick_size);
@@ -1111,7 +1111,7 @@ impl Strategy {
             }
         }
         // 挂空单
-        if self.post_side <= 0 && sell_value == Decimal::ZERO && predictor.inventory.is_zero() && predictor.ask_delta != Decimal::NEGATIVE_ONE {
+        if self.post_side <= 0 && sell_value == Decimal::ZERO && predictor.inventory.is_zero() && predictor.ask_delta != dec!(-2) {
             let mut target_sell_price = predictor.optimal_ask_price;
             // target_sell_price = utils::clip(target_sell_price, self.bp * dec!(0.9995), self.ap * dec!(1.03));
             // 取消大小限制
@@ -1232,7 +1232,7 @@ impl Strategy {
         // 修复相关价格
         self.fix_price(predictor);
 
-        self._cancel_open(&mut command, local_orders, predictor);              // 撤单命令处理
+        self._cancel_open(&mut command, local_orders, predictor);   // 撤单命令处理
         self._post_close(&mut command, local_orders, predictor);    // 平仓单命令处理
         self._post_open(&mut command, local_orders, predictor);     // 限价单命令处理
         self._check_local_orders(&mut command, local_orders);       // 固定时间检查超时订单