|
|
@@ -27,10 +27,10 @@ pub struct Predictor {
|
|
|
pub ask_price: Decimal, // 卖一价
|
|
|
pub bid_price: Decimal, // 买一价
|
|
|
pub last_price: Decimal, // 最后成交价
|
|
|
+ pub trades_volume_10s: Decimal, // 过去10秒的成交量总和
|
|
|
+ pub trades_volume_10s_ema: Decimal, // 过去10秒的成交量总和的ema
|
|
|
pub spread: Decimal, // 当前价差
|
|
|
- pub spread_sma: Decimal, // 价差的sma,默认是sma5000
|
|
|
- pub spread_sma_2000: Decimal, // 价差的sma,2000级别
|
|
|
- pub spread_sma_1000: Decimal, // 价差的sma,1000级别
|
|
|
+ pub spread_ema_1000: Decimal, // 价差的ema,1000级别
|
|
|
|
|
|
pub optimal_ask_price: Decimal, // 卖出挂单价
|
|
|
pub optimal_bid_price: Decimal, // 买入挂单价
|
|
|
@@ -91,7 +91,7 @@ impl Predictor {
|
|
|
const TRADE_SHORT_RANGE_MICROS: i64 = 10_000_000;
|
|
|
// const ONE_MILLION: Decimal = dec!(1_000_000);
|
|
|
// const TWENTY_THOUSAND: Decimal = dec!(20_000);
|
|
|
- const UN_VIEW: Decimal = dec!(14142135623730951);
|
|
|
+ const DONT_VIEW: Decimal = dec!(14142135623730951);
|
|
|
|
|
|
pub fn new(_cci_arc: Arc<Mutex<CentralControlInfo>>, params: Params) -> Self {
|
|
|
// 创建数据通道
|
|
|
@@ -107,7 +107,7 @@ impl Predictor {
|
|
|
while let Some(value) = rx.next().await {
|
|
|
// 数据填充到对应位置
|
|
|
for i in 0..len {
|
|
|
- if value[i] == Self::UN_VIEW {
|
|
|
+ if value[i] == Self::DONT_VIEW {
|
|
|
debugs[i].push_back(None);
|
|
|
} else {
|
|
|
debugs[i].push_back(Some(value[i]));
|
|
|
@@ -153,10 +153,10 @@ impl Predictor {
|
|
|
ask_price: Default::default(),
|
|
|
bid_price: Default::default(),
|
|
|
last_price: Default::default(),
|
|
|
+ trades_volume_10s: Default::default(),
|
|
|
+ trades_volume_10s_ema: Default::default(),
|
|
|
spread: Default::default(),
|
|
|
- spread_sma: Default::default(),
|
|
|
- spread_sma_2000: Default::default(),
|
|
|
- spread_sma_1000: Default::default(),
|
|
|
+ spread_ema_1000: Default::default(),
|
|
|
optimal_ask_price: Default::default(),
|
|
|
optimal_bid_price: Default::default(),
|
|
|
|
|
|
@@ -241,6 +241,7 @@ impl Predictor {
|
|
|
self.error_rate.rescale(4);
|
|
|
|
|
|
self.last_price = trade.price;
|
|
|
+
|
|
|
// self.processor().await;
|
|
|
}
|
|
|
|
|
|
@@ -326,7 +327,7 @@ impl Predictor {
|
|
|
self.fair_price_ema_long = if self.fair_price_ema_long.is_zero() {
|
|
|
self.fair_price
|
|
|
} else {
|
|
|
- self.fair_price_ema_long * dec!(0.999967) + self.fair_price * dec!(0.000033)
|
|
|
+ self.fair_price_ema_long * dec!(0.9999) + self.fair_price * dec!(0.0001)
|
|
|
};
|
|
|
self.fair_price_ema_short = if self.fair_price_ema_short.is_zero() {
|
|
|
self.fair_price
|
|
|
@@ -345,13 +346,11 @@ impl Predictor {
|
|
|
|
|
|
// 重置开仓焦点,条件1
|
|
|
if !self.fair_rate_focus_open.is_zero() {
|
|
|
- // 向上涨,并且mid下穿fair,视为观测阶段结束
|
|
|
- if self.fair_rate_focus_open > Decimal::ZERO && self.spread_sma_1000 < Decimal::ZERO {
|
|
|
+ if self.fair_rate_focus_open > Decimal::ZERO && self.spread_ema_1000 < Decimal::ZERO {
|
|
|
self.fair_rate_focus_open = Decimal::ZERO;
|
|
|
}
|
|
|
|
|
|
- // 向下跌,并且mid上穿fair,视为观测阶段结束
|
|
|
- if self.fair_rate_focus_open < Decimal::ZERO && self.spread_sma_1000 > Decimal::ZERO {
|
|
|
+ if self.fair_rate_focus_open < Decimal::ZERO && self.spread_ema_1000 > Decimal::ZERO {
|
|
|
self.fair_rate_focus_open = Decimal::ZERO;
|
|
|
}
|
|
|
}
|
|
|
@@ -371,10 +370,14 @@ impl Predictor {
|
|
|
self.fair_rate_focus_open = Decimal::ZERO;
|
|
|
}
|
|
|
}
|
|
|
+ // 重置开仓焦点,条件4
|
|
|
+ if !self.fair_price_focus_open.is_zero() && self.trades_volume_10s < self.trades_volume_10s_ema {
|
|
|
+ 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 {
|
|
|
+ // 只有有强度的rate才有资格被称为针,而且要带量
|
|
|
+ if rate.abs() > self.params.open_activate && self.trades_volume_10s > self.trades_volume_10s_ema {
|
|
|
// 向上涨,并且fair下穿mid,视为观测阶段开始
|
|
|
if rate > Decimal::ZERO {
|
|
|
self.fair_rate_focus_open = rate;
|
|
|
@@ -467,26 +470,12 @@ impl Predictor {
|
|
|
// self.spread.rescale(8);
|
|
|
self.spread_vec.push(self.spread);
|
|
|
|
|
|
- self.spread_sma = if self.spread_sma.is_zero() {
|
|
|
- self.spread
|
|
|
- } else {
|
|
|
- self.spread_sma * dec!(0.9998) + self.spread * dec!(0.0002)
|
|
|
- };
|
|
|
- // self.spread_sma.rescale(8);
|
|
|
-
|
|
|
- self.spread_sma_2000 = if self.spread_sma_2000.is_zero() {
|
|
|
- self.spread
|
|
|
- } else {
|
|
|
- self.spread_sma_2000 * dec!(0.9995) + self.spread * dec!(0.0005)
|
|
|
- };
|
|
|
- // self.spread_sma_2000.rescale(8);
|
|
|
-
|
|
|
- self.spread_sma_1000 = if self.spread_sma_1000.is_zero() {
|
|
|
+ self.spread_ema_1000 = if self.spread_ema_1000.is_zero() {
|
|
|
self.spread
|
|
|
} else {
|
|
|
- self.spread_sma_1000 * dec!(0.999) + self.spread * dec!(0.001)
|
|
|
+ self.spread_ema_1000 * dec!(0.999) + self.spread * dec!(0.001)
|
|
|
};
|
|
|
- self.spread_sma_1000_time_vec.push_back(self.spread_sma_1000);
|
|
|
+ self.spread_sma_1000_time_vec.push_back(self.spread_ema_1000);
|
|
|
// self.spread_sma_1000.rescale(8);
|
|
|
|
|
|
while self.spread_vec.len() > 1_000 {
|
|
|
@@ -550,7 +539,7 @@ impl Predictor {
|
|
|
self.optimal_ask_price = if self.ask_delta == dec!(-1) {
|
|
|
self.bid_price
|
|
|
} else if self.ask_delta == dec!(-2) {
|
|
|
- Self::UN_VIEW
|
|
|
+ Self::DONT_VIEW
|
|
|
} else {
|
|
|
max(self.ask_price + self.ask_delta, self.bid_price)
|
|
|
};
|
|
|
@@ -558,7 +547,7 @@ impl Predictor {
|
|
|
self.optimal_bid_price = if self.bid_delta == dec!(-1) {
|
|
|
self.ask_price
|
|
|
} else if self.bid_delta == dec!(-2) {
|
|
|
- Self::UN_VIEW
|
|
|
+ Self::DONT_VIEW
|
|
|
} else {
|
|
|
min(self.bid_price - self.bid_delta, self.ask_price)
|
|
|
};
|
|
|
@@ -607,15 +596,22 @@ impl Predictor {
|
|
|
|
|
|
// #[instrument(skip(self), level="TRACE")]
|
|
|
async fn processor(&mut self) {
|
|
|
- self.update_t_diff();
|
|
|
- self.update_delta();
|
|
|
- self.update_optimal_ask_and_bid();
|
|
|
-
|
|
|
self.check_ready();
|
|
|
if !self.is_ready {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ self.trades_volume_10s = self.trade_short_vec.deque.iter().map(|item| item.value).sum();
|
|
|
+ self.trades_volume_10s_ema = if self.trades_volume_10s_ema.is_zero() {
|
|
|
+ self.trades_volume_10s
|
|
|
+ } else {
|
|
|
+ self.trades_volume_10s_ema * dec!(0.9995) + self.trades_volume_10s * dec!(0.0005)
|
|
|
+ };
|
|
|
+
|
|
|
+ self.update_t_diff();
|
|
|
+ self.update_delta();
|
|
|
+ self.update_optimal_ask_and_bid();
|
|
|
+
|
|
|
// let mut smm = Decimal::ZERO;
|
|
|
// if !self.depth_vec[1].time.is_zero() {
|
|
|
// let sma = self.depth_vec[1].asks[0].price;
|
|
|
@@ -630,10 +626,11 @@ impl Predictor {
|
|
|
let ask_price = self.ask_price;
|
|
|
let bid_price = self.bid_price;
|
|
|
let last_price = self.last_price;
|
|
|
+ let fair_price = self.fair_price_ema_long;
|
|
|
|
|
|
- let spread = self.fair_price;
|
|
|
- let spread_max = self.fair_price_ema_short;
|
|
|
- let spread_min = self.fair_price_ema_long;
|
|
|
+ let spread = self.trades_volume_10s;
|
|
|
+ let spread_max = self.trades_volume_10s_ema;
|
|
|
+ let spread_min = Self::DONT_VIEW;
|
|
|
// 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;
|
|
|
@@ -656,10 +653,9 @@ impl Predictor {
|
|
|
|
|
|
let gamma = self.fair_rate_focus_open;
|
|
|
|
|
|
- let kappa = self.trade_short_vec.deque.iter().map(|item| item.value).sum();
|
|
|
+ let kappa = Decimal::ZERO;
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
- let ref_price = self.fair_price;
|
|
|
|
|
|
let need_append = now - self.prev_insert_time > Decimal::ONE_HUNDRED;
|
|
|
if !need_append {
|
|
|
@@ -682,7 +678,7 @@ impl Predictor {
|
|
|
gamma,
|
|
|
kappa,
|
|
|
flow_ratio,
|
|
|
- ref_price
|
|
|
+ fair_price
|
|
|
]).unwrap();
|
|
|
|
|
|
self.prev_insert_time = Decimal::from(Utc::now().timestamp_millis())
|