|
|
@@ -1,10 +1,10 @@
|
|
|
use std::cmp::{max, min};
|
|
|
use std::collections::HashMap;
|
|
|
use std::ops::{Div, Mul};
|
|
|
+use std::str::FromStr;
|
|
|
use chrono::Utc;
|
|
|
use rust_decimal::Decimal;
|
|
|
use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
|
|
|
-use rust_decimal_macros::dec;
|
|
|
use tracing::{error, info, warn};
|
|
|
use tokio::time::Instant;
|
|
|
use crate::params::Params;
|
|
|
@@ -162,8 +162,8 @@ impl Strategy {
|
|
|
total_amount: Default::default(),
|
|
|
is_ready: false,
|
|
|
_is_print: is_print,
|
|
|
- _min_amount_value: dec!(5.0),
|
|
|
- _max_amount_value: dec!(10000.0),
|
|
|
+ _min_amount_value: Decimal::from_str("5.0").unwrap(),
|
|
|
+ _max_amount_value: Decimal::from_str("10000.0").unwrap(),
|
|
|
mp_ema: Default::default(),
|
|
|
mp: Default::default(),
|
|
|
bp: Default::default(),
|
|
|
@@ -171,8 +171,8 @@ impl Strategy {
|
|
|
ref_price: Default::default(),
|
|
|
ref_bp: Default::default(),
|
|
|
ref_ap: Default::default(),
|
|
|
- step_size: dec!(1e-10),
|
|
|
- tick_size: dec!(1e-10),
|
|
|
+ step_size: Decimal::new(1, 10),
|
|
|
+ tick_size: Decimal::new(1, 10),
|
|
|
max_pos_rate: Default::default(),
|
|
|
profit: Default::default(),
|
|
|
daily_return: Default::default(),
|
|
|
@@ -227,7 +227,7 @@ impl Strategy {
|
|
|
// 开仓下单间隔 均匀下单机会
|
|
|
strategy.post_open_time = now.timestamp_millis();
|
|
|
let post_open_interval_per_second = Decimal::ONE.div(Decimal::from_i64(get_limit_order_requests_num_per_second(params.exchange.clone())).unwrap());
|
|
|
- strategy.post_open_interval = dec!(1000).mul(post_open_interval_per_second).to_i64().unwrap();
|
|
|
+ strategy.post_open_interval = Decimal::from_str("1000").unwrap().mul(post_open_interval_per_second).to_i64().unwrap();
|
|
|
info!("策略模块初始化完成!");
|
|
|
|
|
|
strategy
|
|
|
@@ -254,12 +254,12 @@ impl Strategy {
|
|
|
// 价格值处理
|
|
|
self.bp = agg_market[crate::public_params::BID_PRICE_INDEX];
|
|
|
self.ap = agg_market[crate::public_params::ASK_PRICE_INDEX];
|
|
|
- self.mp = (self.bp + self.ap) * dec!(0.5);
|
|
|
+ self.mp = (self.bp + self.ap) * Decimal::from_str("0.5").unwrap();
|
|
|
// 中间价的ema值处理
|
|
|
if self.mp_ema.eq(&Decimal::ZERO) {
|
|
|
self.mp_ema = self.mp;
|
|
|
} else {
|
|
|
- self.mp_ema = self.mp_ema * dec!(0.999) + self.mp * dec!(0.001);
|
|
|
+ self.mp_ema = self.mp_ema * Decimal::from_str("0.999").unwrap() + self.mp * Decimal::from_str("0.001").unwrap();
|
|
|
}
|
|
|
// debug!(?self.bp, ?self.ap, ?self.mp, ?self.mp_ema);
|
|
|
|
|
|
@@ -267,7 +267,7 @@ impl Strategy {
|
|
|
if self.mp > self.mp_ema {
|
|
|
self.adjust_lever_rate = Decimal::ONE;
|
|
|
} else {
|
|
|
- self.adjust_lever_rate = dec!(0.8);
|
|
|
+ self.adjust_lever_rate = Decimal::from_str("0.8").unwrap();
|
|
|
}
|
|
|
// debug!(?self.adjust_lever_rate);
|
|
|
|
|
|
@@ -387,7 +387,7 @@ impl Strategy {
|
|
|
Decimal::ZERO
|
|
|
};
|
|
|
let run_time = Utc::now().timestamp_millis() - self._start_time;
|
|
|
- let run_time_day = Decimal::from(run_time) / (dec!(86400000));
|
|
|
+ let run_time_day = Decimal::from(run_time) / Decimal::from_str("86400000").unwrap();
|
|
|
self.daily_return = self.profit / run_time_day;
|
|
|
self.daily_return.rescale(2);
|
|
|
self.short_pos_bias.rescale(2);
|
|
|
@@ -494,7 +494,7 @@ impl Strategy {
|
|
|
}
|
|
|
// 跟随做市模式
|
|
|
else if self.maker_mode == "follow".to_string() {
|
|
|
- mp = (ref_bp + ref_ap) * dec!(0.5);
|
|
|
+ mp = (ref_bp + ref_ap) * Decimal::from_str("0.5").unwrap();
|
|
|
buy_start = mp;
|
|
|
sell_start = mp;
|
|
|
} else {
|
|
|
@@ -913,8 +913,8 @@ impl Strategy {
|
|
|
// 平多价值大于最小交易价值,执行平多逻辑
|
|
|
if self.pos.long_pos * self.mp > self._min_amount_value {
|
|
|
if pd_order_num == 0 {
|
|
|
- let mut price = (short_lower + short_upper) * dec!(0.5);
|
|
|
- price = clip(price, self.bp * dec!(0.9995), self.ap * dec!(1.03));
|
|
|
+ let mut price = (short_lower + short_upper) * Decimal::from_str("0.5").unwrap();
|
|
|
+ price = clip(price, self.bp * Decimal::from_str("0.9995").unwrap(), self.ap * Decimal::from_str("1.03").unwrap());
|
|
|
price = fix_price(price, self.tick_size);
|
|
|
let mut amount = self.pos.long_pos;
|
|
|
amount = fix_amount(amount, self.step_size);
|
|
|
@@ -937,8 +937,8 @@ impl Strategy {
|
|
|
// 平空价值大于最小交易价值,执行平空逻辑
|
|
|
if self.pos.short_pos > self._min_amount_value {
|
|
|
if pk_order_num == 0 {
|
|
|
- let mut price = (long_upper + long_lower) * dec!(0.5);
|
|
|
- price = clip(price, self.bp * dec!(0.97), self.ap * dec!(1.0005));
|
|
|
+ let mut price = (long_upper + long_lower) * Decimal::from_str("0.5").unwrap();
|
|
|
+ price = clip(price, self.bp * Decimal::from_str("0.97").unwrap(), self.ap * Decimal::from_str("1.0005").unwrap());
|
|
|
price = fix_price(price, self.tick_size);
|
|
|
let mut amount = self.pos.short_pos;
|
|
|
amount = fix_amount(amount, self.step_size);
|
|
|
@@ -960,7 +960,7 @@ impl Strategy {
|
|
|
} else {
|
|
|
if self.pos.long_pos > Decimal::ZERO {
|
|
|
if pd_order_num == 0 {
|
|
|
- let mut price = (short_lower + short_upper) * dec!(0.5);
|
|
|
+ let mut price = (short_lower + short_upper) * Decimal::from_str("0.5").unwrap();
|
|
|
// 不限制大小
|
|
|
// price = clip(price, self.bp * dec!(0.9995), self.ap * dec!(1.03));
|
|
|
price = fix_price(price, self.tick_size);
|
|
|
@@ -981,7 +981,7 @@ impl Strategy {
|
|
|
|
|
|
if self.pos.short_pos > Decimal::ZERO {
|
|
|
if pk_order_num == 0 {
|
|
|
- let mut price = (long_upper + long_lower) * dec!(0.5);
|
|
|
+ let mut price = (long_upper + long_lower) * Decimal::from_str("0.5").unwrap();
|
|
|
// 不限制大小
|
|
|
// price = clip(price, self.bp * dec!(0.97), self.ap * dec!(1.0005));
|
|
|
price = fix_price(price, self.tick_size);
|
|
|
@@ -1135,15 +1135,15 @@ impl Strategy {
|
|
|
short_free_value = min(coin_value, self.max_short_value) - sell_value;
|
|
|
}
|
|
|
// 一手开单价值计算
|
|
|
- let one_hand_long_value = dec!(0.99) * (self.max_long_value / self.grid);
|
|
|
- let one_hand_short_value = dec!(0.99) * (self.max_short_value / self.grid);
|
|
|
+ let one_hand_long_value = Decimal::from_str("0.99").unwrap() * (self.max_long_value / self.grid);
|
|
|
+ let one_hand_short_value = Decimal::from_str("0.99").unwrap() * (self.max_short_value / self.grid);
|
|
|
|
|
|
// debug!(?self.post_side);
|
|
|
// 挂多单
|
|
|
if self.post_side >= 0 {
|
|
|
// debug!(?buy_price_list);
|
|
|
if buy_price_list.len() == 0 {
|
|
|
- let mut target_buy_price = (long_upper + long_lower) * dec!(0.5);
|
|
|
+ let mut target_buy_price = (long_upper + long_lower) * Decimal::from_str("0.5").unwrap();
|
|
|
// 取消大小限制
|
|
|
target_buy_price = target_buy_price;
|
|
|
// target_buy_price = clip(target_buy_price, self.bp * dec!(0.97), self.ap * dec!(1.0005));
|
|
|
@@ -1172,7 +1172,7 @@ impl Strategy {
|
|
|
if self.post_side <= 0 {
|
|
|
// debug!(?sell_price_list);
|
|
|
if sell_price_list.len() == 0 {
|
|
|
- let mut target_sell_price = (short_lower + short_upper) * dec!(0.5);
|
|
|
+ let mut target_sell_price = (short_lower + short_upper) * Decimal::from_str("0.5").unwrap();
|
|
|
// target_sell_price = clip(target_sell_price, self.bp * dec!(0.9995), self.ap * dec!(1.03));
|
|
|
// 取消大小限制
|
|
|
target_sell_price = target_sell_price;
|