|
|
@@ -1,7 +1,11 @@
|
|
|
use std::collections::{BTreeMap};
|
|
|
+use std::sync::Arc;
|
|
|
+use chrono::Utc;
|
|
|
use rust_decimal::prelude::*;
|
|
|
-use tracing::info;
|
|
|
+use tokio::sync::Mutex;
|
|
|
+use global::cci::CentralControlInfo;
|
|
|
use global::fixed_time_range_deque::FixedTimeRangeDeque;
|
|
|
+use global::predictor_state::PredictorState;
|
|
|
use standard::{Depth, Ticker, Trade};
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
@@ -23,15 +27,18 @@ pub struct AvellanedaStoikov {
|
|
|
pub sigma_square: Decimal, // σ^2,波动性的平方
|
|
|
pub gamma: Decimal, // γ,库存风险厌恶参数
|
|
|
pub kappa: Decimal, // κ 订单簿 流动性 参数
|
|
|
+
|
|
|
pub delta_plus: Decimal, // δa+δb,买卖挂单间距
|
|
|
pub ref_price: Decimal, // 预定价格
|
|
|
+
|
|
|
+ pub cci_arc: Arc<Mutex<CentralControlInfo>> // 中控信息
|
|
|
}
|
|
|
|
|
|
impl AvellanedaStoikov {
|
|
|
// 时间窗口大小(微秒)
|
|
|
- const MAX_TIME_RANGE_MICROS: i64 = 10_000_000_000;
|
|
|
+ const MAX_TIME_RANGE_MICROS: i64 = 10 * 60_000_000;
|
|
|
|
|
|
- pub fn new() -> Self {
|
|
|
+ pub fn new(cci_arc: Arc<Mutex<CentralControlInfo>>) -> Self {
|
|
|
let avellaneda_stoikov = Self {
|
|
|
// 分别给与的长度
|
|
|
depth_vec: FixedTimeRangeDeque::new(Self::MAX_TIME_RANGE_MICROS),
|
|
|
@@ -53,6 +60,8 @@ impl AvellanedaStoikov {
|
|
|
delta_plus: Default::default(),
|
|
|
kappa: Default::default(),
|
|
|
ref_price: Default::default(),
|
|
|
+
|
|
|
+ cci_arc,
|
|
|
};
|
|
|
|
|
|
avellaneda_stoikov
|
|
|
@@ -87,7 +96,7 @@ impl AvellanedaStoikov {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- pub fn on_depth(&mut self, depth: &Depth) {
|
|
|
+ pub async fn on_depth(&mut self, depth: &Depth) {
|
|
|
self.depth_vec.push_back(depth.clone());
|
|
|
|
|
|
self.ask_price = depth.asks[0].price;
|
|
|
@@ -98,18 +107,18 @@ impl AvellanedaStoikov {
|
|
|
self.update_spread(&mid_price_now);
|
|
|
self.mid_price = mid_price_now;
|
|
|
|
|
|
- self.processor();
|
|
|
+ self.processor().await;
|
|
|
}
|
|
|
|
|
|
- pub fn on_trade(&mut self, trade: &Trade) {
|
|
|
+ pub async fn on_trade(&mut self, trade: &Trade) {
|
|
|
self.trade_vec.push_back(trade.clone());
|
|
|
- self.processor();
|
|
|
+ self.processor().await;
|
|
|
}
|
|
|
|
|
|
- pub fn update_inventory(&mut self, inventory: Decimal) {
|
|
|
+ pub async fn update_inventory(&mut self, inventory: Decimal) {
|
|
|
self.inventory = inventory;
|
|
|
|
|
|
- self.processor();
|
|
|
+ self.processor().await;
|
|
|
}
|
|
|
|
|
|
pub fn update_sigma_square(&mut self) {
|
|
|
@@ -183,19 +192,41 @@ impl AvellanedaStoikov {
|
|
|
}
|
|
|
|
|
|
// #[instrument(skip(self), level="TRACE")]
|
|
|
- fn processor(&mut self) {
|
|
|
+ async fn processor(&mut self) {
|
|
|
self.update_sigma_square();
|
|
|
- info!(?self.sigma_square);
|
|
|
+ // info!(?self.sigma_square);
|
|
|
self.update_gamma();
|
|
|
- info!(?self.gamma);
|
|
|
+ // info!(?self.gamma);
|
|
|
self.update_kappa();
|
|
|
- info!(?self.kappa);
|
|
|
+ // info!(?self.kappa);
|
|
|
self.update_ref_price();
|
|
|
- info!(?self.ref_price);
|
|
|
+ // info!(?self.ref_price);
|
|
|
self.update_delta_plus();
|
|
|
- info!(?self.delta_plus);
|
|
|
+ // info!(?self.delta_plus);
|
|
|
self.update_optimal_ask_and_bid();
|
|
|
- info!(?self.spread_max, ?self.mid_price, ?self.ref_price, ?self.inventory);
|
|
|
+ // info!("=============================================");
|
|
|
+
|
|
|
+ let mut cci = self.cci_arc.lock().await;
|
|
|
+ cci.predictor_state_vec.push_back(PredictorState {
|
|
|
+ update_time: Decimal::from_i64(Utc::now().timestamp_millis()).unwrap(),
|
|
|
+
|
|
|
+ mid_price: self.mid_price,
|
|
|
+ ask_price: self.ask_price,
|
|
|
+ bid_price: self.bid_price,
|
|
|
+ spread: self.spread,
|
|
|
+ spread_max: self.spread_max,
|
|
|
+ spread_min: self.spread_min,
|
|
|
+ optimal_ask_price: self.optimal_ask_price,
|
|
|
+ optimal_bid_price: self.optimal_bid_price,
|
|
|
+
|
|
|
+ inventory: self.inventory,
|
|
|
+ sigma_square: self.sigma_square,
|
|
|
+ gamma: self.gamma,
|
|
|
+ kappa: self.kappa,
|
|
|
+
|
|
|
+ delta_plus: self.delta_plus,
|
|
|
+ ref_price: self.ref_price,
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
// #[instrument(skip(self, ref_ticker_map), level="TRACE")]
|