|
|
@@ -356,8 +356,8 @@ impl Strategy {
|
|
|
}
|
|
|
|
|
|
// 取消目标方向订单,原文是_cancel_targit_side_orders
|
|
|
- #[instrument(skip(self), level="TRACE")]
|
|
|
- pub fn _cancel_target_side_orders(&self) -> OrderCommand {
|
|
|
+ #[instrument(skip(self, command), level="TRACE")]
|
|
|
+ pub fn _cancel_target_side_orders(&self, command: &mut OrderCommand) {
|
|
|
// 要取消的目标方向
|
|
|
let target_side = vec![
|
|
|
"kd".to_string(),
|
|
|
@@ -367,7 +367,6 @@ impl Strategy {
|
|
|
];
|
|
|
|
|
|
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();
|
|
|
|
|
|
@@ -382,8 +381,6 @@ impl Strategy {
|
|
|
command.cancel.insert(key, value);
|
|
|
}
|
|
|
trace!(?command);
|
|
|
-
|
|
|
- return command;
|
|
|
}
|
|
|
|
|
|
// 生成各类挂单价格,原文是gen_dist
|
|
|
@@ -620,21 +617,55 @@ impl Strategy {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // TODO onExit
|
|
|
+ // 当退出时调用,全撤全平 准备退出
|
|
|
pub fn on_exit(&mut self, trader_msg: &TraderMsg) -> OrderCommand {
|
|
|
- return OrderCommand::new()
|
|
|
+ let mut command = OrderCommand::new();
|
|
|
+
|
|
|
+ if self._update_data(trader_msg) {
|
|
|
+ if !self.check_ready() {
|
|
|
+ return command;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 取消、平掉所有
|
|
|
+ self._close_all(&mut command);
|
|
|
+ // 更新撤单队列
|
|
|
+ self._update_in_cancel(&mut command);
|
|
|
+ // 检查限频
|
|
|
+ self._check_request_limit(&mut command);
|
|
|
+ // 统计请求频率
|
|
|
+ self._update_request_num(&mut command);
|
|
|
+ }
|
|
|
+ trace!(?command);
|
|
|
+
|
|
|
+ return command;
|
|
|
}
|
|
|
|
|
|
- // TODO onSleep
|
|
|
+ // 休眠时调用,全撤 不再下新订单了 防止影响check_position执行
|
|
|
pub fn on_sleep(&mut self, trader_msg: &TraderMsg) -> OrderCommand {
|
|
|
- return OrderCommand::new()
|
|
|
+ let mut command = OrderCommand::new();
|
|
|
+
|
|
|
+ if self._update_data(trader_msg) {
|
|
|
+ if !self.check_ready() {
|
|
|
+ return command;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只是取消掉目标侧订单
|
|
|
+ self._cancel_target_side_orders(&mut command);
|
|
|
+ // 更新撤单队列
|
|
|
+ self._update_in_cancel(&mut command);
|
|
|
+ // 检查限频
|
|
|
+ self._check_request_limit(&mut command);
|
|
|
+ // 统计请求频率
|
|
|
+ self._update_request_num(&mut command);
|
|
|
+ }
|
|
|
+ trace!(?command);
|
|
|
+
|
|
|
+ return command;
|
|
|
}
|
|
|
|
|
|
// 清空所有挂单和仓位保持休眠状态
|
|
|
- #[instrument(skip(self), level="TRACE")]
|
|
|
- pub fn _close_all(&self) -> OrderCommand {
|
|
|
- let mut command = OrderCommand::new();
|
|
|
-
|
|
|
+ #[instrument(skip(self, command), level="TRACE")]
|
|
|
+ pub fn _close_all(&self, command: &mut OrderCommand) {
|
|
|
// 撤掉全部挂单
|
|
|
let mut pd_amount = dec!(0);
|
|
|
let mut pk_amount = dec!(0);
|
|
|
@@ -701,8 +732,6 @@ impl Strategy {
|
|
|
|
|
|
trace!(?self.pos.short_pos, ?self.mp, ?need_close_short, ?command)
|
|
|
}
|
|
|
-
|
|
|
- return command;
|
|
|
}
|
|
|
|
|
|
// 检查是否完成准备,注意:原文是未准备完成返回true!!!!!!!!!!!!!!!!!!!
|
|
|
@@ -1127,7 +1156,7 @@ impl Strategy {
|
|
|
mod tests {
|
|
|
use rust_decimal::Decimal;
|
|
|
use rust_decimal_macros::dec;
|
|
|
- use tracing::{info, trace};
|
|
|
+ use tracing::{trace};
|
|
|
use crate::model::{OrderInfo, TraderMsg};
|
|
|
use crate::params::Params;
|
|
|
use crate::strategy::Strategy;
|