|
|
@@ -8,7 +8,7 @@ use rust_decimal_macros::dec;
|
|
|
use crate::model::{OrderCommand, OrderInfo, Position, TraderMsg};
|
|
|
use crate::params::Params;
|
|
|
use crate::utils;
|
|
|
-use tracing::{info, trace, instrument, subscriber, debug, error};
|
|
|
+use tracing::{info, trace, instrument, subscriber, error};
|
|
|
use tracing_subscriber;
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
@@ -350,14 +350,40 @@ impl Strategy {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- // TODO
|
|
|
- pub fn _print_summary(&self) {}
|
|
|
+ // 打印状态信息
|
|
|
+ pub fn _print_summary(&self) {
|
|
|
+ info!("_print_summary还没有施工……")
|
|
|
+ }
|
|
|
|
|
|
- // TODO _cancel_targit_side_orders
|
|
|
+ // 取消目标方向订单,原文是_cancel_targit_side_orders
|
|
|
+ #[instrument(skip(self), level="TRACE")]
|
|
|
pub fn _cancel_target_side_orders(&self) -> OrderCommand {
|
|
|
- let order_side = vec!["kd", "kk", "pd", "pk"];
|
|
|
+ // 要取消的目标方向
|
|
|
+ let target_side = vec![
|
|
|
+ "kd".to_string(),
|
|
|
+ "kk".to_string(),
|
|
|
+ "pd".to_string(),
|
|
|
+ "pk".to_string()
|
|
|
+ ];
|
|
|
|
|
|
- return OrderCommand::new()
|
|
|
+ trace!(?self.local_orders);
|
|
|
+ let mut command = OrderCommand::new();
|
|
|
+ for client_id in self.local_orders.keys() {
|
|
|
+ let order = self.local_orders.get(client_id).unwrap();
|
|
|
+
|
|
|
+ // 如果不属于目标方向,则不需要取消
|
|
|
+ if !target_side.contains(&order.side.clone()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 属于目标方向,则取消
|
|
|
+ let key = format!("Cancel{}", client_id);
|
|
|
+ let value = vec![order.client_id.clone(), order.order_id.clone()];
|
|
|
+ command.cancel.insert(key, value);
|
|
|
+ }
|
|
|
+ trace!(?command);
|
|
|
+
|
|
|
+ return command;
|
|
|
}
|
|
|
|
|
|
// 生成各类挂单价格,原文是gen_dist
|
|
|
@@ -604,9 +630,79 @@ impl Strategy {
|
|
|
return OrderCommand::new()
|
|
|
}
|
|
|
|
|
|
- // TODO
|
|
|
+ // 清空所有挂单和仓位保持休眠状态
|
|
|
+ #[instrument(skip(self), level="TRACE")]
|
|
|
pub fn _close_all(&self) -> OrderCommand {
|
|
|
- return OrderCommand::new()
|
|
|
+ let mut command = OrderCommand::new();
|
|
|
+
|
|
|
+ // 撤掉全部挂单
|
|
|
+ let mut pd_amount = dec!(0);
|
|
|
+ let mut pk_amount = dec!(0);
|
|
|
+ trace!(?self.local_orders);
|
|
|
+ for client_id in self.local_orders.keys() {
|
|
|
+ let order = self.local_orders.get(client_id).unwrap();
|
|
|
+
|
|
|
+ // 命令生成
|
|
|
+ let key = format!("Cancel{}", client_id);
|
|
|
+ let value = vec![order.client_id.clone(), order.order_id.clone()];
|
|
|
+ command.cancel.insert(key, value);
|
|
|
+
|
|
|
+ // 统计部分
|
|
|
+ if order.side == "pk".to_string() {
|
|
|
+ pk_amount += order.amount;
|
|
|
+ } else if order.side == "pd".to_string() {
|
|
|
+ pd_amount += order.amount;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ trace!(?pd_amount, ?pk_amount);
|
|
|
+
|
|
|
+ // 批量挂单
|
|
|
+ let need_close_long = self.pos.long_pos - pd_amount;
|
|
|
+ let need_close_short = self.pos.short_pos - pk_amount;
|
|
|
+ trace!(?need_close_long, ?need_close_short);
|
|
|
+
|
|
|
+ // 做多仓位平仓
|
|
|
+ if need_close_long * self.mp > self._min_amount_value {
|
|
|
+ let mut amount = need_close_long;
|
|
|
+ // 现货要对数量精度进行限定处理
|
|
|
+ if self.exchange.contains("spot") {
|
|
|
+ amount = utils::fix_amount(amount, self.step_size);
|
|
|
+ }
|
|
|
+ let price = utils::fix_price(self.mp, self.tick_size);
|
|
|
+ let client_id = utils::generate_client_id(Some(self.broker_id.clone()));
|
|
|
+
|
|
|
+ let value = vec![
|
|
|
+ amount.to_string(),
|
|
|
+ "pd".to_string(),
|
|
|
+ price.to_string(),
|
|
|
+ client_id.to_string()
|
|
|
+ ];
|
|
|
+ command.limits_close.insert(client_id.clone(), value);
|
|
|
+
|
|
|
+ trace!(?self.pos.long_pos, ?self.mp, ?need_close_long, ?command)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 做空仓位平仓
|
|
|
+ if need_close_short * self.mp > self._min_amount_value {
|
|
|
+ let mut amount = need_close_short;
|
|
|
+ if self.exchange.contains("spot") {
|
|
|
+ amount = utils::fix_amount(amount, self.step_size);
|
|
|
+ }
|
|
|
+ let price = utils::fix_price(self.mp, self.tick_size);
|
|
|
+ let client_id = utils::generate_client_id(Some(self.broker_id.clone()));
|
|
|
+
|
|
|
+ let value = vec![
|
|
|
+ amount.to_string(),
|
|
|
+ "pk".to_string(),
|
|
|
+ price.to_string(),
|
|
|
+ client_id.to_string()
|
|
|
+ ];
|
|
|
+ command.limits_close.insert(client_id.clone(), value);
|
|
|
+
|
|
|
+ trace!(?self.pos.short_pos, ?self.mp, ?need_close_short, ?command)
|
|
|
+ }
|
|
|
+
|
|
|
+ return command;
|
|
|
}
|
|
|
|
|
|
// 检查是否完成准备,注意:原文是未准备完成返回true!!!!!!!!!!!!!!!!!!!
|
|
|
@@ -625,9 +721,12 @@ impl Strategy {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // TODO
|
|
|
+ // 接近整点时刻 不允许报单 防止下单bug
|
|
|
pub fn check_allow_post_open(&self) -> bool {
|
|
|
- return true;
|
|
|
+ let local_time_second = self.local_time / 1000;
|
|
|
+ let diff_time = local_time_second % (60 * 60);
|
|
|
+
|
|
|
+ return diff_time > 30 && diff_time < 3570;
|
|
|
}
|
|
|
|
|
|
// 平仓订单处理命令
|
|
|
@@ -864,9 +963,9 @@ impl Strategy {
|
|
|
}
|
|
|
|
|
|
// 报单时延检测
|
|
|
- // if self.local_time - self.post_open_time < self.post_open_interval {
|
|
|
- // return;
|
|
|
- // }
|
|
|
+ if self.local_time - self.post_open_time < self.post_open_interval {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
// 挂单范围获取
|
|
|
let long_upper = self.open_dist[0];
|