|
@@ -43,6 +43,8 @@ pub struct Predictor {
|
|
|
pub level: Decimal, // martin
|
|
pub level: Decimal, // martin
|
|
|
|
|
|
|
|
pub error_rate: Decimal, // 犯错概率(预估)
|
|
pub error_rate: Decimal, // 犯错概率(预估)
|
|
|
|
|
+ pub profit_point: Decimal, // 利润点数
|
|
|
|
|
+ pub profit_point_ema: Decimal, // 利润点数的ema
|
|
|
|
|
|
|
|
pub ask_delta: Decimal, // δa
|
|
pub ask_delta: Decimal, // δa
|
|
|
pub bid_delta: Decimal, // δb
|
|
pub bid_delta: Decimal, // δb
|
|
@@ -188,6 +190,8 @@ impl Predictor {
|
|
|
level: Default::default(),
|
|
level: Default::default(),
|
|
|
pos_amount: Default::default(),
|
|
pos_amount: Default::default(),
|
|
|
error_rate: Default::default(),
|
|
error_rate: Default::default(),
|
|
|
|
|
+ profit_point: Default::default(),
|
|
|
|
|
+ profit_point_ema: Default::default(),
|
|
|
last_update_time: Default::default(),
|
|
last_update_time: Default::default(),
|
|
|
last_index: Default::default(),
|
|
last_index: Default::default(),
|
|
|
pos_avg_price: Default::default(),
|
|
pos_avg_price: Default::default(),
|
|
@@ -224,6 +228,14 @@ impl Predictor {
|
|
|
|
|
|
|
|
let mul = Decimal::from(self.profit_fixed_vec.len());
|
|
let mul = Decimal::from(self.profit_fixed_vec.len());
|
|
|
self.profit_fixed_vec.push(profit_now * mul);
|
|
self.profit_fixed_vec.push(profit_now * mul);
|
|
|
|
|
+
|
|
|
|
|
+ let total: Decimal = self.profit_fixed_vec.iter().sum();
|
|
|
|
|
+ self.profit_point = if total > Decimal::ZERO {
|
|
|
|
|
+ (total + Decimal::ONE).ln()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ -(total.abs() + Decimal::ONE).ln()
|
|
|
|
|
+ };
|
|
|
|
|
+ self.profit_point_ema = self.profit_point_ema * dec!(0.995) + self.profit_point * dec!(0.005);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -306,6 +318,8 @@ impl Predictor {
|
|
|
if prev_inventory != self.inventory && self.inventory.is_zero() {
|
|
if prev_inventory != self.inventory && self.inventory.is_zero() {
|
|
|
self.trade_fixed_vec.clear();
|
|
self.trade_fixed_vec.clear();
|
|
|
self.profit_fixed_vec.clear();
|
|
self.profit_fixed_vec.clear();
|
|
|
|
|
+ self.profit_point = Decimal::ZERO;
|
|
|
|
|
+ self.profit_point_ema = Decimal::ZERO;
|
|
|
|
|
|
|
|
self.error_rate = Decimal::ZERO;
|
|
self.error_rate = Decimal::ZERO;
|
|
|
}
|
|
}
|
|
@@ -437,8 +451,8 @@ impl Predictor {
|
|
|
self.fair_rate_focus_close = Decimal::ZERO;
|
|
self.fair_rate_focus_close = Decimal::ZERO;
|
|
|
}
|
|
}
|
|
|
// 重置平仓焦点,条件2
|
|
// 重置平仓焦点,条件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_rate_focus_close.is_zero() && self.fair_rate_focus_close != dec!(-0.0314159) {
|
|
|
|
|
+ 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 {
|
|
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;
|
|
self.fair_rate_focus_close = Decimal::ZERO;
|
|
@@ -462,10 +476,10 @@ impl Predictor {
|
|
|
|
|
|
|
|
if self.mid_price > target_price {
|
|
if self.mid_price > target_price {
|
|
|
self.fair_rate_focus_close = close_rate;
|
|
self.fair_rate_focus_close = close_rate;
|
|
|
- self.fair_price_focus_close = self.fair_price;
|
|
|
|
|
|
|
+ self.fair_price_focus_close = self.mid_price;
|
|
|
} else if self.t_diff.is_zero() {
|
|
} else if self.t_diff.is_zero() {
|
|
|
self.fair_rate_focus_close = close_rate;
|
|
self.fair_rate_focus_close = close_rate;
|
|
|
- self.fair_price_focus_close = self.fair_price;
|
|
|
|
|
|
|
+ self.fair_price_focus_close = self.mid_price;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -487,6 +501,20 @@ impl Predictor {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // ============================ 止损逻辑处理 =======================
|
|
|
|
|
+ if self.fair_rate_focus_close.is_zero() && !self.inventory.is_zero() && self.profit_point < dec!(-6) {
|
|
|
|
|
+ self.fair_rate_focus_close = dec!(-0.0314159);
|
|
|
|
|
+ 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!("止损, 在价格{}处,成本价{}({})。", self.fair_price_focus_close, self.pos_avg_price, self.pos_amount);
|
|
|
|
|
+ info!("----------------------------------------");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// // 判断价格是否回归
|
|
// // 判断价格是否回归
|
|
@@ -534,8 +562,8 @@ impl Predictor {
|
|
|
// 可能是接针
|
|
// 可能是接针
|
|
|
let is_open_long = self.fair_rate_focus_open < Decimal::ZERO && self.fair_price > self.mid_price;
|
|
let is_open_long = self.fair_rate_focus_open < Decimal::ZERO && self.fair_price > self.mid_price;
|
|
|
let is_open_short = self.fair_rate_focus_open > Decimal::ZERO && self.fair_price < self.mid_price;
|
|
let is_open_short = self.fair_rate_focus_open > Decimal::ZERO && self.fair_price < self.mid_price;
|
|
|
- let is_close_long = self.inventory > Decimal::ZERO && self.fair_rate_focus_close > Decimal::ZERO;
|
|
|
|
|
- let is_close_short = self.inventory < Decimal::ZERO && self.fair_rate_focus_close < Decimal::ZERO;
|
|
|
|
|
|
|
+ let is_close_long = self.inventory > Decimal::ZERO && (self.fair_rate_focus_close > Decimal::ZERO || self.fair_rate_focus_close == dec!(-0.0314159));
|
|
|
|
|
+ let is_close_short = self.inventory < Decimal::ZERO && (self.fair_rate_focus_close < Decimal::ZERO || self.fair_rate_focus_close == dec!(-0.0314159));
|
|
|
|
|
|
|
|
self.bid_delta = dec!(-2);
|
|
self.bid_delta = dec!(-2);
|
|
|
self.ask_delta = dec!(-2);
|
|
self.ask_delta = dec!(-2);
|
|
@@ -664,8 +692,8 @@ 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.spread;
|
|
|
|
|
- let spread_max = self.spread_ema_1000;
|
|
|
|
|
|
|
+ let spread = self.profit_point;
|
|
|
|
|
+ let spread_max = self.profit_point_ema;
|
|
|
let spread_min = Self::DONT_VIEW;
|
|
let spread_min = Self::DONT_VIEW;
|
|
|
// 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];
|
|
@@ -676,17 +704,12 @@ impl Predictor {
|
|
|
|
|
|
|
|
let inventory = self.inventory;
|
|
let inventory = self.inventory;
|
|
|
|
|
|
|
|
- let p: Decimal = self.profit_fixed_vec.iter().sum();
|
|
|
|
|
- let sigma_square = if p > Decimal::ZERO {
|
|
|
|
|
- (p + Decimal::ONE).ln()
|
|
|
|
|
- } else {
|
|
|
|
|
- -(p.abs() + Decimal::ONE).ln()
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ let sigma_square = self.spread;
|
|
|
// let sigma_square = self.error_rate;
|
|
// let sigma_square = self.error_rate;
|
|
|
|
|
|
|
|
let gamma = self.fair_rate_focus_open;
|
|
let gamma = self.fair_rate_focus_open;
|
|
|
|
|
|
|
|
- let kappa = self.error_rate;
|
|
|
|
|
|
|
+ let kappa = self.params.open_activate;
|
|
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
|
|
|