复制链接
克隆策略

    {"description":"实验创建于2022/3/7","graph":{"edges":[{"to_node_id":"-248:instruments","from_node_id":"-290:data"}],"nodes":[{"node_id":"-290","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2022-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-03-11","type":"Literal","bound_global_parameter":"交易日期"},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"600519.SHA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-290"}],"output_ports":[{"name":"data","node_id":"-290"}],"cacheable":false,"seq_num":1,"comment":"输入证券","comment_collapsed":true},{"node_id":"-248","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 context.subscribe(context.instruments[0])\n context.set_universe(context.instruments[0]) # 设置需要处理的合约\n context.bar_series = NumPyDeque(200, dtype='object') # 20个分钟 K线 (包括OHLC)\n context.bar_index = 0 # 分钟K线序号\n context.big_cycle_bar_index = 0 #30分钟大周期K线序号\n context.ma_periods = 26 \n\n \n \n ","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 交易引擎:每个单位时间开盘前调用一次。\ndef bigquant_run(context, data):\n pass","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":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n \n import talib as ta\n context.bar_index += 1\n cur_date = data.current_dt\n cur_hm = cur_date.strftime('%H:%M')\n \n \n if context.bar_index % 30 == 0: # 30m k线\n context.big_cycle_bar_index += 1\n \n sid = context.symbol(context.instruments[0])# 标的为字符串格式\n hist = data.history(sid, [\"open\",\"high\",\"low\",\"close\"], 30, \"1m\")\n big_cycle_bar_data = {'datetime':hist.index[-1],'high':hist.high.max(),'open':hist.open.values[0],'low':hist.low.min(),'close':hist.close.values[-1]}\n context.bar_series.append(big_cycle_bar_data)\n \n if context.big_cycle_bar_index > context.ma_periods : # 30分钟大周期K线的移动平均26个周期的均值 \n price_data = pd.DataFrame(list(context.bar_series))\n \n sma = talib.SMA(price_data['close'], timeperiod=context.ma_periods ).values[-1]\n cur_price = price_data['close'].values[-1]\n cur_position = context.portfolio.positions[sid].amount # 持仓 \n \n if cur_position==0 and cur_price>sma:\n context.order(sid, 100)\n print('buy'*5, cur_date)\n \n elif cur_position>0 and cur_price<sma:\n context.order_target_percent(sid, 0)\n print('sell'*5, cur_date)\n\n \n \n\n \n \n\n \n \n \n \n \n \n \n \n \n \n \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":"500000","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":"open","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":"True","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":"-248"},{"name":"options_data","node_id":"-248"},{"name":"history_ds","node_id":"-248"},{"name":"benchmark_ds","node_id":"-248"}],"output_ports":[{"name":"raw_perf","node_id":"-248"}],"cacheable":false,"seq_num":3,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-290' Position='-75,-332,200,200'/><node_position Node='-248' Position='-66.40879440307617,-202,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [2]:
    # 本代码由可视化策略环境自动生成 2022年12月12日 14:11
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m3_initialize_bigquant_run(context):
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.subscribe(context.instruments[0])
        context.set_universe(context.instruments[0]) # 设置需要处理的合约
        context.bar_series =  NumPyDeque(200, dtype='object')  # 20个分钟 K线 (包括OHLC)
        context.bar_index = 0 # 分钟K线序号
        context.big_cycle_bar_index = 0 #30分钟大周期K线序号
        context.ma_periods  = 26 
    
         
     
     
    # 交易引擎:每个单位时间开盘前调用一次。
    def m3_before_trading_start_bigquant_run(context, data):
        pass
    # 交易引擎:tick数据处理函数,每个tick执行一次
    def m3_handle_tick_bigquant_run(context, tick):
        pass
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m3_handle_data_bigquant_run(context, data):
        
        import talib as ta
        context.bar_index += 1
        cur_date =  data.current_dt
        cur_hm = cur_date.strftime('%H:%M')
     
        
        if context.bar_index % 30 == 0:   # 30m k线
            context.big_cycle_bar_index += 1
                
            sid = context.symbol(context.instruments[0])# 标的为字符串格式
            hist = data.history(sid, ["open","high","low","close"], 30, "1m")
            big_cycle_bar_data = {'datetime':hist.index[-1],'high':hist.high.max(),'open':hist.open.values[0],'low':hist.low.min(),'close':hist.close.values[-1]}
            context.bar_series.append(big_cycle_bar_data)
           
            if context.big_cycle_bar_index > context.ma_periods :  # 30分钟大周期K线的移动平均26个周期的均值  
                price_data = pd.DataFrame(list(context.bar_series))
                
                sma = talib.SMA(price_data['close'], timeperiod=context.ma_periods ).values[-1]
                cur_price = price_data['close'].values[-1]
                cur_position = context.portfolio.positions[sid].amount # 持仓 
                
                if cur_position==0 and cur_price>sma:
                    context.order(sid, 100)
                    print('buy'*5, cur_date)
                    
                elif  cur_position>0 and cur_price<sma:
                    context.order_target_percent(sid, 0)
                    print('sell'*5, cur_date)
    
                
                
    
            
             
    
        
        
        
        
        
        
        
        
        
        
        
        
    
       
    # 交易引擎:成交回报处理函数,每个成交发生时执行一次
    def m3_handle_trade_bigquant_run(context, trade):
        pass 
    
    # 交易引擎:委托回报处理函数,每个委托变化时执行一次
    def m3_handle_order_bigquant_run(context, order):
        pass
    
    # 交易引擎:盘后处理函数,每日盘后执行一次
    def m3_after_trading_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2022-01-01',
        end_date=T.live_run_param('trading_date', '2022-03-11'),
        market='CN_STOCK_A',
        instrument_list='600519.SHA',
        max_count=0,
        m_cached=False
    )
    
    m3 = M.hftrade.v2(
        instruments=m1.data,
        start_date='',
        end_date='',
        initialize=m3_initialize_bigquant_run,
        before_trading_start=m3_before_trading_start_bigquant_run,
        handle_tick=m3_handle_tick_bigquant_run,
        handle_data=m3_handle_data_bigquant_run,
        handle_trade=m3_handle_trade_bigquant_run,
        handle_order=m3_handle_order_bigquant_run,
        after_trading=m3_after_trading_bigquant_run,
        capital_base=500000,
        frequency='minute',
        price_type='真实价格',
        product_type='股票',
        before_start_days='0',
        volume_limit=1,
        order_price_field_buy='open',
        order_price_field_sell='open',
        benchmark='000300.HIX',
        plot_charts=True,
        disable_cache=False,
        replay_bdb=True,
        show_debug_info=False,
        backtest_only=False
    )
    
    buybuybuybuybuy 2022-01-12 10:30:00
    sellsellsellsellsell 2022-01-13 10:00:00
    buybuybuybuybuy 2022-01-18 10:00:00
    sellsellsellsellsell 2022-01-24 13:30:00
    buybuybuybuybuy 2022-01-26 14:30:00
    sellsellsellsellsell 2022-01-28 10:00:00
    buybuybuybuybuy 2022-02-09 13:30:00
    sellsellsellsellsell 2022-02-11 13:30:00
    buybuybuybuybuy 2022-02-14 11:30:00
    sellsellsellsellsell 2022-02-14 14:30:00
    sellsellsellsellsell 2022-02-17 13:30:00
    buybuybuybuybuy 2022-02-18 11:00:00
    sellsellsellsellsell 2022-02-21 10:30:00
    buybuybuybuybuy 2022-02-25 10:00:00
    sellsellsellsellsell 2022-02-25 10:30:00
    sellsellsellsellsell 2022-02-25 11:00:00
    sellsellsellsellsell 2022-02-25 11:30:00
    sellsellsellsellsell 2022-02-25 13:30:00
    sellsellsellsellsell 2022-02-28 10:00:00
    buybuybuybuybuy 2022-02-28 15:00:00
    buybuybuybuybuy 2022-03-01 10:00:00
    sellsellsellsellsell 2022-03-03 10:00:00
    buybuybuybuybuy 2022-03-08 14:00:00
    sellsellsellsellsell 2022-03-08 14:30:00
    sellsellsellsellsell 2022-03-08 15:00:00
    sellsellsellsellsell 2022-03-09 14:00:00
    buybuybuybuybuy 2022-03-09 14:30:00
    sellsellsellsellsell 2022-03-11 10:00:00
    buybuybuybuybuy 2022-03-11 14:30:00
    sellsellsellsellsell 2022-03-11 15:00:00
    
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-2-2232d4dc55fe> in <module>
         97 )
         98 
    ---> 99 m3 = M.hftrade.v2(
        100     instruments=m1.data,
        101     start_date='',
    
    AttributeError: 'BigQuantModule' object has no attribute '_basic_info_ds'