版本 v1.0
可视化策略实现如下:
# 本代码由可视化策略环境自动生成 2021年12月3日 11:18
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# 交易引擎:初始化函数,只执行一次
def m6_initialize_bigquant_run(context):
context.all_data = context.options["data"].read()
# 交易引擎:每个单位时间开盘前调用一次。
def m6_before_trading_start_bigquant_run(context, data):
pass
# 交易引擎:tick数据处理函数,每个tick执行一次
def m6_handle_tick_bigquant_run(context, data):
pass
# 交易引擎:bar数据处理函数,每个时间单位执行一次
def m6_handle_data_bigquant_run(context, data):
import pandas as pd
today = data.current_dt.strftime('%Y-%m-%d') # 当前交易日期
all_data = context.all_data[context.all_data.date == today]
all_data = pd.DataFrame(all_data)
if len(all_data) == 0:#过滤没有指标的数据
return
price = all_data['close'].iloc[0]
high_line = all_data['high_line'].iloc[0]
low_line = all_data['low_line'].iloc[0]
instrument = context.future_symbol(context.instruments[0]) # 交易标的
long_position = context.get_account_position(instrument, direction=Direction.LONG).avail_qty#多头持仓
short_position = context.get_account_position(instrument, direction=Direction.SHORT).avail_qty#空头持仓
curr_position = short_position + long_position#总持仓
if short_position > 0:
if price > high_line:
context.buy_close(instrument, short_position, price, order_type=OrderType.MARKET)
context.buy_open(instrument, 4, price, order_type=OrderType.MARKET)
print(today,'先平空再开多')
elif long_position > 0:
if price < low_line:
context.sell_close(instrument, long_position, price, order_type=OrderType.MARKET)
context.sell_open(instrument, 4, price, order_type=OrderType.MARKET)
print(today,'先平多再开空',curr_position)
elif curr_position==0:
if price > high_line:
context.buy_open(instrument, 4, price, order_type=OrderType.MARKET)
print('空仓开多')
elif price<low_line:
context.sell_open(instrument, 4, price, order_type=OrderType.MARKET)
print('空仓开空')
# 交易引擎:成交回报处理函数,每个成交发生时执行一次
def m6_handle_trade_bigquant_run(context, data):
pass
# 交易引擎:委托回报处理函数,每个委托变化时执行一次
def m6_handle_order_bigquant_run(context, data):
pass
# 交易引擎:盘后处理函数,每日盘后执行一次
def m6_after_trading_bigquant_run(context, data):
pass
m1 = M.instruments.v2(
start_date='2021-02-17',
end_date='2021-11-26',
market='CN_FUTURE',
instrument_list='JM2201.DCE',
max_count=0
)
m2 = M.input_features.v1(
features="""# #号开始的表示注释,注释需单独一行
# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
ATR = ta_atr(high, low, close, timeperiod = 25)
MA25 = mean(close,25)
high_line = MA25 + 2 * ATR # 上轨
low_line = MA25 - 2 * ATR # 下轨"""
)
m5 = M.use_datasource.v1(
instruments=m1.data,
features=m2.data,
datasource_id='bar1d_CN_FUTURE',
start_date='',
end_date=''
)
m4 = M.derived_feature_extractor.v3(
input_data=m5.data,
features=m2.data,
date_col='date',
instrument_col='instrument',
drop_na=False,
remove_extra_columns=False,
user_functions={}
)
m3 = M.dropnan.v2(
input_data=m4.data
)
m6 = M.hftrade.v1(
instruments=m1.data,
options_data=m3.data,
start_date='2021-02-14',
end_date='2021-10-18',
initialize=m6_initialize_bigquant_run,
before_trading_start=m6_before_trading_start_bigquant_run,
handle_tick=m6_handle_tick_bigquant_run,
handle_data=m6_handle_data_bigquant_run,
handle_trade=m6_handle_trade_bigquant_run,
handle_order=m6_handle_order_bigquant_run,
after_trading=m6_after_trading_bigquant_run,
capital_base=1000000,
frequency='daily',
price_type='真实价格',
product_type='期货',
before_start_days='',
benchmark='000300.HIX',
plot_charts=True,
disable_cache=False,
show_debug_info=False,
backtest_only=False
)
[2021-12-03 11:08:36.228204] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-03 11:08:36.237263] INFO: moduleinvoker: 命中缓存
[2021-12-03 11:08:36.239403] INFO: moduleinvoker: instruments.v2 运行完成[0.011209s].
[2021-12-03 11:08:36.254551] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-12-03 11:08:36.265384] INFO: moduleinvoker: 命中缓存
[2021-12-03 11:08:36.267749] INFO: moduleinvoker: input_features.v1 运行完成[0.013178s].
[2021-12-03 11:08:36.274468] INFO: moduleinvoker: use_datasource.v1 开始运行..
[2021-12-03 11:08:36.281686] INFO: moduleinvoker: 命中缓存
[2021-12-03 11:08:36.284030] INFO: moduleinvoker: use_datasource.v1 运行完成[0.009548s].
[2021-12-03 11:08:36.293774] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2021-12-03 11:08:36.310180] INFO: moduleinvoker: 命中缓存
[2021-12-03 11:08:36.312211] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.018439s].
[2021-12-03 11:08:36.322519] INFO: moduleinvoker: dropnan.v2 开始运行..
[2021-12-03 11:08:36.331547] INFO: moduleinvoker: 命中缓存
[2021-12-03 11:08:36.333394] INFO: moduleinvoker: dropnan.v2 运行完成[0.01088s].
[2021-12-03 11:08:36.364753] INFO: moduleinvoker: hfbacktest.v1 开始运行..
[2021-12-03 11:08:36.381358] INFO: moduleinvoker: 命中缓存
[2021-12-03 11:08:36.977940] INFO: moduleinvoker: hfbacktest.v1 运行完成[0.613215s].
[2021-12-03 11:08:36.979408] INFO: moduleinvoker: hftrade.v1 运行完成[0.639133s].