版本 v1.0
可视化策略实现如下:
# 本代码由可视化策略环境自动生成 2021年12月6日 09:56
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# 交易引擎:初始化函数,只执行一次
def m6_initialize_bigquant_run(context):
context.all_data = context.options["data"].read()
context.ar_upr=110
context.ar_dwn=75
# 交易引擎:每个单位时间开盘前调用一次。
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):
today = data.current_dt.strftime('%Y-%m-%d') # 当前交易日期
all_data = context.all_data[context.all_data.date == today]
if len(all_data) == 0:#过滤没有指标的数据
return
ar_value = all_data['AR'].iloc[0]
ma_short = all_data['mean_5'].iloc[0]
ma_medium = all_data['mean_10'].iloc[0]
ma_long = all_data['mean_30'].iloc[0]
price = all_data['close'].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 (ar_value > context.ar_upr) and (ma_short > ma_medium > ma_long): # 开多
if short_position > 0:
print(short_position)
context.buy_close(instrument, short_position, price, order_type=OrderType.MARKET)
context.buy_open(instrument, 4, price, order_type=OrderType.MARKET)
print(today,'先平空再开多')
elif curr_position == 0:
context.buy_open(instrument, 4, price, order_type=OrderType.MARKET)
print(today,'开多')
if ar_value < context.ar_dwn and ma_short < ma_medium < ma_long :# 开空
if long_position > 0:
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:
context.sell_open(instrument, 4, price, order_type=OrderType.MARKET)
print(today,'开空')
if 100 >= ar_value >= 90 :
if long_position > 0:
context.sell_close(instrument, long_position, price, order_type=OrderType.MARKET)
print(today,'AR在90~100之间平多仓')
elif short_position > 0:
context.buy_close(instrument, short_position, price, order_type=OrderType.MARKET)
print(today,'AR在90~100之间平空仓')
# 交易引擎:成交回报处理函数,每个成交发生时执行一次
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="""# #号开始的表示注释,注释需单独一行
# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
AR = sum((high - open),26)/sum((open - low ),26)*100
mean_5 = mean(close,5)
mean_10 = mean(close,10)
mean_30 = mean(close,30)"""
)
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-02 17:47:26.435106] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-12-02 17:47:26.444525] INFO: moduleinvoker: 命中缓存
[2021-12-02 17:47:26.457856] INFO: moduleinvoker: instruments.v2 运行完成[0.022738s].
[2021-12-02 17:47:26.474542] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-12-02 17:47:26.488284] INFO: moduleinvoker: 命中缓存
[2021-12-02 17:47:26.491405] INFO: moduleinvoker: input_features.v1 运行完成[0.016895s].
[2021-12-02 17:47:26.501692] INFO: moduleinvoker: use_datasource.v1 开始运行..
[2021-12-02 17:47:26.514203] INFO: moduleinvoker: 命中缓存
[2021-12-02 17:47:26.516630] INFO: moduleinvoker: use_datasource.v1 运行完成[0.014951s].
[2021-12-02 17:47:26.556097] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2021-12-02 17:47:26.570116] INFO: moduleinvoker: 命中缓存
[2021-12-02 17:47:26.573026] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.016923s].
[2021-12-02 17:47:26.595691] INFO: moduleinvoker: dropnan.v2 开始运行..
[2021-12-02 17:47:26.607471] INFO: moduleinvoker: 命中缓存
[2021-12-02 17:47:26.609579] INFO: moduleinvoker: dropnan.v2 运行完成[0.013912s].
[2021-12-02 17:47:26.643945] INFO: moduleinvoker: hfbacktest.v1 开始运行..
[2021-12-02 17:47:26.650642] INFO: hfbacktest: biglearning V1.3.2
[2021-12-02 17:47:26.652189] INFO: hfbacktest: bigtrader v1.7.9
[2021-12-02 17:47:26.779092] INFO: moduleinvoker: cached.v2 开始运行..
[2021-12-02 17:47:26.790765] INFO: moduleinvoker: 命中缓存
[2021-12-02 17:47:26.794108] INFO: moduleinvoker: cached.v2 运行完成[0.015026s].
[2021-12-02 17:47:26.938058] INFO: moduleinvoker: cached.v2 开始运行..
[2021-12-02 17:47:26.949764] INFO: moduleinvoker: 命中缓存
[2021-12-02 17:47:26.952487] INFO: moduleinvoker: cached.v2 运行完成[0.014448s].
[2021-12-02 17:47:35.700491] INFO: hfbacktest: backtest done, raw_perf_ds:DataSource(25775bdec3ba4f5f8abdd850754b13f8T)
[2021-12-02 17:47:36.915803] INFO: moduleinvoker: hfbacktest.v1 运行完成[10.271854s].
[2021-12-02 17:47:36.920510] INFO: moduleinvoker: hftrade.v1 运行完成[10.303894s].