|
|
@@ -18,14 +18,13 @@ use crate::utils;
|
|
|
pub struct Predictor {
|
|
|
pub depth_vec: Vec<Depth>, // 深度队列
|
|
|
pub record_vec: VecDeque<Record>, // 蜡烛队列
|
|
|
- pub prev_insert_price_time: Decimal,
|
|
|
- pub spread_vec: VecDeque<Decimal>, // 价差队列
|
|
|
- pub mid_price_long_vec: FixedTimeRangeDeque<Decimal>,
|
|
|
- pub mid_price_short_vec: FixedTimeRangeDeque<Decimal>,
|
|
|
- pub fair_price_short_vec: FixedTimeRangeDeque<Decimal>,
|
|
|
- pub trend_long_rate: Decimal,
|
|
|
- pub trend_short_rate: Decimal,
|
|
|
- pub fair_rate: Decimal,
|
|
|
+
|
|
|
+ // 做市所的计算
|
|
|
+ pub close_price_vec: FixedTimeRangeDeque<Record>,
|
|
|
+ pub r_short: Decimal,
|
|
|
+ pub r_long: Decimal,
|
|
|
+ pub speed: Decimal,
|
|
|
+ pub trend: Decimal,
|
|
|
|
|
|
pub prices: Vec<Vec<FixedTimeRangeDeque<Decimal>>>, // [[[做市所], [参考所0]], ...]
|
|
|
pub ks: Vec<Decimal>,
|
|
|
@@ -36,9 +35,6 @@ pub struct Predictor {
|
|
|
pub bid_price: Decimal, // 中间价
|
|
|
pub fair_price: Decimal,
|
|
|
pub last_price: Decimal, // 最后成交价
|
|
|
- pub ma_short: Decimal,
|
|
|
- pub ma_long: Decimal,
|
|
|
- pub trend_rate: Decimal,
|
|
|
|
|
|
pub optimal_ask_price: Decimal, // 卖出挂单价
|
|
|
pub optimal_bid_price: Decimal, // 买入挂单价
|
|
|
@@ -52,13 +48,14 @@ pub struct Predictor {
|
|
|
pub profit_high: Decimal,
|
|
|
|
|
|
pub prev_open_time: Decimal,
|
|
|
- pub trade_condition: Decimal, // 过去1分钟以内,差价是否满足过扩大行为+行情有针
|
|
|
- pub trade_condition_time: Decimal, // 满足时的瞬时时间
|
|
|
+
|
|
|
+ pub trade_condition: Decimal, // 交易信号
|
|
|
+ pub trade_condition_time: Decimal, // 满足时的瞬时时间,用于控制开仓行为的持续时间
|
|
|
|
|
|
pub ask_delta: Decimal, // δa
|
|
|
pub bid_delta: Decimal, // δb
|
|
|
|
|
|
- pub mid_price_vec: Vec<Decimal>, //
|
|
|
+ pub mid_price_vec: Vec<Decimal>, // 每个交易所的中间价
|
|
|
pub fair_price_std_vec: Vec<Decimal>, // 公平价格列表,标准化之后的
|
|
|
pub price_avg_times_vec: Vec<Decimal>, // 公平所与做市所的价格倍率的平均值
|
|
|
pub price_avg_times_long_vec: Vec<Decimal>, // 公平所与做市所的价格倍率的平均值
|
|
|
@@ -161,21 +158,18 @@ impl Predictor {
|
|
|
bs: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
prev_fitting_time_vec: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
|
|
|
- mid_price_long_vec: FixedTimeRangeDeque::new(10_000_000),
|
|
|
- mid_price_short_vec: FixedTimeRangeDeque::new(2_000_000),
|
|
|
- fair_price_short_vec: FixedTimeRangeDeque::new(2_000_000),
|
|
|
+ close_price_vec: FixedTimeRangeDeque::new(600_000_000),
|
|
|
+ r_short: Default::default(),
|
|
|
+ r_long: Default::default(),
|
|
|
+ speed: Default::default(),
|
|
|
+ trend: Default::default(),
|
|
|
|
|
|
- spread_vec: VecDeque::new(),
|
|
|
mid_price: Default::default(),
|
|
|
ask_price: Default::default(),
|
|
|
bid_price: Default::default(),
|
|
|
fair_price: Default::default(),
|
|
|
last_price: Default::default(),
|
|
|
|
|
|
- ma_short: Default::default(),
|
|
|
- ma_long: Default::default(),
|
|
|
- trend_rate: Default::default(),
|
|
|
-
|
|
|
optimal_ask_price: Self::DONT_VIEW,
|
|
|
optimal_bid_price: Self::DONT_VIEW,
|
|
|
|
|
|
@@ -209,10 +203,6 @@ impl Predictor {
|
|
|
debug_sender: tx,
|
|
|
prev_open_time: Default::default(),
|
|
|
|
|
|
- trend_long_rate: Default::default(),
|
|
|
- trend_short_rate: Default::default(),
|
|
|
- fair_rate: Default::default(),
|
|
|
- prev_insert_price_time: Default::default(),
|
|
|
record_vec: Default::default(),
|
|
|
};
|
|
|
|
|
|
@@ -241,27 +231,95 @@ impl Predictor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- self.mid_price_long_vec.push_back(self.mid_price);
|
|
|
- self.mid_price_short_vec.push_back(self.mid_price);
|
|
|
+ // 秒级k线处理,先只用处理收盘价就行
|
|
|
+ let r = Record {
|
|
|
+ time: Decimal::from(Utc::now().timestamp_millis()),
|
|
|
+ open: self.mid_price,
|
|
|
+ high: self.mid_price,
|
|
|
+ low: self.mid_price,
|
|
|
+ close: self.mid_price,
|
|
|
+ volume: Default::default(),
|
|
|
+ symbol: "".to_string(),
|
|
|
+ };
|
|
|
+ let is_need_push = self.close_price_vec.len() == 0
|
|
|
+ || self.close_price_vec.deque.iter().last().unwrap().time - r.time > Decimal::ONE_THOUSAND
|
|
|
+ ;
|
|
|
|
|
|
- self.trend_long_rate = if self.mid_price_long_vec.len() > 2 {
|
|
|
- let first = self.mid_price_long_vec.deque.front().unwrap();
|
|
|
- let last = self.mid_price_long_vec.deque.back().unwrap();
|
|
|
+ if is_need_push {
|
|
|
+ self.close_price_vec.push_back(r);
|
|
|
|
|
|
- (last - first) / first
|
|
|
- } else {
|
|
|
- Decimal::ZERO
|
|
|
- };
|
|
|
- self.trend_long_rate.rescale(6);
|
|
|
- self.trend_short_rate = if self.mid_price_short_vec.len() > 2 {
|
|
|
- let first = self.mid_price_short_vec.deque.front().unwrap();
|
|
|
- let last = self.mid_price_short_vec.deque.back().unwrap();
|
|
|
+ let len = self.close_price_vec.len();
|
|
|
|
|
|
- (last - first) / first
|
|
|
- } else {
|
|
|
- Decimal::ZERO
|
|
|
- };
|
|
|
- self.trend_short_rate.rescale(6);
|
|
|
+ // 求最后10秒的均值
|
|
|
+ let mean_10s;
|
|
|
+ if len >= 10 {
|
|
|
+ let mut i = len - 1;
|
|
|
+ let mut sum: Decimal = Decimal::ZERO;
|
|
|
+ loop {
|
|
|
+ if i == len - 11 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ sum += self.close_price_vec.get(i).unwrap().close;
|
|
|
+
|
|
|
+ i = i - 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ mean_10s = sum / Decimal::from(10);
|
|
|
+ self.r_short = (self.mid_price - mean_10s) / mean_10s;
|
|
|
+ self.r_short.rescale(8);
|
|
|
+ } else {
|
|
|
+ self.r_short = self.mid_price;
|
|
|
+ mean_10s = self.mid_price;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 求最后300秒的均值,如果秒级k不到300秒,就用5分钟k的收盘价凑合用用
|
|
|
+ let mean_300s;
|
|
|
+ if len >= 300 {
|
|
|
+ let mut i = len - 1;
|
|
|
+ let mut sum: Decimal = Decimal::ZERO;
|
|
|
+ loop {
|
|
|
+ if i == len - 301 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ sum += self.close_price_vec.get(i).unwrap().close;
|
|
|
+
|
|
|
+ i = i - 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ mean_300s = sum / Decimal::from(300);
|
|
|
+ self.r_long = (self.mid_price - mean_300s) / mean_300s;
|
|
|
+ self.r_long.rescale(8);
|
|
|
+ } else if self.record_vec.len() == 5 && !self.mid_price.is_zero() {
|
|
|
+ let mut i = self.record_vec.len() - 1;
|
|
|
+ let mut sum: Decimal = Decimal::ZERO;
|
|
|
+ loop {
|
|
|
+ if i == 0 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ sum += self.record_vec[i].close;
|
|
|
+
|
|
|
+ i = i - 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ mean_300s = sum / Decimal::from(5);
|
|
|
+ self.r_long = (self.mid_price - mean_300s) / mean_300s;
|
|
|
+ self.r_long.rescale(8);
|
|
|
+ } else {
|
|
|
+ self.r_long = self.mid_price;
|
|
|
+ mean_300s = self.mid_price;
|
|
|
+ }
|
|
|
+
|
|
|
+ self.speed = if self.r_short > dec!(-0.0001) || self.r_long > dec!(-0.0001) {
|
|
|
+ Decimal::ONE
|
|
|
+ } else {
|
|
|
+ self.r_short / self.r_long
|
|
|
+ };
|
|
|
+
|
|
|
+ self.trend = mean_10s / mean_300s;
|
|
|
+ }
|
|
|
|
|
|
// 拟合k与b
|
|
|
for (mid_index, mp) in self.mid_price_vec.iter().enumerate() {
|
|
|
@@ -327,46 +385,9 @@ impl Predictor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if self.record_vec.len() > 50 {
|
|
|
+ if self.record_vec.len() > 5 {
|
|
|
self.record_vec.pop_front();
|
|
|
}
|
|
|
-
|
|
|
- // 计算ma
|
|
|
- if self.record_vec.len() == 50 && !self.mid_price.is_zero() {
|
|
|
- let len = self.record_vec.len();
|
|
|
- // ma200
|
|
|
- let mut i = len - 1;
|
|
|
- let mut sum: Decimal = Decimal::ZERO;
|
|
|
- loop {
|
|
|
- if i == 0 {
|
|
|
- break
|
|
|
- }
|
|
|
-
|
|
|
- sum += self.record_vec[i].close;
|
|
|
-
|
|
|
- i = i - 1;
|
|
|
- }
|
|
|
- self.ma_long = sum / Decimal::from(self.record_vec.len());
|
|
|
- // ma20
|
|
|
- let mut i = len - 1;
|
|
|
- let mut sum: Decimal = Decimal::ZERO;
|
|
|
- loop {
|
|
|
- if i == len - 21 {
|
|
|
- break
|
|
|
- }
|
|
|
-
|
|
|
- sum += self.record_vec[i].close;
|
|
|
-
|
|
|
- i = i - 1;
|
|
|
- }
|
|
|
- self.ma_short = sum / Decimal::from(20);
|
|
|
-
|
|
|
- self.ma_short.rescale(self.mid_price.scale());
|
|
|
- self.ma_long.rescale(self.mid_price.scale());
|
|
|
-
|
|
|
- self.trend_rate = self.ma_short / self.ma_long;
|
|
|
- self.trend_rate.rescale(8);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
pub async fn on_inventory(&mut self, pos_amount: &Decimal, pos_avg_price: &Decimal, min_amount_value: &Decimal, update_time: Decimal) {
|
|
|
@@ -398,10 +419,6 @@ impl Predictor {
|
|
|
}
|
|
|
// 平仓
|
|
|
if self.pos_amount.is_zero() {
|
|
|
- self.mid_price_long_vec.deque.clear();
|
|
|
- self.mid_price_short_vec.deque.clear();
|
|
|
- self.fair_price_short_vec.deque.clear();
|
|
|
-
|
|
|
self.profit = Decimal::ZERO;
|
|
|
self.profit_high = Decimal::ZERO;
|
|
|
}
|
|
|
@@ -458,18 +475,6 @@ impl Predictor {
|
|
|
dec!(0.9) * self.fair_price + dec!(0.1) * fair_price_sum / Decimal::from(fair_price_count)
|
|
|
};
|
|
|
|
|
|
- self.fair_price_short_vec.push_back(self.fair_price);
|
|
|
-
|
|
|
- self.fair_rate = if self.fair_price_short_vec.len() > 2 {
|
|
|
- let first = self.fair_price_short_vec.deque.front().unwrap();
|
|
|
- let last = self.fair_price_short_vec.deque.back().unwrap();
|
|
|
-
|
|
|
- (last - first) / first
|
|
|
- } else {
|
|
|
- Decimal::ZERO
|
|
|
- };
|
|
|
- self.fair_rate.rescale(6);
|
|
|
-
|
|
|
// let mut spread_abs = ((self.fair_price - self.mid_price) / self.mid_price).abs();
|
|
|
// spread_abs.rescale(5);
|
|
|
//
|
|
|
@@ -509,10 +514,9 @@ impl Predictor {
|
|
|
|
|
|
let is_open_long = self.inventory.is_zero()
|
|
|
&& self.fair_price > self.mid_price * (Decimal::ONE + self.params.open)
|
|
|
- && self.trend_long_rate < dec!(-0.005)
|
|
|
- && self.fair_rate > Decimal::ZERO
|
|
|
- && self.fair_rate > self.trend_short_rate + dec!(0.0001)
|
|
|
- && self.trend_rate < dec!(0.985)
|
|
|
+ && self.r_short < dec!(-0.001)
|
|
|
+ && self.trend < dec!(0.999)
|
|
|
+ && self.speed < dec!(0.15)
|
|
|
;
|
|
|
|
|
|
let is_open_short = self.inventory.is_zero()
|
|
|
@@ -604,14 +608,6 @@ impl Predictor {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if self.ma_long.is_zero() {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if self.ma_short.is_zero() {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
if self.optimal_bid_price.is_zero() {
|
|
|
return;
|
|
|
}
|
|
|
@@ -698,9 +694,9 @@ impl Predictor {
|
|
|
let last_price = Self::DONT_VIEW;
|
|
|
let fair_price = self.fair_price;
|
|
|
|
|
|
- let spread = self.mid_price;
|
|
|
- let spread_max = self.ma_short;
|
|
|
- let spread_min = self.ma_long;
|
|
|
+ let spread = Self::DONT_VIEW;
|
|
|
+ let spread_max = self.r_short;
|
|
|
+ let spread_min = self.r_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;
|
|
|
@@ -708,9 +704,9 @@ impl Predictor {
|
|
|
let inventory = self.inventory;
|
|
|
|
|
|
// let sigma_square = Decimal::from(Utc::now().timestamp_millis()) - data_time;
|
|
|
- let sigma_square = self.trend_rate;
|
|
|
- let gamma = self.balance;
|
|
|
- let kappa = self.profit;
|
|
|
+ let sigma_square = self.speed;
|
|
|
+ let gamma = self.trend;
|
|
|
+ let kappa = self.balance;
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
|