|
|
@@ -49,15 +49,19 @@ pub struct Predictor {
|
|
|
pub fair_price: Decimal, // 公平价格
|
|
|
pub fair_price_ema_short: Decimal, // 公平价格_ema
|
|
|
pub fair_price_ema_long: Decimal, // 公平价格_ema
|
|
|
- pub mid_rate_focus_open: Decimal, // 变化幅度焦点
|
|
|
+ pub mid_rate_focus_open: Decimal, // 变化幅度焦点
|
|
|
pub mid_price_focus_open: Decimal, // 观测焦点时的价格
|
|
|
- pub mid_rate_focus_close: Decimal, // 变化幅度焦点
|
|
|
+ pub mid_rate_focus_close: Decimal, // 变化幅度焦点
|
|
|
pub fair_price_focus_close: Decimal, // 观测焦点时的价格
|
|
|
pub fair_price_when_ordering: Decimal, // 下单时的公平价格
|
|
|
pub price_times_avg: Decimal, // 公平所与做市所的价格倍率的平均值
|
|
|
|
|
|
pub is_ready: bool, // 是否已准备好
|
|
|
|
|
|
+ pub balance: Decimal, // 当前资金
|
|
|
+ pub one_grid_order_value: Decimal, // 每一网格下单价值
|
|
|
+ pub prev_trade_force_order_value: Decimal, // 上次下单时的爆仓价值ln
|
|
|
+
|
|
|
pub prev_trade_time: i64, // 上次交易时间,也就是t
|
|
|
pub t_diff: Decimal, // (T-t)
|
|
|
|
|
|
@@ -164,6 +168,9 @@ impl Predictor {
|
|
|
price_times_avg: Default::default(),
|
|
|
|
|
|
is_ready: false,
|
|
|
+ balance: Default::default(),
|
|
|
+ one_grid_order_value: Default::default(),
|
|
|
+ prev_trade_force_order_value: Default::default(),
|
|
|
prev_trade_time: Utc::now().timestamp_micros(),
|
|
|
t_diff: Default::default(),
|
|
|
level: Default::default(),
|
|
|
@@ -205,7 +212,7 @@ impl Predictor {
|
|
|
if self.mid_price.is_zero() {
|
|
|
return;
|
|
|
}
|
|
|
- self.processor().await;
|
|
|
+ self.processor(false).await;
|
|
|
}
|
|
|
|
|
|
pub async fn on_trade(&mut self, trade: &Trade, _index: usize) {
|
|
|
@@ -252,7 +259,14 @@ impl Predictor {
|
|
|
|
|
|
pub async fn on_record(&mut self, _record: &Record) {}
|
|
|
|
|
|
- pub async fn on_inventory(&mut self, pos_amount: &Decimal, pos_avg_price: &Decimal, min_amount_value: &Decimal) {
|
|
|
+ pub async fn on_balance(&mut self, balance: Decimal) {
|
|
|
+ self.balance = balance;
|
|
|
+ if self.inventory.is_zero() {
|
|
|
+ self.one_grid_order_value = self.balance / self.params.grid;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pub async fn on_inventory(&mut self, pos_amount: &Decimal, pos_avg_price: &Decimal, _min_amount_value: &Decimal) {
|
|
|
if self.mid_price.is_zero() {
|
|
|
return;
|
|
|
}
|
|
|
@@ -260,7 +274,7 @@ impl Predictor {
|
|
|
let prev_inventory = self.inventory;
|
|
|
self.pos_amount = pos_amount.clone();
|
|
|
self.pos_avg_price = pos_avg_price.clone();
|
|
|
- self.inventory = (pos_amount / (min_amount_value / self.mid_price)).trunc();
|
|
|
+ self.inventory = (pos_amount / (self.one_grid_order_value / self.mid_price)).round();
|
|
|
// 小于1但不为0的情况,需要平完
|
|
|
if self.inventory.is_zero() && !pos_amount.is_zero() {
|
|
|
self.inventory = if pos_amount > &Decimal::ZERO {
|
|
|
@@ -270,26 +284,24 @@ impl Predictor {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- if prev_inventory != self.inventory && prev_inventory.is_zero() {
|
|
|
+ // 用于计算加仓
|
|
|
+ if self.inventory.abs() > prev_inventory.abs() {
|
|
|
self.prev_trade_time = Utc::now().timestamp_micros();
|
|
|
+ self.prev_trade_force_order_value = self.force_order_value;
|
|
|
self.force_order_value = Decimal::ZERO;
|
|
|
}
|
|
|
|
|
|
- // 重置fair数据,用于重新计算幅度
|
|
|
- if prev_inventory != self.inventory {
|
|
|
- }
|
|
|
-
|
|
|
- // 重置资金流计算
|
|
|
+ // 重置一些计算
|
|
|
if prev_inventory != self.inventory && self.inventory.is_zero() {
|
|
|
self.profit_point_vec.clear();
|
|
|
self.profit_point = Decimal::ZERO;
|
|
|
self.profit_point_ema = Decimal::ZERO;
|
|
|
|
|
|
- self.money_flow = Decimal::ZERO;
|
|
|
+ self.prev_trade_force_order_value = Decimal::ZERO;
|
|
|
}
|
|
|
|
|
|
self.update_level().await;
|
|
|
- self.processor().await;
|
|
|
+ self.processor(true).await;
|
|
|
}
|
|
|
|
|
|
pub fn get_real_rate(price_vec: &FixedTimeRangeDeque<Decimal>) -> (Decimal, Decimal, Decimal) {
|
|
|
@@ -367,8 +379,14 @@ impl Predictor {
|
|
|
}
|
|
|
|
|
|
// 可能是接针
|
|
|
- let is_open_long = self.force_order_value < -self.params.open && self.inventory.is_zero();
|
|
|
- let is_open_short = self.force_order_value > self.params.open && self.inventory.is_zero();
|
|
|
+ let is_open_long = self.force_order_value < -self.params.open
|
|
|
+ && self.inventory < self.params.grid
|
|
|
+ && self.inventory >= Decimal::ZERO
|
|
|
+ && self.force_order_value < self.prev_trade_force_order_value;
|
|
|
+ let is_open_short = self.force_order_value > self.params.open
|
|
|
+ && self.inventory > -self.params.grid
|
|
|
+ && self.inventory <= Decimal::ZERO
|
|
|
+ && self.force_order_value > self.prev_trade_force_order_value;
|
|
|
|
|
|
let (is_close_long, is_close_short) = match self.params.close_difficulty.as_str() {
|
|
|
"easy" => {
|
|
|
@@ -464,7 +482,7 @@ impl Predictor {
|
|
|
}
|
|
|
|
|
|
// #[instrument(skip(self), level="TRACE")]
|
|
|
- async fn processor(&mut self) {
|
|
|
+ async fn processor(&mut self, is_hard_update: bool) {
|
|
|
self.check_ready();
|
|
|
if !self.is_ready {
|
|
|
return;
|
|
|
@@ -499,16 +517,16 @@ impl Predictor {
|
|
|
|
|
|
let inventory = self.inventory;
|
|
|
|
|
|
- let sigma_square = self.money_flow;
|
|
|
+ let sigma_square = self.balance;
|
|
|
// let sigma_square = self.error_rate;
|
|
|
|
|
|
let gamma = self.force_order_value;
|
|
|
- let kappa = Decimal::ZERO;
|
|
|
+ let kappa = self.inventory;
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
|
|
|
let need_append = now - self.prev_insert_time > dec!(500);
|
|
|
- if !need_append {
|
|
|
+ if !need_append && !is_hard_update {
|
|
|
return;
|
|
|
}
|
|
|
|