复制链接
克隆策略

    {"description":"实验创建于2021/11/25","graph":{"edges":[{"to_node_id":"-16:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-155:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-167:input_data","from_node_id":"-155:data"},{"to_node_id":"-167:features","from_node_id":"-162:data"},{"to_node_id":"-16:options_data","from_node_id":"-167:data"}],"nodes":[{"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.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n context.factor_data = context.options['data'].read_df()","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n factor_data = context.factor_data[\n context.factor_data.date == data.current_dt.strftime('%Y-%m-%d')]\n \n for sid in context.instruments:\n factor = factor_data[factor_data.instrument==sid]\n # 标的为字符串格式\n sid = context.symbol(sid)# 标的为字符串格\n price = data.current(sid, 'price') # 最新价格\n golden_cross = factor.golden_cross.values\n death_cross = factor.death_cross.values\n cur_position = context.portfolio.positions[sid].amount # 持仓数量 \n weight = 1 / len(context.instruments) # 等权重\n\n #交易逻辑\n # 如果短期均线大于长期均线形成金叉,并且没有持仓,并且该股票可以交易\n if golden_cross and cur_position == 0: \n context.order_target_percent(sid, weight) # 买入\n print('{}全仓买入{}基金'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))\n # 如果短期均线小于长期均线形成死叉,并且有持仓,并且该股票可以交易\n elif death_cross and cur_position > 0 : \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","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":"open","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":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2019-09-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2021-09-01","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_FUND","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"159941.ZOF\n515030.HOF","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"cacheable":true,"seq_num":7,"comment":"预测数据,用于回测和模拟","comment_collapsed":true},{"node_id":"-155","module_id":"BigQuantSpace.use_datasource.use_datasource-v2","parameters":[{"name":"datasource_id","value":"bar1d_CN_FUND","type":"Literal","bound_global_parameter":null},{"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":"-155"},{"name":"features","node_id":"-155"}],"output_ports":[{"name":"data","node_id":"-155"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-162","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"golden_cross = ta_macd(close,'golden_cross', fastperiod=12, slowperiod=26, signalperiod=9)\n\ndeath_cross = ta_macd(close,'death_cross', fastperiod=12, slowperiod=26, signalperiod=9)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-162"}],"output_ports":[{"name":"data","node_id":"-162"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-167","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":"False","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":"-167"},{"name":"features","node_id":"-167"}],"output_ports":[{"name":"data","node_id":"-167"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-16' Position='368,230,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='325.911865234375,-93.67277526855469,200,200'/><node_position Node='-155' Position='448.3985595703125,0.2432994842529297,200,200'/><node_position Node='-162' Position='642.330078125,-100.00423049926758,200,200'/><node_position Node='-167' Position='529.4154052734375,83.74541473388672,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [13]:
    # 本代码由可视化策略环境自动生成 2022年10月21日 17:24
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m2_initialize_bigquant_run(context):
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
        context.factor_data = context.options['data'].read_df()
    # 回测引擎:每日数据处理函数,每天执行一次
    def m2_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        factor_data = context.factor_data[
            context.factor_data.date == data.current_dt.strftime('%Y-%m-%d')]
        
        for sid in context.instruments:
            factor = factor_data[factor_data.instrument==sid]
            # 标的为字符串格式
            sid = context.symbol(sid)# 标的为字符串格
            price = data.current(sid, 'price') # 最新价格
            golden_cross = factor.golden_cross.values
            death_cross = factor.death_cross.values
            cur_position =  context.portfolio.positions[sid].amount # 持仓数量 
            weight = 1 / len(context.instruments) # 等权重
    
            #交易逻辑
            # 如果短期均线大于长期均线形成金叉,并且没有持仓,并且该股票可以交易
            if golden_cross and cur_position == 0:  
                context.order_target_percent(sid, weight) # 买入
                print('{}全仓买入{}基金'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))
            # 如果短期均线小于长期均线形成死叉,并且有持仓,并且该股票可以交易
            elif death_cross and cur_position > 0 :  
                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
    
    
    m7 = M.instruments.v2(
        start_date='2019-09-01',
        end_date='2021-09-01',
        market='CN_FUND',
        instrument_list="""159941.ZOF
    515030.HOF""",
        max_count=0
    )
    
    m1 = M.use_datasource.v2(
        instruments=m7.data,
        datasource_id='bar1d_CN_FUND',
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m3 = M.input_features.v1(
        features="""golden_cross = ta_macd(close,'golden_cross', fastperiod=12, slowperiod=26, signalperiod=9)
    
    death_cross = ta_macd(close,'death_cross', fastperiod=12, slowperiod=26, signalperiod=9)"""
    )
    
    m4 = M.derived_feature_extractor.v3(
        input_data=m1.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m2 = M.trade.v4(
        instruments=m7.data,
        options_data=m4.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,
        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=''
    )
    
    2019-09-02全仓买入159941.ZOF基金
    2019-09-20卖出159941.ZOF基金
    2019-10-16全仓买入159941.ZOF基金
    2019-12-05卖出159941.ZOF基金
    2019-12-23全仓买入159941.ZOF基金
    2020-01-08卖出159941.ZOF基金
    2020-01-09全仓买入159941.ZOF基金
    2020-01-23卖出159941.ZOF基金
    2020-02-05全仓买入159941.ZOF基金
    2020-02-21卖出159941.ZOF基金
    2020-03-25全仓买入159941.ZOF基金
    2020-05-14卖出159941.ZOF基金
    2020-05-29全仓买入515030.HOF基金
    2020-06-09全仓买入159941.ZOF基金
    2020-06-12卖出159941.ZOF基金
    2020-06-19全仓买入159941.ZOF基金
    2020-06-29卖出159941.ZOF基金
    2020-07-02全仓买入159941.ZOF基金
    2020-07-16卖出159941.ZOF基金
    2020-07-17卖出515030.HOF基金
    2020-07-21全仓买入159941.ZOF基金
    2020-07-24卖出159941.ZOF基金
    2020-08-04全仓买入159941.ZOF基金
    2020-08-12卖出159941.ZOF基金
    2020-08-14全仓买入159941.ZOF基金
    2020-09-03全仓买入515030.HOF基金
    2020-09-04卖出515030.HOF基金
    2020-09-09卖出159941.ZOF基金
    2020-09-17全仓买入515030.HOF基金
    2020-09-28卖出515030.HOF基金
    2020-09-29全仓买入515030.HOF基金
    2020-10-12全仓买入159941.ZOF基金
    2020-10-21卖出159941.ZOF基金
    2020-11-06全仓买入159941.ZOF基金
    2020-11-12卖出159941.ZOF基金
    2020-11-17卖出515030.HOF基金
    2020-11-24全仓买入159941.ZOF基金
    2020-12-14全仓买入515030.HOF基金
    2021-01-05卖出159941.ZOF基金
    2021-01-13卖出515030.HOF基金
    2021-01-20全仓买入159941.ZOF基金
    2021-02-01卖出159941.ZOF基金
    2021-02-03全仓买入159941.ZOF基金
    2021-02-23卖出159941.ZOF基金
    2021-03-16全仓买入159941.ZOF基金
    2021-03-18全仓买入515030.HOF基金
    2021-03-24卖出515030.HOF基金
    2021-03-26全仓买入515030.HOF基金
    2021-04-23卖出159941.ZOF基金
    2021-05-11卖出515030.HOF基金
    2021-05-17全仓买入515030.HOF基金
    2021-05-26全仓买入159941.ZOF基金
    2021-06-16卖出515030.HOF基金
    2021-06-23全仓买入515030.HOF基金
    2021-07-16卖出159941.ZOF基金
    2021-07-19卖出515030.HOF基金
    2021-07-27全仓买入159941.ZOF基金
    2021-07-28卖出159941.ZOF基金
    2021-08-05全仓买入515030.HOF基金
    2021-08-10卖出515030.HOF基金
    2021-08-25全仓买入159941.ZOF基金
    
    • 收益率63.91%
    • 年化收益率29.14%
    • 基准收益率28.16%
    • 阿尔法0.22
    • 贝塔0.35
    • 夏普比率1.39
    • 胜率0.63
    • 盈亏比2.01
    • 收益波动率17.39%
    • 信息比率0.04
    • 最大回撤10.82%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-96615cd85da44cec977f4017809623a0"}/bigcharts-data-end