|
|
@@ -9,6 +9,7 @@ use rust_decimal::Decimal;
|
|
|
use rust_decimal::prelude::{ToPrimitive, Zero};
|
|
|
use rust_decimal_macros::dec;
|
|
|
use serde_json::Value;
|
|
|
+use tokio::spawn;
|
|
|
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
|
|
use tokio::sync::Mutex;
|
|
|
use tokio::task::JoinHandle;
|
|
|
@@ -240,7 +241,7 @@ impl Quant {
|
|
|
}
|
|
|
|
|
|
pub async fn handle_signals(quant_arc: Arc<Mutex<Quant>>, mut rx: Receiver<Order>) {
|
|
|
- tokio::spawn(async move{
|
|
|
+ spawn(async move{
|
|
|
loop {
|
|
|
sleep(Duration::from_millis(1)).await;
|
|
|
match rx.try_recv() {
|
|
|
@@ -318,7 +319,6 @@ impl Quant {
|
|
|
if self.local_cancel_log.contains_key(&data.client_id) {
|
|
|
self.local_cancel_log.remove(&data.client_id);
|
|
|
}
|
|
|
- // TODO 本地收到移除动作之后,会去继续撤销指定订单,有逻辑问题.
|
|
|
if self.local_orders.contains_key(&data.client_id) {
|
|
|
info!("删除本地订单, client_id:{}", data.client_id);
|
|
|
self.local_orders.remove(&data.client_id);
|
|
|
@@ -472,7 +472,7 @@ impl Quant {
|
|
|
//交易所处理订单信号
|
|
|
let mut platform_rest_fb = self.platform_rest.clone_box();
|
|
|
// info!("订单指令:{:?}", order);
|
|
|
- tokio::spawn(async move{
|
|
|
+ spawn(async move{
|
|
|
info!("update_local_order订单指令:{:?}", order);
|
|
|
platform_rest_fb.command_order(order).await;
|
|
|
});
|
|
|
@@ -603,7 +603,7 @@ impl Quant {
|
|
|
//异步交易所处理订单信号
|
|
|
let mut platform_rest_fb = self.platform_rest.clone_box();
|
|
|
// info!("订单指令:{:?}", orders);
|
|
|
- tokio::spawn(async move{
|
|
|
+ spawn(async move{
|
|
|
info!("_update_depth订单指令:{:?}", orders);
|
|
|
platform_rest_fb.command_order(orders).await;
|
|
|
});
|
|
|
@@ -803,7 +803,7 @@ impl Quant {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- pub fn check_risk(&mut self) {
|
|
|
+ pub async fn check_risk(&mut self) {
|
|
|
// 参数检查的风控
|
|
|
if self.strategy.start_cash == Decimal::ZERO {
|
|
|
warn!("请检查交易账户余额");
|
|
|
@@ -826,8 +826,7 @@ impl Quant {
|
|
|
self.params.account_name, draw_back, self.strategy.equity, self.strategy.max_equity);
|
|
|
warn!(exit_msg);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // _self.stop();
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
}
|
|
|
// 回撤风控2
|
|
|
@@ -836,8 +835,7 @@ impl Quant {
|
|
|
let exit_msg = format!("{} 交易亏损,触发止损,准备停机。", self.params.account_name);
|
|
|
warn!(exit_msg);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // _self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
// 报单延迟风控,平均延迟允许上限5000ms
|
|
|
// TODO quant.platform_rest.avg_delay不存在
|
|
|
@@ -873,8 +871,7 @@ impl Quant {
|
|
|
let exit_msg = format!("{} 合约连续检查本地仓位和推算仓位不符合,退出。", self.params.account_name);
|
|
|
warn!(exit_msg);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -883,8 +880,7 @@ impl Quant {
|
|
|
let exit_msg = format!("{} 开仓量为0,退出。", self.params.account_name);
|
|
|
warn!(exit_msg);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
|
|
|
// 行情更新异常风控
|
|
|
@@ -902,8 +898,7 @@ impl Quant {
|
|
|
warn!(?now_time_millis, ?last_update_millis, ?limit);
|
|
|
warn!(exit_msg);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
}
|
|
|
let local_orders = self.local_orders.clone();
|
|
|
@@ -914,8 +909,7 @@ impl Quant {
|
|
|
let exit_msg = format!("{}订单停留过长,怀疑异常,退出,cid:{}。", self.params.account_name, client_id);
|
|
|
warn!(exit_msg);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -926,8 +920,7 @@ impl Quant {
|
|
|
let exit_msg = format!("{} long_pos_bias: {},持仓均价异常,退出。", self.params.account_name, self.strategy.long_pos_bias);
|
|
|
warn!(exit_msg);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -937,8 +930,7 @@ impl Quant {
|
|
|
let exit_msg = format!("{} short_pos_bias: {},持仓均价异常,退出。", self.params.account_name, self.strategy.long_pos_bias);
|
|
|
warn!(exit_msg);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -950,8 +942,7 @@ impl Quant {
|
|
|
warn!(exit_msg);
|
|
|
warn!(?self.strategy.ref_price, ?self.strategy.mp);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -961,8 +952,7 @@ impl Quant {
|
|
|
warn!(exit_msg);
|
|
|
warn!(?self.strategy.ref_price, ?self.strategy.mp);
|
|
|
self.exit_msg = exit_msg;
|
|
|
- // TODO quant.stop()不存在
|
|
|
- // self.stop()
|
|
|
+ self.exit(10).await;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1032,8 +1022,16 @@ impl Quant {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
pub async fn exit(&mut self, delay: i8){
|
|
|
+ /**
|
|
|
+ * 停机函数
|
|
|
+ * mode_signal 不能小于80
|
|
|
+ * 前6秒用于maker平仓
|
|
|
+ * 后2秒用于撤maker平仓单
|
|
|
+ * 休眠2秒再执行check_position 避免卡单导致漏仓位
|
|
|
+ **/
|
|
|
+ info!("进入停机流程...");
|
|
|
+ self.mode_signal = 80;
|
|
|
info!("预约退出操作 delay:{}", delay);
|
|
|
if delay > 0i8 {
|
|
|
sleep(Duration::from_secs(delay as u64)).await;
|
|
|
@@ -1068,6 +1066,7 @@ impl Quant {
|
|
|
if start_cash.is_zero() && start_coin.is_zero() {
|
|
|
self.exit_msg = format!("{}{}{}{}", "初始为零 cash: ", start_cash, " coin: ", start_coin);
|
|
|
// 停止程序
|
|
|
+ self.exit(10).await;
|
|
|
return false;
|
|
|
}
|
|
|
info!("初始cash: {start_cash} 初始coin: {start_coin}");
|
|
|
@@ -1075,6 +1074,7 @@ impl Quant {
|
|
|
if mp <= Decimal::ZERO {
|
|
|
self.exit_msg = format!("{}{}", "初始价格获取错误: ", mp);
|
|
|
// 停止程序
|
|
|
+ self.exit(10).await;
|
|
|
return false;
|
|
|
} else {
|
|
|
info!("初始价格为 {}", mp);
|
|
|
@@ -1099,6 +1099,7 @@ impl Quant {
|
|
|
if self.strategy.step_size.is_zero() || self.strategy.tick_size.is_zero() {
|
|
|
self.exit_msg = format!("{}{}{}{}", "交易精度未正常获取 step_size: ", self.strategy.step_size, " tick_size:", self.strategy.tick_size);
|
|
|
// 停止程序
|
|
|
+ self.exit(10).await;
|
|
|
return false;
|
|
|
} else {
|
|
|
info!("数量精度 {}", self.strategy.step_size);
|
|
|
@@ -1124,6 +1125,7 @@ impl Quant {
|
|
|
(long_one_hand_value < hand_min_limit && short_one_hand_value < hand_min_limit) {
|
|
|
self.exit_msg = format!("{}{}{}{}", "初始下单量太少 buy: ", long_one_hand_amount, " sell: ", short_one_hand_amount);
|
|
|
// 停止程序
|
|
|
+ self.exit(10).await;
|
|
|
return false;
|
|
|
}
|
|
|
// 初始化调度器
|
|
|
@@ -1148,7 +1150,7 @@ fn parse_json_array(json: &str) -> serde_json::Result<Vec<Value>> {
|
|
|
}
|
|
|
|
|
|
pub fn run_strategy(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()>{
|
|
|
- return tokio::spawn(async move {
|
|
|
+ return spawn(async move {
|
|
|
//定期触发策略
|
|
|
info!("定时触发器启动");
|
|
|
info!("前期准备完成");
|
|
|
@@ -1179,7 +1181,7 @@ pub fn run_strategy(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()>{
|
|
|
if orders.is_not_empty() {
|
|
|
info!("触发onExit");
|
|
|
quant._update_local_orders(&orders);
|
|
|
- tokio::spawn(async move {
|
|
|
+ spawn(async move {
|
|
|
platform_rest_fb.command_order(orders).await;
|
|
|
});
|
|
|
}
|
|
|
@@ -1189,7 +1191,7 @@ pub fn run_strategy(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()>{
|
|
|
// 记录指令触发信息
|
|
|
if orders.is_not_empty() {
|
|
|
quant._update_local_orders(&orders);
|
|
|
- tokio::spawn(async move {
|
|
|
+ spawn(async move {
|
|
|
platform_rest_fb.command_order(orders).await;
|
|
|
});
|
|
|
}
|
|
|
@@ -1213,7 +1215,7 @@ pub fn run_strategy(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()>{
|
|
|
pub fn on_timer(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()> {
|
|
|
let quant_arc_clone = quant_arc.clone();
|
|
|
|
|
|
- return tokio::spawn(async move {
|
|
|
+ return spawn(async move {
|
|
|
tokio::time::sleep(Duration::from_secs(20)).await;
|
|
|
|
|
|
loop {
|