|
|
@@ -21,7 +21,9 @@ pub struct Predictor {
|
|
|
pub depth_vec: Vec<Depth>, // 深度队列
|
|
|
pub spread_vec: Vec<Decimal>, // 价差队列
|
|
|
pub record_vec: VecDeque<Record>, // 蜡烛队列
|
|
|
- pub trade_price_long_vec: FixedTimeRangeDeque<Decimal>, // 交易队列
|
|
|
+ pub trade_price_long_vec: FixedTimeRangeDeque<Decimal>,
|
|
|
+ pub trade_0_vec: FixedTimeRangeDeque<Trade>,
|
|
|
+ pub trade_1_vec: FixedTimeRangeDeque<Trade>,
|
|
|
|
|
|
pub mid_price: Decimal, // 中间价
|
|
|
pub fair_price: Decimal,
|
|
|
@@ -69,7 +71,7 @@ impl Predictor {
|
|
|
// const TIME_DIFF_RANGE_MICROS: i64 = 15 * 60_000_000;
|
|
|
const TRADE_LONG_RANGE_MICROS: i64 = 10 * 60_000_000;
|
|
|
// const SPREAD_RANGE_MICROS: i64 = 15 * 60_000_000;
|
|
|
- // const TRADE_SHORT_RANGE_MICROS: i64 = 2 * 60_000_000;
|
|
|
+ const TRADE_SHORT_RANGE_MICROS: i64 = 2 * 60_000_000;
|
|
|
// const ONE_MILLION: Decimal = dec!(1_000_000);
|
|
|
// const TWENTY_THOUSAND: Decimal = dec!(20_000);
|
|
|
const DONT_VIEW: Decimal = dec!(14142135623730951);
|
|
|
@@ -130,6 +132,8 @@ impl Predictor {
|
|
|
record_vec: VecDeque::new(),
|
|
|
|
|
|
trade_price_long_vec: FixedTimeRangeDeque::new(Self::TRADE_LONG_RANGE_MICROS),
|
|
|
+ trade_0_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
|
|
|
+ trade_1_vec: FixedTimeRangeDeque::new(Self::TRADE_SHORT_RANGE_MICROS),
|
|
|
mid_price: Default::default(),
|
|
|
fair_price: Default::default(),
|
|
|
ask_price: Default::default(),
|
|
|
@@ -188,11 +192,17 @@ impl Predictor {
|
|
|
self.processor(depth.time, false).await;
|
|
|
}
|
|
|
|
|
|
- pub async fn on_trade(&mut self, trade: &Trade, _index: usize) {
|
|
|
+ pub async fn on_trade(&mut self, trade: &Trade, index: usize) {
|
|
|
self.last_price = trade.price;
|
|
|
|
|
|
self.trade_price_long_vec.push_back(trade.price);
|
|
|
|
|
|
+ if index == 0 {
|
|
|
+ self.trade_0_vec.push_back(trade.clone());
|
|
|
+ } else if index == 1 {
|
|
|
+ self.trade_1_vec.push_back(trade.clone());
|
|
|
+ }
|
|
|
+
|
|
|
// self.processor().await;
|
|
|
}
|
|
|
|
|
|
@@ -408,13 +418,13 @@ impl Predictor {
|
|
|
let last_price = Self::DONT_VIEW;
|
|
|
let fair_price = self.fair_price;
|
|
|
|
|
|
- let spread = if self.trade_price_long_vec.len() > 1 {
|
|
|
- let front = self.trade_price_long_vec.deque.front().unwrap();
|
|
|
- let back = self.trade_price_long_vec.deque.back().unwrap();
|
|
|
+ let total_amount_0: Decimal = self.trade_0_vec.deque.iter().map(|trade| trade.value).sum();
|
|
|
+ let total_amount_1: Decimal = self.trade_1_vec.deque.iter().map(|trade| trade.value).sum();
|
|
|
|
|
|
- (back / front) - Decimal::ONE
|
|
|
- } else {
|
|
|
+ let spread = if total_amount_0 + total_amount_1 == Decimal::ZERO {
|
|
|
Decimal::ZERO
|
|
|
+ } else {
|
|
|
+ total_amount_0 / (total_amount_0 + total_amount_1)
|
|
|
};
|
|
|
let spread_min = Self::DONT_VIEW;
|
|
|
let spread_max = Self::DONT_VIEW;
|
|
|
@@ -427,7 +437,14 @@ impl Predictor {
|
|
|
let sigma_square = self.params.open;
|
|
|
|
|
|
let gamma = self.balance;
|
|
|
- let kappa = Decimal::ZERO;
|
|
|
+ let kappa = if self.trade_price_long_vec.len() > 1 {
|
|
|
+ let front = self.trade_price_long_vec.deque.front().unwrap();
|
|
|
+ let back = self.trade_price_long_vec.deque.back().unwrap();
|
|
|
+
|
|
|
+ (back / front) - Decimal::ONE
|
|
|
+ } else {
|
|
|
+ Decimal::ZERO
|
|
|
+ };
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
|