请老师们看看,回测正常,但是模拟、实盘不交易
由bqem2dtb创建,最终由small_q 被浏览 57 用户
本代码由可视化策略环境自动生成 2024年1月17日 01:02
本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
交易引擎:初始化函数,只执行一次
def m6_initialize_bigquant_run(context): # 加载预测数据 context.ranker_prediction = context.options['data'].read_df() context.ranker_prediction.set_index('date',inplace=True)
交易引擎:每个单位时间开盘前调用一次。
def m6_before_trading_start_bigquant_run(context, data): # 盘前处理,订阅行情等 pass
交易引擎:tick数据处理函数,每个tick执行一次
def m6_handle_tick_bigquant_run(context, tick): pass
def m6_handle_data_bigquant_run(context, data): #获取当前日期 remainder = context.trading_day_index % 1 #如果没到调仓期直接结束运行 if remainder != 0: return
import datetime
#初始化
buy_list = [] #买入列表
sell_list = [] #卖出列表
#=============== 教据准备 ===============
#获取当前日期
today = data.current_dt.strftime('%Y-%m-%d')
time = data.current_dt
account_pos = context.get_account_positions()#获取所有持仓
holding_list = list({key: value for key, value in account_pos.items() if value.avail_qty > 0}.keys())#持有数量大于0的股票
holding_num = len(holding_list)
#读取当日数据
try:
today_data = context.ranker_prediction.loc[today,:]
today_data.reset_index(inplace=True)
except:
return
#策略
today_data=today_data[today_data['流通市值'] <=2500000000] #总市值的过滤
today_data=today_data[today_data['上市时间'] >= 365] #上市时间的过滤
today_data=today_data[today_data['市盈率ttm'] >= 0] #财务数据过滤
#today_data.sort_values(by='总市值',ascending=True,inplace=True)
today_data['总市值_score'] = today_data['总市值'].rank(ascending=True)*2
today_data['流通市值_score'] = today_data['流通市值'].rank(ascending=True)*3
today_data['收盘价_score'] = today_data['收盘价'].rank(ascending=True)*4
today_data['total_score'] = today_data['总市值_score'] + today_data['流通市值_score'] + today_data['收盘价_score']
today_data.sort_values(by='total_score',ascending=True,inplace=True)
#构建目标列表
target_sell_list = today_data.instrument.to_list()[:5]
target_buy_list = today_data.instrument.to_list()[:4]
#构建卖出列表
for ins in holding_list:
if ins not in target_sell_list:
sell_list.append(ins)
if holding_num < 4:
for ins in target_buy_list:
if ins not in holding_list:
buy_list.append(ins)
for ins in sell_list:
context.order_target(ins,0)
for ins in buy_list:
context.order_percent(ins,0.25)
交易引擎:成交回报处理函数,每个成交发生时执行一次
def m6_handle_trade_bigquant_run(context, trade): pass
交易引擎:委托回报处理函数,每个委托变化时执行一次
def m6_handle_order_bigquant_run(context, order): pass
交易引擎:盘后处理函数,每日盘后执行一次
def m6_after_trading_bigquant_run(context, data): pass
m1 = M.input_features.v1( features="""
流通市值=market_cap_float_0
总市值=market_cap_0
市盈率ttm=pe_ttm_0
上市时间=list_days_0
收盘价=close_0""" )
m7 = M.instruments.v2( start_date='2018-01-01', end_date='2023-09-15', market='CN_STOCK_A', instrument_list='', max_count=0 )
m3 = M.general_feature_extractor.v7( instruments=m7.data, features=m1.data, start_date='', end_date='', before_start_days=90 )
m4 = M.derived_feature_extractor.v3( input_data=m3.data, features=m1.data, date_col='date', instrument_col='instrument', drop_na=False, remove_extra_columns=False, user_functions={} )
m5 = M.chinaa_stock_filter.v1( input_data=m4.data, index_constituent_cond=['全部'], board_cond=['深证主板'], industry_cond=['全部'], st_cond=['正常'], delist_cond=['非退市'], output_left_data=False )
m6 = M.hftrade.v2( instruments=m7.data, options_data=m5.data, start_date='', end_date='', 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=100000, frequency='daily', price_type='真实价格', product_type='股票', before_start_days='0', benchmark='000300.HIX',
plot_charts=True, disable_cache=False, replay_bdb=True, show_debug_info=False, backtest_only=False ) [2024-01-17 01:02:57.432635] INFO moduleinvoker: input_features.v1 开始运行.. [2024-01-17 01:02:57.440979] INFO moduleinvoker: 命中缓存 [2024-01-17 01:02:57.441837] INFO moduleinvoker: input_features.v1 运行完成[0.009242s]. [2024-01-17 01:02:57.444440] INFO moduleinvoker: instruments.v2 开始运行.. [2024-01-17 01:02:57.448630] INFO moduleinvoker: 命中缓存 [2024-01-17 01:02:57.449374] INFO moduleinvoker: instruments.v2 运行完成[0.004939s]. [2024-01-17 01:02:57.457173] INFO moduleinvoker: general_feature_extractor.v7 开始运行.. [2024-01-17 01:02:57.461327] INFO moduleinvoker: 命中缓存 [2024-01-17 01:02:57.462116] INFO moduleinvoker: general_feature_extractor.v7 运行完成[0.004958s]. [2024-01-17 01:02:57.465034] INFO moduleinvoker: derived_feature_extractor.v3 开始运行.. [2024-01-17 01:02:57.468949] INFO moduleinvoker: 命中缓存 [2024-01-17 01:02:57.469713] INFO moduleinvoker: derived_feature_extractor.v3 运行完成[0.004686s]. [2024-01-17 01:02:57.472788] INFO moduleinvoker: chinaa_stock_filter.v1 开始运行.. [2024-01-17 01:02:57.477596] INFO moduleinvoker: 命中缓存 [2024-01-17 01:02:57.478454] INFO moduleinvoker: chinaa_stock_filter.v1 运行完成[0.005664s]. [2024-01-17 01:02:58.765409] INFO moduleinvoker: forward_register.v2 运行完成[1.260174s]. do_paper_run lm_1={'version': 'v3', 'algo_id': 3304, 'algo_unique_id': 'jssqbbc001_0f2be478-b491-11ee-a787-5259eb70f819', 'algo_name': '传说级别,打髋转过来的', 'algo_desc': '', 'first_date': '2024-01-16', 'benchmark_symbol': '000300.HIX', 'price_type': '真实价格', 'product_type': 'equity', 'data_frequency': '1d'}, options=DataSource(7a8d49cdfae541f784f8438af33a1f9fT) [2024-01-17 01:02:58.839014] INFO hfpapertrading: init options_data:DataSource(7a8d49cdfae541f784f8438af33a1f9fT) [2024-01-17 01:02:58.839767] INFO hfpapertrading: init username:jssqbbc001, algo_id:3304, first_date:2024-01-16, frequency:1d, adjust_type:none, instruments:['000001.SZA', '000002.SZA', '000004.SZA', '000005.SZA', '000006.SZA', '000007.SZA', '000008.SZA', '000009.SZA', '000010.SZA', '000011.SZA', '000012.SZA', '000014.SZA', '000016.SZA', '000017.SZA', '000018.SZA', '000019.SZA', '000020.SZA', '000021.SZA', '000022.SZA', '000023.SZA', '000025.SZA', '000026.SZA', '000027.SZA', '000028.SZA', '000029.SZA', '000030.SZA', '000031.SZA', '000032.SZA', '000034.SZA', '000035.SZA', '000036.SZA', '000037.SZA'] [2024-01-17 01:02:58.840286] INFO hfpapertrading: init benchmark_symbol=000300.HIX [2024-01-17 01:02:59.149856] INFO hfpapertrading: biglearning hfpapertrading:V1.2.6 [2024-01-17 01:02:59.150591] INFO hfpapertrading: bigtrader v1.9.8 2022-09-27 [2024-01-17 01:03:00.371735] INFO hfpapertrading: hfpapertrading invoke_micro_service done True [2024-01-17 01:03:00.452503] INFO hfpapertrading: run_date: 2024-01-16, notebook_id:jssqbbc001_0f2be478-b491-11ee-a787-5259eb70f819, algo_name:传说级别,打髋转过来的, is_third_match:0 [2024-01-17 01:03:02.743397] INFO hfpapertrading: first_trading_date=2024-01-16, cash=100000.0 [2024-01-17 01:03:02.744323] INFO hfpapertrading: hfpapertrading init_equity_name_map... [2024-01-17 01:03:04.662226] INFO hfpapertrading: cached_benchmark_ds=DataSource(7542cd16651b4027a458b8c4f082386bT) by benchmark_kwargs={'start_date': '2024-01-15', 'end_date': '2024-01-16', 'instruments': ['000300.HIX'], 'frequency': '1d', 'product_type': '', 'adjust_type': 'post'} [2024-01-17 01:03:11.794753] INFO hfpapertrading: cached_daily_ds=DataSource(f0a7c3371e724bc3bc08bedefd030d73T) [2024-01-17 01:03:12.903829] WARNING bigdatasource: cannot read data from table rightsissue_CN_STOCK_A 2023-01-11 00:00:00->2024-01-16! [2024-01-17 01:03:12.905007] INFO hfpapertrading: create_history_data done [2024-01-17 01:03:12.905575] INFO hfpapertrading: hfpapertrading get_adjust_factor_map... [2024-01-17 01:03:12.906068] INFO hfpapertrading: last_positions=[] [2024-01-17 01:03:12.923532] INFO hfpapertrading: daily_data= adjust_factor amount close instrument deal_number 0 1.022473 17837256.0 34.400002 688228.SHA 1469.01 4.212338 23014685.0 11.870001 600449.SHA 4045.02 1.554736 775621651.0 66.820000 601689.SHA 45778.03 1.313717 118496892.0 26.300001 301198.SZA 11137.04 1.439566 13227754.0 41.889999 688058.SHA 944.0... ... ... ... ... ...1286606 6.918054 148120585.0 25.719999 300286.SZA 19803.01286607 12.473256 140343774.0 20.000000 300285.SZA 21336.01286608 3.969476 28501046.0 5.390000 300284.SZA 4432.01286609 6.257471 56552246.0 6.420000 300283.SZA 11866.01286610 6.269833 28785866.0 5.740000 300281.SZA 12448.0
date high low open turn volume \
0 2023-01-11 35.520000 34.210003 35.520000 1.729798 511547.01 2023-01-11 11.930000 11.830000 11.880000 0.405089 1937058.02 2023-01-11 69.559998 66.510002 67.669998 1.038476 11444494.03 2023-01-11 27.019999 25.619999 27.000000 7.522362 4504240.04 2023-01-11 43.549999 41.889999 43.079998 0.553920 310195.0... ... ... ... ... ... ...1286606 2024-01-16 25.999998 24.699999 25.550001 3.339849 5832148.01286607 2024-01-16 20.250000 19.650000 20.100000 0.876981 7044018.01286608 2024-01-16 5.420000 5.310000 5.410000 0.597785 5313462.01286609 2024-01-16 6.460000 6.270000 6.360000 2.890602 8879700.01286610 2024-01-16 5.890000 5.650000 5.800000 1.264642 5024750.0
upper_limit lower_limit suspended st_status price_limit_status \
0 42.47 28.31 False 0.0 2.01 13.06 10.68 False 0.0 2.02 74.69 61.11 False 0.0 2.03 32.10 21.40 False 0.0 2.04 51.59 34.39 False 0.0 2.0... ... ... ... ... ...1286606 30.46 20.30 False 0.0 2.01286607 24.35 16.23 False 0.0 2.01286608 6.49 4.33 False 0.0 2.01286609 7.67 5.11 False 0.0 2.01286610 6.91 4.61 False 0.0 2.0
name delist_date list_date
0 开普云 NaT 2020-03-271 宁夏建材 NaT 2003-08-292 拓普集团 NaT 2015-03-193 喜悦智行 NaT 2021-12-024 宝兰德 NaT 2019-11-01... ... ... ...1286606 安科瑞 NaT 2012-01-131286607 国瓷材料 NaT 2012-01-131286608 苏交科 NaT 2012-01-101286609 温州宏丰 NaT 2012-01-101286610 金明精机 NaT 2011-12-29
[1286611 rows x 19 columns] 2024-01-17 01:03:13.220654 init history datas... 2024-01-17 01:03:13.221330 init history datas done. 2024-01-17 01:03:13.288372 run_backtest() capital_base:100000.0, frequency:1d, product_type:equity, date:2024-01-16 ~ 2024-01-16 2024-01-17 01:03:13.289096 run_backtest() running... 2024-01-17 01:03:13.309148 initial contracts len=0 2024-01-17 01:03:13.309306 backtest inited. [2024-01-17 01:03:14.889029] INFO hfpapertrading: c_initialize() first_trading_date:2024-01-16, trading_day_index:0 [2024-01-17 01:03:15.981810] INFO hfpapertrading: c_initialize() positions len:0,value:0.0,margin:0.0 [2024-01-17 01:03:15.982648] INFO hfpapertrading: c_initialize() orders len:0, orders_date=2024-01-16 2024-01-17 01:03:15.983391 backtest transforming 1d, bars=1... 2024-01-17 01:03:15.984226 transform start_trading_day=2024-01-16 00:00:00, simulation period=2024-01-16 ~ 2024-01-16 2024-01-17 01:03:15.984274 transform source=None, before_start_days=0 2024-01-17 01:03:15.984306 transform replay_func=<cyfunction BacktestEngine.transform.<locals>.replay_bars_dt at 0x7fe2e4106e10> 2024-01-17 01:03:16.004932 backtest run end! 2024-01-17 01:03:16.014473 run_backtest() finished! time cost 2.725s! [2024-01-17 01:03:17.137096] INFO hfpapertrading: raw_perf_ds=DataSource(6a67ba70a5fe432ba3b50878004501d5T) [2024-01-17 01:03:17.143683] INFO hfpapertrading: get last row of algo result_1: algorithm_period_return benchmark_period_return capital_used 2024-01-16 0.0 0.0 0.0
ending_cash ending_value long_value longs_count
2024-01-16 100000.0 0 0 0[2024-01-17 01:03:17.147489] INFO hfpapertrading: get last row of algo result_2: pnl portfolio_value returns short_value shorts_count 2024-01-16 0.0 100000.0 0.0 0.0 0
starting_cash starting_value trading_days
2024-01-16 100000.0 0.0 1[2024-01-17 01:03:17.148808] INFO hfpaper_helper: new_orders len=0 [2024-01-17 01:03:17.188572] INFO hfpaper_helper: get_orders new_orders 0: json_orders:
[2024-01-17 01:03:17.196134] INFO hfpaper_helper: get_last_sale_date_dict: {} [2024-01-17 01:03:17.255581] INFO hfpaper_helper: get_positions_stocks position_value:0.0 new_positions:
json_positions:
[2024-01-17 01:03:17.256797] INFO hfpaper_helper: get_portfolio last_pv:100000.0, current_pv:100000.0, json_portfolio:{'cum_return': 0.0, 'annual_return': 0.0, 'today_return': 0.0, 'pv': 0.0, 'max_pv': 100000.0, 'drawdown': 0.0, 'first_date': '2024-01-16', 'portfolio_value': 100000.0, 'cash': 100000.0} [2024-01-17 01:03:17.257504] INFO hfpaper_helper: get_transactions transaction num 0 [2024-01-17 01:03:17.257988] INFO hfpaper_helper: get_transactions total_buy:0.0, total_sell:0.0, json_transaction:
reading benchmark data... 000300.HIX 2024-01-16 2024-01-16 00:00:00+00:00 close date instrument 0 3300.876221 2024-01-16 000300.HIX [2024-01-17 01:03:17.273980] INFO hfpapertrading: json_cum_benchmark={"000300.HIX": 0.0, "000300.HIX.CUM": 0.0} [2024-01-17 01:03:17.274569] INFO hfpaper_helper: get_risk_indicators json_risk_indicator:{'alpha': 0.0, 'beta': 0.0, 'volatility': 0.0, 'sharpe': 0.0, 'ir': 0.0} [2024-01-17 01:03:17.275105] INFO hfpapertrading: result json_extension:{"order_price_field_buy": "open", "order_price_field_sell": "close", "is_stock": "True"} processing to update_database... [2024-01-17 01:03:17.275741] INFO hfpapertrading: call add_new_daily_record for portfolio_value=100000.0, position_value=0.0 [2024-01-17 01:03:18.393532] INFO hfpapertrading: invoke add_new_daily_record(jssqbbc001,3304,2024-01-16,json_portfolio={"cum_return": 0.0, "annual_return": 0.0, "today_return": 0.0, "pv": 0.0, "max_pv": 100000.0, "drawdown": 0.0, "first_date": "2024-01-16", "portfolio_value": 100000.0, "cash": 100000.0},json_cum_benchmark={"000300.HIX": 0.0, "000300.HIX.CUM": 0.0}) done rv=None cum_return_plot: 1, before_shared_cum_return_plot: 0, after_shared_cum_return_plot: 0, benchmark_cum_return_plot: 1, hold_percent_plot: 1 [2024-01-17 01:03:19.571860] INFO hfpapertrading: invoke update_equity_algo(jssqbbc001,3304,2024-01-16,cum_return=0.0,today_return=0.0) done rv=None [2024-01-17 01:03:19.572797] INFO hfpapertrading: hfpapertrading result, new_cash 100000.0 [2024-01-17 01:03:19.645929] INFO moduleinvoker: hfpapertrading.v1 运行完成[20.869471s]. [2024-01-17 01:03:19.647059] INFO moduleinvoker: hftrade.v2 运行完成[22.164411s].
\