通过证券代码列表输入回测的起止日期
通过 trade 模块中的初始化函数定义交易手续费和滑点;
通过 trade 模块中的主函数(handle函数)查看每日的买卖交易信号,按照买卖原则执行相应的买入/卖出/调仓操作。
# 本代码由可视化策略环境自动生成 2021年12月10日 15:17
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
def m8_run_bigquant_run(input_1, input_2, input_3):
# 示例代码如下。在这里编写您的代码
df_C2201 = input_1.read()
df_C2205 = input_2.read()
df_C2201=df_C2201.set_index(df_C2201['date'])
df_C2205=df_C2205.set_index(df_C2205['date'])
df_sp = df_C2201['close'] - df_C2205['close']
a=df_C2201[['close','open']]
a.rename(columns={'close':'C2201_close'}, inplace = True)
b=df_C2205[['close','open']]
b.rename(columns={'close':'C2205_close'}, inplace = True)
data=pd.concat([a,b],axis=1)
data = data[['C2201_close','C2205_close']]
data['p'] = df_sp
data['mean_20'] = data['p'].rolling(20).mean()
data.dropna(inplace=True)
data['std'] = np.std(data['p'])
data['sig'] = (data['p'] - data['mean_20'])/data['std']
data['up_line'] = data['mean_20'] + data['std']
data['low_line'] = data['mean_20'] - data['std']
data['date'] = data.index
data_1 = DataSource.write_df(data)
return Outputs(data_1=data_1, data_2=None, data_3=None)
# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
def m8_post_run_bigquant_run(outputs):
return outputs
# 交易引擎:初始化函数,只执行一次
def m3_initialize_bigquant_run(context):
# 加载预测数据
context.all_data = context.options["data"].read()
context.order_num = 1
context.set_slippage_value(volume_limit=1)
# 交易引擎:每个单位时间开盘前调用一次。
def m3_before_trading_start_bigquant_run(context, data):
#订阅合约
context.subscribe([context.instruments[0],context.instruments[1]])
context.set_slippage_value(volume_limit=1)
# 交易引擎:tick数据处理函数,每个tick执行一次
def m3_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)
# 交易引擎:bar数据处理函数,每个时间单位执行一次
def m3_handle_data_bigquant_run(context, data):
today = data.current_dt.strftime('%Y-%m-%d') # 当前交易日期
all_data = context.all_data[context.all_data.date == today]
if len(all_data) ==0:#过滤没有指标的数据
return
up_line = all_data['up_line'].iloc[0]
low_line = all_data['low_line'].iloc[0]
sig = all_data['sig'].iloc[0]
sp = all_data['p'].iloc[0]
instrument_2201 = context.future_symbol(context.instruments[0]) # 交易标的
instrument_2205 = context.future_symbol(context.instruments[1]) # 交易标的
#正向开仓
if sig >= 1 and sp > up_line:
#先平掉所有持仓
close_orders(context,data)
#做多
price = data.current(context.instruments[0],"close")
context.buy_open(context.instruments[0], context.order_num, price, order_type=OrderType.MARKET)
#做空
price = data.current(context.instruments[1],"close")
context.sell_open(context.instruments[1], context.order_num, price, order_type=OrderType.MARKET)
#反向开仓
elif sig <= -1 and sp < low_line:
#先平掉所有持仓
close_orders(context,data)
#做多
price = data.current(context.instruments[1],"close")
context.buy_open(context.instruments[1], context.order_num, price, order_type=OrderType.MARKET)
#做空
price = data.current(context.instruments[0],"close")
context.sell_open(context.instruments[0], context.order_num, price, order_type=OrderType.MARKET)
# 交易引擎:成交回报处理函数,每个成交发生时执行一次
def m3_handle_trade_bigquant_run(context, data):
pass
# 交易引擎:委托回报处理函数,每个委托变化时执行一次
def m3_handle_order_bigquant_run(context, data):
pass
# 交易引擎:盘后处理函数,每日盘后执行一次
def m3_after_trading_bigquant_run(context, data):
pass
m1 = M.instruments.v2(
start_date='2021-06-01',
end_date='2021-12-1',
market='CN_FUTURE',
instrument_list='C2205.DCE',
max_count=0
)
m2 = M.use_datasource.v1(
instruments=m1.data,
datasource_id='bar1d_CN_FUTURE',
start_date='',
end_date=''
)
m6 = M.instruments.v2(
start_date='2021-06-01',
end_date='2021-12-1',
market='CN_FUTURE',
instrument_list="""C2201.DCE
""",
max_count=0
)
m7 = M.use_datasource.v1(
instruments=m6.data,
datasource_id='bar1d_CN_FUTURE',
start_date='',
end_date=''
)
m8 = M.cached.v3(
input_1=m7.data,
input_2=m2.data,
run=m8_run_bigquant_run,
post_run=m8_post_run_bigquant_run,
input_ports='',
params='{}',
output_ports=''
)
m5 = M.instruments.v2(
start_date='2021-06-01',
end_date='2021-12-1',
market='CN_FUTURE',
instrument_list="""C2201.DCE
C2205.DCE
""",
max_count=0
)
m3 = M.hftrade.v2(
instruments=m5.data,
options_data=m8.data_1,
start_date='2021-06-01',
end_date='2021-12-1',
initialize=m3_initialize_bigquant_run,
before_trading_start=m3_before_trading_start_bigquant_run,
handle_tick=m3_handle_tick_bigquant_run,
handle_data=m3_handle_data_bigquant_run,
handle_trade=m3_handle_trade_bigquant_run,
handle_order=m3_handle_order_bigquant_run,
after_trading=m3_after_trading_bigquant_run,
capital_base=8000,
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-12-10 15:08:57.982032] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-10 15:08:58.030849] INFO: moduleinvoker: instruments.v2 运行完成[0.048833s].
[2021-12-10 15:08:58.048469] INFO: moduleinvoker: use_datasource.v1 开始运行..
[2021-12-10 15:08:58.201424] INFO: moduleinvoker: use_datasource.v1 运行完成[0.152975s].
[2021-12-10 15:08:58.210867] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-10 15:08:58.594040] INFO: moduleinvoker: instruments.v2 运行完成[0.383171s].
[2021-12-10 15:08:58.600622] INFO: moduleinvoker: use_datasource.v1 开始运行..
[2021-12-10 15:08:58.857347] INFO: moduleinvoker: use_datasource.v1 运行完成[0.25675s].
[2021-12-10 15:08:58.877051] INFO: moduleinvoker: cached.v3 开始运行..
[2021-12-10 15:08:59.122008] INFO: moduleinvoker: cached.v3 运行完成[0.244966s].
[2021-12-10 15:08:59.127548] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-10 15:08:59.284549] INFO: moduleinvoker: instruments.v2 运行完成[0.156987s].
[2021-12-10 15:08:59.355535] INFO: moduleinvoker: hfbacktest.v1 开始运行..
[2021-12-10 15:08:59.360765] INFO: hfbacktest: biglearning V1.3.8
[2021-12-10 15:08:59.362662] INFO: hfbacktest: bigtrader v1.8.0 2021-12-06
[2021-12-10 15:08:59.416230] INFO: moduleinvoker: cached.v2 开始运行..
[2021-12-10 15:08:59.715305] INFO: moduleinvoker: cached.v2 运行完成[0.29909s].
[2021-12-10 15:08:59.779059] INFO: moduleinvoker: cached.v2 开始运行..
[2021-12-10 15:09:00.076465] INFO: moduleinvoker: cached.v2 运行完成[0.297419s].
[2021-12-10 15:09:05.696678] INFO: hfbacktest: backtest done, raw_perf_ds:DataSource(a17dbf5f9af747cfaf3e2995b6d9cb81T)
[2021-12-10 15:09:06.413317] INFO: moduleinvoker: hfbacktest.v1 运行完成[7.057795s].
[2021-12-10 15:09:06.416849] INFO: moduleinvoker: hftrade.v2 运行完成[7.111281s].