|
|
@@ -176,27 +176,9 @@ impl Predictor {
|
|
|
self.last_index = Decimal::from(index);
|
|
|
|
|
|
if index == 233 {
|
|
|
- } else {
|
|
|
- self.depth_vec[index] = depth.clone();
|
|
|
- }
|
|
|
-
|
|
|
- if self.mid_price.is_zero() {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- self.processor(depth.time, false).await;
|
|
|
- }
|
|
|
-
|
|
|
- pub async fn on_trade(&mut self, trade: &Trade, index: usize) {
|
|
|
- // index == 233代表做市所
|
|
|
- // index == 0,1,2,3...代表参考所
|
|
|
-
|
|
|
- self.last_price = trade.price;
|
|
|
-
|
|
|
- self.trade_price_long_vec.push_back(trade.price);
|
|
|
-
|
|
|
- if index == 233 {
|
|
|
- self.mid_price = trade.price;
|
|
|
+ let ask_price = depth.asks[0].price;
|
|
|
+ let bid_price = depth.bids[0].price;
|
|
|
+ self.mid_price = (ask_price + bid_price) / Decimal::TWO;
|
|
|
|
|
|
// 拟合k与b
|
|
|
for (mid_index, mp) in self.mid_price_vec.iter().enumerate() {
|
|
|
@@ -231,7 +213,26 @@ impl Predictor {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ } else {
|
|
|
+ self.depth_vec[index] = depth.clone();
|
|
|
+ }
|
|
|
+
|
|
|
+ if self.mid_price.is_zero() {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ self.processor(depth.time, false).await;
|
|
|
+ }
|
|
|
|
|
|
+ pub async fn on_trade(&mut self, trade: &Trade, index: usize) {
|
|
|
+ // index == 233代表做市所
|
|
|
+ // index == 0,1,2,3...代表参考所
|
|
|
+
|
|
|
+ self.last_price = trade.price;
|
|
|
+
|
|
|
+ self.trade_price_long_vec.push_back(trade.price);
|
|
|
+
|
|
|
+ if index == 233 {
|
|
|
self.trade_233_vec.push_back(trade.clone());
|
|
|
} else {
|
|
|
self.update_fair_price(trade, index).await;
|
|
|
@@ -335,20 +336,20 @@ impl Predictor {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- let is_close_long = self.inventory > Decimal::ZERO;
|
|
|
- let is_close_short = self.inventory < Decimal::ZERO;
|
|
|
+ let is_close_long = self.inventory > Decimal::ZERO && (self.fair_price < self.mid_price * (Decimal::ONE + self.params.close));
|
|
|
+ let is_close_short = self.inventory < Decimal::ZERO && (self.fair_price > self.mid_price * (Decimal::ONE - self.params.close));
|
|
|
|
|
|
if is_close_long {
|
|
|
self.ask_delta = dec!(0);
|
|
|
self.bid_delta = dec!(-2);
|
|
|
|
|
|
- self.optimal_ask_price = self.fair_price + self.fair_price * self.params.close;
|
|
|
+ self.optimal_ask_price = self.mid_price;
|
|
|
self.optimal_bid_price = Self::DONT_VIEW;
|
|
|
} else if is_close_short {
|
|
|
self.bid_delta = dec!(0);
|
|
|
self.ask_delta = dec!(-2);
|
|
|
|
|
|
- self.optimal_bid_price = self.fair_price - self.fair_price * self.params.close;
|
|
|
+ self.optimal_bid_price = self.mid_price;
|
|
|
self.optimal_ask_price = Self::DONT_VIEW;
|
|
|
} else {
|
|
|
if self.fair_price > self.mid_price {
|