|
@@ -38,7 +38,7 @@ pub struct Predictor {
|
|
|
|
|
|
|
|
pub profit_point: Decimal, // 利润点数
|
|
pub profit_point: Decimal, // 利润点数
|
|
|
pub profit_point_ema: Decimal, // 利润点数的ema
|
|
pub profit_point_ema: Decimal, // 利润点数的ema
|
|
|
- pub profit_fixed_vec: Vec<Decimal>, // 利润队列
|
|
|
|
|
|
|
+ pub profit_point_vec: Vec<Decimal>, // 利润队列
|
|
|
|
|
|
|
|
pub inventory: Decimal, // 库存,也就是q
|
|
pub inventory: Decimal, // 库存,也就是q
|
|
|
pub pos_amount: Decimal, // 原始持仓量
|
|
pub pos_amount: Decimal, // 原始持仓量
|
|
@@ -147,7 +147,7 @@ impl Predictor {
|
|
|
trade_long_vec: FixedTimeRangeDeque::new(Self::TRADE_LONG_RANGE_MICROS),
|
|
trade_long_vec: FixedTimeRangeDeque::new(Self::TRADE_LONG_RANGE_MICROS),
|
|
|
trade_short_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
|
|
trade_short_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
|
|
|
trade_fixed_vec: vec![],
|
|
trade_fixed_vec: vec![],
|
|
|
- profit_fixed_vec: vec![],
|
|
|
|
|
|
|
+ profit_point_vec: vec![],
|
|
|
record_vec: VecDeque::new(),
|
|
record_vec: VecDeque::new(),
|
|
|
|
|
|
|
|
mid_price: Default::default(),
|
|
mid_price: Default::default(),
|
|
@@ -215,13 +215,16 @@ impl Predictor {
|
|
|
self.mid_price_time_vec.push_back(self.mid_price);
|
|
self.mid_price_time_vec.push_back(self.mid_price);
|
|
|
|
|
|
|
|
if !self.inventory.is_zero() {
|
|
if !self.inventory.is_zero() {
|
|
|
- let profit_now = if self.inventory > Decimal::ZERO {
|
|
|
|
|
|
|
+ let mut profit_now = if self.inventory > Decimal::ZERO {
|
|
|
(self.mid_price - self.pos_avg_price) / self.pos_avg_price
|
|
(self.mid_price - self.pos_avg_price) / self.pos_avg_price
|
|
|
} else {
|
|
} else {
|
|
|
(self.pos_avg_price - self.mid_price) / self.pos_avg_price
|
|
(self.pos_avg_price - self.mid_price) / self.pos_avg_price
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- self.profit_fixed_vec.push(profit_now - dec!(0.001));
|
|
|
|
|
|
|
+ profit_now -= dec!(0.001);
|
|
|
|
|
+ profit_now.rescale(4);
|
|
|
|
|
+
|
|
|
|
|
+ self.profit_point_vec.push(profit_now);
|
|
|
|
|
|
|
|
// let total: Decimal = self.profit_fixed_vec.iter().sum();
|
|
// let total: Decimal = self.profit_fixed_vec.iter().sum();
|
|
|
self.profit_point = profit_now;
|
|
self.profit_point = profit_now;
|
|
@@ -306,7 +309,7 @@ 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_point_vec.clear();
|
|
|
self.profit_point = Decimal::ZERO;
|
|
self.profit_point = Decimal::ZERO;
|
|
|
self.profit_point_ema = Decimal::ZERO;
|
|
self.profit_point_ema = Decimal::ZERO;
|
|
|
|
|
|
|
@@ -440,7 +443,7 @@ 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() && self.fair_rate_focus_close != dec!(-0.0314159) {
|
|
|
|
|
|
|
+ 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;
|
|
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 {
|
|
@@ -491,9 +494,9 @@ impl Predictor {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // ============================ 止损逻辑处理 =======================
|
|
|
|
|
|
|
+ // ============================ 止损逻辑处理1 =======================
|
|
|
if self.fair_rate_focus_close.is_zero() && !self.inventory.is_zero() && self.profit_point < self.params.open_activate * dec!(-5) {
|
|
if self.fair_rate_focus_close.is_zero() && !self.inventory.is_zero() && self.profit_point < self.params.open_activate * dec!(-5) {
|
|
|
- self.fair_rate_focus_close = dec!(-0.0314159);
|
|
|
|
|
|
|
+ self.fair_rate_focus_close = dec!(-0.11);
|
|
|
self.fair_price_focus_close = self.mid_price;
|
|
self.fair_price_focus_close = self.mid_price;
|
|
|
|
|
|
|
|
// let prev_open_activate = self.params.open_activate;
|
|
// let prev_open_activate = self.params.open_activate;
|
|
@@ -501,9 +504,23 @@ impl Predictor {
|
|
|
|
|
|
|
|
info!("----------------------------------------");
|
|
info!("----------------------------------------");
|
|
|
// info!("止损,参数调整:{} -> {}", prev_open_activate, self.params.open_activate);
|
|
// info!("止损,参数调整:{} -> {}", prev_open_activate, self.params.open_activate);
|
|
|
- info!("止损, 在价格{}处,成本价{}({})。", self.fair_price_focus_close, self.pos_avg_price, self.pos_amount);
|
|
|
|
|
|
|
+ info!("硬止损, 在价格{}处,成本价{},p={}。", self.fair_price_focus_close, self.pos_avg_price, self.profit_point);
|
|
|
info!("----------------------------------------");
|
|
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 {
|
|
|
|
|
+ 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.profit_point);
|
|
|
|
|
+ info!("----------------------------------------");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// // 判断价格是否回归
|
|
// // 判断价格是否回归
|
|
@@ -551,8 +568,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 || 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));
|
|
|
|
|
|
|
+ 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));
|
|
|
|
|
|
|
|
self.bid_delta = dec!(-2);
|
|
self.bid_delta = dec!(-2);
|
|
|
self.ask_delta = dec!(-2);
|
|
self.ask_delta = dec!(-2);
|
|
@@ -697,8 +714,7 @@ impl Predictor {
|
|
|
// 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.params.open_activate;
|
|
|
|
|
|
|
+ let kappa = self.fair_rate_focus_close;
|
|
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
|
|
|