Ver Fonte

追踪止盈

skyffire há 8 meses atrás
pai
commit
4a6000d1f4
1 ficheiros alterados com 26 adições e 1 exclusões
  1. 26 1
      strategy/src/predictor.rs

+ 26 - 1
strategy/src/predictor.rs

@@ -44,6 +44,8 @@ pub struct Predictor {
     pub pos_avg_price: Decimal,                                                 // 原始持仓价格
     pub balance: Decimal,                                                       // 初始余额
     pub prev_balance: Decimal,
+    pub profit: Decimal,
+    pub profit_high: Decimal,
 
     pub prev_open_time: Decimal,
     pub trade_condition: Decimal,                                               // 过去1分钟以内,差价是否满足过扩大行为+行情有针
@@ -181,6 +183,9 @@ impl Predictor {
             balance: Default::default(),
             prev_balance: Default::default(),
 
+            profit: Default::default(),
+            profit_high: Default::default(),
+
             trade_condition: Default::default(),
             trade_condition_time: Default::default(),
 
@@ -214,6 +219,19 @@ impl Predictor {
             self.bid_price = depth.bids[0].price;
             self.mid_price = (self.ask_price + self.bid_price) / Decimal::TWO;
 
+            // 计算利润(预估)
+            if !self.inventory.is_zero() {
+                self.profit = if self.inventory > Decimal::ZERO {
+                    (self.mid_price - self.pos_avg_price) / self.pos_avg_price
+                } else {
+                    (self.pos_avg_price - self.mid_price) / self.pos_avg_price
+                };
+                self.profit.rescale(6);
+                if self.profit_high < self.profit {
+                    self.profit_high = self.profit
+                }
+            }
+
             self.mid_price_long_vec.push_back(self.mid_price);
             self.mid_price_short_vec.push_back(self.mid_price);
 
@@ -326,6 +344,9 @@ impl Predictor {
                 self.mid_price_long_vec.deque.clear();
                 self.mid_price_short_vec.deque.clear();
                 self.fair_price_short_vec.deque.clear();
+
+                self.profit = Decimal::ZERO;
+                self.profit_high = Decimal::ZERO;
             }
 
             self.processor(update_time, true).await;
@@ -418,12 +439,16 @@ impl Predictor {
             (self.trend_long_rate > dec!(0.005))
             // 达到最大持仓时间还未盈利就平仓
             || (now - self.prev_open_time > is_holding_time_over && self.mid_price < self.pos_avg_price)
+            // 达到最大持仓时间,追踪止盈
+            || (now - self.prev_open_time > is_holding_time_over && self.profit < self.profit_high * dec!(0.75))
         );
         let is_close_short = self.inventory < Decimal::ZERO && (
             // 反转平仓1
             (self.trend_long_rate < dec!(-0.005))
             // 达到最大持仓时间还未盈利就平仓
             || (now - self.prev_open_time > is_holding_time_over && self.mid_price > self.pos_avg_price)
+            // 达到最大持仓时间,追踪止盈
+            || (now - self.prev_open_time > is_holding_time_over && self.profit < self.profit_high * dec!(0.75))
         );
         let is_open_long = self.inventory.is_zero()
             && self.fair_price > self.mid_price * (Decimal::ONE + self.params.open)
@@ -617,7 +642,7 @@ impl Predictor {
         let sigma_square = Decimal::from(Utc::now().timestamp_millis()) - data_time;
 
         let gamma = self.balance;
-        let kappa = self.trend_long_rate;
+        let kappa = self.profit;
 
         let flow_ratio = Decimal::ZERO;