克隆策略

双均线模板策略

版本 v1.0

目录

策略交易规则

策略构建步骤

正文

一、双均线模板策略的交易规则

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

二、策略构建步骤

1、确定股票池和回测时间 通过证券代码列表输入要回测的两只股票,以及回测的起止日期。

2、确定买卖原则

当短期均线上穿长期均线时,形成金叉,此时买入股票。当短期均线下穿长期均线时,形成死叉,此时卖出股票。

3、模拟回测

通过 trade 模块中的初始化函数定义交易手续费。 通过 trade 模块中的主函数(handle函数)形成金叉买入股票;形成死叉卖出股票。并打印交易日志。

    {"description":"实验创建于2021/11/25","graph":{"edges":[{"to_node_id":"-16:instruments","from_node_id":"-4:data"},{"to_node_id":"-41:instruments","from_node_id":"-4:data"},{"to_node_id":"-41:features","from_node_id":"-36:data"},{"to_node_id":"-48:features","from_node_id":"-36:data"},{"to_node_id":"-48:input_data","from_node_id":"-41:data"},{"to_node_id":"-16:options_data","from_node_id":"-48:data"}],"nodes":[{"node_id":"-4","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2017-11-24","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2021-11-24","type":"Literal","bound_global_parameter":null},{"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":"-4"}],"output_ports":[{"name":"data","node_id":"-4"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-16","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 # 加载预测数据\n context.ranker_prediction = context.options['data'].read_df()\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n\n \n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n options_data = context.ranker_prediction[context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]\n # 标的为字符串格式\n sid = context.symbol(context.instruments[0])# 标的为字符串格式\n price = data.current(sid, 'price') # 最新价格\n cash = context.portfolio.cash # 现金\n cur_position = context.portfolio.positions[sid].amount # 持仓\n short_mavg = options_data['mean5'].values # 短期均线值\n long_mavg = options_data['mean50'].values # 长期均线值\n #交易逻辑\n # 如果短期均线大于长期均线形成金叉,并且没有持仓,并且该股票可以交易\n if short_mavg > long_mavg and cur_position == 0 and data.can_trade(sid): \n context.order(sid, int(cash/price/100)*100) # 买入\n print('{}全仓买入{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))\n # 如果短期均线小于长期均线形成死叉,并且有持仓,并且该股票可以交易\n elif short_mavg < long_mavg and cur_position > 0 and data.can_trade(sid): \n context.order_target_percent(sid, 0) # 全部卖出\n print('{}卖出{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))\n \n \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 pass\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":"-16"},{"name":"options_data","node_id":"-16"},{"name":"history_ds","node_id":"-16"},{"name":"benchmark_ds","node_id":"-16"},{"name":"trading_calendar","node_id":"-16"}],"output_ports":[{"name":"raw_perf","node_id":"-16"}],"cacheable":false,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-36","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nmean5=mean(close_0,5)\nmean50=mean(close_0,50)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-36"}],"output_ports":[{"name":"data","node_id":"-36"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-41","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"90","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-41"},{"name":"features","node_id":"-41"}],"output_ports":[{"name":"data","node_id":"-41"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-48","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"True","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"{}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-48"},{"name":"features","node_id":"-48"}],"output_ports":[{"name":"data","node_id":"-48"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-4' Position='298,88,200,200'/><node_position Node='-16' Position='384,391,200,200'/><node_position Node='-36' Position='613,81,200,200'/><node_position Node='-41' Position='598,193,200,200'/><node_position Node='-48' Position='624,269,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [14]:
    # 本代码由可视化策略环境自动生成 2021年11月25日 17:38
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m2_initialize_bigquant_run(context):
        # 加载预测数据
        context.ranker_prediction = context.options['data'].read_df()
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    
       
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m2_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        options_data = context.ranker_prediction[context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
        # 标的为字符串格式
        sid = context.symbol(context.instruments[0])# 标的为字符串格式
        price = data.current(sid, 'price') # 最新价格
        cash = context.portfolio.cash  # 现金
        cur_position = context.portfolio.positions[sid].amount # 持仓
        short_mavg = options_data['mean5'].values  # 短期均线值
        long_mavg = options_data['mean50'].values # 长期均线值
        #交易逻辑
        # 如果短期均线大于长期均线形成金叉,并且没有持仓,并且该股票可以交易
        if short_mavg > long_mavg and cur_position == 0 and data.can_trade(sid):  
            context.order(sid, int(cash/price/100)*100) # 买入
            print('{}全仓买入{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))
        # 如果短期均线小于长期均线形成死叉,并且有持仓,并且该股票可以交易
        elif short_mavg < long_mavg and cur_position > 0 and data.can_trade(sid):  
            context.order_target_percent(sid, 0) # 全部卖出
            print('{}卖出{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))
            
            
    
    # 回测引擎:准备数据,只执行一次
    def m2_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m2_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2017-11-24',
        end_date='2021-11-24',
        market='CN_STOCK_A',
        instrument_list='600519.SHA',
        max_count=0
    )
    
    m3 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    mean5=mean(close_0,5)
    mean50=mean(close_0,50)"""
    )
    
    m4 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m5 = M.derived_feature_extractor.v3(
        input_data=m4.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m2 = M.trade.v4(
        instruments=m1.data,
        options_data=m5.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=''
    )
    
    2017-11-24全仓买入600519.SHA股票
    2018-02-09卖出600519.SHA股票
    2018-02-23全仓买入600519.SHA股票
    2018-03-06卖出600519.SHA股票
    2018-03-20全仓买入600519.SHA股票
    2018-03-23卖出600519.SHA股票
    2018-05-11全仓买入600519.SHA股票
    
    2018-07-03卖出600519.SHA股票
    2018-07-16全仓买入600519.SHA股票
    2018-07-27卖出600519.SHA股票
    2018-09-25全仓买入600519.SHA股票
    2018-10-12卖出600519.SHA股票
    2018-12-18全仓买入600519.SHA股票
    2018-12-20卖出600519.SHA股票
    2019-01-03全仓买入600519.SHA股票
    2019-05-28卖出600519.SHA股票
    2019-05-30全仓买入600519.SHA股票
    2019-06-05卖出600519.SHA股票
    2019-06-17全仓买入600519.SHA股票
    
    2019-11-29卖出600519.SHA股票
    2020-03-05全仓买入600519.SHA股票
    2020-03-18卖出600519.SHA股票
    2020-04-01全仓买入600519.SHA股票
    
    2020-09-22卖出600519.SHA股票
    2020-10-13全仓买入600519.SHA股票
    2020-10-27卖出600519.SHA股票
    2020-11-09全仓买入600519.SHA股票
    2021-03-04卖出600519.SHA股票
    2021-05-19全仓买入600519.SHA股票
    2021-06-23卖出600519.SHA股票
    2021-09-28全仓买入600519.SHA股票
    
    • 收益率128.82%
    • 年化收益率23.94%
    • 基准收益率19.85%
    • 阿尔法0.21
    • 贝塔0.62
    • 夏普比率0.87
    • 胜率0.33
    • 盈亏比5.64
    • 收益波动率24.84%
    • 信息比率0.05
    • 最大回撤31.96%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-f612dc24747d431a970221227070cb62"}/bigcharts-data-end