Browse Source

使用穿越間隔來觀察。

skyfffire 1 year ago
parent
commit
cfeeca9af9
1 changed files with 19 additions and 5 deletions
  1. 19 5
      strategy/src/avellaneda_stoikov.rs

+ 19 - 5
strategy/src/avellaneda_stoikov.rs

@@ -48,6 +48,8 @@ pub struct AvellanedaStoikov {
 
     pub is_ready: bool,
     pub prev_trade_time: i64,                                                   // 上次交易时间,也就是t
+    pub cross_time: i64,                                                        // 上次穿過0軸的時間
+    pub cross_time_diff: i64,                                                   // 穿越0軸的diff
     pub t_diff: Decimal,                                                        // (T-t)
     pub prev_close_time: i64,                                                   // 上次平倉時間
     pub prev_prev_close_time: i64,                                              // 上上次平倉時間
@@ -96,6 +98,8 @@ impl AvellanedaStoikov {
 
             is_ready: false,
             prev_trade_time: Utc::now().timestamp_micros(),
+            cross_time: 0,
+            cross_time_diff: 0,
             t_diff: Default::default(),
             flow_ratio_long: Decimal::ONE,
             level: Default::default(),
@@ -302,11 +306,11 @@ impl AvellanedaStoikov {
             let prev_trade_iter = trades.deque.get(index - 1).unwrap();
             let trade = trade_iter;
             if trade.price > prev_trade_iter.price {
-                // flow_in_value += Decimal::ONE * (prev_trade_iter.price - trade.price).abs();
-                flow_in_value += Decimal::ONE;
+                flow_in_value += Decimal::ONE * (prev_trade_iter.price - trade.price).abs();
+                // flow_in_value += Decimal::ONE;
             } else if trade.price < prev_trade_iter.price {
-                // flow_out_value += Decimal::ONE * (prev_trade_iter.price - trade.price).abs();
-                flow_out_value += Decimal::ONE;
+                flow_out_value += Decimal::ONE * (prev_trade_iter.price - trade.price).abs();
+                // flow_out_value += Decimal::ONE;
             } else {
                 // if trade.size > Decimal::ZERO {
                 //     flow_in_value += trade.value;
@@ -375,7 +379,17 @@ impl AvellanedaStoikov {
     }
 
     pub fn update_flow_ratio(&mut self) {
+        let prev_flow_ratio_long = self.flow_ratio_long;
         self.flow_ratio_long = Self::calc_flow_ratio(&self.flow_ratio_long, &dec!(0), &mut self.trade_long_vec);
+        if (self.flow_ratio_long > Decimal::ZERO && prev_flow_ratio_long <= Decimal::ZERO)
+            || (self.flow_ratio_long < Decimal::ZERO && prev_flow_ratio_long >= Decimal::ZERO) {
+            self.cross_time = Utc::now().timestamp_millis();
+        }
+
+        if self.cross_time != 0 {
+            self.cross_time_diff = (Utc::now().timestamp_millis() - self.cross_time) / 1000;
+        }
+
         self.flow_ratio_short = Self::calc_flow_ratio(&self.flow_ratio_short, &dec!(0), &mut self.trade_short_vec);
     }
 
@@ -457,7 +471,7 @@ impl AvellanedaStoikov {
             inventory: self.inventory,
             sigma_square: self.flow_ratio_long,
             gamma: self.flow_ratio_short,
-            kappa: self.t_diff,
+            kappa: Decimal::from(self.cross_time_diff),
 
             flow_ratio: self.flow_ratio_long,
             ref_price: self.ref_price,