|
|
@@ -20,9 +20,6 @@ pub struct Predictor {
|
|
|
pub spread_vec: Vec<Decimal>, // 价差队列
|
|
|
pub record_vec: VecDeque<Record>, // 蜡烛队列
|
|
|
|
|
|
- pub trade_long_vec: FixedTimeRangeDeque<Trade>, // 交易队列
|
|
|
- pub trade_short_vec: FixedTimeRangeDeque<Trade>, // 交易队列
|
|
|
-
|
|
|
pub mid_price: Decimal, // 中间价
|
|
|
pub ask_price: Decimal, // 卖一价
|
|
|
pub bid_price: Decimal, // 买一价
|
|
|
@@ -31,35 +28,19 @@ pub struct Predictor {
|
|
|
pub optimal_ask_price: Decimal, // 卖出挂单价
|
|
|
pub optimal_bid_price: Decimal, // 买入挂单价
|
|
|
|
|
|
- pub profit_point: Decimal, // 利润点数
|
|
|
- pub profit_point_ema: Decimal, // 利润点数的ema
|
|
|
- pub profit_point_vec: Vec<Decimal>, // 利润队列
|
|
|
-
|
|
|
pub inventory: Decimal, // 库存,也就是q
|
|
|
pub pos_amount: Decimal, // 原始持仓量
|
|
|
pub pos_avg_price: Decimal, // 原始持仓价格
|
|
|
|
|
|
pub signal: Decimal, // 大于0代表此时是正向信号,小于0则相反
|
|
|
|
|
|
- pub money_flow: Decimal, // 资金流
|
|
|
-
|
|
|
pub ask_delta: Decimal, // δa
|
|
|
pub bid_delta: Decimal, // δb
|
|
|
|
|
|
- pub mid_price_time_vec: FixedTimeRangeDeque<Decimal>, // 中间价格队列
|
|
|
- pub fair_price_time_vec: FixedTimeRangeDeque<Decimal>, // 公平价格队列
|
|
|
- pub fair_price_long_time_vec: FixedTimeRangeDeque<Decimal>, //
|
|
|
pub fair_price_vec: Vec<Decimal>, // 公平价格列表
|
|
|
pub fair_price_std_vec: Vec<Decimal>, // 公平价格列表,标准化之后的
|
|
|
pub price_avg_times_vec: Vec<Decimal>, // 公平所与做市所的价格倍率的平均值
|
|
|
|
|
|
- pub fair_price_ema_short: Decimal, // 公平价格_ema
|
|
|
- pub fair_price_ema_long: Decimal, // 公平价格_ema
|
|
|
- pub fair_rate_focus_open: Decimal, // 变化幅度焦点
|
|
|
- pub mid_price_focus_open: Decimal, // 观测焦点时的价格
|
|
|
- pub fair_rate_focus_close: Decimal, // 变化幅度焦点
|
|
|
- pub mid_price_focus_close: Decimal, // 观测焦点时的价格
|
|
|
-
|
|
|
pub is_ready: bool, // 是否已准备好
|
|
|
|
|
|
pub prev_trade_time: i64, // 上次交易时间,也就是t
|
|
|
@@ -80,10 +61,10 @@ pub struct Predictor {
|
|
|
impl Predictor {
|
|
|
// 时间窗口大小(微秒)
|
|
|
// const MAX_TIME_RANGE_MICROS: i64 = 3 * 60_000_000;
|
|
|
- const TIME_DIFF_RANGE_MICROS: i64 = 15 * 60_000_000;
|
|
|
- const TRADE_LONG_RANGE_MICROS: i64 = 60_000_000;
|
|
|
+ // const TIME_DIFF_RANGE_MICROS: i64 = 15 * 60_000_000;
|
|
|
+ // const TRADE_LONG_RANGE_MICROS: i64 = 60_000_000;
|
|
|
// const SPREAD_RANGE_MICROS: i64 = 15 * 60_000_000;
|
|
|
- const TRADE_SHORT_RANGE_MICROS: i64 = 2 * 60_000_000;
|
|
|
+ // const TRADE_SHORT_RANGE_MICROS: i64 = 2 * 60_000_000;
|
|
|
// const ONE_MILLION: Decimal = dec!(1_000_000);
|
|
|
// const TWENTY_THOUSAND: Decimal = dec!(20_000);
|
|
|
const DONT_VIEW: Decimal = dec!(14142135623730951);
|
|
|
@@ -140,9 +121,6 @@ impl Predictor {
|
|
|
price_avg_times_vec: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
spread_vec: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
|
|
|
- trade_long_vec: FixedTimeRangeDeque::new(Self::TRADE_LONG_RANGE_MICROS),
|
|
|
- trade_short_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
|
|
|
- profit_point_vec: vec![],
|
|
|
record_vec: VecDeque::new(),
|
|
|
|
|
|
mid_price: Default::default(),
|
|
|
@@ -156,26 +134,13 @@ impl Predictor {
|
|
|
ask_delta: Default::default(),
|
|
|
bid_delta: Default::default(),
|
|
|
|
|
|
- fair_price_time_vec: FixedTimeRangeDeque::new((params.second_observation_time.to_f64().unwrap() * 1_000_000f64).to_i64().unwrap()),
|
|
|
- fair_price_long_time_vec: FixedTimeRangeDeque::new(5 * 60_000_000),
|
|
|
- mid_price_time_vec: FixedTimeRangeDeque::new(100_000),
|
|
|
- fair_price_ema_short: Default::default(),
|
|
|
- fair_price_ema_long: Default::default(),
|
|
|
- fair_rate_focus_open: Default::default(),
|
|
|
- mid_price_focus_open: Default::default(),
|
|
|
- fair_rate_focus_close: Default::default(),
|
|
|
- mid_price_focus_close: Default::default(),
|
|
|
-
|
|
|
is_ready: false,
|
|
|
prev_trade_time: Utc::now().timestamp_micros(),
|
|
|
t_diff: Default::default(),
|
|
|
pos_amount: Default::default(),
|
|
|
|
|
|
- money_flow: Default::default(),
|
|
|
signal: Default::default(),
|
|
|
|
|
|
- profit_point: Default::default(),
|
|
|
- profit_point_ema: Default::default(),
|
|
|
last_update_time: Default::default(),
|
|
|
last_index: Default::default(),
|
|
|
pos_avg_price: Default::default(),
|
|
|
@@ -200,28 +165,8 @@ impl Predictor {
|
|
|
self.ask_price = depth.asks[0].price;
|
|
|
self.bid_price = depth.bids[0].price;
|
|
|
self.mid_price = (self.ask_price + self.bid_price) / Decimal::TWO;
|
|
|
-
|
|
|
- self.mid_price_time_vec.push_back(self.mid_price);
|
|
|
-
|
|
|
- if !self.inventory.is_zero() {
|
|
|
- let mut profit_now = if self.inventory > Decimal::ZERO {
|
|
|
- (self.mid_price - self.pos_avg_price) / self.pos_avg_price
|
|
|
- } else {
|
|
|
- (self.pos_avg_price - self.mid_price) / self.pos_avg_price
|
|
|
- };
|
|
|
-
|
|
|
- profit_now -= dec!(0.0006);
|
|
|
- profit_now.rescale(8);
|
|
|
-
|
|
|
- self.profit_point_vec.push(profit_now);
|
|
|
-
|
|
|
- // let total: Decimal = self.profit_fixed_vec.iter().sum();
|
|
|
- self.profit_point = profit_now;
|
|
|
- self.profit_point_ema = self.profit_point_ema * dec!(0.99) + self.profit_point * dec!(0.01);
|
|
|
- }
|
|
|
} else {
|
|
|
self.update_fair_price(depth, index).await;
|
|
|
- self.update_spread();
|
|
|
|
|
|
self.depth_vec[index] = depth.clone();
|
|
|
}
|
|
|
@@ -233,9 +178,6 @@ impl Predictor {
|
|
|
}
|
|
|
|
|
|
pub async fn on_trade(&mut self, trade: &Trade, _index: usize) {
|
|
|
- self.trade_long_vec.push_back(trade.clone());
|
|
|
- self.trade_short_vec.push_back(trade.clone());
|
|
|
-
|
|
|
self.last_price = trade.price;
|
|
|
|
|
|
// self.processor().await;
|
|
|
@@ -267,20 +209,6 @@ impl Predictor {
|
|
|
self.prev_trade_time = Utc::now().timestamp_micros();
|
|
|
}
|
|
|
|
|
|
- // 重置fair数据,用于重新计算幅度
|
|
|
- if prev_inventory != self.inventory {
|
|
|
- self.fair_price_time_vec.deque.clear();
|
|
|
- }
|
|
|
-
|
|
|
- // 重置资金流计算
|
|
|
- if prev_inventory != self.inventory && self.inventory.is_zero() {
|
|
|
- self.profit_point_vec.clear();
|
|
|
- self.profit_point = Decimal::ZERO;
|
|
|
- self.profit_point_ema = Decimal::ZERO;
|
|
|
-
|
|
|
- self.money_flow = Decimal::ZERO;
|
|
|
- }
|
|
|
-
|
|
|
self.processor().await;
|
|
|
}
|
|
|
|
|
|
@@ -341,44 +269,6 @@ impl Predictor {
|
|
|
}
|
|
|
self.signal = self.signal / self.params.min_spread;
|
|
|
self.signal.rescale(0);
|
|
|
-
|
|
|
- // 更新程序关注的开仓焦点
|
|
|
- if self.fair_rate_focus_open.is_zero() && self.inventory.is_zero() {
|
|
|
- // 只有有强度的rate才有资格被称为针,并且差价够大,
|
|
|
- if self.signal.abs() == Decimal::from(self.params.ref_exchange.len()) {
|
|
|
- if self.signal > Decimal::ZERO {
|
|
|
- self.fair_rate_focus_open = dec!(0.01);
|
|
|
- self.mid_price_focus_open = self.mid_price;
|
|
|
- }
|
|
|
-
|
|
|
- if self.signal < Decimal::ZERO {
|
|
|
- self.fair_rate_focus_open = dec!(-0.01);
|
|
|
- self.mid_price_focus_open = self.mid_price;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- pub fn update_spread(&mut self) {
|
|
|
- // if self.mid_price.is_zero() || self.fair_price.is_zero() {
|
|
|
- // return;
|
|
|
- // }
|
|
|
- //
|
|
|
- // self.spread = (self.fair_price - self.mid_price) / self.mid_price;
|
|
|
- // // self.spread.rescale(8);
|
|
|
- // self.spread_vec.push(self.spread);
|
|
|
- //
|
|
|
- // self.spread_ema_1000 = if self.spread_ema_1000.is_zero() {
|
|
|
- // self.spread
|
|
|
- // } else {
|
|
|
- // self.spread_ema_1000 * dec!(0.999) + self.spread * dec!(0.001)
|
|
|
- // };
|
|
|
- // // self.spread_sma_1000.rescale(8);
|
|
|
- // // self.spread_sma_1000_time_vec.push_back(self.spread_ema_1000);
|
|
|
- //
|
|
|
- // while self.spread_vec.len() > 1_000 {
|
|
|
- // self.spread_vec.remove(0);
|
|
|
- // }
|
|
|
}
|
|
|
|
|
|
pub fn update_delta(&mut self) {
|
|
|
@@ -465,10 +355,6 @@ impl Predictor {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if self.trade_long_vec.len() < 100 {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
self.is_ready = true;
|
|
|
info!("========================================行情数据预热完毕==================================")
|
|
|
}
|
|
|
@@ -510,15 +396,10 @@ impl Predictor {
|
|
|
|
|
|
let inventory = self.inventory;
|
|
|
|
|
|
- let sigma_square = if self.fair_price_time_vec.len() > 1 {
|
|
|
- Self::get_real_rate(&self.fair_price_time_vec)
|
|
|
- } else {
|
|
|
- Decimal::ZERO
|
|
|
- };
|
|
|
- // let sigma_square = self.error_rate;
|
|
|
+ let sigma_square = self.signal;
|
|
|
|
|
|
- let gamma = self.signal;
|
|
|
- let kappa = self.fair_rate_focus_close;
|
|
|
+ let gamma = Decimal::ZERO;
|
|
|
+ let kappa = Decimal::ZERO;
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
|