【宽客学院】开发传统趋势策略


(iQuant) #1
作者:bigquant
阅读时间:3分钟
本文由BigQuant宽客学院推出,难度标签:☆☆

导语:本文以双均线策略为例,如何开发一个传统的趋势跟踪策略。

在BigQuant策略平台上,除了开发AI策略,还可以开发传统策略,比如趋势跟踪、套利、事件驱动策略、多因子选股策略。本文以双均线策略为例,帮助大家更好地理解BigQuant回测机制

双均线策略的策略思想是:当短期均线上穿长期均线时,形成金叉,此时买入股票。当短期均线下穿长期均线时,形成死叉,此时卖出股票。研究表明,双均线系统虽然简单,但只要严格执行,也能长期盈利。

策略的构建流程如下图所示:

我们可以在BigStudio环境下构建如下流程

第一步,我们通过证券代码列表模m1块指定回测的股票和回测起止日期。

第二步,我们通过特征输入列表模块m2定义买入和卖出信号

  • 定义5日均线大于50日均线作为买入条件信号buy_condition,
  • 定义5日均线小于50日均线作为卖出条件信号sell_condition。

这里的等号表示将表达式重命名,以免表达式过长导致不方便后续的过滤操作。

buy_condition=where(mean(close_0,5)>mean(close_0,50),1,0)
sell_condition=where(mean(close_0,5)<mean(close_0,50),1,0)

上述代码中使用的close_0默认是后复权价格,如果想采用真实价格计算信号,那么可以改写为

buy_condition=where(mean(close_0/adjust_factor_0,5)>mean(close_0/adjust_factor_0, 50),1,0)
sell_condition=where(mean(close_0/adjust_factor_0,5,5)<mean(close_0/adjust_factor_0, 50),1,0)

第三步,通过基础特征抽取模块获取表达式中基础因子数据close_0,

第四步,通过衍生特征抽取m8获取buy_condition和sell_condition对应的因子表达式数据。

第五步,利用缺失值处理模块m6对含有缺失值的行进行删除处理。

第六步,我们将证券代买列表模块的输出和计算好的买卖条件信号输出给回测模块m3。

在回测模块中,首先我们在初始化函数里通过context.set_commission设置交易手续费

    # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))

本例中,我们设置股票的买入手续费按照成交额的0.0003收取,卖出手续费按成交额的0.0013收取,不足5元按照5元收取。

然后,我们在数据准备函数中我们定义全局变量context.daily_stock_buy和context.daily_stock_sell分别获取每日符合买卖条件的股票列表

# 回测引擎:准备数据,只执行一次
def bigquant_run(context):
    # 加载计算数据
    df = context.options['data'].read_df()

    # 函数:求满足开仓条件的股票列表
    def open_pos_con(df):
        return list(df[df['buy_condition']>0].instrument)

    # 函数:求满足平仓条件的股票列表
    def close_pos_con(df):
        return list(df[df['sell_condition']>0].instrument)

    # 逐日计算每日买入股票列表
    context.daily_stock_buy= df.groupby('date').apply(open_pos_con)
    # 逐日计算每日卖出股票列表
    context.daily_stock_sell= df.groupby('date').apply(close_pos_con)

最后,我们在主函数中进行每日交易逻辑的编写

