# 本代码由可视化策略环境自动生成 2022年10月25日 15:47
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# 回测引擎:初始化函数,只执行一次
def m3_initialize_bigquant_run(context):
# 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
# 回测引擎:每日数据处理函数,每天执行一次
def m3_handle_data_bigquant_run(context, data):
# 获取今日的日期
today = data.current_dt.strftime('%Y-%m-%d')
# 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
stock_hold_now = {e.symbol: p.amount * p.last_sale_price
for e, p in context.perf_tracker.position_tracker.positions.items()}
# 记录用于买入股票的可用现金,因为是早盘卖股票,需要记录卖出的股票市值并在买入下单前更新可用现金;
# 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用
cash_for_buy = context.portfolio.cash
try:
buy_stock = context.daily_stock_buy[today] # 当日符合买入条件的股票
except:
buy_stock=[] # 如果没有符合条件的股票,就设置为空
try:
sell_stock = context.daily_stock_sell[today] # 当日符合卖出条件的股票
except:
sell_stock=[] # 如果没有符合条件的股票,就设置为空
# 需要卖出的股票:已有持仓中符合卖出条件的股票
stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]
# 需要买入的股票:没有持仓且符合买入条件的股票
stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now ]
# 需要调仓的股票:已有持仓且不符合卖出条件的股票
stock_to_adjust=[ i for i in stock_hold_now if i not in sell_stock ]
# 如果有卖出信号
if len(stock_to_sell)>0:
for instrument in stock_to_sell:
sid = context.symbol(instrument) # 将标的转化为equity格式
cur_position = context.portfolio.positions[sid].amount # 持仓
if cur_position > 0 and data.can_trade(sid):
context.order_target_percent(sid, 0) # 全部卖出
# 因为设置的是早盘卖出早盘买入,需要根据卖出的股票更新可用现金;如果设置尾盘卖出早盘买入,则不需更新可用现金(可以删除下面的语句)
cash_for_buy += stock_hold_now[instrument]
# 如果有买入信号/有持仓
if len(stock_to_buy)+len(stock_to_adjust)>0:
weight = 1/(len(stock_to_buy)+len(stock_to_adjust)) # 每只股票的比重为等资金比例持有
for instrument in stock_to_buy+stock_to_adjust:
sid = context.symbol(instrument) # 将标的转化为equity格式
if data.can_trade(sid):
context.order_target_value(sid, weight*cash_for_buy) # 买入
# 回测引擎:准备数据,只执行一次
def m3_prepare_bigquant_run(context):
# 加载预测数据
df = context.options['data'].read_df()
# print('df',df)
# 函数:求满足开仓条件的股票列表
def open_pos_con(df):
return list(df[df['buy_condition']>0].instrument)
# 函数:求满足平仓条件的股票列表
def close_pos_con(df):
return list(df[df['sell_condition']>0].instrument)
# 每日买入股票的数据框
context.daily_stock_buy= df.groupby('date').apply(open_pos_con)
# 每日卖出股票的数据框
context.daily_stock_sell= df.groupby('date').apply(close_pos_con)
m1 = M.input_features.v1(
features="""# #号开始的表示注释
# 多个特征,每行一个,可以包含基础特征和衍生特征
buy_condition=where(mean(close_0,5)>mean(close_0,10),1,0)
sell_condition=where(mean(close_0,5)<mean(close_0,10),1,0)""",
m_cached=False
)
# m2 = M.instruments.v2(
# start_date=T.live_run_param('trading_date', '2022-09-27'),
# end_date=T.live_run_param('trading_date', '2022-11-01'),
# market='CN_STOCK_A',
# instrument_list="""600519.SHA
# 600333.SHA""",
# max_count=0
# )
m2 = M.instruments.v2(
start_date=T.live_run_param('trading_date', '2022-09-01'),
end_date=T.live_run_param('trading_date', '2022-11-01'),
market='CN_STOCK_A',
instrument_list=
['000001.SZA',
'000002.SZA',
'000063.SZA',
'000069.SZA',
'000100.SZA',
'000157.SZA',
'000166.SZA',
'000301.SZA',
'000333.SZA',
'000338.SZA',
'000425.SZA',
'000538.SZA',
'000568.SZA',
'000596.SZA',
'000617.SZA',
'000625.SZA',
'000651.SZA',
'000661.SZA',
'000708.SZA',
'000725.SZA',
'000768.SZA',
'000776.SZA',
'000786.SZA',
'000792.SZA',
'000858.SZA',
'000876.SZA',
'000877.SZA',
'000895.SZA',
'000938.SZA',
'000963.SZA',
'000977.SZA',
'001979.SZA',
'002001.SZA',
'002007.SZA',
'002008.SZA',
'002027.SZA',
'002032.SZA',
'002049.SZA',
'002050.SZA',
'002129.SZA',
'002142.SZA',
'002179.SZA',
'002202.SZA',
'002230.SZA',
'002236.SZA',
'002241.SZA',
'002271.SZA',
'002304.SZA',
'002311.SZA',
'002352.SZA',
'002371.SZA',
'002410.SZA',
'002414.SZA',
'002415.SZA',
'002459.SZA',
'002460.SZA',
'002466.SZA',
'002475.SZA',
'002493.SZA',
'002555.SZA',
'002594.SZA',
'002601.SZA',
'002602.SZA',
'002709.SZA',
'002714.SZA',
'002736.SZA',
'002812.SZA',
'002821.SZA',
'002841.SZA',
'002916.SZA',
'002938.SZA',
'003816.SZA',
'300014.SZA',
'300015.SZA',
'300033.SZA',
'300059.SZA',
'300122.SZA',
'300124.SZA',
'300142.SZA',
'300274.SZA',
'300316.SZA',
'300347.SZA',
'300408.SZA',
'300413.SZA',
'300433.SZA',
'300450.SZA',
'300454.SZA',
'300498.SZA',
'300601.SZA',
'300628.SZA',
'300661.SZA',
'300750.SZA',
'300759.SZA',
'300760.SZA',
'300782.SZA',
'300896.SZA',
'300919.SZA',
'300957.SZA',
'300979.SZA',
'300999.SZA'],
max_count=0
)
#注意模拟盘需要与实盘绑定+'trading_date'
m7 = M.general_feature_extractor.v7(
instruments=m2.data,
features=m1.data,
start_date='',
end_date='',
before_start_days=60
)
m8 = M.derived_feature_extractor.v3(
input_data=m7.data,
features=m1.data,
date_col='date',
instrument_col='instrument',
drop_na=False,
remove_extra_columns=False
)
m4 = M.dropnan.v2(
input_data=m8.data
)
m3 = M.trade.v4(
instruments=m2.data,
options_data=m4.data,
start_date='',
end_date='',
initialize=m3_initialize_bigquant_run,
handle_data=m3_handle_data_bigquant_run,
prepare=m3_prepare_bigquant_run,
volume_limit=0,
order_price_field_buy='open',
order_price_field_sell='open',
capital_base=1000000,
auto_cancel_non_tradable_orders=True,
data_frequency='daily',
price_type='前复权',
product_type='股票',
plot_charts=True,
backtest_only=False,
benchmark='000300.HIX'
)
# df = context.options['data'].read_df()
# df
# help(context.perf_tracker)
[2023-02-17 14:42:12.731748] INFO: moduleinvoker: input_features.v1 开始运行..
[2023-02-17 14:42:12.756922] INFO: moduleinvoker: input_features.v1 运行完成[0.025183s].
[2023-02-17 14:42:12.764589] INFO: moduleinvoker: instruments.v2 开始运行..
[2023-02-17 14:42:12.818777] INFO: moduleinvoker: instruments.v2 运行完成[0.054191s].
[2023-02-17 14:42:12.857829] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2023-02-17 14:42:13.056815] INFO: 基础特征抽取: 年份 2022, 特征行数=8088
[2023-02-17 14:42:13.088099] INFO: 基础特征抽取: 总行数: 8088
[2023-02-17 14:42:13.092922] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.235093s].
[2023-02-17 14:42:13.101247] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2023-02-17 14:42:13.183208] INFO: derived_feature_extractor: 提取完成 buy_condition=where(mean(close_0,5)>mean(close_0,10),1,0), 0.036s
[2023-02-17 14:42:13.222911] INFO: derived_feature_extractor: 提取完成 sell_condition=where(mean(close_0,5)[2023-02-17 14:42:13.275289] INFO: derived_feature_extractor: /y_2022, 8088[2023-02-17 14:42:13.324494] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.223228s].[2023-02-17 14:42:13.338225] INFO: moduleinvoker: dropnan.v2 开始运行..[2023-02-17 14:42:13.419759] INFO: dropnan: /y_2022, 8088/8088[2023-02-17 14:42:13.451054] INFO: dropnan: 行数: 8088/8088[2023-02-17 14:42:13.456348] INFO: moduleinvoker: dropnan.v2 运行完成[0.118132s].[2023-02-17 14:42:13.511582] INFO: moduleinvoker: backtest.v8 开始运行..[2023-02-17 14:42:13.525559] INFO: backtest: biglearning backtest:V8.6.3[2023-02-17 14:42:13.733166] INFO: backtest: product_type:stock by specified[2023-02-17 14:42:13.848842] INFO: moduleinvoker: cached.v2 开始运行..[2023-02-17 14:42:14.841145] INFO: backtest: 读取股票行情完成:169585[2023-02-17 14:42:15.063745] INFO: moduleinvoker: cached.v2 运行完成[1.214906s].[2023-02-17 14:42:16.023125] INFO: backtest: algo history_data=DataSource(efa6f1becc5b464bb1d31dcb2e62c106T)[2023-02-17 14:42:16.025499] INFO: algo: TradingAlgorithm V1.8.9[2023-02-17 14:42:16.106128] INFO: algo: trading transform...[2023-02-17 14:42:18.909707] INFO: Performance: Simulated 38 trading days out of 38.[2023-02-17 14:42:18.911021] INFO: Performance: first open: 2022-09-01 09:30:00+00:00[2023-02-17 14:42:18.920316] INFO: Performance: last close: 2022-11-01 15:00:00+00:00[2023-02-17 14:42:21.503334] INFO: moduleinvoker: backtest.v8 运行完成[7.991745s].[2023-02-17 14:42:21.504913] INFO: moduleinvoker: trade.v4 运行完成[8.040853s].
m3.get_attr
# 本代码由可视化策略环境自动生成 2022年10月25日 15:52
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
pd.set_option('display.max_rows',None)
# 回测引擎:初始化函数,只执行一次
def m5_initialize_bigquant_run(context):
# 加载股票指标数据,数据继承自m6模块
context.indicator_data = context.options['data'].read_df()
# 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
# 设置股票数量
context.stock_num = 5
# 调仓天数,22个交易日大概就是一个月。可以理解为一个月换仓一次
# context.rebalance_days = 22
context.rebalance_days = 2
# 如果策略运行中,需要将数据进行保存,可以借用extension这个对象,类型为dict
# 比如当前运行的k线的索引,比如个股持仓天数、买入均价
# print('context.extension',context.extension)
if 'index' not in context.extension:
context.extension['index'] = 0
# print('context.extension',context.extension)
# 回测引擎:每日数据处理函数,每天执行一次
def m5_handle_data_bigquant_run(context, data):
context.extension['index'] += 1
# 不在换仓日就return,相当于后面的代码只会一个月运行一次,买入的股票会持有1个月
# if context.extension['index'] % context.rebalance_days != 0:
# if context.extension['index'] % context.rebalance_days != 0:
# return
# 当前的日期
date = data.current_dt.strftime('%Y-%m-%d')
print('date',date)
cur_data = context.indicator_data[context.indicator_data['date'] == date]
# print('cur_data',cur_data)
# print('indicator_dat',context.indicator_dat)
# 根据日期获取调仓需要买入的股票的列表
stock_to_buy = list(cur_data.instrument[:context.stock_num])
# print('stock_to_buy',stock_to_buy)
# 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
# print('stock_hold_now',stock_hold_now)
# 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有
no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]
# 需要卖出的股票
stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
# 卖出
for stock in stock_to_sell:
# 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
# 如果返回真值,则可以正常下单,否则会出错
# 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
if data.can_trade(context.symbol(stock)):
# order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,
# 即卖出全部股票,可参考回测文档
context.order_target_percent(context.symbol(stock), 0)
# 如果当天没有买入的股票,就返回
if len(stock_to_buy) == 0:
return
# 等权重买入
weight = 1 / len(stock_to_buy)
# print('weight',weight)
# 买入
for stock in stock_to_buy:
if data.can_trade(context.symbol(stock)):
# 下单使得某只股票的持仓权重达到weight,因为
# weight大于0,因此是等权重买入
context.order_target_percent(context.symbol(stock), weight)
# 回测引擎:准备数据,只执行一次
def m5_prepare_bigquant_run(context):
pass
# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
def m5_before_trading_start_bigquant_run(context, data):
pass
m1 = M.instruments.v2(
start_date=T.live_run_param('trading_date', '2022-09-01'),
end_date=T.live_run_param('trading_date', '2022-11-01'),
market='CN_STOCK_A',
instrument_list='',
max_count=0
)
m2 = M.input_features.v1(
features="""market_cap_float_0
amount_0"""
)
m3 = M.general_feature_extractor.v7(
instruments=m1.data,
features=m2.data,
start_date='',
end_date='',
before_start_days=1
)
m4 = M.sort.v4(
input_ds=m3.data,
sort_by='market_cap_float_0',
group_by='instrument',
keep_columns='--',
ascending=True
)
m6 = M.filter.v3(
input_data=m4.sorted_data,
expr='amount_0 > 10000',
output_left_data=False
)
m5 = M.trade.v4(
instruments=m1.data,
options_data=m6.data,
start_date='',
end_date='',
initialize=m5_initialize_bigquant_run,
handle_data=m5_handle_data_bigquant_run,
prepare=m5_prepare_bigquant_run,
before_trading_start=m5_before_trading_start_bigquant_run,
volume_limit=0.025,
order_price_field_buy='open',
order_price_field_sell='open',
capital_base=1000000,
auto_cancel_non_tradable_orders=True,
data_frequency='daily',
price_type='前复权',
product_type='股票',
plot_charts=True,
backtest_only=False,
benchmark=''
)
[2023-02-17 14:43:31.007234] INFO: moduleinvoker: instruments.v2 开始运行..
[2023-02-17 14:43:31.142331] INFO: moduleinvoker: instruments.v2 运行完成[0.137094s].
[2023-02-17 14:43:31.147156] INFO: moduleinvoker: input_features.v1 开始运行..
[2023-02-17 14:43:31.154643] INFO: moduleinvoker: 命中缓存
[2023-02-17 14:43:31.155861] INFO: moduleinvoker: input_features.v1 运行完成[0.008712s].
[2023-02-17 14:43:31.176385] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2023-02-17 14:43:31.542079] INFO: 基础特征抽取: 年份 2022, 特征行数=192261
[2023-02-17 14:43:31.571631] INFO: 基础特征抽取: 总行数: 192261
[2023-02-17 14:43:31.576463] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.400082s].
[2023-02-17 14:43:31.581412] INFO: moduleinvoker: sort.v4 开始运行..
[2023-02-17 14:43:32.018971] INFO: moduleinvoker: sort.v4 运行完成[0.437542s].
[2023-02-17 14:43:32.027799] INFO: moduleinvoker: filter.v3 开始运行..
[2023-02-17 14:43:32.040167] INFO: filter: 使用表达式 amount_0 > 10000 过滤
[2023-02-17 14:43:32.337840] INFO: filter: 过滤 /data, 192256/0/192261
[2023-02-17 14:43:32.363521] INFO: moduleinvoker: filter.v3 运行完成[0.33571s].
[2023-02-17 14:43:32.409908] INFO: moduleinvoker: backtest.v8 开始运行..
[2023-02-17 14:43:32.415514] INFO: backtest: biglearning backtest:V8.6.3
[2023-02-17 14:43:32.416711] INFO: backtest: product_type:stock by specified
[2023-02-17 14:43:32.472085] INFO: moduleinvoker: cached.v2 开始运行..
[2023-02-17 14:43:36.384377] INFO: backtest: 读取股票行情完成:1457214
[2023-02-17 14:43:37.406285] INFO: moduleinvoker: cached.v2 运行完成[4.934212s].
[2023-02-17 14:43:42.174411] INFO: backtest: algo history_data=DataSource(927f112b30df4a1296b3a2af2e54dac2T)
[2023-02-17 14:43:42.175830] INFO: algo: TradingAlgorithm V1.8.9
[2023-02-17 14:43:42.630902] INFO: algo: trading transform...
[2023-02-17 14:43:43.624486] INFO: Performance: Simulated 38 trading days out of 38.
[2023-02-17 14:43:43.626330] INFO: Performance: first open: 2022-09-01 09:30:00+00:00
[2023-02-17 14:43:43.627697] INFO: Performance: last close: 2022-11-01 15:00:00+00:00
[2023-02-17 14:43:45.955485] INFO: moduleinvoker: backtest.v8 运行完成[13.545568s].
[2023-02-17 14:43:45.956964] INFO: moduleinvoker: trade.v4 运行完成[13.587656s].
m5.get_attr
# # 策略组合
# m = M.multi_strategy_analysis.v3(
# raw_perfs=[DataSource('b7615d75bae54e2d82eda37cb549896aT'), DataSource('ab8c541c8cd94e1aa26905de007b161eT') ],
# weights=[1/3, 2/3]
# )
# # M.multi_strategy_analysis.v3
M.multi_strategy_analysis.v3(
raw_perf_1=DataSource('f2a32208dbc04dfb98e0d4c849652e92T'),
raw_perf_2=DataSource('8df6f1b69fa3498c940dc318e1dd91b1T'),
raw_perfs_list='',
weights_list='0.5,0.5'
)
[2023-02-17 14:44:09.598842] ERROR: moduleinvoker: module name: multi_strategy_analysis, module version: v3, trackeback: KeyError: "None of [Index(['returns', 'benchmark_period_return'], dtype='object')] are in the [columns]"