520量化编程

标题: 委托单超时撤单 [打印本页]

作者: 520量化编程    时间: 2024-7-19 21:50
标题: 委托单超时撤单
  1. #encoding:gbk

  2. import pandas as pd
  3. import numpy as np
  4. import datetime
  5. import talib

  6. def init(ContextInfo):
  7.         ContextInfo.accID='80041016'
  8.         ContextInfo.set_account(ContextInfo.accID)
  9.         ContextInfo.run_time('cancel_order_timer','10nSecond','2023-01-01 00:00:00')
  10.       
  11. def handlebar(ContextInfo):
  12.         pass

  13. def cancel_order_timer(ContextInfo):
  14.         #获取委托对象列表
  15.         orders=get_trade_detail_data(ContextInfo.accID,'STOCK','ORDER')
  16.         #遍历委托对象列表
  17.         for order in orders:
  18.                 #判断委托对象是否可以撤销
  19.                 if can_cancel_order(order.m_strOrderSysID,ContextInfo.accID,'STOCK'):
  20.                         ordercode=order.m_strInstrumentID+'.'+order.m_strExchangeID #获取委托单的证券代码
  21.                         dt=order.m_strInsertDate+' '+order.m_strInsertTime #委托日期+委托时间
  22.                         dt=datetime.datetime.strptime(dt,'%Y%m%d %H%M%S') #委托日期时间字符串解析成日期时间对象
  23.                         now=datetime.datetime.now() #获取当前日期时间
  24.                         second=(now-dt).seconds #当前日期时间-委托日期时间,获取秒数时间差
  25.                         if second>60: #如果超过60秒
  26.                                 #买入委托超时,撤单并重新下单,委托价格需要修改
  27.                                 cancel(order.m_strOrderSysID,ContextInfo.accID,'STOCK',ContextInfo)
  28.                                 print('合同编号',order.m_strOrderSysID,'股票代码',ordercode,order.m_strInstrumentName,order.m_strOptName,'超时60秒撤单')
  29.                                 if order.m_strOptName=='限价买入': #如果是限价买入的委托撤单,则重新委托买入
  30.                                         passorder(23,1101,ContextInfo.accID,ordercode,11,order.m_dLimitPrice,order.m_nVolumeTotal,'定时撤单策略',1,'委托买入',ContextInfo)
  31.                                         print('重新委托买入')
  32.                                 if order.m_strOptName=='限价卖出': #如果是限价卖出的委托撤单,则重新委托卖出
  33.                                         passorder(24,1101,ContextInfo.accID,ordercode,11,order.m_dLimitPrice,order.m_nVolumeTotal,'定时撤单策略',1,'委托卖出',ContextInfo)
  34.                                         print('重新委托卖出')

复制代码






欢迎光临 520量化编程 (https://520lhjy.com/) Powered by Discuz! X3.4