# 回测引擎:每日数据处理函数,每天执行一次
def bigquant_run(context, data):
    # 获取今日的日期字符串
    today = data.current_dt.strftime('%Y-%m-%d')  
    # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
    stock_hold_now = {e.symbol: p.amount * p.last_sale_price
                 for e, p in context.portfolio.positions.items()}
    
    try:
        buy_stock = context.daily_stock_buy[today]  # 当日符合买入条件的股票
    except:
        buy_stock=[]  # 如果没有符合条件的股票,就设置为空
    
    try:
        sell_stock = context.daily_stock_sell[today]  # 当日符合卖出条件的股票
    except:
        sell_stock=[] # 如果没有符合条件的股票,就设置为空
    
    # 需要卖出的股票:已有持仓中符合卖出条件的股票
    stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]
    
    # 如果有卖出信号
    if len(stock_to_sell)>0:
        for instrument in stock_to_sell:
            # 将标的转化为equity格式以便下单操作
            sid = context.symbol(instrument) 
            # 获取股票持仓
            cur_position = context.portfolio.positions[sid].amount
            # 如果有股票持仓并且处于可交易状态,则下单卖出
            if cur_position > 0 and data.can_trade(sid):
                # 通过order_target_percent下单至指定仓位比例(通常0到1之间)
                context.order_target_percent(sid, 0) 
   
    # 如果有买入信号/需要调整持仓
    if len(buy_stock )>0:
        # 每只股票的比重为等资金比例持有
        weight = 1/len(buy_stock )
        for instrument in buy_stock :
            # 将标的转化为equity格式以便下单操作
            sid = context.symbol(instrument)
            if  data.can_trade(sid):
                context.order_target_percent(sid, weight) # 买入
