|
@@ -379,132 +379,6 @@ impl Predictor {
|
|
|
} else {
|
|
} else {
|
|
|
self.fair_price_ema_short * dec!(0.999) + self.fair_price * dec!(0.001)
|
|
self.fair_price_ema_short * dec!(0.999) + self.fair_price * dec!(0.001)
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
- if self.fair_price_time_vec.len() < 2 {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- let mut rate = Self::get_real_rate(&self.fair_price_time_vec);
|
|
|
|
|
- rate.rescale(8);
|
|
|
|
|
-
|
|
|
|
|
- // 重置开仓焦点,条件1
|
|
|
|
|
- if !self.fair_rate_focus_open.is_zero() {
|
|
|
|
|
- if self.fair_rate_focus_open > Decimal::ZERO && self.spread_ema_1000 < Decimal::ZERO {
|
|
|
|
|
- self.fair_rate_focus_open = Decimal::ZERO;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if self.fair_rate_focus_open < Decimal::ZERO && self.spread_ema_1000 > Decimal::ZERO {
|
|
|
|
|
- self.fair_rate_focus_open = Decimal::ZERO;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // 重置开仓焦点,条件2
|
|
|
|
|
- if !self.fair_rate_focus_open.is_zero() && !self.inventory.is_zero() {
|
|
|
|
|
- self.fair_rate_focus_open = Decimal::ZERO;
|
|
|
|
|
- }
|
|
|
|
|
- // 重置开仓焦点,条件3
|
|
|
|
|
- if !self.mid_price_focus_open.is_zero() {
|
|
|
|
|
- let focus_rate = (self.mid_price - self.mid_price_focus_open) / self.mid_price_focus_open;
|
|
|
|
|
-
|
|
|
|
|
- if self.fair_rate_focus_open > Decimal::ZERO && focus_rate < Decimal::NEGATIVE_ONE * self.params.open_activate / dec!(3) {
|
|
|
|
|
- self.fair_rate_focus_open = Decimal::ZERO;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if self.fair_rate_focus_open < Decimal::ZERO && focus_rate > self.params.open_activate / dec!(3) {
|
|
|
|
|
- self.fair_rate_focus_open = Decimal::ZERO;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // 更新程序关注的开仓焦点
|
|
|
|
|
- if self.fair_rate_focus_open.is_zero() && self.inventory.is_zero() {
|
|
|
|
|
- // 只有有强度的rate才有资格被称为针,并且差价够大,
|
|
|
|
|
- if rate.abs() > self.params.open_activate && self.spread.abs() > self.params.min_spread {
|
|
|
|
|
- // 向上涨,并且fair下穿mid,视为观测阶段开始
|
|
|
|
|
- if rate > Decimal::ZERO && self.spread < Decimal::ZERO {
|
|
|
|
|
- self.fair_rate_focus_open = rate;
|
|
|
|
|
- self.mid_price_focus_open = self.mid_price;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 向下跌,并且fair上穿mid,视为观测阶段开始
|
|
|
|
|
- if rate < Decimal::ZERO && self.spread > Decimal::ZERO {
|
|
|
|
|
- self.fair_rate_focus_open = rate;
|
|
|
|
|
- self.mid_price_focus_open = self.mid_price;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ============================ 平仓逻辑处理 =======================
|
|
|
|
|
- // close_rate:
|
|
|
|
|
- // 大于0:当前价格在均线之上
|
|
|
|
|
- // 小于0:当前价格在均线之下
|
|
|
|
|
- let close_rate = rate.clone();
|
|
|
|
|
- // 重置平仓焦点,条件1
|
|
|
|
|
- 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() && self.fair_rate_focus_close > dec!(-0.1) {
|
|
|
|
|
- let focus_rate = (self.mid_price - self.fair_price_focus_close) / self.fair_price_focus_close;
|
|
|
|
|
-
|
|
|
|
|
- if self.fair_rate_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_rate_focus_close < Decimal::ZERO && focus_rate > self.params.close_activate / Decimal::TWO {
|
|
|
|
|
- self.fair_rate_focus_close = Decimal::ZERO;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // 更新程序关注的平仓焦点
|
|
|
|
|
- let close_activate = self.params.open_activate;
|
|
|
|
|
- if self.fair_rate_focus_close.is_zero() && !self.inventory.is_zero() && close_rate.abs() > close_activate {
|
|
|
|
|
- // 多单平仓逻辑
|
|
|
|
|
- if self.inventory > Decimal::ZERO && close_rate > Decimal::ZERO {
|
|
|
|
|
- if self.profit_point > Decimal::ZERO {
|
|
|
|
|
- self.fair_rate_focus_close = close_rate;
|
|
|
|
|
- self.fair_price_focus_close = self.mid_price;
|
|
|
|
|
- } else if self.t_diff.is_zero() {
|
|
|
|
|
- self.fair_rate_focus_close = close_rate;
|
|
|
|
|
- self.fair_price_focus_close = self.mid_price;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 空单平仓逻辑
|
|
|
|
|
- if self.inventory < Decimal::ZERO && close_rate < Decimal::ZERO {
|
|
|
|
|
- if self.profit_point > Decimal::ZERO {
|
|
|
|
|
- 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;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ============================ 平仓特殊逻辑处理1 =======================
|
|
|
|
|
- // if self.fair_rate_focus_close.is_zero() && !self.inventory.is_zero() && self.profit_point < dec!(-0.01) {
|
|
|
|
|
- // self.fair_rate_focus_close = dec!(-0.11);
|
|
|
|
|
- // self.fair_price_focus_close = self.mid_price;
|
|
|
|
|
- //
|
|
|
|
|
- // // let prev_open_activate = self.params.open_activate;
|
|
|
|
|
- // // self.params.open_activate = self.params.open_activate * dec!(1.5);
|
|
|
|
|
- //
|
|
|
|
|
- // info!("----------------------------------------");
|
|
|
|
|
- // // info!("止损,参数调整:{} -> {}", prev_open_activate, self.params.open_activate);
|
|
|
|
|
- // info!("硬止损, 在价格{}处,成本价{},价值={}, p={}。", self.fair_price_focus_close, self.pos_avg_price, self.pos_avg_price*self.pos_amount, self.profit_point);
|
|
|
|
|
- // info!("----------------------------------------");
|
|
|
|
|
- // }
|
|
|
|
|
- // ============================ 平仓特殊逻辑处理2 =======================
|
|
|
|
|
- if self.fair_rate_focus_close.is_zero() && !self.inventory.is_zero() && self.profit_point_vec.len() > 1 {
|
|
|
|
|
- let prev_profit_point = self.profit_point_vec[self.profit_point_vec.len() - 2];
|
|
|
|
|
- let profit_point = self.profit_point_vec[self.profit_point_vec.len() - 1];
|
|
|
|
|
-
|
|
|
|
|
- if (prev_profit_point >= Decimal::ZERO && profit_point < Decimal::ZERO) || (prev_profit_point > Decimal::ZERO && profit_point <= Decimal::ZERO) {
|
|
|
|
|
- self.fair_rate_focus_close = dec!(-0.12);
|
|
|
|
|
- self.fair_price_focus_close = self.mid_price;
|
|
|
|
|
-
|
|
|
|
|
- info!("----------------------------------------");
|
|
|
|
|
- info!("止损逻辑2, 在价格{}处,成本价{},价值={}, p={}。", self.fair_price_focus_close, self.pos_avg_price, self.pos_avg_price*self.pos_amount, self.profit_point);
|
|
|
|
|
- info!("----------------------------------------");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// // 判断价格是否回归
|
|
// // 判断价格是否回归
|
|
@@ -542,69 +416,32 @@ impl Predictor {
|
|
|
// -1表示市价成交(委托对手盘的价格,但不一定能市价成交),这里再想想吧,经常委托出去没成交,明显比别人慢了
|
|
// -1表示市价成交(委托对手盘的价格,但不一定能市价成交),这里再想想吧,经常委托出去没成交,明显比别人慢了
|
|
|
// 0是买一/卖一成交
|
|
// 0是买一/卖一成交
|
|
|
|
|
|
|
|
- if self.fair_price.is_zero() {
|
|
|
|
|
|
|
+ if self.fair_price.is_zero() || self.mid_price.is_zero() {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 可能是趋势
|
|
|
|
|
- // let is_open_long = self.spread_sma_1000 - self.spread_sma > self.params.open && self.fair_price > self.mid_price;
|
|
|
|
|
- // let is_open_short = self.spread_sma_1000 - self.spread_sma < self.params.open * Decimal::NEGATIVE_ONE && self.fair_price < self.mid_price;
|
|
|
|
|
// 可能是接针
|
|
// 可能是接针
|
|
|
- let is_open_long = self.fair_rate_focus_open < Decimal::ZERO;
|
|
|
|
|
- let is_open_short = self.fair_rate_focus_open > Decimal::ZERO;
|
|
|
|
|
- let is_close_long = self.inventory > Decimal::ZERO && (self.fair_rate_focus_close > Decimal::ZERO || self.fair_rate_focus_close < dec!(-0.1));
|
|
|
|
|
- let is_close_short = self.inventory < Decimal::ZERO && (self.fair_rate_focus_close < Decimal::ZERO || self.fair_rate_focus_close < dec!(-0.1));
|
|
|
|
|
|
|
+ let is_open_long = true;
|
|
|
|
|
+ let is_open_short = true;
|
|
|
|
|
+ let is_close_long = self.inventory > Decimal::ZERO;
|
|
|
|
|
+ let is_close_short = self.inventory < Decimal::ZERO;
|
|
|
|
|
|
|
|
self.bid_delta = dec!(-2);
|
|
self.bid_delta = dec!(-2);
|
|
|
self.ask_delta = dec!(-2);
|
|
self.ask_delta = dec!(-2);
|
|
|
|
|
|
|
|
if is_close_long {
|
|
if is_close_long {
|
|
|
- // let close_rate = (self.error_rate / dec!(0.5)) * self.params.close;
|
|
|
|
|
- //
|
|
|
|
|
- // self.ask_delta = self.mid_price * close_rate;
|
|
|
|
|
- self.ask_delta = self.mid_price * self.params.close;
|
|
|
|
|
|
|
+ self.optimal_ask_price = max(self.mid_price, self.fair_price) + self.mid_price * self.params.close;
|
|
|
} else if is_close_short {
|
|
} else if is_close_short {
|
|
|
- // let close_rate = (self.error_rate / dec!(0.5)) * self.params.close;
|
|
|
|
|
- //
|
|
|
|
|
- // self.bid_delta = self.mid_price * close_rate;
|
|
|
|
|
- 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 {
|
|
|
|
|
- // dec!(-1)
|
|
|
|
|
- // } else {
|
|
|
|
|
- // dec!(0)
|
|
|
|
|
- // };
|
|
|
|
|
-
|
|
|
|
|
- self.bid_delta = self.params.open * self.mid_price;
|
|
|
|
|
- } else if is_open_short {
|
|
|
|
|
- // let is_open_short_market = self.spread_sma_1000 - self.spread_sma < self.params.open_market * Decimal::NEGATIVE_ONE;
|
|
|
|
|
- // self.ask_delta = if is_open_short_market {
|
|
|
|
|
- // dec!(-1)
|
|
|
|
|
- // } else {
|
|
|
|
|
- // dec!(0)
|
|
|
|
|
- // }
|
|
|
|
|
-
|
|
|
|
|
- self.ask_delta = self.params.open * self.mid_price;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- pub fn update_optimal_ask_and_bid(&mut self) {
|
|
|
|
|
- self.optimal_ask_price = if self.ask_delta == dec!(-1) {
|
|
|
|
|
- self.bid_price
|
|
|
|
|
- } else if self.ask_delta == dec!(-2) {
|
|
|
|
|
- Self::DONT_VIEW
|
|
|
|
|
|
|
+ self.optimal_bid_price = min(self.mid_price, self.fair_price) - self.mid_price * self.params.close;
|
|
|
} else {
|
|
} else {
|
|
|
- max(self.ask_price + self.ask_delta, self.bid_price)
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ if is_open_long {
|
|
|
|
|
+ self.optimal_bid_price = min(self.mid_price, self.fair_price) - self.mid_price * self.params.open;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- self.optimal_bid_price = if self.bid_delta == dec!(-1) {
|
|
|
|
|
- self.ask_price
|
|
|
|
|
- } else if self.bid_delta == dec!(-2) {
|
|
|
|
|
- Self::DONT_VIEW
|
|
|
|
|
- } else {
|
|
|
|
|
- min(self.bid_price - self.bid_delta, self.ask_price)
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ if is_open_short {
|
|
|
|
|
+ self.optimal_ask_price = max(self.mid_price, self.fair_price) + self.mid_price * self.params.open;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
self.optimal_ask_price.rescale(self.mid_price.scale());
|
|
self.optimal_ask_price.rescale(self.mid_price.scale());
|
|
|
self.optimal_bid_price.rescale(self.mid_price.scale());
|
|
self.optimal_bid_price.rescale(self.mid_price.scale());
|
|
@@ -664,7 +501,7 @@ impl Predictor {
|
|
|
|
|
|
|
|
self.update_t_diff();
|
|
self.update_t_diff();
|
|
|
self.update_delta();
|
|
self.update_delta();
|
|
|
- self.update_optimal_ask_and_bid();
|
|
|
|
|
|
|
+ // self.update_optimal_ask_and_bid();
|
|
|
|
|
|
|
|
// let mut smm = Decimal::ZERO;
|
|
// let mut smm = Decimal::ZERO;
|
|
|
// if !self.depth_vec[1].time.is_zero() {
|
|
// if !self.depth_vec[1].time.is_zero() {
|
|
@@ -682,9 +519,9 @@ impl Predictor {
|
|
|
let last_price = self.last_price;
|
|
let last_price = self.last_price;
|
|
|
let fair_price = self.fair_price;
|
|
let fair_price = self.fair_price;
|
|
|
|
|
|
|
|
- let spread = self.profit_point;
|
|
|
|
|
- let spread_max = self.profit_point_ema;
|
|
|
|
|
- let spread_min = Self::DONT_VIEW;
|
|
|
|
|
|
|
+ let spread = self.mid_price;
|
|
|
|
|
+ let spread_max = self.optimal_ask_price;
|
|
|
|
|
+ let spread_min = self.optimal_bid_price;
|
|
|
// let spread = self.price_times_avg;
|
|
// let spread = self.price_times_avg;
|
|
|
// let spread_max = self.fair_price_vec[1] / self.fair_price_vec[0];
|
|
// let spread_max = self.fair_price_vec[1] / self.fair_price_vec[0];
|
|
|
// let spread_min = self.fair_price / self.mid_price;
|
|
// let spread_min = self.fair_price / self.mid_price;
|