|
|
@@ -51,7 +51,8 @@ pub struct Predictor {
|
|
|
pub spread_sma_1000_time_vec: FixedTimeRangeDeque<Decimal>, // spread队列,10ms以内的所有spread_sma_1000
|
|
|
pub fair_price_vec: Vec<Decimal>, // 公平价格列表,0表示做市所,1表示参考所
|
|
|
pub fair_price: Decimal, // 公平价格
|
|
|
- pub fair_price_ema_30000: Decimal, // 公平价格_ema
|
|
|
+ pub fair_price_ema_short: Decimal, // 公平价格_ema
|
|
|
+ pub fair_price_ema_long: Decimal, // 公平价格_ema
|
|
|
pub fair_rate_focus_open: Decimal, // 变化幅度焦点
|
|
|
pub fair_price_focus_open: Decimal, // 观测焦点时的价格
|
|
|
pub fair_rate_focus_close: Decimal, // 变化幅度焦点
|
|
|
@@ -168,7 +169,8 @@ impl Predictor {
|
|
|
fair_price_long_time_vec: FixedTimeRangeDeque::new(2 * 60_000_000),
|
|
|
mid_price_time_vec: FixedTimeRangeDeque::new(100_000),
|
|
|
fair_price: Default::default(),
|
|
|
- fair_price_ema_30000: Default::default(),
|
|
|
+ fair_price_ema_short: Default::default(),
|
|
|
+ fair_price_ema_long: Default::default(),
|
|
|
fair_rate_focus_open: Default::default(),
|
|
|
fair_price_focus_open: Default::default(),
|
|
|
fair_rate_focus_close: Default::default(),
|
|
|
@@ -320,12 +322,17 @@ impl Predictor {
|
|
|
let fair_price_part0 = self.fair_price_vec[0] * dec!(0.2);
|
|
|
let fair_price_part1 = (self.fair_price_vec[1] / self.price_times_avg) * dec!(0.8);
|
|
|
self.fair_price = fair_price_part0 + fair_price_part1;
|
|
|
- self.fair_price_ema_30000 = if self.fair_price_ema_30000.is_zero() {
|
|
|
+ self.fair_price_time_vec.push_back(self.fair_price);
|
|
|
+ self.fair_price_ema_long = if self.fair_price_ema_long.is_zero() {
|
|
|
self.fair_price
|
|
|
} else {
|
|
|
- self.fair_price_ema_30000 * dec!(0.999967) + self.fair_price * dec!(0.000033)
|
|
|
+ self.fair_price_ema_long * dec!(0.999967) + self.fair_price * dec!(0.000033)
|
|
|
+ };
|
|
|
+ self.fair_price_ema_short = if self.fair_price_ema_short.is_zero() {
|
|
|
+ self.fair_price
|
|
|
+ } else {
|
|
|
+ self.fair_price_ema_short * dec!(0.999) + self.fair_price * dec!(0.001)
|
|
|
};
|
|
|
- self.fair_price_time_vec.push_back(self.fair_price);
|
|
|
|
|
|
if self.fair_price_time_vec.len() < 2 {
|
|
|
return;
|
|
|
@@ -382,6 +389,8 @@ impl Predictor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // ============================ 平仓逻辑处理 =======================
|
|
|
+ let close_rate = (self.fair_price - self.fair_price_ema_long) / self.fair_price_ema_long;
|
|
|
// 重置平仓焦点,条件1
|
|
|
if !self.fair_rate_focus_close.is_zero() && self.inventory.is_zero() {
|
|
|
self.fair_rate_focus_close = Decimal::ZERO;
|
|
|
@@ -400,28 +409,25 @@ impl Predictor {
|
|
|
}
|
|
|
// 更新程序关注的平仓焦点
|
|
|
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 {
|
|
|
- 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 && close_rate > Decimal::ZERO {
|
|
|
+ if self.mid_price > self.pos_avg_price {
|
|
|
+ self.fair_rate_focus_close = close_rate;
|
|
|
+ self.fair_price_focus_close = self.fair_price;
|
|
|
+ } else if self.t_diff.is_zero() {
|
|
|
+ self.fair_rate_focus_close = close_rate;
|
|
|
+ self.fair_price_focus_close = self.fair_price;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 空单平仓逻辑
|
|
|
- if self.inventory < Decimal::ZERO && rate < Decimal::ZERO {
|
|
|
- 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 && close_rate < Decimal::ZERO {
|
|
|
+ if self.mid_price < self.pos_avg_price {
|
|
|
+ self.fair_rate_focus_close = close_rate;
|
|
|
+ self.fair_price_focus_close = self.fair_price;
|
|
|
+ } else if self.t_diff.is_zero() {
|
|
|
+ self.fair_rate_focus_close = close_rate;
|
|
|
+ self.fair_price_focus_close = self.fair_price;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -608,9 +614,9 @@ impl Predictor {
|
|
|
let bid_price = self.bid_price;
|
|
|
let last_price = self.last_price;
|
|
|
|
|
|
- let spread = self.mid_price;
|
|
|
- let spread_max = self.fair_price;
|
|
|
- let spread_min = self.fair_price_ema_30000;
|
|
|
+ let spread = self.fair_price;
|
|
|
+ let spread_max = self.fair_price_ema_short;
|
|
|
+ let spread_min = self.fair_price_ema_long;
|
|
|
// let spread = self.price_times_avg;
|
|
|
// let spread_max = self.fair_price_vec[1] / self.fair_price_vec[0];
|
|
|
// let spread_min = self.fair_price / self.mid_price;
|
|
|
@@ -620,20 +626,20 @@ impl Predictor {
|
|
|
|
|
|
let inventory = self.inventory;
|
|
|
|
|
|
- // // 这个是过去一段时间的价格变化幅度显示
|
|
|
- // let sigma_square = if self.fair_price_time_vec.len() > 1 {
|
|
|
- // let last_fair_price = self.fair_price_time_vec.deque.iter().last().unwrap();
|
|
|
- // let first_fair_price = self.fair_price_time_vec.deque[0];
|
|
|
- // (last_fair_price - first_fair_price) / first_fair_price
|
|
|
- // } else {
|
|
|
- // Decimal::ZERO
|
|
|
- // };
|
|
|
+ // 这个是过去一段时间的价格变化幅度显示
|
|
|
+ let sigma_square = if self.fair_price_time_vec.len() > 1 {
|
|
|
+ let last_fair_price = self.fair_price_time_vec.deque.iter().last().unwrap();
|
|
|
+ let first_fair_price = self.fair_price_time_vec.deque[0];
|
|
|
+ (last_fair_price - first_fair_price) / first_fair_price
|
|
|
+ } else {
|
|
|
+ Decimal::ZERO
|
|
|
+ };
|
|
|
|
|
|
- let sigma_square = self.error_rate;
|
|
|
+ // let sigma_square = self.error_rate;
|
|
|
|
|
|
let gamma = self.fair_rate_focus_open;
|
|
|
|
|
|
- let kappa = self.fair_rate_focus_close;
|
|
|
+ let kappa = (self.fair_price - self.fair_price_ema_long) / self.fair_price_ema_long;
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
let ref_price = self.fair_price;
|