|
|
@@ -13,7 +13,8 @@ use standard::{Depth, Record, Ticker, Trade};
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
pub struct AvellanedaStoikov {
|
|
|
- pub depth_vec: FixedTimeRangeDeque<Depth>, // 深度队列
|
|
|
+ pub depth_vec: Vec<Depth>, // 深度队列
|
|
|
+ pub fair_price_vec: Vec<Decimal>, // 深度队列
|
|
|
pub trade_long_vec: FixedTimeRangeDeque<Trade>, // 交易队列
|
|
|
pub trade_short_vec: FixedTimeRangeDeque<Trade>, // 交易队列
|
|
|
pub spread_vec: FixedTimeRangeDeque<Decimal>,
|
|
|
@@ -59,8 +60,11 @@ impl AvellanedaStoikov {
|
|
|
|
|
|
pub fn new(cci_arc: Arc<Mutex<CentralControlInfo>>) -> Self {
|
|
|
let avellaneda_stoikov = Self {
|
|
|
- // 分别给与的长度
|
|
|
- depth_vec: FixedTimeRangeDeque::new(Self::MAX_TIME_RANGE_MICROS),
|
|
|
+ // 接针版本
|
|
|
+ depth_vec: vec![Depth::new(); 10],
|
|
|
+ fair_price_vec: vec![Decimal::ZERO; 10],
|
|
|
+
|
|
|
+ // 老的队列
|
|
|
spread_vec: FixedTimeRangeDeque::new(Self::MAX_TIME_RANGE_MICROS),
|
|
|
trade_long_vec: FixedTimeRangeDeque::new(Self::TRADE_LONG_RANGE_MICROS),
|
|
|
trade_short_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
|
|
|
@@ -142,13 +146,14 @@ impl AvellanedaStoikov {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- pub async fn on_depth(&mut self, depth: &Depth, _index: u32) {
|
|
|
- self.depth_vec.push_back(depth.clone());
|
|
|
+ pub async fn on_depth(&mut self, depth: &Depth, index: usize) {
|
|
|
+ self.depth_vec[index] = depth.clone();
|
|
|
|
|
|
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.update_fair_price(depth, index).await;
|
|
|
self.processor().await;
|
|
|
}
|
|
|
|
|
|
@@ -161,6 +166,28 @@ impl AvellanedaStoikov {
|
|
|
self.processor().await;
|
|
|
}
|
|
|
|
|
|
+ pub async fn update_fair_price(&mut self, depth: &Depth, index: usize) {
|
|
|
+ let a1 = &depth.asks[0];
|
|
|
+ let b1 = &depth.bids[0];
|
|
|
+ let mid = (a1.price + b1.price) / Decimal::TWO;
|
|
|
+
|
|
|
+ let i_upper = (a1.value - b1.value) / (a1.value + b1.value);
|
|
|
+ // let s_upper = (a1.price - b1.price) / mid;
|
|
|
+ // let f_t = Decimal::ZERO;
|
|
|
+ // let s = (s_upper + f_t) / Decimal::TWO;
|
|
|
+
|
|
|
+ // let a_upper_t = dec!(0.6);
|
|
|
+ // let c_t = dec!(0.4);
|
|
|
+ // let theta = a_upper_t * s + c_t;
|
|
|
+ // let fair_price = mid + theta * (i_upper * (i_upper.powd(Decimal::TWO) + Decimal::ONE)) / Decimal::TWO;
|
|
|
+
|
|
|
+ let fair_price = mid + (a1.price - b1.price) * i_upper / Decimal::TWO;
|
|
|
+
|
|
|
+ info!(?mid, ?fair_price, ?index);
|
|
|
+
|
|
|
+ self.fair_price_vec[index] = fair_price;
|
|
|
+ }
|
|
|
+
|
|
|
pub async fn update_level(&mut self) {
|
|
|
self.level = (Decimal::NEGATIVE_ONE + (Decimal::ONE + dec!(8) * self.inventory.abs()).sqrt().unwrap()) / Decimal::TWO;
|
|
|
self.level = min(self.level, dec!(6));
|