版本 v1.0
可视化策略实现如下:
# 本代码由可视化策略环境自动生成 2021年12月3日 19:10
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
def m1_initialize_bigquant_run(context):
import talib
all_data = context.options["data"].read()
all_data['DIF'],all_data['DEA'],all_data['macd']=talib.MACD(all_data['settle'], fastperiod=12, slowperiod=26, signalperiod=9)
context.signal_data = all_data
context.order_num = 1
context.PRINT = 1
# 交易引擎:每个单位时间开盘前调用一次。
def m1_before_trading_start_bigquant_run(context, data):
# 订阅要交易的合约的行情
context.subscribe([context.instruments[0]])
# 交易引擎:tick数据处理函数,每个tick执行一次
def m1_handle_tick_bigquant_run(context, data):
pass
def m1_handle_data_bigquant_run(context, data):
#获取当前时间
cur_date = data.current_dt.strftime('%Y-%m-%d')
all_data = context.signal_data
cur_data = all_data[all_data['date'] == cur_date]
DIF = cur_data['DIF'].values[0]
DEA = cur_data['DEA'].values[0]
context.ins = context.instruments[0]
#print('=======', cur_date, close, ma_close, context.ins)
position_long = context.get_position(context.ins, Direction.LONG)
position_short = context.get_position(context.ins, Direction.SHORT)
# 空头
if position_long.current_qty != -1 and DIF < DEA:
#平多
if position_long.current_qty == 1:
rv = context.sell_close(context.ins, context.order_num, None, order_type=OrderType.MARKET)
msg = "{} 平多 for {} 下单函数返回={}".format(str(data.current_dt), context.ins, str(rv))
context.write_log(msg, stdout=1)
# 开空
if position_short.current_qty == 0:
rv = context.sell_open(context.ins, context.order_num, None, order_type=OrderType.MARKET)
msg = "{} 开空 for {} 下单函数返回={}".format(str(data.current_dt), context.ins, str(rv))
context.write_log(msg, stdout=1)
# 多头
if (position_long.current_qty != 1) and DIF > DEA:
# 平空
if position_short.current_qty == 1 :
rv = context.buy_close(context.ins, context.order_num, None, order_type=OrderType.MARKET)
msg = "{} 平空 for {} 下单函数返回={}".format(str(data.current_dt), context.ins, str(rv))
context.write_log(msg, stdout=1)
# 开多
if position_long.current_qty == 0:
rv = context.buy_open(context.ins, context.order_num, None, order_type=OrderType.MARKET)
msg = "{} 开多 for {} 下单函数返回={}".format(str(data.current_dt), context.ins, str(rv))
context.write_log(msg, stdout=1)
# 交易引擎:成交回报处理函数,每个成交发生时执行一次
def m1_handle_trade_bigquant_run(context, data):
msg = "handle_trade data:{}".format(data.log_str())
context.write_log(msg, stdout=context.PRINT)
# 交易引擎:委托回报处理函数,每个委托变化时执行一次
def m1_handle_order_bigquant_run(context, data):
msg = "handle_order data:{}".format(data.log_str())
context.write_log(msg, stdout=context.PRINT)
# 交易引擎:盘后处理函数,每日盘后执行一次
def m1_after_trading_bigquant_run(context, data):
pass
m2 = M.instruments.v2(
start_date='2021-05-01',
end_date='2021-09-20',
market='CN_FUTURE',
instrument_list='SF8888.CZC',
max_count=0
)
m3 = M.use_datasource.v1(
instruments=m2.data,
datasource_id='bar1d_CN_FUTURE',
start_date='',
end_date='',
m_cached=False
)
m1 = M.hftrade.v1(
instruments=m2.data,
options_data=m3.data,
start_date='',
end_date='',
initialize=m1_initialize_bigquant_run,
before_trading_start=m1_before_trading_start_bigquant_run,
handle_tick=m1_handle_tick_bigquant_run,
handle_data=m1_handle_data_bigquant_run,
handle_trade=m1_handle_trade_bigquant_run,
handle_order=m1_handle_order_bigquant_run,
after_trading=m1_after_trading_bigquant_run,
capital_base=50000,
frequency='daily',
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-11-30 21:00:08.736953] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-11-30 21:00:08.749185] INFO: moduleinvoker: 命中缓存
[2021-11-30 21:00:08.753096] INFO: moduleinvoker: instruments.v2 运行完成[0.016128s].
[2021-11-30 21:00:08.775037] INFO: moduleinvoker: use_datasource.v1 开始运行..
[2021-11-30 21:00:09.217702] INFO: moduleinvoker: use_datasource.v1 运行完成[0.442722s].
[2021-11-30 21:00:09.287539] WARNING: moduleinvoker: 此模块版本 M.hftrade.v1 已不再维护。你仍然可以使用,但建议升级到最新版本:请更新到 hftrade 最新版本
[2021-11-30 21:00:09.451952] INFO: moduleinvoker: hfbacktest.v1 开始运行..
[2021-11-30 21:00:09.461579] INFO: hfbacktest: biglearning V1.3.6
[2021-11-30 21:00:09.466293] INFO: hfbacktest: bigtrader v1.7.11 2021-11-27
[2021-11-30 21:00:09.585324] INFO: moduleinvoker: cached.v2 开始运行..
[2021-11-30 21:00:09.599104] INFO: moduleinvoker: 命中缓存
[2021-11-30 21:00:09.602734] INFO: moduleinvoker: cached.v2 运行完成[0.017432s].
[2021-11-30 21:00:09.830178] INFO: moduleinvoker: cached.v2 开始运行..
[2021-11-30 21:00:09.845674] INFO: moduleinvoker: 命中缓存
[2021-11-30 21:00:09.848237] INFO: moduleinvoker: cached.v2 运行完成[0.018081s].
[2021-11-30 21:00:21.755850] INFO: hfbacktest: backtest done, raw_perf_ds:DataSource(91008b5d831042d2878a2d9433bb2865T)
[2021-11-30 21:00:23.418022] INFO: moduleinvoker: hfbacktest.v1 运行完成[13.966084s].
[2021-11-30 21:00:23.420927] INFO: moduleinvoker: hftrade.v1 运行完成[14.13028s].