可视化事件驱动策略

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

(iQuant) #1
克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"}],"ModuleNodes":[{"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":"2018-05-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","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":1,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"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 date = data.current_dt.strftime('%Y-%m-%d') # 日期\n \n # 目前仓位里面的股票列表\n equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}\n \n for k in equities.keys():\n # 如果持仓时间大于40天\n if context.trading_day_index - context.hold_days[k] >= context.hold_periods \\\n and data.can_trade(context.symbol(k)):\n # 卖完\n context.order_target_percent(context.symbol(k),0)\n \n # 还允许建仓的股票数目\n stock_can_buy_num = context.stock_max_num - len(equities)\n # 获取当日买入股票的代码\n stock_to_buy = context.daily_buy_stock[date][:stock_can_buy_num]\n\n # 等权重买入 \n weight = 1 / context.stock_max_num\n \n # 买入\n for stock in stock_to_buy:\n if data.can_trade(context.symbol(stock)):\n context.order_target_percent(context.symbol(stock), weight)\n # 记录建仓时间的日期索引\n context.hold_days[stock] = context.trading_day_index ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n instruments = context.instruments\n start_date = context.start_date\n end_date = context.end_date\n # 获取数据\n data = D.financial_statements(instruments, start_date, end_date, \n fields=['instrument','fs_publish_date','fs_quarter_year',\n 'fs_quarter_index','fs_net_profit_yoy'])\n # 选择净利润同比增长率大于30%的股票\n selected = data[data['fs_net_profit_yoy'] > 30] \n # 获取交易日历\n date = D.trading_days(start_date=start_date,end_date=end_date)\n # 将日期型格式转化为字符型格式\n date = date['date'].apply(lambda x : x.strftime('%Y-%m-%d'))\n # 为尽量接近实盘,事件日期应为财报公布日的前一天\n publish_date = date.shift(-1)\n shift = dict(zip(date, publish_date))\n\n # 建立事件表\n event = {}\n for dt in date:\n if type(shift[dt]) is str:\n event[dt] = list(selected[selected['fs_publish_date'] == shift[dt]].sort_values(\n 'fs_net_profit_yoy',ascending=False ).instrument)\n else:\n event[dt] = [] \n context.event = event ","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 context.daily_buy_stock = context.event\n context.hold_periods = 40 # 持有40天,固定持仓期\n context.stock_max_num = 50 # 最大持仓数量为50只\n context.hold_days = {}","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":"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":2,"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-62' Position='1074,124,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-81' Position='1055.273193359375,304.726806640625,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 [10]:
    # 本代码由可视化策略环境自动生成 2018年6月13日 11:50
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2015-01-01'),
        end_date=T.live_run_param('trading_date', '2018-05-01'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m2_handle_data_bigquant_run(context, data):
        date = data.current_dt.strftime('%Y-%m-%d') # 日期
        
        # 目前仓位里面的股票列表
        equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
        
        for k in equities.keys():
            # 如果持仓时间大于40天
            if context.trading_day_index - context.hold_days[k] >= context.hold_periods \
                   and data.can_trade(context.symbol(k)):
                # 卖完
                context.order_target_percent(context.symbol(k),0)
       
        # 还允许建仓的股票数目
        stock_can_buy_num = context.stock_max_num - len(equities)
        # 获取当日买入股票的代码
        stock_to_buy = context.daily_buy_stock[date][:stock_can_buy_num]
    
        # 等权重买入 
        weight =  1 / context.stock_max_num
        
        # 买入
        for stock in stock_to_buy:
            if data.can_trade(context.symbol(stock)):
                context.order_target_percent(context.symbol(stock), weight)
                # 记录建仓时间的日期索引
                context.hold_days[stock] = context.trading_day_index  
    # 回测引擎:准备数据,只执行一次
    def m2_prepare_bigquant_run(context):
        instruments = context.instruments
        start_date = context.start_date
        end_date = context.end_date
        # 获取数据
        data = D.financial_statements(instruments, start_date, end_date, 
              fields=['instrument','fs_publish_date','fs_quarter_year',
              'fs_quarter_index','fs_net_profit_yoy'])
        # 选择净利润同比增长率大于30%的股票
        selected = data[data['fs_net_profit_yoy'] > 30] 
        # 获取交易日历
        date = D.trading_days(start_date=start_date,end_date=end_date)
        # 将日期型格式转化为字符型格式
        date = date['date'].apply(lambda x : x.strftime('%Y-%m-%d'))
        # 为尽量接近实盘,事件日期应为财报公布日的前一天
        publish_date = date.shift(-1)
        shift = dict(zip(date, publish_date))
    
        # 建立事件表
        event = {}
        for dt in date:
            if type(shift[dt]) is str:
                event[dt] = list(selected[selected['fs_publish_date'] == shift[dt]].sort_values(
                    'fs_net_profit_yoy',ascending=False ).instrument)
            else:
                event[dt] = []    
        context.event = event 
    # 回测引擎:初始化函数,只执行一次
    def m2_initialize_bigquant_run(context):   
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) 
        context.daily_buy_stock = context.event
        context.hold_periods = 40 # 持有40天,固定持仓期
        context.stock_max_num = 50 # 最大持仓数量为50只
        context.hold_days = {}
    m2 = M.trade.v3(
        instruments=m1.data,
        start_date='',
        end_date='',
        handle_data=m2_handle_data_bigquant_run,
        prepare=m2_prepare_bigquant_run,
        initialize=m2_initialize_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        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 16:06:10.521319] INFO: bigquant: instruments.v2 开始运行..
    [2018-06-01 16:06:10.527321] INFO: bigquant: 命中缓存
    [2018-06-01 16:06:10.531073] INFO: bigquant: instruments.v2 运行完成[0.009765s].
    [2018-06-01 16:06:10.576238] INFO: bigquant: backtest.v7 开始运行..
    [2018-06-01 16:06:10.580655] INFO: bigquant: 命中缓存
    
    • 收益率59.95%
    • 年化收益率15.76%
    • 基准收益率6.32%
    • 阿尔法0.13
    • 贝塔0.56
    • 夏普比率0.44
    • 胜率0.481
    • 盈亏比1.608
    • 收益波动率25.7%
    • 信息比率0.58
    • 最大回撤35.29%
    [2018-06-01 16:06:13.822818] INFO: bigquant: backtest.v7 运行完成[3.246529s].
    

    传统策略的可视化开发