|
|
@@ -19,6 +19,8 @@ pub struct AvellanedaStoikov {
|
|
|
pub trade_long_vec: FixedTimeRangeDeque<Trade>, // 交易队列
|
|
|
pub trade_short_vec: FixedTimeRangeDeque<Trade>, // 交易队列
|
|
|
pub spread_vec: Vec<Decimal>, // 市场冲击队列
|
|
|
+ pub spread_long_vec: Vec<Decimal>,
|
|
|
+ pub spread_short_vec: Vec<Decimal>,
|
|
|
pub spread_count_map: HashMap<Decimal, usize>, // 市场次数的字典
|
|
|
pub record_vec: VecDeque<Record>, // 蜡烛队列
|
|
|
|
|
|
@@ -28,6 +30,8 @@ pub struct AvellanedaStoikov {
|
|
|
pub last_price: Decimal, // 最后成交价
|
|
|
pub spread: Decimal, // 市场冲击
|
|
|
pub spread_max: Decimal, // 最大市场冲击
|
|
|
+ pub spread_long_max: Decimal,
|
|
|
+ pub spread_short_max: Decimal,
|
|
|
pub spread_best: Decimal, // 最佳市场冲击
|
|
|
pub optimal_ask_price: Decimal, // 卖出挂单价
|
|
|
pub optimal_bid_price: Decimal, // 买入挂单价
|
|
|
@@ -74,6 +78,8 @@ impl AvellanedaStoikov {
|
|
|
|
|
|
// 老的队列
|
|
|
spread_vec: vec![],
|
|
|
+ spread_long_vec: vec![],
|
|
|
+ spread_short_vec: vec![],
|
|
|
spread_count_map: Default::default(),
|
|
|
trade_long_vec: FixedTimeRangeDeque::new(Self::TRADE_LONG_RANGE_MICROS),
|
|
|
trade_short_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
|
|
|
@@ -85,6 +91,8 @@ impl AvellanedaStoikov {
|
|
|
last_price: Default::default(),
|
|
|
spread: Default::default(),
|
|
|
spread_max: Default::default(),
|
|
|
+ spread_long_max: Default::default(),
|
|
|
+ spread_short_max: Default::default(),
|
|
|
spread_best: Default::default(),
|
|
|
optimal_ask_price: Default::default(),
|
|
|
optimal_bid_price: Default::default(),
|
|
|
@@ -121,6 +129,16 @@ impl AvellanedaStoikov {
|
|
|
} else {
|
|
|
Decimal::NEGATIVE_ONE
|
|
|
};
|
|
|
+ self.spread_long_max = if let Some(&max_value) = self.spread_long_vec.iter().max() {
|
|
|
+ max_value
|
|
|
+ } else {
|
|
|
+ Decimal::NEGATIVE_ONE
|
|
|
+ };
|
|
|
+ self.spread_short_max = if let Some(&max_value) = self.spread_short_vec.iter().max() {
|
|
|
+ max_value
|
|
|
+ } else {
|
|
|
+ Decimal::NEGATIVE_ONE
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
// 更新最佳市场冲击
|
|
|
@@ -161,6 +179,11 @@ impl AvellanedaStoikov {
|
|
|
if !now_spread.is_zero() {
|
|
|
self.spread = now_spread;
|
|
|
self.spread_vec.push(self.spread);
|
|
|
+ if last_trade_price > first_trade_price {
|
|
|
+ self.spread_long_vec.push(self.spread);
|
|
|
+ } else {
|
|
|
+ self.spread_short_vec.push(self.spread);
|
|
|
+ }
|
|
|
self.spread_count_map.insert(self.spread, self.spread_count_map.get(&self.spread).unwrap_or(&0) + 1);
|
|
|
|
|
|
if self.spread_vec.len() > 2_000 {
|
|
|
@@ -203,16 +226,16 @@ impl AvellanedaStoikov {
|
|
|
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 = self.spread_max;
|
|
|
- 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;
|
|
|
+ // 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 = self.spread_max;
|
|
|
+ // 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;
|
|
|
|
|
|
self.fair_price_vec[index] = fair_price;
|
|
|
self.volume_vec[index] = a1.size + b1.size;
|