通过 trade 模块中的初始化函数定义交易手续费和滑点;
通过 trade 模块中的主函数(handle函数)查看每日的买卖交易信号,按照买卖原则执行相应的买入/卖出/调仓操作。
# 本代码由可视化策略环境自动生成 2021年12月10日 15:38
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# 交易引擎:初始化函数,只执行一次
def m2_initialize_bigquant_run(context):
# 加载预测数据
# 设置手续费,买入时万3,卖出是千分之1.3,不足5元以5元计
context.set_commission(futures_commission=PerContract(cost={'RB':(0.000045, 0.000045, 0.000045)}))
context.set_margin('RB', 0.07)
context.index = 1 #日期记录
context.window = 60
# 交易引擎:每个单位时间开盘前调用一次。
def m2_before_trading_start_bigquant_run(context, data):
pass
# 交易引擎:tick数据处理函数,每个tick执行一次
def m2_handle_tick_bigquant_run(context, data):
pass
#平仓
def close_orders(context,data):
#获取当前时间
cur_date = data.current_dt.strftime('%Y-%m-%d')
for ins in context.instruments:
# 分别获取多头持仓和空头持仓
position_long = context.get_position(ins, Direction.LONG)
position_short = context.get_position(ins, Direction.SHORT)
price = data.current(ins,"close")
if(position_long.current_qty != 0):
context.sell_close(ins, position_long.avail_qty, price, order_type=OrderType.MARKET)
if(position_short.current_qty != 0):
context.buy_close(ins, position_short.avail_qty, price, order_type=OrderType.MARKET)
def order_target(context,data,instrument,lots,price,order_type_ = OrderType.MARKET):
#获取多空持仓情况
position_long = context.get_position(instrument, Direction.LONG)
position_short = context.get_position(instrument, Direction.SHORT)
# ------------------------------------------------------
if lots > 0:
if position_long.avail_qty > 0:
if position_long.avail_qty > lots:
context.sell_close(instrument, position_long.avail_qty - lots, price, order_type=order_type_)
elif position_long.avail_qty < lots:
context.buy_open(instrument, lots - position_long.avail_qty, price, order_type=order_type_)
elif position_short.avail_qty > 0:
context.buy_close(instrument, position_short.avail_qty, price, order_type=order_type_)
context.buy_open(instrument, lots, price, order_type=order_type_)
elif (position_long.avail_qty + position_short.avail_qty) == 0:
context.buy_open(instrument, lots, price, order_type=order_type_)
elif lots < 0:
if position_long.avail_qty > 0:
context.sell_close(instrument, position_long.avail_qty, price, order_type=order_type_)
context.sell_open(instrument, abs(lots), price, order_type=order_type_)
elif position_short.avail_qty > 0:
if position_short.avail_qty > abs(lots):
context.buy_close(instrument, position_short.avail_qty - abs(lots) , price, order_type=order_type_)
elif position_short.avail_qty < abs(lots):
context.sell_open(instrument, abs(lots) - position_short.avail_qty, price, order_type=order_type_)
elif (position_long.avail_qty + position_short.avail_qty) == 0:
context.sell_open(instrument, abs(lots), price, order_type=order_type_)
elif lots == 0:
if(position_long.current_qty != 0):
context.sell_close(instrument, position_long.avail_qty, price, order_type=order_type_)
if(position_short.current_qty != 0):
context.buy_close(instrument, position_short.avail_qty, price, order_type=order_type_)
# 交易引擎:bar数据处理函数,每个时间单位执行一次
def m2_handle_data_bigquant_run(context, data):
import talib as ta
today = data.current_dt#.strftime('%Y-%m-%d-%H-%M')
if context.index < context.window: # 在形成均线后才开始交易
context.index+=1
return
if today.hour > 15:
return
instrument = context.future_symbol(context.instruments[0]) # 交易标的
curr_position = context.portfolio.positions[instrument].amount # 持仓数量
prices = data.history(instrument, 'close', context.window, '1m') # 历史价格
if np.any(np.isnan(prices)):
return
n=40
kama = ta.KAMA(prices, timeperiod=n)
ft = 0.8*np.std((kama-kama.shift(1))[-n:])
if kama[-1] - np.min(kama[-n:]) > ft:
order_target(context,data,instrument = context.instruments[0] ,lots = 2,price = prices[-1],order_type_ = OrderType.MARKET)
if np.max(kama[-n:]) - kama[-1] > ft:
order_target(context,data,instrument = context.instruments[0],lots = -2,price = prices[-1],order_type_ = OrderType.MARKET)
# 交易引擎:成交回报处理函数,每个成交发生时执行一次
def m2_handle_trade_bigquant_run(context, data):
pass
# 交易引擎:委托回报处理函数,每个委托变化时执行一次
def m2_handle_order_bigquant_run(context, data):
pass
# 交易引擎:盘后处理函数,每日盘后执行一次
def m2_after_trading_bigquant_run(context, data):
pass
m1 = M.instruments.v2(
start_date='2018-04-20',
end_date='2018-05-30',
market='CN_FUTURE',
instrument_list='RB1901.SHF',
max_count=0
)
m2 = M.hftrade.v2(
instruments=m1.data,
start_date='',
end_date='',
initialize=m2_initialize_bigquant_run,
before_trading_start=m2_before_trading_start_bigquant_run,
handle_tick=m2_handle_tick_bigquant_run,
handle_data=m2_handle_data_bigquant_run,
handle_trade=m2_handle_trade_bigquant_run,
handle_order=m2_handle_order_bigquant_run,
after_trading=m2_after_trading_bigquant_run,
capital_base=1000000,
frequency='minute',
price_type='真实价格',
product_type='期货',
before_start_days='0',
benchmark='000300.HIX',
plot_charts=True,
disable_cache=False,
show_debug_info=False,
backtest_only=False
)
[2021-12-10 15:36:34.735893] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-10 15:36:34.747149] INFO: moduleinvoker: 命中缓存
[2021-12-10 15:36:34.748851] INFO: moduleinvoker: instruments.v2 运行完成[0.012971s].
[2021-12-10 15:36:34.777238] INFO: moduleinvoker: hfbacktest.v1 开始运行..
[2021-12-10 15:36:34.798234] INFO: moduleinvoker: 命中缓存
[2021-12-10 15:36:35.841055] INFO: moduleinvoker: hfbacktest.v1 运行完成[1.063833s].
[2021-12-10 15:36:35.843640] INFO: moduleinvoker: hftrade.v2 运行完成[1.087883s].