[Python] 复制代码 # 本Python代码主要用于策略交易
# 可以自己import我们平台支持的第三方python模块,比如pandas、numpy等。
from PythonApi import *
import datetime
import sys
def init(context):
pass
def handle_bar(context):
order_list = get_orders("all", 0)
current_time = datetime.datetime.now()
if order_list:
for order in order_list:
seconds_difference = (current_time - order.datetime ).total_seconds()
#5秒未成交,追撤单
if seconds_difference>=10:
Chase_withdraw(context,order)
def order_status(context, order):
pass
def Chase_withdraw(context,order):
vol = order.unfilled_quantity
order_id = order.order_id
book_id = order.order_book_id
side = order.side # 订单方向 "buy"买:"sell"卖
position_effect = order.position_effect # 开平标志 "open"开仓 "close"平仓
#撤单语句
cancel_order(order_id) #拼接函数名称
function_name = side+"_"+position_effect
# 从全局环境中直接获取到 具体的函数,这样就可以避免写四条不同的追单语句
function_object = getattr(sys.modules[__name__], function_name)
try:
function_object(book_id,"Market",volume = vol)
except Exception as ex:
raise
|