|
|
@@ -24,6 +24,8 @@ pub struct Predictor {
|
|
|
pub trade_0_vec: FixedTimeRangeDeque<Trade>,
|
|
|
|
|
|
pub prices: Vec<Vec<VecDeque<Decimal>>>, // [[[做市所], [参考所0]], ...]
|
|
|
+ pub ks: Vec<Decimal>,
|
|
|
+ pub bs: Vec<Decimal>,
|
|
|
|
|
|
pub mid_price: Decimal, // 中间价
|
|
|
pub fair_price: Decimal,
|
|
|
@@ -43,6 +45,7 @@ pub struct Predictor {
|
|
|
pub ask_delta: Decimal, // δa
|
|
|
pub bid_delta: Decimal, // δb
|
|
|
|
|
|
+ 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>, // 公平所与做市所的价格倍率的平均值
|
|
|
@@ -121,10 +124,13 @@ impl Predictor {
|
|
|
// 接针版本
|
|
|
depth_vec: vec![Depth::new(); params.ref_exchange.len()],
|
|
|
fair_price_std_vec: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
+ mid_price_vec: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
price_avg_times_vec: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
price_avg_times_long_vec: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
|
|
|
prices: vec![vec![VecDeque::new(); 2]; params.ref_exchange.len()],
|
|
|
+ ks: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
+ bs: vec![Decimal::ZERO; params.ref_exchange.len()],
|
|
|
|
|
|
record_vec: VecDeque::new(),
|
|
|
|
|
|
@@ -171,10 +177,37 @@ impl Predictor {
|
|
|
self.last_update_time = depth.time;
|
|
|
self.last_index = Decimal::from(index);
|
|
|
|
|
|
- if index == 233 {
|
|
|
+ if index == 233 {
|
|
|
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;
|
|
|
+
|
|
|
+ // 拟合k与b
|
|
|
+ for (mid_index, mp) in self.mid_price_vec.iter().enumerate() {
|
|
|
+ if mp.is_zero() {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ self.prices[mid_index][0].push_back(self.mid_price);
|
|
|
+ self.prices[mid_index][1].push_back(mp.clone());
|
|
|
+
|
|
|
+ // 长度限制
|
|
|
+ if self.prices[mid_index][0].len() > 10000 {
|
|
|
+ self.prices[mid_index][0].pop_front();
|
|
|
+ self.prices[mid_index][1].pop_front();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 拟合
|
|
|
+ let prev = Utc::now().timestamp_millis();
|
|
|
+ if let Some((k, b)) = self.linear_least_squares(mid_index).await {
|
|
|
+ self.ks[mid_index] = k;
|
|
|
+ self.bs[mid_index] = b;
|
|
|
+
|
|
|
+ self.nihe_time = Decimal::from(Utc::now().timestamp_millis() - prev);
|
|
|
+ } else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
self.update_fair_price(depth, index).await;
|
|
|
|
|
|
@@ -262,29 +295,15 @@ impl Predictor {
|
|
|
// let total = a1.value + b1.value;
|
|
|
// let fair_price = (a1.price + b1.price) / Decimal::TWO;
|
|
|
|
|
|
- // 生成fp
|
|
|
// self.fair_price_vec[index] = a1.price * b1.value / total + b1.price * a1.value / total;
|
|
|
let mut mp = (a1.price + b1.price) / Decimal::TWO;
|
|
|
mp.rescale(self.mid_price.scale());
|
|
|
+ self.mid_price_vec[index] = mp;
|
|
|
|
|
|
- self.prices[index][0].push_back(self.mid_price);
|
|
|
- self.prices[index][1].push_back(mp);
|
|
|
-
|
|
|
- // 长度限制
|
|
|
- if self.prices[index][0].len() > 10000 {
|
|
|
- self.prices[index][0].pop_front();
|
|
|
- self.prices[index][1].pop_front();
|
|
|
- }
|
|
|
-
|
|
|
- // 拟合
|
|
|
- let prev = Utc::now().timestamp_millis();
|
|
|
- if let Some((k, b)) = self.linear_least_squares(index).await {
|
|
|
- self.fair_price_std_vec[index] = k * mp + b;
|
|
|
+ // 生成fp
|
|
|
+ self.fair_price_std_vec[index] = mp * self.ks[index] + self.bs[index];
|
|
|
+ self.fair_price_std_vec[index].rescale(self.mid_price.scale());
|
|
|
|
|
|
- self.nihe_time = Decimal::from(Utc::now().timestamp_millis() - prev);
|
|
|
- } else {
|
|
|
- return;
|
|
|
- }
|
|
|
|
|
|
// 生成最终用于挂单的公平价格
|
|
|
let fair_price_sum: Decimal = self.fair_price_std_vec.iter().sum();
|
|
|
@@ -386,7 +405,7 @@ impl Predictor {
|
|
|
}
|
|
|
|
|
|
// 最小二乘法拟合函数,支持VecDeque
|
|
|
- pub async fn linear_least_squares(&mut self, index: usize) -> Option<(Decimal, Decimal)> {
|
|
|
+ pub async fn linear_least_squares(&self, index: usize) -> Option<(Decimal, Decimal)> {
|
|
|
let x = &self.prices[index][1];
|
|
|
let y = &self.prices[index][0];
|
|
|
|