克隆策略

策略逻辑

  • 分钟级别回测
  • 上轨:昨日收盘价 (1 + 0.03), 下轨:昨日收盘价 (1 - 0.03)
  • 下单逻辑:当分钟close突破上轨时卖出,当分钟close突破下轨时买入,收盘时根据当天买卖情况平仓

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-250:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-01-03","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2021-01-27","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000333.SZA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-250","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n # 最低底仓\n context.min_amount = 500\n # 每次交易股数\n context.per_order_amount = 1000\n # 建仓标志\n context.first_order_flag = True\n # 今日已发单标志\n context.today_order_flag = False\n # 已发买单标志\n context.buy_flag = False\n # 已发卖单标志\n context.sell_flag = False\n # 通道上轨、下轨\n context.up_track, context.down_track = 0.0, 0.0\n ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n if context.first_order_flag:\n print(\"建立底仓\")\n # 第一天买入, 设置底仓\n context.order(context.symbol(\"000333.SZA\"), 2000)\n context.last_order_price = data.current(context.symbol(\"000333.SZA\"), \"close\")\n context.first_order_flag = False\n return\n if context.trading_day_index < 1:\n # 首日除了建仓,不进行其他发单操作\n return\n \n # 当天第一根分钟行情到来时,打开交易标志位\n if data.current_dt.strftime(\"%H%M%S\") == \"093100\":\n # 计算今天交易的上轨和下轨\n sid = context.symbol(\"000333.SZA\")\n pre_close = data.history(sid, fields=\"close\", bar_count=1, frequency=\"1d\").values[0]\n context.up_track, context.down_track = pre_close * (1+0.03), pre_close * (1-0.03)\n \n context.today_order_flag = True\n context.buy_flag = False\n context.sell_flag = False\n # 产生下单信号\n sid = context.symbol(\"000333.SZA\")\n close = data.current(sid, \"close\")\n sell_signal = close > context.up_track\n buy_signal = close < context.down_track\n # 买入信号\n if buy_signal and context.today_order_flag:\n context.order(sid, context.per_order_amount)\n context.today_order_flag = False\n context.buy_flag = True\n # 卖出信号\n if sell_signal and context.today_order_flag:\n if context.portfolio.positions[sid].amount > context.min_amount:\n context.order(sid, (-1) * context.per_order_amount)\n context.today_order_flag = False\n context.sell_flag = True\n \n # 收盘时卖出或买入平仓\n if data.current_dt.strftime(\"%H%M%S\") == \"145900\" and context.buy_flag:\n context.order(sid, (-1) * context.per_order_amount)\n if data.current_dt.strftime(\"%H%M%S\") == \"145900\" and context.sell_flag:\n context.order(sid, context.per_order_amount)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"100000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"minute","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"真实价格","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-250"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-250"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-250","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":19,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='348,233,200,200'/><NodePosition Node='-250' Position='309,420,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [3]:
    # 本代码由可视化策略环境自动生成 2021年1月28日16:16
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m19_initialize_bigquant_run(context):
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        # 最低底仓
        context.min_amount = 500
        # 每次交易股数
        context.per_order_amount = 1000
        # 建仓标志
        context.first_order_flag = True
        # 今日已发单标志
        context.today_order_flag = False
        # 已发买单标志
        context.buy_flag = False
        # 已发卖单标志
        context.sell_flag = False
        # 通道上轨、下轨
        context.up_track, context.down_track = 0.0, 0.0
        
    # 回测引擎:每日数据处理函数,每天执行一次
    def m19_handle_data_bigquant_run(context, data):
        if context.first_order_flag:
            print("建立底仓")
            # 第一天买入, 设置底仓
            context.order(context.symbol("000333.SZA"), 2000)
            context.last_order_price = data.current(context.symbol("000333.SZA"), "close")
            context.first_order_flag = False
            return
        if context.trading_day_index < 1:
            # 首日除了建仓,不进行其他发单操作
            return
        
        # 当天第一根分钟行情到来时,打开交易标志位
        if data.current_dt.strftime("%H%M%S") == "093100":
            # 计算今天交易的上轨和下轨
            sid = context.symbol("000333.SZA")
            pre_close = data.history(sid, fields="close", bar_count=1, frequency="1d").values[0]
            context.up_track, context.down_track = pre_close * (1+0.03), pre_close * (1-0.03)
            
            context.today_order_flag = True
            context.buy_flag = False
            context.sell_flag = False
        # 产生下单信号
        sid = context.symbol("000333.SZA")
        close = data.current(sid, "close")
        sell_signal = close > context.up_track
        buy_signal = close < context.down_track
        # 买入信号
        if buy_signal and context.today_order_flag:
            context.order(sid, context.per_order_amount)
            context.today_order_flag = False
            context.buy_flag = True
        # 卖出信号
        if sell_signal and context.today_order_flag:
            if context.portfolio.positions[sid].amount > context.min_amount:
                context.order(sid, (-1) * context.per_order_amount)
                context.today_order_flag = False
                context.sell_flag = True
        
        # 收盘时卖出或买入平仓
        if data.current_dt.strftime("%H%M%S") == "145900" and context.buy_flag:
            context.order(sid, (-1) * context.per_order_amount)
        if data.current_dt.strftime("%H%M%S") == "145900" and context.sell_flag:
            context.order(sid, context.per_order_amount)
    # 回测引擎:准备数据,只执行一次
    def m19_prepare_bigquant_run(context):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2019-01-03',
        end_date='2021-01-27',
        market='CN_STOCK_A',
        instrument_list='000333.SZA',
        max_count=0
    )
    
    m19 = M.trade.v4(
        instruments=m1.data,
        start_date='',
        end_date='',
        initialize=m19_initialize_bigquant_run,
        handle_data=m19_handle_data_bigquant_run,
        prepare=m19_prepare_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=100000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='minute',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark='000300.SHA'
    )
    
    • 收益率79.8%
    • 年化收益率34.09%
    • 基准收益率86.16%
    • 阿尔法0.11
    • 贝塔0.57
    • 夏普比率1.61
    • 胜率0.86
    • 盈亏比4.82
    • 收益波动率17.32%
    • 信息比率-0.01
    • 最大回撤10.6%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-6c001348781845c890c48e78601c879c"}/bigcharts-data-end