可视化海龟策略

策略分享
新手专区
标签: #<Tag:0x00007fcf66642de0> #<Tag:0x00007fcf66642ca0>

(iQuant) #1
克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:options_data","SourceOutputPortId":"-86:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nts_max(close_0,20)\nts_min(close_0,10)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600006.SHA","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-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-70","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v6","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"30","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v2","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","ModuleId":"BigQuantSpace.trade.trade-v3","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n today=data.current_dt.strftime('%Y-%m-%d')\n # 长期均线值要有意义,需要在50根k线之后\n k = context.instruments[0] # 标的为字符串格式\n sid = context.symbol(k) # 将标的转化为equity格式\n price = data.current(sid, 'price') # 最新价格\n #如果今天有均线值\n if today in context.high_point.index:\n high_point =context.high_point.ix[today] # 短期均线值\n low_point = context.low_point.ix[today] # 长期均线值\n else:\n return\n \n cash = context.portfolio.cash # 现金\n cur_position = context.portfolio.positions[sid].amount # 持仓\n \n # 持仓\n cur_position = context.portfolio.positions[sid].amount \n \n # 交易逻辑\n # 最新价大于20日高点,并且处于空仓状态,并且该股票当日可以交易\n if price >= high_point and cur_position == 0 and data.can_trade(sid): \n context.order_target_percent(sid, 1) \n # 最新价小鱼等于10日低点,并且持有股票,并且该股票当日可以交易 \n elif price <= low_point and cur_position > 0 and data.can_trade(sid): \n context.order_target_percent(sid, 0) \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n df = context.options['data'].read_df().set_index('date')\n context.instruments=df.instrument.unique()\n context.low_point=df['ts_min(close_0,10)']\n context.high_point=df['ts_max(close_0,20)']\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\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":"1000001","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_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":"amount_integer","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-86","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-86","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"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-24' Position='765,21,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1074,124,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-70' Position='1078,236,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-76' Position='1076,327,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-81' Position='1032,547,200,200'/><NodePosition Node='-86' Position='1078,418,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":false}
    In [7]:
    # 本代码由可视化策略环境自动生成 2018年6月13日 13:58
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    ts_max(close_0,20)
    ts_min(close_0,10)"""
    )
    
    m2 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2015-01-01'),
        end_date=T.live_run_param('trading_date', '2017-01-01'),
        market='CN_STOCK_A',
        instrument_list='600006.SHA',
        max_count=0
    )
    
    m3 = M.general_feature_extractor.v6(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=30,
        m_cached=False
    )
    
    m4 = M.derived_feature_extractor.v2(
        input_data=m3.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        m_cached=False
    )
    
    m6 = M.dropnan.v1(
        input_data=m4.data
    )
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m5_handle_data_bigquant_run(context, data):
        today=data.current_dt.strftime('%Y-%m-%d')
        # 长期均线值要有意义,需要在50根k线之后
        k = context.instruments[0] # 标的为字符串格式
        sid = context.symbol(k) # 将标的转化为equity格式
        price = data.current(sid, 'price') # 最新价格
        #如果今天有均线值
        if today in context.high_point.index:
            high_point =context.high_point.ix[today]  # 短期均线值
            low_point = context.low_point.ix[today]   # 长期均线值
        else:
            return
          
        cash = context.portfolio.cash  # 现金
        cur_position = context.portfolio.positions[sid].amount # 持仓
        
        # 持仓
        cur_position = context.portfolio.positions[sid].amount  
                   
        # 交易逻辑
        # 最新价大于20日高点,并且处于空仓状态,并且该股票当日可以交易
        if price >= high_point  and cur_position == 0 and data.can_trade(sid):  
            context.order_target_percent(sid, 1) 
        # 最新价小鱼等于10日低点,并且持有股票,并且该股票当日可以交易    
        elif price <= low_point  and cur_position > 0 and data.can_trade(sid): 
            context.order_target_percent(sid, 0) 
    
    # 回测引擎:准备数据,只执行一次
    def m5_prepare_bigquant_run(context):
        # 加载预测数据
        df = context.options['data'].read_df().set_index('date')
        context.instruments=df.instrument.unique()
        context.low_point=df['ts_min(close_0,10)']
        context.high_point=df['ts_max(close_0,20)']
    
    # 回测引擎:初始化函数,只执行一次
    def m5_initialize_bigquant_run(context):
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    
    m5 = M.trade.v3(
        instruments=m2.data,
        options_data=m6.data,
        start_date='',
        end_date='',
        handle_data=m5_handle_data_bigquant_run,
        prepare=m5_prepare_bigquant_run,
        initialize=m5_initialize_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='close',
        capital_base=1000001,
        benchmark='000300.SHA',
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        plot_charts=True,
        backtest_only=False,
        amount_integer=False
    )
    
    [2018-06-01 10:01:34.683277] INFO: bigquant: input_features.v1 开始运行..
    [2018-06-01 10:01:34.690334] INFO: bigquant: 命中缓存
    [2018-06-01 10:01:34.701564] INFO: bigquant: input_features.v1 运行完成[0.018295s].
    [2018-06-01 10:01:34.734207] INFO: bigquant: instruments.v2 开始运行..
    [2018-06-01 10:01:34.737693] INFO: bigquant: 命中缓存
    [2018-06-01 10:01:34.749081] INFO: bigquant: instruments.v2 运行完成[0.014856s].
    [2018-06-01 10:01:34.781740] INFO: bigquant: general_feature_extractor.v6 开始运行..
    [2018-06-01 10:01:34.855148] INFO: 基础特征抽取: 年份 2014, 特征行数=22
    [2018-06-01 10:01:34.887358] INFO: 基础特征抽取: 年份 2015, 特征行数=241
    [2018-06-01 10:01:34.923618] INFO: 基础特征抽取: 年份 2016, 特征行数=200
    [2018-06-01 10:01:34.938681] INFO: 基础特征抽取: 年份 2017, 特征行数=0
    [2018-06-01 10:01:34.952404] INFO: 基础特征抽取: 总行数: 463
    [2018-06-01 10:01:34.958591] INFO: bigquant: general_feature_extractor.v6 运行完成[0.176856s].
    [2018-06-01 10:01:34.974503] INFO: bigquant: derived_feature_extractor.v2 开始运行..
    [2018-06-01 10:01:35.067940] INFO: derived_feature_extractor: 提取完成 ts_max(close_0,20), 0.007s
    [2018-06-01 10:01:35.078394] INFO: derived_feature_extractor: 提取完成 ts_min(close_0,10), 0.006s
    [2018-06-01 10:01:35.186363] INFO: derived_feature_extractor: /y_2014, 22
    [2018-06-01 10:01:35.232683] INFO: derived_feature_extractor: /y_2015, 241
    [2018-06-01 10:01:35.281297] INFO: derived_feature_extractor: /y_2016, 200
    [2018-06-01 10:01:35.336972] INFO: bigquant: derived_feature_extractor.v2 运行完成[0.362462s].
    [2018-06-01 10:01:35.353547] INFO: bigquant: dropnan.v1 开始运行..
    [2018-06-01 10:01:35.441563] INFO: dropnan: /y_2014, 3/22
    [2018-06-01 10:01:35.484887] INFO: dropnan: /y_2015, 241/241
    [2018-06-01 10:01:35.525610] INFO: dropnan: /y_2016, 200/200
    [2018-06-01 10:01:35.540812] INFO: dropnan: 行数: 444/463
    [2018-06-01 10:01:35.546161] INFO: bigquant: dropnan.v1 运行完成[0.192613s].
    [2018-06-01 10:01:35.583584] INFO: bigquant: backtest.v7 开始运行..
    [2018-06-01 10:01:35.821391] INFO: algo: set price type:backward_adjusted
    [2018-06-01 10:01:42.164858] INFO: Performance: Simulated 488 trading days out of 488.
    [2018-06-01 10:01:42.169220] INFO: Performance: first open: 2015-01-05 01:30:00+00:00
    [2018-06-01 10:01:42.172251] INFO: Performance: last close: 2016-12-30 07:00:00+00:00
    
    • 收益率-33.16%
    • 年化收益率-18.78%
    • 基准收益率-6.33%
    • 阿尔法-0.19
    • 贝塔0.52
    • 夏普比率-0.52
    • 胜率0.364
    • 盈亏比1.221
    • 收益波动率42.75%
    • 信息比率-0.37
    • 最大回撤66.73%
    [2018-06-01 10:01:45.583193] INFO: bigquant: backtest.v7 运行完成[9.999581s].
    

    传统策略的可视化开发
    怎么改为每只股等量资金买入