|
|
@@ -1,24 +1,31 @@
|
|
|
-use std::cmp::{max, min};
|
|
|
use std::collections::{BTreeMap, VecDeque};
|
|
|
use rust_decimal::prelude::*;
|
|
|
use tracing::info;
|
|
|
use standard::{Depth, Ticker, Trade};
|
|
|
|
|
|
+const MAX_DATA_LENGTH: usize = 1000;
|
|
|
+
|
|
|
#[derive(Debug)]
|
|
|
pub struct AvellanedaStoikov {
|
|
|
pub depth_vec: VecDeque<Depth>, // 深度队列
|
|
|
pub trade_vec: VecDeque<Trade>, // 交易队列
|
|
|
pub spread_vec: VecDeque<Decimal>, // 市场冲击队列
|
|
|
|
|
|
- pub optimal_ask_price: Decimal,
|
|
|
- pub optimal_bid_price: Decimal,
|
|
|
pub mid_price: Decimal,
|
|
|
pub ask_price: Decimal,
|
|
|
pub bid_price: Decimal,
|
|
|
- pub ref_price: Decimal,
|
|
|
pub spread: Decimal,
|
|
|
pub spread_max: Decimal,
|
|
|
pub spread_min: Decimal,
|
|
|
+ pub optimal_ask_price: Decimal,
|
|
|
+ pub optimal_bid_price: Decimal,
|
|
|
+
|
|
|
+ pub inventory: Decimal, // 库存,也就是q
|
|
|
+ pub gamma: Decimal, // γ,库存风险厌恶参数
|
|
|
+ pub sigma: Decimal, // σ,波动性
|
|
|
+ pub delta_plus: Decimal, // δa+δb,买卖挂单间距
|
|
|
+ pub kappa: Decimal, // κ 订单簿 流动性 参数
|
|
|
+ pub ref_price: Decimal, // 预定价格
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
@@ -27,48 +34,48 @@ pub struct AvellanedaStoikov {
|
|
|
*/
|
|
|
impl AvellanedaStoikov {
|
|
|
pub fn new() -> Self {
|
|
|
- let mut avellaneda_stoikov = Self {
|
|
|
- // 分别给与10000的长度
|
|
|
- depth_vec: VecDeque::with_capacity(10000),
|
|
|
- trade_vec: VecDeque::with_capacity(10000),
|
|
|
- spread_vec: VecDeque::with_capacity(10000),
|
|
|
+ let avellaneda_stoikov = Self {
|
|
|
+ // 分别给与MAX_DATA_LENGTH的长度
|
|
|
+ depth_vec: VecDeque::with_capacity(MAX_DATA_LENGTH),
|
|
|
+ trade_vec: VecDeque::with_capacity(MAX_DATA_LENGTH),
|
|
|
+ spread_vec: VecDeque::with_capacity(MAX_DATA_LENGTH),
|
|
|
|
|
|
- optimal_ask_price: Default::default(),
|
|
|
- optimal_bid_price: Default::default(),
|
|
|
mid_price: Default::default(),
|
|
|
ask_price: Default::default(),
|
|
|
bid_price: Default::default(),
|
|
|
- ref_price: Default::default(),
|
|
|
spread: Default::default(),
|
|
|
spread_max: Default::default(),
|
|
|
spread_min: Default::default(),
|
|
|
- };
|
|
|
+ optimal_ask_price: Default::default(),
|
|
|
+ optimal_bid_price: Default::default(),
|
|
|
|
|
|
- // 初始化市场冲击相关参数
|
|
|
- avellaneda_stoikov.init_spread();
|
|
|
+ inventory: Default::default(),
|
|
|
+ gamma: Default::default(),
|
|
|
+ sigma: Default::default(),
|
|
|
+ delta_plus: Default::default(),
|
|
|
+ kappa: Default::default(),
|
|
|
+ ref_price: Default::default(),
|
|
|
+ };
|
|
|
|
|
|
avellaneda_stoikov
|
|
|
}
|
|
|
|
|
|
- // 初始化市场冲击参数,初始化时机等后面再研究吧
|
|
|
- pub fn init_spread(&mut self) {
|
|
|
- // -1就是未赋值
|
|
|
- self.spread_max = Decimal::NEGATIVE_ONE;
|
|
|
- self.spread_min = Decimal::NEGATIVE_ONE;
|
|
|
- }
|
|
|
-
|
|
|
// 更新最大市场冲击
|
|
|
pub fn update_spread_max(&mut self) {
|
|
|
- self.spread_max = max(self.spread_max, self.spread);
|
|
|
+ self.spread_max = if let Some(&max_value) = self.spread_vec.iter().max() {
|
|
|
+ max_value
|
|
|
+ } else {
|
|
|
+ Decimal::NEGATIVE_ONE
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 更新最小市场冲击
|
|
|
pub fn update_spread_min(&mut self) {
|
|
|
- self.spread_min = if self.spread_min == Decimal::NEGATIVE_ONE {
|
|
|
- self.spread
|
|
|
+ self.spread_min = if let Some(&min_value) = self.spread_vec.iter().min() {
|
|
|
+ min_value
|
|
|
} else {
|
|
|
- min(self.spread_min, self.spread)
|
|
|
- };
|
|
|
+ Decimal::NEGATIVE_ONE
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
pub fn update_spread(&mut self, mid_price_now: &Decimal) {
|
|
|
@@ -80,7 +87,7 @@ impl AvellanedaStoikov {
|
|
|
self.spread_vec.push_back(self.spread);
|
|
|
|
|
|
// 简易的长度控制
|
|
|
- if self.spread_vec.len() > 10000 {
|
|
|
+ if self.spread_vec.len() > MAX_DATA_LENGTH {
|
|
|
self.spread_vec.pop_front();
|
|
|
}
|
|
|
}
|
|
|
@@ -100,10 +107,10 @@ impl AvellanedaStoikov {
|
|
|
|
|
|
self.update_ref_price();
|
|
|
|
|
|
- info!(?self.mid_price, ?self.spread, ?self.spread_max, ?self.spread_min);
|
|
|
+ info!(?self.mid_price, ?self.spread, ?self.spread_max, ?self.spread_min, ?self.inventory);
|
|
|
|
|
|
// 简易的长度控制
|
|
|
- if self.depth_vec.len() > 10000 {
|
|
|
+ if self.depth_vec.len() > MAX_DATA_LENGTH {
|
|
|
self.depth_vec.pop_front();
|
|
|
}
|
|
|
}
|
|
|
@@ -113,7 +120,7 @@ impl AvellanedaStoikov {
|
|
|
self.processor();
|
|
|
|
|
|
// 简易的长度控制
|
|
|
- if self.trade_vec.len() > 10000 {
|
|
|
+ if self.trade_vec.len() > MAX_DATA_LENGTH {
|
|
|
self.trade_vec.pop_front();
|
|
|
}
|
|
|
}
|