|
|
@@ -1,4 +1,3 @@
|
|
|
-use std::cmp::{max};
|
|
|
use std::collections::{BTreeMap, VecDeque};
|
|
|
use std::sync::Arc;
|
|
|
use chrono::{Utc};
|
|
|
@@ -43,9 +42,6 @@ pub struct Predictor {
|
|
|
|
|
|
pub is_ready: bool, // 是否已准备好
|
|
|
|
|
|
- pub prev_trade_time: i64, // 上次交易时间,也就是t
|
|
|
- pub t_diff: Decimal, // (T-t)
|
|
|
-
|
|
|
pub last_update_time: Decimal, // 最后更新时间(depth)
|
|
|
pub last_index: Decimal, // 最后更新的index
|
|
|
|
|
|
@@ -130,20 +126,19 @@ impl Predictor {
|
|
|
optimal_ask_price: Default::default(),
|
|
|
optimal_bid_price: Default::default(),
|
|
|
|
|
|
- inventory: Default::default(),
|
|
|
ask_delta: Default::default(),
|
|
|
bid_delta: Default::default(),
|
|
|
|
|
|
is_ready: false,
|
|
|
- prev_trade_time: Utc::now().timestamp_micros(),
|
|
|
- t_diff: Default::default(),
|
|
|
+
|
|
|
+ inventory: Default::default(),
|
|
|
+ pos_avg_price: Default::default(),
|
|
|
pos_amount: Default::default(),
|
|
|
|
|
|
signal: Default::default(),
|
|
|
|
|
|
last_update_time: Default::default(),
|
|
|
last_index: Default::default(),
|
|
|
- pos_avg_price: Default::default(),
|
|
|
|
|
|
prev_insert_time: Default::default(),
|
|
|
prev_save_time: Decimal::from(Utc::now().timestamp_millis()),
|
|
|
@@ -192,7 +187,6 @@ impl Predictor {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- let prev_inventory = self.inventory;
|
|
|
self.pos_amount = pos_amount.clone();
|
|
|
self.pos_avg_price = pos_avg_price.clone();
|
|
|
self.inventory = (pos_amount / (min_amount_value / self.mid_price)).trunc();
|
|
|
@@ -205,10 +199,6 @@ impl Predictor {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- if prev_inventory != self.inventory && prev_inventory.is_zero() {
|
|
|
- self.prev_trade_time = Utc::now().timestamp_micros();
|
|
|
- }
|
|
|
-
|
|
|
self.processor().await;
|
|
|
}
|
|
|
|
|
|
@@ -236,15 +226,10 @@ impl Predictor {
|
|
|
|
|
|
// https://quant.stackexchange.com/questions/50651/how-to-understand-micro-price-aka-weighted-mid-price
|
|
|
let total = a1.value + b1.value;
|
|
|
- let fp = a1.price * b1.value / total + b1.price * a1.value / total;
|
|
|
// let fair_price = (a1.price + b1.price) / Decimal::TWO;
|
|
|
|
|
|
- // 生成mid price
|
|
|
- self.fair_price_vec[index] = if self.fair_price_vec[index].is_zero() {
|
|
|
- fp
|
|
|
- } else {
|
|
|
- self.fair_price_vec[index] * dec!(0.5) + fp * dec!(0.5)
|
|
|
- };
|
|
|
+ // 生成fp
|
|
|
+ self.fair_price_vec[index] = a1.price * b1.value / total + b1.price * a1.value / total;
|
|
|
self.fair_price_vec[index].rescale(self.mid_price.scale());
|
|
|
|
|
|
// 求价格倍率
|
|
|
@@ -323,15 +308,6 @@ impl Predictor {
|
|
|
self.optimal_bid_price.rescale(self.mid_price.scale());
|
|
|
}
|
|
|
|
|
|
- pub fn update_t_diff(&mut self) {
|
|
|
- if self.prev_trade_time > 0 {
|
|
|
- let time_diff_decimal = Decimal::from_i64(Utc::now().timestamp_micros() - self.prev_trade_time).unwrap();
|
|
|
- self.t_diff = max(Decimal::ONE - time_diff_decimal / Decimal::from_i64(Self::TIME_DIFF_RANGE_MICROS).unwrap(), Decimal::ZERO);
|
|
|
- } else {
|
|
|
- self.t_diff = Decimal::ONE;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
pub fn check_ready(&mut self) {
|
|
|
if self.is_ready {
|
|
|
return;
|
|
|
@@ -366,7 +342,6 @@ impl Predictor {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- self.update_t_diff();
|
|
|
self.update_delta();
|
|
|
|
|
|
// let mut smm = Decimal::ZERO;
|