|
|
@@ -51,8 +51,9 @@ pub struct Predictor {
|
|
|
pub fair_price_vec: Vec<Decimal>, // 公平价格列表,0表示做市所,1表示参考所
|
|
|
pub fair_price: Decimal, // 预定价格
|
|
|
pub fair_rate_focus_open: Decimal, // 变化幅度焦点
|
|
|
- pub fair_rate_focus_close: Decimal, // 变化幅度焦点
|
|
|
pub fair_price_focus_open: Decimal, // 观测焦点时的价格
|
|
|
+ pub fair_rate_focus_close: Decimal, // 变化幅度焦点
|
|
|
+ pub fair_price_focus_close: Decimal, // 观测焦点时的价格
|
|
|
pub fair_price_when_ordering: Decimal, // 下单时的预定价格
|
|
|
pub price_times_avg: Decimal, // 公平所与做市所的价格倍率的平均值
|
|
|
pub is_regressed: bool, // 做市所的价格是否已经回归
|
|
|
@@ -165,8 +166,9 @@ impl Predictor {
|
|
|
mid_price_time_vec: FixedTimeRangeDeque::new(100_000),
|
|
|
fair_price: Default::default(),
|
|
|
fair_rate_focus_open: Default::default(),
|
|
|
- fair_rate_focus_close: Default::default(),
|
|
|
fair_price_focus_open: Default::default(),
|
|
|
+ fair_rate_focus_close: Default::default(),
|
|
|
+ fair_price_focus_close: Default::default(),
|
|
|
fair_price_when_ordering: Default::default(),
|
|
|
|
|
|
price_times_avg: Default::default(),
|
|
|
@@ -375,16 +377,42 @@ impl Predictor {
|
|
|
if !self.fair_rate_focus_close.is_zero() && self.inventory.is_zero() {
|
|
|
self.fair_rate_focus_close = Decimal::ZERO;
|
|
|
}
|
|
|
+ // 重置平仓焦点,条件2
|
|
|
+ if !self.fair_rate_focus_close.is_zero() {
|
|
|
+ let focus_rate = (self.fair_price - self.fair_price_focus_close) / self.fair_price_focus_close;
|
|
|
+
|
|
|
+ if self.fair_price_focus_close > Decimal::ZERO && focus_rate < Decimal::NEGATIVE_ONE * self.params.close_activate / Decimal::TWO {
|
|
|
+ self.fair_rate_focus_close = Decimal::ZERO;
|
|
|
+ }
|
|
|
+
|
|
|
+ if self.fair_price_focus_close < Decimal::ZERO && focus_rate > self.params.close_activate / Decimal::TWO {
|
|
|
+ self.fair_rate_focus_close = Decimal::ZERO;
|
|
|
+ }
|
|
|
+ }
|
|
|
// 更新程序关注的平仓焦点
|
|
|
if self.fair_rate_focus_close.is_zero() && !self.inventory.is_zero() {
|
|
|
// 只有有强度的rate才有资格被称为针
|
|
|
if rate.abs() > self.params.close_activate {
|
|
|
+ // 多单平仓逻辑
|
|
|
if self.inventory > Decimal::ZERO && rate > Decimal::ZERO {
|
|
|
- self.fair_rate_focus_close = rate;
|
|
|
+ if self.mid_price > self.pos_avg_price {
|
|
|
+ self.fair_rate_focus_close = rate;
|
|
|
+ self.fair_price_focus_close = self.fair_price;
|
|
|
+ } else if self.t_diff.is_zero() {
|
|
|
+ self.fair_rate_focus_close = rate;
|
|
|
+ self.fair_price_focus_close = self.fair_price;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ // 空单平仓逻辑
|
|
|
if self.inventory < Decimal::ZERO && rate < Decimal::ZERO {
|
|
|
- self.fair_rate_focus_close = rate;
|
|
|
+ if self.mid_price < self.pos_avg_price {
|
|
|
+ self.fair_rate_focus_close = rate;
|
|
|
+ self.fair_price_focus_close = self.fair_price;
|
|
|
+ } else if self.t_diff.is_zero() {
|
|
|
+ self.fair_rate_focus_close = rate;
|
|
|
+ self.fair_price_focus_close = self.fair_price;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -456,31 +484,31 @@ impl Predictor {
|
|
|
self.ask_delta = dec!(-2);
|
|
|
|
|
|
if is_close_long {
|
|
|
- if self.mid_price > self.pos_avg_price {
|
|
|
- // let profit_rate = (self.mid_price - self.pos_avg_price) / self.pos_avg_price;
|
|
|
- // let fill_rate = profit_rate / (self.params.open * dec!(50));
|
|
|
- // let close_rate = (Decimal::ONE - fill_rate) * self.params.open + self.params.close;
|
|
|
- //
|
|
|
- // self.ask_delta = self.mid_price * close_rate * self.t_diff;
|
|
|
- self.ask_delta = self.mid_price * self.params.close;
|
|
|
- } else if self.mid_price > self.fair_price {
|
|
|
- self.ask_delta = self.mid_price * self.params.close;
|
|
|
- }
|
|
|
+ // if self.mid_price > self.pos_avg_price {
|
|
|
+ // // let profit_rate = (self.mid_price - self.pos_avg_price) / self.pos_avg_price;
|
|
|
+ // // let fill_rate = profit_rate / (self.params.open * dec!(50));
|
|
|
+ // // let close_rate = (Decimal::ONE - fill_rate) * self.params.open + self.params.close;
|
|
|
+ // //
|
|
|
+ // // self.ask_delta = self.mid_price * close_rate * self.t_diff;
|
|
|
+ // self.ask_delta = self.mid_price * self.params.close;
|
|
|
+ // } else if self.mid_price > self.fair_price {
|
|
|
+ // self.ask_delta = self.mid_price * self.params.close;
|
|
|
+ // }
|
|
|
|
|
|
- // self.ask_delta = self.mid_price * self.params.close;
|
|
|
+ self.ask_delta = self.mid_price * self.params.close;
|
|
|
} else if is_close_short {
|
|
|
- if self.mid_price < self.pos_avg_price {
|
|
|
- // let profit_rate = (self.pos_avg_price - self.mid_price) / self.pos_avg_price;
|
|
|
- // let fill_rate = profit_rate / (self.params.open * dec!(50));
|
|
|
- // let close_rate = (Decimal::ONE - fill_rate) * self.params.open + self.params.close;
|
|
|
- //
|
|
|
- // self.bid_delta = self.mid_price * close_rate * self.t_diff;
|
|
|
- self.bid_delta = self.mid_price * self.params.close;
|
|
|
- } else if self.mid_price < self.fair_price {
|
|
|
- self.bid_delta = self.mid_price * self.params.close;
|
|
|
- }
|
|
|
+ // if self.mid_price < self.pos_avg_price {
|
|
|
+ // // let profit_rate = (self.pos_avg_price - self.mid_price) / self.pos_avg_price;
|
|
|
+ // // let fill_rate = profit_rate / (self.params.open * dec!(50));
|
|
|
+ // // let close_rate = (Decimal::ONE - fill_rate) * self.params.open + self.params.close;
|
|
|
+ // //
|
|
|
+ // // self.bid_delta = self.mid_price * close_rate * self.t_diff;
|
|
|
+ // self.bid_delta = self.mid_price * self.params.close;
|
|
|
+ // } else if self.mid_price < self.fair_price {
|
|
|
+ // self.bid_delta = self.mid_price * self.params.close;
|
|
|
+ // }
|
|
|
|
|
|
- // self.bid_delta = self.mid_price * self.params.close;
|
|
|
+ self.bid_delta = self.mid_price * self.params.close;
|
|
|
} else if is_open_long {
|
|
|
// let is_open_long_market = self.spread_sma_1000 - self.spread_sma > self.params.open_market;
|
|
|
// self.bid_delta = if is_open_long_market {
|