{"description":"实验创建于2018/10/16","graph":{"edges":[{"to_node_id":"-43:instruments","from_node_id":"-1483:data_1"},{"to_node_id":"-43:options_data","from_node_id":"-1483:data_2"}],"nodes":[{"node_id":"-1483","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3,before_days):\n tmp = DataSource('dominant_CN_FUTURE').read()\n \n future_ins = list(set(tmp['instrument']))\n commodity_ins = [i for i in future_ins if not i.endswith('CFE')]\n contracts = tmp.dominant.unique().tolist()\n\n fields=['open','high','low','close','volume','open_intl']\n start_date= '2022-01-01'\n end_date = '2022-08-24'\n price_df = DataSource('bar1d_CN_FUTURE').read(commodity_ins,start_date,end_date,fields)\n price_df['adjust_factor']=1.0\n \n ds3 = DataSource.write_df(price_df)\n\n ds1 = DataSource.write_pickle({'start_date':start_date, 'end_date':end_date, 'instruments':contracts})\n\n def caclu(df):\n length = 1\n df['mom'] = df['open'] / df['open'].shift(length) - 1\n df['roll_open_intl'] = df['open_intl'].rolling(22).mean()\n return df \n mom_df = price_df.groupby('instrument').apply(caclu)\n mom_df = mom_df[mom_df['roll_open_intl']>=100000]\n \n symbol_nums = 5\n winners = mom_df.groupby('date').apply(lambda x:x.dropna().sort_values('mom', ascending=False)['instrument'][:symbol_nums].tolist())\n losers = mom_df.groupby('date').apply(lambda x:x.dropna().sort_values('mom', ascending=False)['instrument'][-1*symbol_nums:].tolist())\n\n ds2 = DataSource.write_pickle({'winners':winners, 'losers':losers})\n \n return Outputs(data_1=ds1, data_2=ds2)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{'before_days':1}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-1483"},{"name":"input_2","node_id":"-1483"},{"name":"input_3","node_id":"-1483"}],"output_ports":[{"name":"data_1","node_id":"-1483"},{"name":"data_2","node_id":"-1483"},{"name":"data_3","node_id":"-1483"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-43","module_id":"BigQuantSpace.hftrade.hftrade-v2","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"initialize","value":"# 交易引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n print(\"initialize\") \n\n context.today_open = 0 #当天开盘价\n context.closetime_day = \"14:58\"#日内策略白盘平仓时间,一般14:58\n context.closetime_night = \"22:58\"#日内策略夜盘平仓时间,一般22:58,注意有些商品夜盘收盘时间不一样\n \n import re\n # 设置是否是结算模式\n # context.set_need_settle(False)\n # 设置最大杠杆\n context.strategy_leverage = 3\n # context.set_max_leverage(1, 'fill_amap')\n outer_data = context.options['data'].read_pickle()\n context.winners = outer_data['winners']\n context.losers = outer_data['losers']\n context.rebalance_days = 1\n# if 'index' not in context.extension:\n# context.extension['index'] = 0 \n \n context.dom = DataSource('dominant_CN_FUTURE').read() ","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 交易引擎:每个单位时间开盘前调用一次。\ndef bigquant_run(context, data):\n context.flag = 1 #用于获取今开\n # 盘前处理,订阅行情等\n \n","type":"Literal","bound_global_parameter":null},{"name":"handle_tick","value":"# 交易引擎:tick数据处理函数,每个tick执行一次\ndef bigquant_run(context, tick):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 交易引擎:bar数据处理函数,每个时间单位执行一次\ndef bigquant_run(context, data):\n if context.flag == 1 :\n# history_data = data.history(context.ins, [\"open\"], 1, \"1m\")\n# context.today_open = history_data.iloc[-1]['open'] \n context.flag += 1\n import re\n today = data.current_dt.strftime('%Y-%m-%d') # 当前交易日期\n\n# context.extension['index'] += 1\n\n # 整理赢者和输者\n try:\n winners = context.winners[today]\n losers = context.losers[today]\n\n except KeyError as e:\n return \n\n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n contract_hold_now = [equity.symbol for equity in context.portfolio.positions]\n\n\n # if context.extension['index'] % context.rebalance_days != 0: # 调仓日不需要换合约\n # #if True:\n # for old_contract in contract_hold_now:\n # dom_contract = ''.join(re.findall(r'[A-Za-z]', old_contract.split('.')[0])) +'8888'+'.'+ old_contract.split('.')[1]\n\n # try:\n # new_contract = context.dom[(context.dom['instrument'] == dom_contract) & (context.dom['date']==today)]['dominant'].tolist()[0]\n # except IndexError as e:\n\n # continue\n # # 如果出现换月的情形,就移仓换月(先平 后开)\n # if old_contract != new_contract:\n\n # new_sid = context.future_symbol(new_contract)\n # sid = context.future_symbol(old_contract)\n # hold_amount = context.portfolio.positions[sid]['amount']\n\n # if data.can_trade(sid) and data.can_trade(new_sid) and hold_amount >0: # 卖平 再买开\n # # Long\n # context.order(sid, -1 * (hold_amount), position_effect='close')\n # context.order(new_sid, 1 * (hold_amount), position_effect='open')\n # print(today, '出现移仓换月!','当前持仓是多头!', '旧合约:',old_contract, '新合约:',new_contract,'当前持仓', hold_amount)\n\n # elif data.can_trade(sid) and data.can_trade(new_sid) and hold_amount <0: # 买平 再卖开\n # # Short\n # context.order(sid, -1 * (hold_amount), position_effect='close')\n # context.order(new_sid, 1 * (hold_amount), position_effect='open')\n # print(today, '出现移仓换月!','当前持仓是空头!', '旧合约:',old_contract, '新合约:',new_contract,'当前持仓', hold_amount)\n\n\n\n # # 不在换仓日就return,相当于后面的代码只会一个月运行一次,买入的股票会持有1个月\n # if context.extension['index'] % context.rebalance_days != 0:\n # return \n\n print(today, 'winners:',winners ,'losers:',losers)\n\n # 月初换仓第一步\n for old_contract in contract_hold_now:\n sid = context.future_symbol(old_contract )\n hold_amount = context.portfolio.positions[sid]['amount'] \n\n if data.can_trade(sid) and hold_amount >0: # 卖平\n context.order(sid, -1 * (hold_amount), position_effect='close')\n # context.order(sid, 0, position_effect='target')\n\n print(today,'_-_-'*3,'月初多仓进行平仓!', '合约',old_contract, '手数', hold_amount)\n\n elif data.can_trade(sid) and hold_amount <0: # 买平 \n\n context.order(sid, -1 * (hold_amount), position_effect='close')\n # context.order(sid, 0, position_effect='target')\n\n print(today,'____'*3,'月初空仓进行平仓!', '合约',old_contract, '手数', hold_amount)\n\n\n # if len(winners)+len(losers) <=5:\n # return \n\n # 等权重分配仓位\n weight = 1 / (len(winners)+len(losers))\n\n portfolio_value = context.portfolio.portfolio_value\n book_value = portfolio_value * context.strategy_leverage * weight\n\n # 月初换仓第二步\n for domin_contract in winners: # ['RB8888.SHF']\n future_variety = ''.join(re.findall(r'[A-Za-z]', domin_contract.split('.')[0])) # RB\n try:\n contract = context.dom[(context.dom['instrument'] == domin_contract ) & (context.dom['date']==today)]['dominant'].tolist()[0] # RB1901.SHF\n except IndexError as e:\n continue \n sid =context.future_symbol(contract)\n\n if data.can_trade(sid):\n# unit = context.get_multiplier(future_variety)\n unit = context.get_contract(future_variety).multiplier\n print(sid)\n print(data)\n\n print(data.current(sid,'open') )\n\n price = data.current(sid, 'price') \n lots = math.floor(book_value/(unit*price))\n if lots <= 0:\n continue\n context.order(sid, lots, position_effect='open')\n print(today,'____'*3,'月初建多仓!', '合约',contract, '手数',lots)\n\n for domin_contract in losers:\n future_variety = ''.join(re.findall(r'[A-Za-z]', domin_contract.split('.')[0])) # RB\n try:\n contract = context.dom[(context.dom['instrument'] == domin_contract ) & (context.dom['date']==today)]['dominant'].tolist()[0] # RB1901.SHF\n except IndexError as e:\n continue \n sid =context.future_symbol(contract)\n\n if data.can_trade(sid):\n# unit = context.get_multiplier(future_variety)\n unit = context.get_contract(future_variety).multiplier\n \n print(sid)\n print(data)\n\n print(data.current(sid,'open') )\n\n price = data.current(sid,'price') \n lots = math.floor(book_value/(unit*price))\n if lots <= 0:\n continue\n context.order(sid, -1*lots, position_effect='open')\n print(today,'____'*3,'月初建空仓!', '合约',contract, '手数',-1*lots)\n\n\n","type":"Literal","bound_global_parameter":null},{"name":"handle_trade","value":"# 交易引擎:成交回报处理函数,每个成交发生时执行一次\ndef bigquant_run(context, trade):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_order","value":"# 交易引擎:委托回报处理函数,每个委托变化时执行一次\ndef bigquant_run(context, order):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"after_trading","value":"# 交易引擎:盘后处理函数,每日盘后执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":1000000,"type":"Literal","bound_global_parameter":null},{"name":"frequency","value":"minute","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"真实价格","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"股票","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"0","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":1,"type":"Literal","bound_global_parameter":null},{"name":"order_price_field_buy","value":"open","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"close","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000300.HIX","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"disable_cache","value":"False","type":"Literal","bound_global_parameter":null},{"name":"replay_bdb","value":"False","type":"Literal","bound_global_parameter":null},{"name":"show_debug_info","value":"False","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-43"},{"name":"options_data","node_id":"-43"},{"name":"history_ds","node_id":"-43"},{"name":"benchmark_ds","node_id":"-43"}],"output_ports":[{"name":"raw_perf","node_id":"-43"}],"cacheable":false,"seq_num":1,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-1483' Position='90,-100,200,200'/><node_position Node='-43' Position='359.8341979980469,217.12115478515625,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
[2022-08-25 23:27:12.742038] INFO: moduleinvoker: cached.v3 开始运行..
[2022-08-25 23:27:12.752970] INFO: moduleinvoker: 命中缓存
[2022-08-25 23:27:12.755662] INFO: moduleinvoker: cached.v3 运行完成[0.013647s].
[2022-08-25 23:27:12.810970] INFO: moduleinvoker: hfbacktest.v1 开始运行..
[2022-08-25 23:27:12.817370] INFO: hfbacktest: biglearning V1.4.18
[2022-08-25 23:27:12.819532] INFO: hfbacktest: bigtrader v1.9.7_sp5 2022-08-16
[2022-08-25 23:27:12.859651] INFO: moduleinvoker: cached.v2 开始运行..
[2022-08-25 23:27:12.879527] INFO: moduleinvoker: 命中缓存
[2022-08-25 23:27:12.881879] INFO: moduleinvoker: cached.v2 运行完成[0.022231s].
[2022-08-25 23:27:13.056447] INFO: moduleinvoker: cached.v2 开始运行..
[2022-08-25 23:27:13.063600] INFO: moduleinvoker: 命中缓存
[2022-08-25 23:27:13.067544] INFO: moduleinvoker: cached.v2 运行完成[0.011096s].
[2022-08-25 23:27:37.528086] ERROR: moduleinvoker: module name: hfbacktest, module version: v1, trackeback: AttributeError: 'NoneType' object has no attribute 'get_value'
[2022-08-25 23:27:37.542032] ERROR: moduleinvoker: module name: hftrade, module version: v2, trackeback: AttributeError: 'NoneType' object has no attribute 'get_value'
initialize
2022-02-09 winners: ['AL8888.SHF', 'HC8888.SHF', 'SP8888.SHF', 'C8888.DCE', 'IC8888.CFX'] losers: ['P8888.DCE', 'OI8888.CZC', 'AP8888.CZC', 'FU8888.SHF', 'PP8888.DCE']
Future(48306 [AL2203.SHF])
BarDatas(current_dt:2022-02-09 21:01:00)
2022-08-25 23:27:37.527220 strategy strategy exception:Traceback (most recent call last):
File "bigtrader/strategy/engine.py", line 713, in bigtrader2.bigtrader.strategy.engine.StrategyEngine._call_strategy_func
File "bigtrader/strategy/strategy_base.py", line 2235, in bigtrader2.bigtrader.strategy.strategy_base.StrategyBase.call_handle_data
File "<ipython-input-44-e20e00b3bddc>", line 179, in m1_handle_data_bigquant_run
print(data.current(sid,'open') )
File "/var/app/enabled/bigtrader2/bigtrader/protocol.py", line 285, in current
return self.__data_engine.get_current_value(asset, curr_dt, fields)
File "bigtrader/mdata/engine.py", line 690, in bigtrader2.bigtrader.mdata.engine.DataEngine._get_minute_value_from_df
AttributeError: 'NoneType' object has no attribute 'get_value'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-44-e20e00b3bddc> in <module>
233 )
234
--> 235 m1 = M.hftrade.v2(
236 instruments=m4.data_1,
237 options_data=m4.data_2,
<ipython-input-44-e20e00b3bddc> in m1_handle_data_bigquant_run(context, data)
177 print(data)
178
--> 179 print(data.current(sid,'open') )
180
181 price = data.current(sid, 'price')
AttributeError: 'NoneType' object has no attribute 'get_value'