复制链接
克隆策略

    {"description":"实验创建于2020/10/29","graph":{"edges":[{"to_node_id":"-152:instruments","from_node_id":"-140:data"}],"nodes":[{"node_id":"-140","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2016-06-28","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2016-07-18","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"600000.SHA\n000002.SZA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-140"}],"output_ports":[{"name":"data","node_id":"-140"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-152","module_id":"BigQuantSpace.trade.trade-v4","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 pass","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n \n dt = data.current_dt.strftime('%Y-%m-%d')\n k = context.instruments[0] # 标的为字符串格式\n sid = context.symbol(k) # 将标的转化为equity格式\n \n if dt == '2016-07-13':\n order(sid, 100)\n print(dt,'生成一个买入100股的虚拟订单')\n ","type":"Literal","bound_global_parameter":null},{"name":"prepare","value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n \n dt = data.current_dt.strftime('%Y-%m-%d')\n \n k = context.instruments[0] # 标的为字符串格式\n \n sid = context.symbol(k) # 将标的转化为equity格式\n \n print('撤单之前的委托:', context.get_open_orders().values())\n \n for open_orders in context.get_open_orders().values():\n\n \n for order in open_orders:\n \n sid = order.sid\n today_open = data.current(sid, 'open')\n \n if True: \n context.cancel_order(order)\n print(dt,'在before_trading_start取消订单:',sid)\n print('撤单之后的委托:',context.get_open_orders().values())\n \n # 买入另外一只\n from zipline.finance.order import Order\n from zipline.finance.execution import MarketOrder\n \n order = Order(\n dt = order['dt'],\n asset=context.symbol(context.instruments[1]),\n amount=100,\n stop=None,\n limit=None,\n price_field='open')\n \n try:\n context.blotter.open_orders[order.asset].append(order)\n except Exception:\n context.blotter.open_orders[order.asset] = [order]\n\n context.blotter.orders[order.id] = order\n context.blotter.new_orders.append(order)\n continue\n \n print('新提交订单后的委托:',context.get_open_orders().values())\n","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":0.025,"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":"capital_base","value":1000000,"type":"Literal","bound_global_parameter":null},{"name":"auto_cancel_non_tradable_orders","value":"True","type":"Literal","bound_global_parameter":null},{"name":"data_frequency","value":"daily","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":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-152"},{"name":"options_data","node_id":"-152"},{"name":"history_ds","node_id":"-152"},{"name":"benchmark_ds","node_id":"-152"},{"name":"trading_calendar","node_id":"-152"}],"output_ports":[{"name":"raw_perf","node_id":"-152"}],"cacheable":false,"seq_num":2,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-140' Position='225,157,200,200'/><node_position Node='-152' Position='185,341,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [4]:
    # 本代码由可视化策略环境自动生成 2022年12月8日 16:14
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m2_initialize_bigquant_run(context):
        pass
    # 回测引擎:每日数据处理函数,每天执行一次
    def m2_handle_data_bigquant_run(context, data):
        
        dt = data.current_dt.strftime('%Y-%m-%d')
        k = context.instruments[0] # 标的为字符串格式
        sid = context.symbol(k) # 将标的转化为equity格式
        
        if dt == '2016-07-13':
            order(sid, 100)
            print(dt,'生成一个买入100股的虚拟订单')
        
    # 回测引擎:准备数据,只执行一次
    def m2_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m2_before_trading_start_bigquant_run(context, data):
        
        dt = data.current_dt.strftime('%Y-%m-%d')
        
        k = context.instruments[0] # 标的为字符串格式
        
        sid = context.symbol(k) # 将标的转化为equity格式
        
        print('撤单之前的委托:', context.get_open_orders().values())
        
        for open_orders in context.get_open_orders().values():
    
            
            for order in open_orders:
            
                sid = order.sid
                today_open = data.current(sid, 'open')
               
                if True:   
                    context.cancel_order(order)
                    print(dt,'在before_trading_start取消订单:',sid)
                    print('撤单之后的委托:',context.get_open_orders().values())
                    
                    # 买入另外一只
                    from zipline.finance.order import Order
                    from zipline.finance.execution import MarketOrder
                    
                    order = Order(
                        dt = order['dt'],
                        asset=context.symbol(context.instruments[1]),
                        amount=100,
                        stop=None,
                        limit=None,
                        price_field='open')
                  
                    try:
                        context.blotter.open_orders[order.asset].append(order)
                    except Exception:
                        context.blotter.open_orders[order.asset] = [order]
    
                    context.blotter.orders[order.id] = order
                    context.blotter.new_orders.append(order)
                    continue
                    
        print('新提交订单后的委托:',context.get_open_orders().values())
    
    
    m1 = M.instruments.v2(
        start_date='2016-06-28',
        end_date='2016-07-18',
        market='CN_STOCK_A',
        instrument_list="""600000.SHA
    000002.SZA""",
        max_count=0
    )
    
    m2 = M.trade.v4(
        instruments=m1.data,
        start_date='',
        end_date='',
        initialize=m2_initialize_bigquant_run,
        handle_data=m2_handle_data_bigquant_run,
        prepare=m2_prepare_bigquant_run,
        before_trading_start=m2_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    • 收益率-0.0%
    • 年化收益率-0.01%
    • 基准收益率4.53%
    • 阿尔法-0.03
    • 贝塔0.0
    • 夏普比率-521.75
    • 胜率0.0
    • 盈亏比0.0
    • 收益波动率0.01%
    • 信息比率-0.41
    • 最大回撤0.0%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-46568f3e175e4b72b8ac4e269016259d"}/bigcharts-data-end