克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-50:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-57:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-50:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-102:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-102:options_data","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-57:input_data","SourceOutputPortId":"-50:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-57:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nbuy_condition=where(mean(close_0,5)>mean(close_0,50),1,0)\nsell_condition=where(mean(close_0,5)<mean(close_0,50),1,0)","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":false,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2012-05-29","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2017-11-08","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600519.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":"-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},{"Id":"-50","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","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":"-50"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-50"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-50","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-57","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-57"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-57"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-57","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-102","ModuleId":"BigQuantSpace.trade.trade-v4","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 # 获取今日的日期字符串\n today = data.current_dt.strftime('%Y-%m-%d') \n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n \n try:\n buy_stock = context.daily_stock_buy[today] # 当日符合买入条件的股票\n except:\n buy_stock=[] # 如果没有符合条件的股票,就设置为空\n \n try:\n sell_stock = context.daily_stock_sell[today] # 当日符合卖出条件的股票\n except:\n sell_stock=[] # 如果没有符合条件的股票,就设置为空\n \n # 需要卖出的股票:已有持仓中符合卖出条件的股票\n stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]\n \n # 如果有卖出信号\n if len(stock_to_sell)>0:\n for instrument in stock_to_sell:\n # 将标的转化为equity格式以便下单操作\n sid = context.symbol(instrument) \n # 获取股票持仓\n cur_position = context.portfolio.positions[sid].amount\n # 如果有股票持仓并且处于可交易状态,则下单卖出\n if cur_position > 0 and data.can_trade(sid):\n # 通过order_target_percent下单至指定仓位比例(通常0到1之间)\n context.order_target_percent(sid, 0) # 卖出操作\n \n # 如果有买入信号/需要调整持仓\n if len(buy_stock )>0:\n # 每只股票的比重为等资金比例持有\n weight = 1/len(buy_stock )\n for instrument in buy_stock :\n # 将标的转化为equity格式以便下单操作\n sid = context.symbol(instrument)\n if data.can_trade(sid):\n context.order_target_percent(sid, weight) # 调仓至指定的比例","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n df = context.options['data'].read_df()\n\n # 函数:求满足开仓条件的股票列表\n def open_pos_con(df):\n return list(df[df['buy_condition']>0].instrument)\n\n # 函数:求满足平仓条件的股票列表\n def close_pos_con(df):\n return list(df[df['sell_condition']>0].instrument)\n\n # 每日买入股票的数据框\n context.daily_stock_buy= df.groupby('date').apply(open_pos_con)\n # 每日卖出股票的数据框\n context.daily_stock_sell= df.groupby('date').apply(close_pos_con)","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":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"1000000","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":"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":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-102"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-102","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"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,20,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1073,124,200,200'/><NodePosition Node='-86' Position='1078,418,200,200'/><NodePosition Node='-50' Position='1078,232,200,200'/><NodePosition Node='-57' Position='1076,327,200,200'/><NodePosition Node='-102' Position='1048,531,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 [1]:
    # 本代码由可视化策略环境自动生成 2019年3月4日 14:46
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m3_handle_data_bigquant_run(context, data):
        # 获取今日的日期字符串
        today = data.current_dt.strftime('%Y-%m-%d')  
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
        stock_hold_now = {e.symbol: p.amount * p.last_sale_price
                     for e, p in context.portfolio.positions.items()}
        
        try:
            buy_stock = context.daily_stock_buy[today]  # 当日符合买入条件的股票
        except:
            buy_stock=[]  # 如果没有符合条件的股票,就设置为空
        
        try:
            sell_stock = context.daily_stock_sell[today]  # 当日符合卖出条件的股票
        except:
            sell_stock=[] # 如果没有符合条件的股票,就设置为空
        
        # 需要卖出的股票:已有持仓中符合卖出条件的股票
        stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]
        
        # 如果有卖出信号
        if len(stock_to_sell)>0:
            for instrument in stock_to_sell:
                # 将标的转化为equity格式以便下单操作
                sid = context.symbol(instrument) 
                # 获取股票持仓
                cur_position = context.portfolio.positions[sid].amount
                # 如果有股票持仓并且处于可交易状态,则下单卖出
                if cur_position > 0 and data.can_trade(sid):
                    # 通过order_target_percent下单至指定仓位比例(通常0到1之间)
                    context.order_target_percent(sid, 0) # 卖出操作
       
        # 如果有买入信号/需要调整持仓
        if len(buy_stock )>0:
            # 每只股票的比重为等资金比例持有
            weight = 1/len(buy_stock )
            for instrument in buy_stock :
                # 将标的转化为equity格式以便下单操作
                sid = context.symbol(instrument)
                if  data.can_trade(sid):
                    context.order_target_percent(sid, weight) # 调仓至指定的比例
    # 回测引擎:准备数据,只执行一次
    def m3_prepare_bigquant_run(context):
        # 加载预测数据
        df = context.options['data'].read_df()
    
        # 函数:求满足开仓条件的股票列表
        def open_pos_con(df):
            return list(df[df['buy_condition']>0].instrument)
    
        # 函数:求满足平仓条件的股票列表
        def close_pos_con(df):
            return list(df[df['sell_condition']>0].instrument)
    
        # 每日买入股票的数据框
        context.daily_stock_buy= df.groupby('date').apply(open_pos_con)
        # 每日卖出股票的数据框
        context.daily_stock_sell= df.groupby('date').apply(close_pos_con)
    # 回测引擎:初始化函数,只执行一次
    def m3_initialize_bigquant_run(context):
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    
    
    m1 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    buy_condition=where(mean(close_0,5)>mean(close_0,50),1,0)
    sell_condition=where(mean(close_0,5)<mean(close_0,50),1,0)""",
        m_cached=False
    )
    
    m2 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2012-05-29'),
        end_date=T.live_run_param('trading_date', '2017-11-08'),
        market='CN_STOCK_A',
        instrument_list='600519.SHA',
        max_count=0
    )
    
    m7 = M.general_feature_extractor.v7(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=30
    )
    
    m8 = M.derived_feature_extractor.v3(
        input_data=m7.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False
    )
    
    m6 = M.dropnan.v1(
        input_data=m8.data
    )
    
    m3 = M.trade.v4(
        instruments=m2.data,
        options_data=m6.data,
        start_date='',
        end_date='',
        handle_data=m3_handle_data_bigquant_run,
        prepare=m3_prepare_bigquant_run,
        initialize=m3_initialize_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    • 收益率460.07%
    • 年化收益率38.71%
    • 基准收益率54.82%
    • 阿尔法0.3
    • 贝塔0.32
    • 夏普比率1.41
    • 胜率0.54
    • 盈亏比7.58
    • 收益波动率23.01%
    • 信息比率0.06
    • 最大回撤17.96%

    小结: BigQuant不仅支持AI量化策略,同样也支持传统量化策略,大家可参考上述步骤,自行开发您所需的传统量化策略。


       本文由BigQuant宽客学院推出,版权归BigQuant所有,转载请注明出处。
    


    【宽客学院】策略止盈止损