|
|
@@ -5,12 +5,14 @@ use std::collections::VecDeque;
|
|
|
use std::ops::{Div, Mul};
|
|
|
use std::str::FromStr;
|
|
|
use chrono::Utc;
|
|
|
+use futures_util::StreamExt;
|
|
|
use rust_decimal::Decimal;
|
|
|
use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
|
|
|
use tracing::{error, info, warn};
|
|
|
use tokio::time::Instant;
|
|
|
use crate::params::Params;
|
|
|
use crate::model::{LocalPosition, OrderCommand, OrderInfo};
|
|
|
+use crate::utils;
|
|
|
use crate::utils::{clip, fix_amount, fix_price, generate_client_id, get_limit_order_requests_num_per_second, get_limit_requests_num_per_second};
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
@@ -73,6 +75,9 @@ pub struct Strategy {
|
|
|
pub ref_price: Decimal, //
|
|
|
pub ref_bp: Decimal, //
|
|
|
pub ref_ap: Decimal, //
|
|
|
+ pub ref_bv: Decimal,
|
|
|
+ pub ref_av: Decimal,
|
|
|
+
|
|
|
pub step_size: Decimal, // 原文的stepSize
|
|
|
pub tick_size: Decimal, // 原文的tickSize
|
|
|
|
|
|
@@ -220,6 +225,8 @@ impl Strategy {
|
|
|
ref_price: Default::default(),
|
|
|
ref_bp: Default::default(),
|
|
|
ref_ap: Default::default(),
|
|
|
+ ref_bv: Default::default(),
|
|
|
+ ref_av: Default::default(),
|
|
|
step_size: Decimal::new(1, 10),
|
|
|
tick_size: Decimal::new(1, 10),
|
|
|
max_pos_rate: Default::default(),
|
|
|
@@ -287,7 +294,6 @@ impl Strategy {
|
|
|
// 更新当前strategy的各类信息
|
|
|
pub fn _update_data(&mut self,
|
|
|
local_position: &LocalPosition,
|
|
|
- agg_market: &Vec<Decimal>,
|
|
|
local_cash: &Decimal,
|
|
|
local_coin: &Decimal,
|
|
|
ref_price: &Vec<Vec<Decimal>>) -> bool {
|
|
|
@@ -303,8 +309,8 @@ impl Strategy {
|
|
|
// debug!(?self.pos);
|
|
|
|
|
|
// 价格值处理
|
|
|
- self.bp = agg_market[crate::public_params::BID_PRICE_INDEX];
|
|
|
- self.ap = agg_market[crate::public_params::ASK_PRICE_INDEX];
|
|
|
+ self.bp = ref_price[self.ref_index][0];
|
|
|
+ self.ap = ref_price[self.ref_index][1];
|
|
|
self.mp = (self.bp + self.ap) * Decimal::from_str("0.5").unwrap();
|
|
|
// 中间价的ema值处理
|
|
|
if self.mp_ema.eq(&Decimal::ZERO) {
|
|
|
@@ -339,12 +345,12 @@ impl Strategy {
|
|
|
self.ref_ap = self.ap;
|
|
|
self.ref_price = self.mp;
|
|
|
} else {
|
|
|
- self.ref_bp = ref_price[self.ref_index][0];
|
|
|
- self.ref_ap = ref_price[self.ref_index][1];
|
|
|
- let bv = ref_price[self.ref_index][2];
|
|
|
- let av = ref_price[self.ref_index][3];
|
|
|
+ self.ref_bp = self.bp;
|
|
|
+ self.ref_ap = self.ap;
|
|
|
+ self.ref_bv = ref_price[self.ref_index][2];
|
|
|
+ self.ref_av = ref_price[self.ref_index][3];
|
|
|
// 微价格(加权中间价)作为基价 S = (ap*bv+bp*av)/(av+bv)
|
|
|
- self.ref_price = (self.ref_ap * bv + self.ref_bp * av) / (av + bv);
|
|
|
+ self.ref_price = (self.ref_ap * self.ref_bv + self.ref_bp * self.ref_av) / (self.ref_av + self.ref_bv);
|
|
|
// self.ref_price = (self.ref_bp + self.ref_ap) * dec!(0.5);
|
|
|
}
|
|
|
// debug!(?self.ref_bp, ?self.ref_ap, %self.ref_price);
|
|
|
@@ -741,14 +747,12 @@ impl Strategy {
|
|
|
pub fn on_exit(&mut self,
|
|
|
local_orders: &HashMap<String, OrderInfo>,
|
|
|
local_position: &LocalPosition,
|
|
|
- agg_market: &Vec<Decimal>,
|
|
|
local_cash: &Decimal,
|
|
|
local_coin: &Decimal,
|
|
|
ref_price: &Vec<Vec<Decimal>>) -> OrderCommand {
|
|
|
let mut command = OrderCommand::new();
|
|
|
|
|
|
if self._update_data(local_position,
|
|
|
- agg_market,
|
|
|
local_cash,
|
|
|
local_coin,
|
|
|
ref_price) {
|
|
|
@@ -774,14 +778,12 @@ impl Strategy {
|
|
|
pub fn on_sleep(&mut self,
|
|
|
local_orders: &HashMap<String, OrderInfo>,
|
|
|
local_position: &LocalPosition,
|
|
|
- agg_market: &Vec<Decimal>,
|
|
|
local_cash: &Decimal,
|
|
|
local_coin: &Decimal,
|
|
|
ref_price: &Vec<Vec<Decimal>>) -> OrderCommand {
|
|
|
let mut command = OrderCommand::new();
|
|
|
|
|
|
if self._update_data(local_position,
|
|
|
- agg_market,
|
|
|
local_cash,
|
|
|
local_coin,
|
|
|
ref_price) {
|
|
|
@@ -886,7 +888,7 @@ impl Strategy {
|
|
|
info!("策略预热完毕,可以执行后续逻辑!")
|
|
|
}
|
|
|
|
|
|
- return false;
|
|
|
+ false
|
|
|
}
|
|
|
|
|
|
// 接近整点时刻 不允许报单 防止下单bug
|
|
|
@@ -1270,7 +1272,6 @@ impl Strategy {
|
|
|
pub fn on_tick(&mut self,
|
|
|
local_orders: &HashMap<String, OrderInfo>,
|
|
|
local_position: &LocalPosition,
|
|
|
- agg_market: &Vec<Decimal>,
|
|
|
local_cash: &Decimal,
|
|
|
local_coin: &Decimal,
|
|
|
ref_price: &Vec<Vec<Decimal>>,
|
|
|
@@ -1282,7 +1283,6 @@ impl Strategy {
|
|
|
|
|
|
// 更新逻辑数据出错时,不进行后面的逻辑处理
|
|
|
if !self._update_data(local_position,
|
|
|
- agg_market,
|
|
|
local_cash,
|
|
|
local_coin,
|
|
|
ref_price) {
|
|
|
@@ -1318,18 +1318,13 @@ impl Strategy {
|
|
|
if command.limits_open.len() != 0 {
|
|
|
self.prev_place_order_timestamp = Utc::now().timestamp_millis();
|
|
|
}
|
|
|
+ // 数据录入
|
|
|
+ self.processor();
|
|
|
command
|
|
|
}
|
|
|
|
|
|
|
|
|
- async fn processor(&mut self) {
|
|
|
- self.update_t_diff();
|
|
|
- self.update_sigma_square();
|
|
|
- self.update_gamma();
|
|
|
- self.update_kappa();
|
|
|
- self.update_delta();
|
|
|
- self.update_optimal_ask_and_bid();
|
|
|
-
|
|
|
+ fn processor(&mut self) {
|
|
|
self.check_ready();
|
|
|
if !self.is_ready {
|
|
|
return;
|
|
|
@@ -1345,28 +1340,26 @@ impl Strategy {
|
|
|
|
|
|
// let cci_arc = self.cci_arc.clone();
|
|
|
let now = Decimal::from_i64(Utc::now().timestamp_millis()).unwrap();
|
|
|
- let mid_price = self.mid_price;
|
|
|
- let ask_price = self.ask_price;
|
|
|
- let bid_price = self.bid_price;
|
|
|
- let last_price = self.last_price;
|
|
|
-
|
|
|
- let spread = self.mid_price;
|
|
|
- let spread_max = self.optimal_ask_price;
|
|
|
- let spread_min = self.optimal_bid_price;
|
|
|
- // let spread = self.price_times_avg;
|
|
|
- // let spread_max = self.fair_price_vec[1] / self.fair_price_vec[0];
|
|
|
- // let spread_min = self.fair_price / self.mid_price;
|
|
|
-
|
|
|
- let optimal_ask_price = self.optimal_ask_price;
|
|
|
- let optimal_bid_price = self.optimal_bid_price;
|
|
|
-
|
|
|
- let inventory = self.inventory;
|
|
|
- let sigma_square = if self.is_regressed { Decimal::ONE } else { Decimal::ZERO };
|
|
|
- let gamma = now - self.last_update_time;
|
|
|
- let kappa = self.fair_price / self.mid_price;
|
|
|
+ let mid_price = self.mp;
|
|
|
+ let ask_price = self.ref_ap;
|
|
|
+ let bid_price = self.ref_bp;
|
|
|
+ let last_price = Decimal::ZERO;
|
|
|
+
|
|
|
+ let spread = self.ref_price;
|
|
|
+ let spread_max = self.open_dist[2];
|
|
|
+ let spread_min = self.open_dist[0];
|
|
|
+
|
|
|
+
|
|
|
+ let optimal_ask_price = self.open_dist[2];
|
|
|
+ let optimal_bid_price = self.open_dist[0];
|
|
|
+
|
|
|
+ let inventory = Decimal::ZERO;
|
|
|
+ let sigma_square = Decimal::ZERO;
|
|
|
+ let gamma = Decimal::ZERO;
|
|
|
+ let kappa = Decimal::ZERO;
|
|
|
|
|
|
let flow_ratio = Decimal::ZERO;
|
|
|
- let ref_price = self.fair_price;
|
|
|
+ let ref_price = self.ref_price;
|
|
|
|
|
|
let need_append = now - self.prev_insert_time > Decimal::ONE_HUNDRED;
|
|
|
if !need_append {
|