strategy.rs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. use std::cmp::{max, min};
  2. use std::collections::HashMap;
  3. use std::ops::{Add, Div, Mul};
  4. use chrono::Utc;
  5. use rust_decimal::Decimal;
  6. use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
  7. use rust_decimal_macros::dec;
  8. use crate::model::{LocalPosition, OrderInfo, TraderMsg};
  9. use crate::params::Params;
  10. use crate::utils;
  11. use tracing::{info, instrument, error, debug};
  12. use tracing::field::debug;
  13. use tracing_subscriber;
  14. use standard::OrderCommand;
  15. #[derive(Debug)]
  16. pub struct Strategy {
  17. // pub interval: Decimal, // 原文没有使用过这个参数
  18. // 各类时间戳和时延,我们都改成了毫秒级
  19. pub _print_time: i64, // 上次打印时间
  20. pub _print_interval: i64, // 打印时延
  21. pub _start_time: i64, // 开始时间
  22. pub local_time: i64, // 本地时间
  23. pub local_start_time: i64, // 本地开始时间
  24. pub post_open_time: i64, // 上次提交订单的时间戳
  25. pub post_open_interval: i64, // 提交订单时延
  26. pub _check_local_orders_time: i64, // 上次查单时间
  27. pub _check_local_orders_interval: i64, // 查单间距,原文是秒级,这里改成毫秒级
  28. pub in_cancel: HashMap<String, i64>, //撤单队列
  29. pub cancel_wait_interval: i64, // 取消等待时延
  30. pub in_check: HashMap<String, i64>, // 查单队列
  31. pub check_wait_interval: i64, // 检测时延
  32. pub request_limit_check_time: i64, // 上次检查订单的时间
  33. pub request_limit_check_interval: i64, // 原文是秒级,这里改成毫秒级
  34. pub request_count: i64, // 记录请求次数,原文的request_num
  35. pub request_order_count: i64, // 记录下单次数,原文的request_order_num
  36. pub limit_requests_num: i64, // 单位(时延)时间内请求次数上限
  37. pub limit_order_requests_num: i64, // 单位(时延)时间内下单次数上限
  38. pub _req_num_per_window: i64, // 单位(时延)时间内请求上限窗口
  39. pub place_order_limit: i64, // 参数层面传进来的下单数量限制
  40. pub params: Params, //
  41. pub exchange: String, //
  42. pub broker_id: String, //
  43. pub trade_name: String, //
  44. pub ref_exchange_length: usize, //
  45. pub ref_name: Vec<String>, //
  46. pub maker_mode: String, //
  47. pub local_orders: HashMap<String, OrderInfo>, // 本地订单
  48. pub pos: LocalPosition, //
  49. pub long_hold_value: Decimal, //
  50. pub short_hold_value: Decimal, //
  51. pub equity: Decimal, //
  52. pub coin: Decimal, //
  53. pub cash: Decimal, //
  54. pub start_equity: Decimal, //
  55. pub start_coin: Decimal, //
  56. pub start_cash: Decimal, //
  57. pub max_equity: Decimal, //
  58. pub local_profit: Decimal, //
  59. pub total_amount: Decimal, //
  60. pub is_ready: bool, // 程序是否已经准备好,ready
  61. pub _is_print: bool, //
  62. pub _min_amount_value: Decimal, //
  63. pub _max_amount_value: Decimal, //
  64. pub mp_ema: Decimal, // 原文的mp_ewma
  65. pub mp: Decimal, //
  66. pub bp: Decimal, //
  67. pub ap: Decimal, //
  68. pub ref_price: Decimal, //
  69. pub ref_bp: Decimal, //
  70. pub ref_ap: Decimal, //
  71. pub step_size: Decimal, // 原文的stepSize
  72. pub tick_size: Decimal, // 原文的tickSize
  73. pub max_pos_rate: Decimal, // 原文的maxPos,其实是最大持仓比例
  74. pub profit: Decimal, //
  75. pub daily_return: Decimal, //
  76. pub adjust_lever_rate: Decimal, // 原文的adjust_leverrate
  77. pub lever_rate: Decimal, // 原文的leverrate
  78. pub long_pos_bias: Decimal, //
  79. pub short_pos_bias: Decimal, //
  80. pub long_hold_rate: Decimal, //
  81. pub short_hold_rate: Decimal, //
  82. pub max_long_value: Decimal, // 最大做多持仓
  83. pub max_short_value: Decimal, // 最大做空持仓
  84. pub open_dist: Vec<Decimal>, // 开仓相关价格
  85. pub close_dist: Vec<Decimal>, // 平仓相关价格
  86. pub trade_close_dist: Decimal, //
  87. pub trade_open_dist: Decimal, //
  88. pub ref_index: usize, //
  89. pub predict: Decimal, //
  90. pub predict_alpha: Decimal, //
  91. pub post_side: i64, // 交易方向
  92. pub trade_vol_24h_w: Decimal, // 24小时成交额(单位:万)
  93. pub grid: Decimal, // 网格数量
  94. }
  95. impl Strategy {
  96. pub fn new(params: &Params, is_print: bool) -> Self {
  97. if params.ref_exchange.len() != params.ref_pair.len() {
  98. panic!("参考盘口数不等于参考品种数,退出,请检查配置!")
  99. }
  100. // strategy的初始化,里面已经有一些参数初始化了
  101. let mut strategy = Self {
  102. _print_time: 0,
  103. _start_time: 0,
  104. local_time: 0,
  105. local_start_time: 0,
  106. request_count: 0,
  107. request_order_count: 0,
  108. _print_interval: 5 * 1000,
  109. in_cancel: Default::default(),
  110. cancel_wait_interval: (0.2 * 1000f64).to_i64().unwrap(),
  111. in_check: Default::default(),
  112. check_wait_interval: 10 * 1000,
  113. _check_local_orders_time: 0,
  114. _check_local_orders_interval: 0,
  115. place_order_limit: 0,
  116. request_limit_check_time: 0,
  117. request_limit_check_interval: 0,
  118. limit_requests_num: 0,
  119. limit_order_requests_num: 0,
  120. _req_num_per_window: 0,
  121. post_open_time: 0,
  122. post_open_interval: 0,
  123. params: params.clone(),
  124. exchange: params.exchange.clone(),
  125. broker_id: params.broker_id.clone(),
  126. trade_name: "".to_string(),
  127. ref_exchange_length: params.ref_exchange.len(),
  128. ref_name: vec![],
  129. maker_mode: "free".to_string(),
  130. local_orders: Default::default(),
  131. pos: LocalPosition {
  132. long_pos: Default::default(),
  133. short_pos: Default::default(),
  134. long_avg: Default::default(),
  135. short_avg: Default::default(),
  136. },
  137. long_hold_value: Default::default(),
  138. short_hold_value: Default::default(),
  139. equity: Default::default(),
  140. coin: Default::default(),
  141. cash: Default::default(),
  142. start_equity: Default::default(),
  143. start_coin: Default::default(),
  144. start_cash: Default::default(),
  145. max_equity: Default::default(),
  146. local_profit: Default::default(),
  147. total_amount: Default::default(),
  148. is_ready: false,
  149. _is_print: is_print,
  150. _min_amount_value: dec!(30.0),
  151. _max_amount_value: dec!(10000.0),
  152. mp_ema: Default::default(),
  153. mp: Default::default(),
  154. bp: Default::default(),
  155. ap: Default::default(),
  156. ref_price: Default::default(),
  157. ref_bp: Default::default(),
  158. ref_ap: Default::default(),
  159. step_size: dec!(1e-10),
  160. tick_size: dec!(1e-10),
  161. max_pos_rate: Default::default(),
  162. profit: Default::default(),
  163. daily_return: Default::default(),
  164. adjust_lever_rate: Decimal::ONE,
  165. lever_rate: Default::default(),
  166. long_pos_bias: Default::default(),
  167. short_pos_bias: Default::default(),
  168. long_hold_rate: Default::default(),
  169. short_hold_rate: Default::default(),
  170. max_long_value: Default::default(),
  171. max_short_value: Default::default(),
  172. open_dist: vec![],
  173. close_dist: vec![],
  174. trade_close_dist: params.close,
  175. trade_open_dist: params.open,
  176. ref_index: 0,
  177. predict: Default::default(),
  178. predict_alpha: Default::default(),
  179. post_side: 0,
  180. trade_vol_24h_w: Default::default(),
  181. grid: Decimal::from(params.grid),
  182. };
  183. // 交易名字
  184. strategy.trade_name = format!("{}@{}", params.exchange.clone(), params.pair.clone());
  185. // 参考交易所的trade_name
  186. for index in 0..strategy.ref_exchange_length {
  187. strategy.ref_name.push(format!("{}@{}", params.ref_exchange[index], params.ref_pair[index]));
  188. }
  189. // 杠杆比例处理
  190. strategy.lever_rate = params.lever_rate;
  191. if strategy.exchange.contains("spot") {
  192. strategy.lever_rate = min(params.lever_rate, Decimal::ONE);
  193. }
  194. // 各类时间戳
  195. let now = Utc::now();
  196. strategy.local_time = now.timestamp_millis();
  197. strategy.local_start_time = now.timestamp_millis();
  198. strategy._print_time = now.timestamp_millis();
  199. strategy._start_time = now.timestamp_millis();
  200. // 检查订单的时间戳
  201. strategy._check_local_orders_time = now.timestamp_millis();
  202. strategy._check_local_orders_interval = 10 * 1000;
  203. // 下单的相关限制处理
  204. strategy.place_order_limit = params.place_order_limit;
  205. strategy.request_limit_check_time = now.timestamp_millis();
  206. strategy.request_limit_check_interval = 10 * 1000;
  207. // 求得正常请求数量和下单请求数量(interval时间内)
  208. let request_limit_check_interval_per_second = strategy.request_limit_check_interval / 1000;
  209. strategy.limit_requests_num = utils::get_limit_requests_num_per_second(params.exchange.clone(), params.place_order_limit) * (request_limit_check_interval_per_second);
  210. strategy.limit_order_requests_num = utils::get_limit_order_requests_num_per_second(params.exchange.clone(), params.place_order_limit) * (request_limit_check_interval_per_second);
  211. // 开仓下单间隔 均匀下单机会
  212. strategy.post_open_time = now.timestamp_millis();
  213. let post_open_interval_per_second = Decimal::ONE.div(Decimal::from_i64(utils::get_limit_order_requests_num_per_second(params.exchange.clone(), 0)).unwrap());
  214. strategy.post_open_interval = dec!(1000).mul(post_open_interval_per_second).to_i64().unwrap();
  215. info!("策略模块初始化完成!");
  216. return strategy;
  217. }
  218. // 更新当前strategy的各类信息
  219. #[instrument(skip(self, trader_msg), level="TRACE")]
  220. pub fn _update_data(&mut self, trader_msg: &TraderMsg) -> bool {
  221. debug!(?self);
  222. debug!(?trader_msg);
  223. self.local_orders.clear();
  224. self.local_orders = trader_msg.orders.clone();
  225. // position信息更新
  226. if self.pos.long_pos != trader_msg.position.long_pos {
  227. self.pos.long_pos = trader_msg.position.long_pos;
  228. self.pos.long_avg = trader_msg.position.long_avg;
  229. }
  230. if self.pos.short_pos != trader_msg.position.short_pos {
  231. self.pos.short_pos = trader_msg.position.short_pos;
  232. self.pos.short_avg = trader_msg.position.short_avg;
  233. }
  234. debug!(?self.pos);
  235. // 价格值处理
  236. self.bp = trader_msg.market[global::public_params::BID_PRICE_INDEX];
  237. self.ap = trader_msg.market[global::public_params::ASK_PRICE_INDEX];
  238. self.mp = (self.bp + self.ap) * dec!(0.5);
  239. // 中间价的ema值处理
  240. if self.mp_ema.eq(&Decimal::ZERO) {
  241. self.mp_ema = self.mp;
  242. } else {
  243. self.mp_ema = self.mp_ema * dec!(0.999) + self.mp * dec!(0.001);
  244. }
  245. debug!(?self.bp, ?self.ap, ?self.mp, ?self.mp_ema);
  246. // 动态杠杆调节
  247. if self.mp > self.mp_ema {
  248. self.adjust_lever_rate = Decimal::ONE;
  249. } else {
  250. self.adjust_lever_rate = dec!(0.8);
  251. }
  252. debug!(?self.adjust_lever_rate);
  253. // 当前持仓价值处理
  254. self.long_hold_value = self.pos.long_pos * self.mp;
  255. self.short_hold_value = self.pos.short_pos * self.mp;
  256. debug!(?self.long_hold_value, ?self.short_hold_value);
  257. // 分现货或合约计算最大开仓价值
  258. if self.exchange.contains("spot") {
  259. self.max_long_value = self.start_cash * self.lever_rate * self.adjust_lever_rate;
  260. self.max_short_value = self.start_cash * self.lever_rate * self.adjust_lever_rate * self.mp;
  261. } else {
  262. self.max_long_value = self.equity * self.lever_rate * self.adjust_lever_rate;
  263. self.max_short_value = self.max_long_value;
  264. }
  265. debug!(?self.max_long_value, ?self.max_short_value, ?self.equity, ?self.lever_rate, ?self.adjust_lever_rate);
  266. // 做市模式识别
  267. if self.ref_name[self.ref_index].eq(&self.trade_name) {
  268. self.maker_mode = "free".to_string();
  269. } else {
  270. self.maker_mode = "follow".to_string();
  271. }
  272. debug!(?self.maker_mode);
  273. // 参考价格
  274. if trader_msg.ref_price.len() == 0 {
  275. self.ref_bp = self.bp;
  276. self.ref_ap = self.ap;
  277. self.ref_price = self.mp;
  278. } else {
  279. self.ref_bp = trader_msg.ref_price[self.ref_index][0];
  280. self.ref_ap = trader_msg.ref_price[self.ref_index][1];
  281. self.ref_price = (self.ref_bp + self.ref_ap) * dec!(0.5);
  282. }
  283. debug!(?self.ref_bp, ?self.ref_ap, %self.ref_price);
  284. // spread
  285. let temp_predict = trader_msg.predict * self.predict_alpha;
  286. self.predict = utils::clip(temp_predict, -self.trade_open_dist, self.trade_open_dist);
  287. debug!(?self.predict);
  288. // 计算当前账户cash和coin
  289. self.coin = trader_msg.coin;
  290. self.cash = trader_msg.cash;
  291. self.equity = trader_msg.cash + trader_msg.coin * self.mp;
  292. if self.equity > self.max_equity {
  293. self.max_equity = self.equity;
  294. }
  295. debug!(?self.coin, ?self.cash, ?self.equity, ?self.max_equity);
  296. // 总可开数量
  297. self.total_amount = self.equity * self.lever_rate * self.adjust_lever_rate / self.mp;
  298. self.total_amount = utils::fix_amount(self.total_amount, self.step_size);
  299. debug!(?self.total_amount);
  300. if self.total_amount.eq(&Decimal::ZERO) {
  301. error!("总可开数量太少!equity={}, lever_rate={}, adjust_lever_rate={}", self.equity, self.lever_rate, self.adjust_lever_rate);
  302. return false;
  303. }
  304. // 求最大pos
  305. if self.equity > Decimal::ZERO {
  306. let max_pos_rate = max(self.pos.long_pos, self.pos.short_pos) * self.mp / self.equity;
  307. if max_pos_rate > self.max_pos_rate {
  308. self.max_pos_rate = max_pos_rate;
  309. }
  310. debug!(?max_pos_rate, ?self.max_pos_rate);
  311. }
  312. return true;
  313. }
  314. // 打印状态信息
  315. pub fn _print_summary(&self) {
  316. info!("_print_summary还没有施工……")
  317. }
  318. // 取消目标方向订单,原文是_cancel_targit_side_orders
  319. #[instrument(skip(self, command), level="TRACE")]
  320. pub fn _cancel_target_side_orders(&self, command: &mut OrderCommand) {
  321. // 要取消的目标方向
  322. let target_side = vec![
  323. "kd".to_string(),
  324. "kk".to_string(),
  325. "pd".to_string(),
  326. "pk".to_string()
  327. ];
  328. debug!(?self.local_orders);
  329. for client_id in self.local_orders.keys() {
  330. let order = self.local_orders.get(client_id).unwrap();
  331. // 如果不属于目标方向,则不需要取消
  332. if !target_side.contains(&order.side.clone()) {
  333. continue;
  334. }
  335. // 属于目标方向,则取消
  336. let key = format!("Cancel{}", client_id);
  337. let value = vec![order.client_id.clone(), order.order_id.clone()];
  338. command.cancel.insert(key, value);
  339. }
  340. debug!(?command);
  341. }
  342. // 生成各类挂单价格,原文是gen_dist
  343. #[instrument(skip(self), level="TRACE")]
  344. pub fn generate_dist(&mut self) {
  345. let open = self.trade_open_dist;
  346. let close = self.trade_close_dist;
  347. let pos_rate = vec![self.long_hold_rate, self.short_hold_rate];
  348. let ref_bp = self.ref_bp;
  349. let ref_ap = self.ref_ap;
  350. let predict = self.predict;
  351. let grid = self.grid;
  352. let mode = self.maker_mode.clone();
  353. // 平仓相关
  354. let mut mp = (ref_bp + ref_ap) * dec!(0.5);
  355. let mut buy_start = mp;
  356. let mut sell_start = mp;
  357. let mut avoid = min(dec!(0.0005), close * dec!(0.5));
  358. let mut close_dist = vec![
  359. buy_start * (Decimal::ONE + predict - close + avoid), // buy upper
  360. buy_start * (Decimal::ONE + predict - close - avoid), // buy lower
  361. sell_start * (Decimal::ONE + predict + close - avoid), // sell lower
  362. sell_start * (Decimal::ONE + predict + close + avoid), // sell upper
  363. ];
  364. debug!(?mp, ?buy_start, ?sell_start, ?avoid, ?close_dist);
  365. // 自由做市模式
  366. if mode == "free".to_string() {
  367. buy_start = ref_bp;
  368. sell_start = ref_ap;
  369. }
  370. // 跟随做市模式
  371. else if mode == "follow".to_string() {
  372. mp = (ref_bp + ref_ap) * dec!(0.5);
  373. buy_start = mp;
  374. sell_start = mp;
  375. } else {
  376. panic!("未知做市类型:mode={}", mode)
  377. }
  378. debug!(?mode, ?buy_start, ?sell_start, ?mp);
  379. // 开仓相关
  380. avoid = min(dec!(0.001), open * dec!(0.05));
  381. // 持仓偏移
  382. let buy_shift = Decimal::ONE + pos_rate[0] * grid;
  383. let sell_shift = Decimal::ONE + pos_rate[1] * grid;
  384. let mut open_dist = vec![
  385. buy_start * (Decimal::ONE + predict - open * buy_shift + avoid), // buy upper
  386. buy_start * (Decimal::ONE + predict - open * buy_shift - avoid), // buy lower
  387. sell_start * (Decimal::ONE + predict + open * sell_shift - avoid), // sell lower
  388. sell_start * (Decimal::ONE + predict + open * sell_shift + avoid), // sell upper
  389. ];
  390. debug!(?avoid, ?buy_shift, ?sell_shift, ?avoid, ?open_dist);
  391. // 修复价格
  392. for open_price in &mut open_dist {
  393. *open_price = utils::fix_price(*open_price, self.tick_size);
  394. }
  395. for close_price in &mut close_dist {
  396. *close_price = utils::fix_price(*close_price, self.tick_size);
  397. }
  398. self.open_dist = open_dist.clone();
  399. self.close_dist = close_dist.clone();
  400. debug!(?open_dist);
  401. debug!(?close_dist);
  402. }
  403. // 统计请求次数
  404. #[instrument(skip(self, command), level="TRACE")]
  405. pub fn _update_request_num(&mut self, command: &OrderCommand) {
  406. debug!(?command);
  407. debug!(?self.request_order_count, ?self.request_count);
  408. let order_count = (command.limits_open.len() + command.limits_close.len()).to_i64().unwrap();
  409. let request_count = order_count + (command.cancel.len() + command.check.len()).to_i64().unwrap();
  410. self.request_order_count += order_count;
  411. self.request_count += request_count;
  412. debug!(?self.request_order_count, ?self.request_count);
  413. }
  414. // 根据平均请求次数限制开仓下单
  415. #[instrument(skip(self, command), level="TRACE")]
  416. pub fn _check_request_limit(&mut self, command: &mut OrderCommand) {
  417. debug!(?command);
  418. // 如果当前请求数超过限制
  419. if self.request_count > self.limit_requests_num {
  420. command.cancel.clear();
  421. command.check.clear();
  422. command.limits_open.clear();
  423. command.limits_close.clear();
  424. error!("请求频率溢出,程序禁止任何操作!");
  425. debug!(?command);
  426. return;
  427. }
  428. // 普通request超过普通请求频率上限的50%再进行判断
  429. // 下单request要超过下单请求频率上限的80%再进行判断
  430. if self.request_count < self.limit_requests_num * 5 / 10
  431. && self.request_order_count < self.limit_order_requests_num * 8 / 10 {
  432. return;
  433. }
  434. // 超过80%,直接取消limits_open的下单指令
  435. error!("80%+下单频率,程序禁止开仓!");
  436. debug!(?self.request_order_count, ?self.limit_order_requests_num);
  437. command.limits_open.clear();
  438. // 100%超过下单频率,则不再进行平仓挂单
  439. if self.request_order_count >= self.limit_order_requests_num {
  440. command.limits_close.clear();
  441. error!("100%下单频率!程序禁止平仓!");
  442. debug!(?self.request_order_count, ?self.limit_order_requests_num)
  443. }
  444. debug!(?command);
  445. }
  446. // 新增正在撤单、检查撤单队列,释放过时限制
  447. #[instrument(skip(self), level="TRACE")]
  448. pub fn _update_in_cancel(&mut self, command: &mut OrderCommand) {
  449. let mut new_cancel: HashMap<String, Vec<String>> = HashMap::new();
  450. for cancel_name in command.cancel.keys() {
  451. let cancel = command.cancel.get(cancel_name).unwrap();
  452. let client_id = cancel[0].clone();
  453. let mut need_limit_cancel = true;
  454. let order_some = self.local_orders.get(&client_id);
  455. // 判断是否在本地挂单表中
  456. if let Some(order) = order_some {
  457. // 如果订单创建时间大于100ms,才能有撤单操作
  458. if self.local_time - order.create_time < 100 {
  459. need_limit_cancel = false;
  460. }
  461. }
  462. if need_limit_cancel {
  463. // 如果已经不在撤销队列里,增加到撤销队列
  464. if self.in_cancel.get(&client_id).is_none() {
  465. self.in_cancel.insert(client_id.clone(), self.local_time);
  466. new_cancel.insert(cancel_name.clone(), cancel.clone());
  467. }
  468. }
  469. }
  470. debug!(?command);
  471. command.cancel = new_cancel;
  472. debug!(?command);
  473. // 释放撤单限制
  474. self._release_in_cancel();
  475. }
  476. // 维护查单队列,检查是否在撤单
  477. #[instrument(skip(self), level="TRACE")]
  478. pub fn _release_in_check(&mut self) {
  479. debug!(?self.in_check);
  480. // 为什么要移出来:Rust不允许边循环边修改map
  481. let mut to_remove = Vec::new();
  482. for client_id in self.in_check.keys() {
  483. let time = self.in_check.get(client_id).unwrap();
  484. // 等待不超时,就不移除
  485. if self.local_time - time <= self.check_wait_interval {
  486. continue;
  487. }
  488. // 等待超时,就移除正在撤单队列
  489. info!("移除查单队列:{}", client_id.clone());
  490. to_remove.push(client_id.clone());
  491. }
  492. // 在后面的循环中去单独处理map的更新
  493. for client_id in to_remove {
  494. self.in_check.remove(&client_id);
  495. }
  496. debug!(?self.in_check);
  497. }
  498. // 检查是否正在撤单
  499. #[instrument(skip(self), level="TRACE")]
  500. pub fn _release_in_cancel(&mut self) {
  501. debug!(?self.in_cancel);
  502. // 为什么要移出来:Rust不允许边循环边修改map
  503. let mut to_remove = Vec::new();
  504. for client_id in self.in_cancel.keys() {
  505. let time = self.in_cancel.get(client_id).unwrap();
  506. // 等待不超时,就不移除
  507. if self.local_time - time <= self.cancel_wait_interval {
  508. continue;
  509. }
  510. // 等待超时,就移除正在撤单队列
  511. info!("移除取消队列:{}", client_id.clone());
  512. to_remove.push(client_id.clone());
  513. }
  514. // 在后面的循环中去单独处理map的更新
  515. for client_id in to_remove {
  516. self.in_cancel.remove(&client_id);
  517. }
  518. debug!(?self.in_cancel);
  519. }
  520. // 刷新请求限制
  521. pub fn _refresh_request_limit(&mut self) {
  522. if self.local_time - self.request_limit_check_time < self.request_limit_check_interval {
  523. return;
  524. }
  525. self._req_num_per_window = self.request_count;
  526. self.request_count = 0;
  527. self.request_order_count = 0;
  528. self.request_limit_check_time = self.local_time;
  529. }
  530. // 刷新持仓比例
  531. #[instrument(skip(self), level="TRACE")]
  532. pub fn _pos_rate(&mut self) {
  533. debug!(?self);
  534. if self.max_long_value > Decimal::ZERO {
  535. self.long_hold_rate = self.long_hold_value / self.max_long_value;
  536. debug!(?self.long_hold_rate);
  537. }
  538. if self.max_short_value > Decimal::ZERO {
  539. self.short_hold_rate = self.short_hold_value / self.max_short_value;
  540. debug!(?self.short_hold_rate);
  541. }
  542. }
  543. // 当退出时调用,全撤全平 准备退出
  544. pub fn on_exit(&mut self, trader_msg: &TraderMsg) -> OrderCommand {
  545. let mut command = OrderCommand::new();
  546. if self._update_data(trader_msg) {
  547. if !self.check_ready() {
  548. return command;
  549. }
  550. // 取消、平掉所有
  551. self._close_all(&mut command);
  552. // 更新撤单队列
  553. self._update_in_cancel(&mut command);
  554. // 检查限频
  555. self._check_request_limit(&mut command);
  556. // 统计请求频率
  557. self._update_request_num(&mut command);
  558. }
  559. debug!(?command);
  560. return command;
  561. }
  562. // 休眠时调用,全撤 不再下新订单了 防止影响check_position执行
  563. pub fn on_sleep(&mut self, trader_msg: &TraderMsg) -> OrderCommand {
  564. let mut command = OrderCommand::new();
  565. if self._update_data(trader_msg) {
  566. if !self.check_ready() {
  567. return command;
  568. }
  569. // 只是取消掉目标侧订单
  570. self._cancel_target_side_orders(&mut command);
  571. // 更新撤单队列
  572. self._update_in_cancel(&mut command);
  573. // 检查限频
  574. self._check_request_limit(&mut command);
  575. // 统计请求频率
  576. self._update_request_num(&mut command);
  577. }
  578. debug!(?command);
  579. return command;
  580. }
  581. // 清空所有挂单和仓位保持休眠状态
  582. #[instrument(skip(self, command), level="TRACE")]
  583. pub fn _close_all(&self, command: &mut OrderCommand) {
  584. // 撤掉全部挂单
  585. let mut pd_amount = Decimal::ZERO;
  586. let mut pk_amount = Decimal::ZERO;
  587. debug!(?self.local_orders);
  588. for client_id in self.local_orders.keys() {
  589. let order = self.local_orders.get(client_id).unwrap();
  590. // 命令生成
  591. let key = format!("Cancel{}", client_id);
  592. let value = vec![order.client_id.clone(), order.order_id.clone()];
  593. command.cancel.insert(key, value);
  594. // 统计部分
  595. if order.side == "pk".to_string() {
  596. pk_amount += order.amount;
  597. } else if order.side == "pd".to_string() {
  598. pd_amount += order.amount;
  599. }
  600. }
  601. debug!(?pd_amount, ?pk_amount);
  602. // 批量挂单
  603. let need_close_long = self.pos.long_pos - pd_amount;
  604. let need_close_short = self.pos.short_pos - pk_amount;
  605. debug!(?need_close_long, ?need_close_short);
  606. // 做多仓位平仓
  607. if need_close_long * self.mp > self._min_amount_value {
  608. let mut amount = need_close_long;
  609. // 现货要对数量精度进行限定处理
  610. if self.exchange.contains("spot") {
  611. amount = utils::fix_amount(amount, self.step_size);
  612. }
  613. let price = utils::fix_price(self.mp, self.tick_size);
  614. let client_id = utils::generate_client_id(Some(self.broker_id.clone()));
  615. let value = vec![
  616. amount.to_string(),
  617. "pd".to_string(),
  618. price.to_string(),
  619. client_id.to_string()
  620. ];
  621. command.limits_close.insert(client_id.clone(), value);
  622. debug!(?self.pos.long_pos, ?self.mp, ?need_close_long, ?command)
  623. }
  624. // 做空仓位平仓
  625. if need_close_short * self.mp > self._min_amount_value {
  626. let mut amount = need_close_short;
  627. if self.exchange.contains("spot") {
  628. amount = utils::fix_amount(amount, self.step_size);
  629. }
  630. let price = utils::fix_price(self.mp, self.tick_size);
  631. let client_id = utils::generate_client_id(Some(self.broker_id.clone()));
  632. let value = vec![
  633. amount.to_string(),
  634. "pk".to_string(),
  635. price.to_string(),
  636. client_id.to_string()
  637. ];
  638. command.limits_close.insert(client_id.clone(), value);
  639. debug!(?self.pos.short_pos, ?self.mp, ?need_close_short, ?command)
  640. }
  641. }
  642. // 检查是否完成准备,注意:原文是未准备完成返回true!!!!!!!!!!!!!!!!!!!
  643. pub fn check_ready(&mut self) -> bool {
  644. if self.is_ready {
  645. return true;
  646. }
  647. let pre_hot:i64 = 10 * 1000;
  648. if !self.mp.eq(&Decimal::ZERO) && self.local_time - self.local_start_time > pre_hot {
  649. self.is_ready = true;
  650. debug!(?self.mp, ?self.local_time, ?self.local_start_time, ?pre_hot);
  651. info!("策略预热完毕,可以执行后续逻辑!")
  652. }
  653. return false;
  654. }
  655. // 接近整点时刻 不允许报单 防止下单bug
  656. pub fn check_allow_post_open(&self) -> bool {
  657. let local_time_second = self.local_time / 1000;
  658. let diff_time = local_time_second % (60 * 60);
  659. return diff_time > 30 && diff_time < 3570;
  660. }
  661. // 平仓订单处理命令
  662. #[instrument(skip(self, command), level="TRACE")]
  663. pub fn _post_close(&self, command: &mut OrderCommand) {
  664. debug!(?command);
  665. let mut pd_amount = Decimal::ZERO;
  666. let mut pd_order_num = 0;
  667. let mut pk_amount = Decimal::ZERO;
  668. let mut pk_order_num = 0;
  669. // 各类价格
  670. let long_upper = self.close_dist[0];
  671. let long_lower = self.close_dist[1];
  672. let short_lower = self.close_dist[2];
  673. let short_upper = self.close_dist[3];
  674. // 获取当前挂单,如果超过挂单范围则取消当前平仓单
  675. for order_client_id in self.local_orders.keys() {
  676. let order = self.local_orders.get(order_client_id).unwrap();
  677. let key = format!("Cancel{}", (*order_client_id).clone());
  678. let value = vec![order.client_id.clone(), order.order_id.clone()];
  679. if order.side == "pk".to_string() {
  680. if order.price > long_upper || order.price < long_lower {
  681. command.cancel.insert(key.clone(), value.clone());
  682. }
  683. pk_amount += order.amount;
  684. pk_order_num += 1;
  685. }
  686. if order.side == "pd".to_string() {
  687. if order.price < short_lower || order.price > short_upper {
  688. command.cancel.insert(key.clone(), value.clone());
  689. }
  690. pd_amount += order.amount;
  691. pd_order_num += 1;
  692. }
  693. }
  694. debug!(?command);
  695. // 判断是否需要全平
  696. let is_need_cancel_all_close =
  697. (pd_amount - self.pos.long_pos).abs() * self.mp > self._min_amount_value
  698. || (pk_amount - self.pos.short_pos).abs() * self.mp > self._min_amount_value;
  699. if is_need_cancel_all_close {
  700. for order_client_id in self.local_orders.keys() {
  701. let order = self.local_orders.get(order_client_id).unwrap();
  702. if order.side == "pk".to_string() || order.side == "pd".to_string() {
  703. let key = format!("Cancel{}", (*order_client_id).clone());
  704. let value = vec![order.client_id.clone(), order.order_id.clone()];
  705. command.cancel.insert(key.clone(), value.clone());
  706. }
  707. }
  708. }
  709. debug!(?command);
  710. // 区分现货和期货
  711. if self.exchange.contains("spot") {
  712. // 平多价值大于最小交易价值,执行平多逻辑
  713. if self.pos.long_pos * self.mp > self._min_amount_value {
  714. if pd_order_num == 0 {
  715. let mut price = (short_lower + short_upper) * dec!(0.5);
  716. price = utils::clip(price, self.bp * dec!(0.9995), self.ap * dec!(1.03));
  717. price = utils::fix_price(price, self.tick_size);
  718. let mut amount = self.pos.long_pos;
  719. amount = utils::fix_amount(amount, self.step_size);
  720. if amount * price > self._min_amount_value {
  721. let order_client_id = utils::generate_client_id(Some(self.broker_id.clone()));
  722. let order = vec![
  723. amount.to_string(),
  724. "pd".to_string(),
  725. price.to_string(),
  726. order_client_id.clone()
  727. ];
  728. command.limits_open.insert(order_client_id, order.clone());
  729. debug!(?command);
  730. }
  731. }
  732. }
  733. // 平空价值大于最小交易价值,执行平空逻辑
  734. if self.pos.short_pos > self._min_amount_value {
  735. if pk_order_num == 0 {
  736. let mut price = (long_upper + long_lower) * dec!(0.5);
  737. price = utils::clip(price, self.bp * dec!(0.97), self.ap * dec!(1.0005));
  738. price = utils::fix_price(price, self.tick_size);
  739. let mut amount = self.pos.short_pos;
  740. amount = utils::fix_amount(amount, self.step_size);
  741. if amount * price > self._min_amount_value {
  742. let order_client_id = utils::generate_client_id(Some(self.broker_id.clone()));
  743. let order = vec![
  744. amount.to_string(),
  745. "pk".to_string(),
  746. price.to_string(),
  747. order_client_id.clone()
  748. ];
  749. command.limits_open.insert(order_client_id, order.clone());
  750. debug!(?command);
  751. }
  752. }
  753. }
  754. } else {
  755. if self.pos.long_pos > Decimal::ZERO {
  756. let mut price = (short_lower + short_upper) * dec!(0.5);
  757. price = utils::clip(price, self.bp * dec!(0.9995), self.ap * dec!(1.03));
  758. price = utils::fix_price(price, self.tick_size);
  759. let order_client_id = utils::generate_client_id(Some(self.broker_id.clone()));
  760. let order = vec![
  761. self.pos.long_pos.to_string(),
  762. "pd".to_string(),
  763. price.to_string(),
  764. order_client_id.clone()
  765. ];
  766. command.limits_open.insert(order_client_id, order.clone());
  767. debug!(?command);
  768. }
  769. if self.pos.short_pos > Decimal::ZERO {
  770. let mut price = (long_upper + long_lower) * dec!(0.5);
  771. price = utils::clip(price, self.bp * dec!(0.97), self.ap * dec!(1.0005));
  772. price = utils::fix_price(price, self.tick_size);
  773. let order_client_id = utils::generate_client_id(Some(self.broker_id.clone()));
  774. let order = vec![
  775. self.pos.short_pos.to_string(),
  776. "pk".to_string(),
  777. price.to_string(),
  778. order_client_id.clone()
  779. ];
  780. debug!(?command);
  781. }
  782. }
  783. }
  784. // 生成取消订单的指令
  785. #[instrument(skip(self, command), level="TRACE")]
  786. pub fn _cancel_open(&self, command: &mut OrderCommand) {
  787. debug!(?command);
  788. // 挂单范围
  789. let long_upper = self.open_dist[0];
  790. let long_lower = self.open_dist[1];
  791. let short_lower = self.open_dist[2];
  792. let short_upper = self.open_dist[3];
  793. for order_client_id in self.local_orders.keys() {
  794. let order = self.local_orders.get(order_client_id).unwrap();
  795. let key = format!("Cancel{}", (*order_client_id).clone());
  796. let value = vec![order.client_id.clone(), order.order_id.clone()];
  797. // 开多订单处理
  798. if order.side == "kd".to_string() {
  799. // 在价格范围内时不处理
  800. if order.price < long_upper && order.price > long_lower {
  801. continue
  802. }
  803. debug!(?key, ?order.price, ?long_upper, ?long_lower);
  804. command.cancel.insert(key.clone(), value.clone());
  805. }
  806. // 开空订单处理
  807. if order.side == "kk".to_string() {
  808. // 在价格范围内时不处理
  809. if order.price > short_lower && order.price < short_upper {
  810. continue
  811. }
  812. debug!(?key, ?order.price, ?short_lower, ?short_upper);
  813. command.cancel.insert(key.clone(), value.clone());
  814. }
  815. }
  816. }
  817. // 超时触发查单信号
  818. #[instrument(skip(self, command), level="TRACE")]
  819. pub fn _check_local_orders(&mut self, command: &mut OrderCommand) {
  820. debug!(?command);
  821. // 超时检测
  822. if self.local_time - self._check_local_orders_time < self._check_local_orders_interval {
  823. return;
  824. }
  825. // 查单指令生成主逻辑
  826. for client_id in self.local_orders.keys() {
  827. let check_some = self.in_check.get(client_id);
  828. // 如果在查单队列中,不需要再添加
  829. if let Some(_) = check_some {
  830. continue;
  831. }
  832. let order = self.local_orders.get(client_id).unwrap();
  833. // 没有超过10s的订单,不需要检查
  834. if self.local_time - order.local_time < self._check_local_orders_interval {
  835. continue;
  836. }
  837. let key = format!("Check{}", client_id.clone());
  838. let value = vec![
  839. client_id.clone(),
  840. order.order_id.clone(),
  841. ];
  842. command.check.insert(key, value);
  843. self.in_check.insert(client_id.clone(), self.local_time);
  844. info!("查询订单:{:?}", client_id.clone());
  845. debug!(?command);
  846. }
  847. // 维护查单队列
  848. self._release_in_check();
  849. // 更新查单时间
  850. self._check_local_orders_time = self.local_time;
  851. }
  852. // 开单指令生成逻辑
  853. #[instrument(skip(self, command), level="TRACE")]
  854. pub fn _post_open(&mut self, command: &mut OrderCommand) {
  855. debug!(?command);
  856. // 开仓逻辑检测,主要是检测整点开仓逻辑
  857. if !self.check_allow_post_open() {
  858. return;
  859. }
  860. // 报单时延检测
  861. if self.local_time - self.post_open_time < self.post_open_interval {
  862. return;
  863. }
  864. // 挂单范围获取
  865. let long_upper = self.open_dist[0];
  866. let long_lower = self.open_dist[1];
  867. let short_lower = self.open_dist[2];
  868. let short_upper = self.open_dist[3];
  869. // 获取当前挂单价值
  870. let mut buy_price_list: Vec<Decimal> = vec![];
  871. let mut sell_price_list: Vec<Decimal> = vec![];
  872. let mut buy_value = Decimal::ZERO;
  873. let mut sell_value = Decimal::ZERO;
  874. for client_id in self.local_orders.keys() {
  875. let order = self.local_orders.get(client_id).unwrap();
  876. if order.side == "kd".to_string() {
  877. buy_price_list.push(order.price);
  878. buy_value += order.amount * order.price;
  879. }
  880. if order.side == "kk".to_string() {
  881. sell_price_list.push(order.price);
  882. sell_value += order.amount * order.price;
  883. }
  884. }
  885. // 计算可开价值
  886. let mut long_free_value = self.max_long_value - self.long_hold_value - buy_value;
  887. let mut short_free_value = self.max_short_value - self.short_hold_value - sell_value;
  888. debug!(?long_free_value, ?short_free_value);
  889. // 现货要特殊处理
  890. if self.exchange.contains("spot") {
  891. let coin_value = self.coin * self.mp * self.lever_rate * self.adjust_lever_rate;
  892. let cash_value = self.cash * self.lever_rate * self.adjust_lever_rate;
  893. long_free_value = min(cash_value, self.max_long_value) - buy_value;
  894. short_free_value = min(coin_value, self.max_short_value) - sell_value;
  895. }
  896. // 一手开单价值计算
  897. let one_hand_long_value = dec!(0.99) * (self.max_long_value / self.grid);
  898. let one_hand_short_value = dec!(0.99) * (self.max_short_value / self.grid);
  899. debug!(?self.post_side);
  900. // 挂多单
  901. if self.post_side >= 0 {
  902. debug!(?buy_price_list);
  903. if buy_price_list.len() == 0 {
  904. let mut target_buy_price = (long_upper + long_lower) * dec!(0.5);
  905. target_buy_price = utils::clip(target_buy_price, self.bp * dec!(0.97), self.ap * dec!(1.0005));
  906. target_buy_price = utils::fix_price(target_buy_price, self.tick_size);
  907. let value = min(one_hand_long_value, long_free_value);
  908. let amount = utils::fix_amount(value / self.mp, self.step_size);
  909. let amount_value = amount * self.mp;
  910. debug!(?one_hand_long_value, ?long_free_value, ?amount);
  911. // 下单价值不能太大,也不能太小
  912. if amount_value >= self._min_amount_value && amount_value <= long_free_value {
  913. let client_id = utils::generate_client_id(Some(self.broker_id.clone()));
  914. let order = vec![
  915. amount.to_string(),
  916. "kd".to_string(),
  917. target_buy_price.to_string(),
  918. client_id.clone(),
  919. ];
  920. debug!(?order);
  921. command.limits_open.insert(client_id.clone(), order);
  922. // 报单时间更新
  923. self.post_open_time = self.local_time;
  924. }
  925. }
  926. }
  927. // 挂空单
  928. if self.post_side <= 0 {
  929. debug!(?sell_price_list);
  930. if sell_price_list.len() == 0 {
  931. let mut target_sell_price = (short_lower + short_upper) * dec!(0.5);
  932. target_sell_price = utils::clip(target_sell_price, self.bp * dec!(0.9995), self.ap * dec!(1.03));
  933. target_sell_price = utils::fix_price(target_sell_price, self.tick_size);
  934. let value = min(one_hand_short_value, short_free_value);
  935. let amount = utils::fix_amount(value / self.mp, self.step_size);
  936. let amount_value = amount * self.mp;
  937. // 下单价值不能太大,也不能太小
  938. if amount_value >= self._min_amount_value && amount_value <= short_free_value {
  939. let client_id = utils::generate_client_id(Some(self.broker_id.clone()));
  940. let order = vec![
  941. amount.to_string(),
  942. "kk".to_string(),
  943. target_sell_price.to_string(),
  944. client_id.clone(),
  945. ];
  946. debug!(?order);
  947. command.limits_open.insert(client_id.clone(), order);
  948. // 报单时间更新
  949. self.post_open_time = self.local_time;
  950. }
  951. }
  952. }
  953. }
  954. // 定时打印
  955. pub fn on_time_print(&mut self) {
  956. if self.local_time - self._print_time < self._print_interval {
  957. return;
  958. }
  959. // 记录上次打印时间
  960. self._print_time = self.local_time;
  961. if !self._is_print {
  962. return;
  963. }
  964. // 准备好了的话就不打印预热中了
  965. if self.is_ready {
  966. return;
  967. }
  968. info!("预热中");
  969. }
  970. // 在满足条件后,返回非空command,否则返回一个空的command。原文的onTime。
  971. pub fn on_time(&mut self, trader_msg: &TraderMsg) -> OrderCommand {
  972. self.on_time_print();
  973. let mut command = OrderCommand::new();
  974. // 更新逻辑数据出错时,不进行后面的逻辑处理
  975. if !self._update_data(trader_msg) {
  976. return command;
  977. }
  978. // 检查是否准备充分
  979. if !self.check_ready() {
  980. return command;
  981. }
  982. // 刷新多空双方持仓比例
  983. self._pos_rate();
  984. // 生成开仓、平仓等相关价格
  985. self.generate_dist();
  986. // 下单指令处理逻辑
  987. self._cancel_open(&mut command); // 撤单命令处理
  988. self._post_open(&mut command); // 限价单命令处理
  989. self._post_close(&mut command); // 平仓单命令处理
  990. self._check_local_orders(&mut command); // 固定时间检查超时订单
  991. self._update_in_cancel(&mut command); // 更新撤单队列,是一个filter
  992. self._check_request_limit(&mut command); // 限制频率,移除不合规则之订单,是一个filter
  993. self._refresh_request_limit(); // 刷新频率限制
  994. self._update_request_num(&mut command); // 统计刷新频率
  995. return command;
  996. }
  997. }
  998. #[cfg(test)]
  999. mod tests {
  1000. use rust_decimal::Decimal;
  1001. use rust_decimal_macros::dec;
  1002. use tracing::{debug};
  1003. use crate::model::{OrderInfo, TraderMsg};
  1004. use crate::params::Params;
  1005. use crate::strategy::Strategy;
  1006. #[test]
  1007. fn on_time_test() {
  1008. global::log_utils::init_log_with_debug();
  1009. let params = Params::new("config.toml").unwrap();
  1010. let mut strategy = Strategy::new(&params, true);
  1011. let mut trader_msg = TraderMsg::new();
  1012. trader_msg.market.append(&mut vec![dec!(0.92), dec!(1.0), dec!(0.89), dec!(0.79), dec!(0.99), dec!(1.0), dec!(0.89), dec!(0.79), dec!(0.89), dec!(0.79), dec!(0.99), dec!(1.0), dec!(0.89), dec!(0.79)]);
  1013. trader_msg.position.long_pos = dec!(100);
  1014. trader_msg.position.long_avg = dec!(0.9);
  1015. trader_msg.orders.insert("0001".to_string(), OrderInfo{
  1016. symbol: "".to_string(),
  1017. amount: Default::default(),
  1018. side: "pd".to_string(),
  1019. price: dec!(10),
  1020. client_id: "0001".to_string(),
  1021. filled_price: Default::default(),
  1022. filled: Decimal::ZERO,
  1023. order_id: "".to_string(),
  1024. local_time: 0,
  1025. create_time: 0,
  1026. status: "".to_string(),
  1027. fee: Default::default(),
  1028. });
  1029. trader_msg.cash = dec!(1000);
  1030. trader_msg.coin = Decimal::ZERO;
  1031. strategy.is_ready = true;
  1032. strategy.equity = dec!(1000);
  1033. strategy.lever_rate = Decimal::ONE;
  1034. debug!("{:?}", strategy.on_time(&trader_msg));
  1035. }
  1036. }