|
|
@@ -13,7 +13,7 @@ use reqwest::{Client};
|
|
|
use tokio::spawn;
|
|
|
use tokio::time::Instant;
|
|
|
use global::params::Params;
|
|
|
-use standard::{OrderCommand};
|
|
|
+use standard::{OrderCommand, PriceOrder};
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
pub struct Strategy {
|
|
|
@@ -565,6 +565,7 @@ impl Strategy {
|
|
|
command.check.clear();
|
|
|
command.limits_open.clear();
|
|
|
command.limits_close.clear();
|
|
|
+ command.cancel_price_order.clear();
|
|
|
msg = format!("请求频率溢出,程序禁止任何操作!({}/{})", self.request_count, self.limit_requests_num);
|
|
|
} else if self.request_order_count >= self.limit_order_requests_num { // 100%超过下单频率,则不再进行平仓挂单
|
|
|
command.limits_close.clear();
|
|
|
@@ -1225,6 +1226,44 @@ impl Strategy {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 取消止损订单
|
|
|
+ pub fn _cancel_price_order(&mut self,
|
|
|
+ command: &mut OrderCommand,
|
|
|
+ local_orders: &HashMap<String, OrderInfo>,
|
|
|
+ local_price_orders: &HashMap<String, PriceOrder>) {
|
|
|
+ // 1. 正在取消的订单对应的止损单要取消
|
|
|
+ for key in command.cancel.keys() {
|
|
|
+ let client_id = command.cancel[key][0].clone();
|
|
|
+
|
|
|
+ for id in local_price_orders.keys() {
|
|
|
+ // 不存在的话,进行下一个止损单的判定
|
|
|
+ if !client_id.eq(id) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果在取消的订单存在对应的止损单
|
|
|
+ command.cancel_price_order.insert(id.clone(), local_price_orders[id].clone());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 止损单对应的订单不存在的也要取消
|
|
|
+ for id_x in local_price_orders.keys() {
|
|
|
+ let mut is_exists = false;
|
|
|
+
|
|
|
+ // 判断本地订单里面是不是有这个止损单对应的单
|
|
|
+ for id_y in local_orders.keys() {
|
|
|
+ if id_x.eq(id_y) {
|
|
|
+ is_exists = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果本地订单不存在这个止损单,说明此止损单不合法
|
|
|
+ if !is_exists {
|
|
|
+ command.cancel_price_order.insert(id_x.clone(), local_price_orders[id_x].clone());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 定时打印
|
|
|
pub fn on_time_print(&mut self) {
|
|
|
if self.local_time - self._print_time < self._print_interval {
|
|
|
@@ -1249,6 +1288,7 @@ impl Strategy {
|
|
|
// 在满足条件后,返回非空command,否则返回一个空的command。原文的onTime。
|
|
|
pub fn on_tick(&mut self,
|
|
|
local_orders: &HashMap<String, OrderInfo>,
|
|
|
+ local_price_orders: &HashMap<String, PriceOrder>,
|
|
|
local_position: &LocalPosition,
|
|
|
agg_market: &Vec<Decimal>,
|
|
|
local_cash: &Decimal,
|
|
|
@@ -1281,15 +1321,16 @@ impl Strategy {
|
|
|
self.generate_dist();
|
|
|
|
|
|
// 下单指令处理逻辑
|
|
|
- self._cancel_open(&mut command, local_orders); // 撤单命令处理
|
|
|
- self._post_close(&mut command, local_orders); // 平仓单命令处理
|
|
|
- self._post_open(&mut command, local_orders); // 限价单命令处理
|
|
|
-
|
|
|
- self._check_local_orders(&mut command, local_orders); // 固定时间检查超时订单
|
|
|
- self._update_in_cancel(&mut command, local_orders); // 更新撤单队列,是一个filter
|
|
|
- self._check_request_limit(&mut command); // 限制频率,移除不合规则之订单,是一个filter
|
|
|
- self._refresh_request_limit(); // 刷新频率限制
|
|
|
- self._update_request_num(&mut command); // 统计刷新频率
|
|
|
+ self._cancel_open(&mut command, local_orders); // 撤单命令处理
|
|
|
+ self._post_close(&mut command, local_orders); // 平仓单命令处理
|
|
|
+ self._post_open(&mut command, local_orders); // 限价单命令处理
|
|
|
+
|
|
|
+ self._check_local_orders(&mut command, local_orders); // 固定时间检查超时订单
|
|
|
+ self._update_in_cancel(&mut command, local_orders); // 更新撤单队列,是一个filter
|
|
|
+ self._cancel_price_order(&mut command, local_orders, local_price_orders); // 取消止损单命令处理
|
|
|
+ self._check_request_limit(&mut command); // 限制频率,移除不合规则之订单,是一个filter
|
|
|
+ self._refresh_request_limit(); // 刷新频率限制
|
|
|
+ self._update_request_num(&mut command); // 统计刷新频率
|
|
|
|
|
|
if command.limits_open.len() != 0 || command.limits_close.len() != 0 {
|
|
|
let name = self.params.account_name.clone();
|