复制链接
克隆策略

TALIB指标选股策略

版本 v1.0

目录

  • ### TALIB指标选股策略的交易规则

  • ### 策略构建步骤

  • ### 策略的实现

正文

一、TALIB指标选股策略的交易规则

  • 买入条件:满足 1)今日开盘价大于昨日收盘价;2)5日收盘价均线大于10日收盘价均线的股票按PE升序排名取前十名,次日以开盘价买入;
  • 买入后,如果5日收盘价均线小于10日收盘价均线,则次日以开盘价卖出。

二、策略构建步骤

1、确定股票池和回测时间

  • 通过证券代码列表输入要回测的单只/多只股票,以及回测的起止日期

2、确定买卖条件信号

  • 在输入特征列表m1中通过表达式引擎定义 close_0 因子,并通过基础特征抽取模块m7获取数据;
  • 在自定义模块m10中利用talib库计算macd相关技术指标,通过前移1天macd指标来实现当日收盘下单;
  • 在输入特征列表m11模块中通过表达式引擎定义 buy_condition=where((macd_1<signal_1)&(macd>signal_1),1,0),实现买入信号。
  • 在输入特征列表m11模块中通过表达式引擎定义 sell_condition=where((macd_1>signal_1)&(macd<signal_1),1,0),实现卖出信号。
  • 通过衍生特征抽取模块m8实现买卖条件指标 buy_condition 和 sell_condition 数据的抽取。
  • 通过缺失数据处理模块删去有缺失值的数据。

3、确定买卖原则

  • 已有持仓中满足卖出条件的股票为卖出股票列表,执行卖出操作。
  • 满足买入条件且没有持仓的股票为买入股票列表,需根据可用现金执行等资金买入操作,此例采用整百股数下单。

4、模拟回测

  • 通过 trade 模块中的初始化函数定义交易手续费和滑点;
  • 通过 trade 模块中的准备函数定义 context.daily_stock_buy 和 context.daily_stock_sell 变量来获取并存放每日符合买卖条件的股票列表;
  • 通过 trade 模块中的主函数(handle函数)查看每日的买卖交易信号,按照买卖原则执行相应的买入/卖出操作。

三、策略的实现

可视化策略实现如下:

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-802:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-802:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-819:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"to_node_id":"-858:input_1","from_node_id":"-802:data"},{"to_node_id":"-1348:input_data","from_node_id":"-809:data"},{"to_node_id":"-809:features","from_node_id":"-850:data"},{"to_node_id":"-809:input_data","from_node_id":"-858:data_1"},{"to_node_id":"-819:options_data","from_node_id":"-1348:data"}],"nodes":[{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nclose_0/adjust_factor_0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2016-01-01","type":"Literal","bound_global_parameter":"交易日期"},{"name":"end_date","value":"2016-05-01","type":"Literal","bound_global_parameter":"交易日期"},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"600006.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":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"cacheable":true,"seq_num":2,"comment":"预测数据,用于回测和模拟","comment_collapsed":false},{"node_id":"-802","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":"100","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-802"},{"name":"features","node_id":"-802"}],"output_ports":[{"name":"data","node_id":"-802"}],"cacheable":true,"seq_num":7,"comment":"","comment_collapsed":true},{"node_id":"-809","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":"-809"},{"name":"features","node_id":"-809"}],"output_ports":[{"name":"data","node_id":"-809"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-819","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 # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n","type":"Literal","bound_global_parameter":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.perf_tracker.position_tracker.positions.items()}\n\n # 记录用于买入股票的可用现金\n cash_for_buy = context.portfolio.cash\n \n # 获取当日符合买入/卖出条件的股票列表\n try:\n buy_stock = context.daily_stock_buy[today] # 当日符合买入条件的股票\n except:\n buy_stock=[]\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 stock_to_buy = [i for i in buy_stock if i not in stock_hold_now]\n # 卖出\n for instrument in stock_to_sell:\n # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态\n # 如果返回真值,则可以正常下单,否则会出错\n # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式\n if data.can_trade(context.symbol(instrument)):\n # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,即卖出全部股票,可参考回测文档\n context.order_target_percent(context.symbol(instrument), 0)\n # 由于是收盘卖出且开盘买入,因此买入时无需更新当日可用现金,如果是收盘买入开盘卖出则需更新可用现金\n # cash_for_buy += stock_hold_now[instrument]\n # 如果当天没有买入的股票,就返回\n if len(stock_to_buy) == 0:\n return\n \n # 买入\n for instrument in stock_to_buy:\n # 针对当日可用现金使用等资金比例下单买入,整百股数下单\n cash = cash_for_buy / len(stock_to_buy)\n if data.can_trade(context.symbol(instrument)):\n current_price = data.current(context.symbol(instrument), 'price')\n amount = math.floor(cash / current_price / 100) * 100\n context.order(context.symbol(instrument), amount)","type":"Literal","bound_global_parameter":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)","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"","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":"000300.HIX","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-819"},{"name":"options_data","node_id":"-819"},{"name":"history_ds","node_id":"-819"},{"name":"benchmark_ds","node_id":"-819"},{"name":"trading_calendar","node_id":"-819"}],"output_ports":[{"name":"raw_perf","node_id":"-819"}],"cacheable":false,"seq_num":9,"comment":"","comment_collapsed":true},{"node_id":"-850","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\n\nbuy_condition=where((macd_1<signal_1)&(macd>signal_1),1,0)\nsell_condition=where((macd_1>signal_1)&(macd<signal_1),1,0)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-850"}],"output_ports":[{"name":"data","node_id":"-850"}],"cacheable":true,"seq_num":11,"comment":"","comment_collapsed":true},{"node_id":"-858","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n df = input_1.read_df()\n prices=[float(x) for x in df['close_0']]\n import talib\n macd, signal, hist = talib.MACD(np.array(prices),12,26,9)#使用talib库计算指标值\n df['macd']=pd.Series(macd,index=df.index)\n df['signal']=pd.Series(signal,index=df.index) \n #计算昨日macd和signal\n df['macd_1']=df['macd'].shift(1)\n df['signal_1']=df['signal'].shift(1)\n data_1 = DataSource.write_df(df)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-858"},{"name":"input_2","node_id":"-858"},{"name":"input_3","node_id":"-858"}],"output_ports":[{"name":"data_1","node_id":"-858"},{"name":"data_2","node_id":"-858"},{"name":"data_3","node_id":"-858"}],"cacheable":true,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-1348","module_id":"BigQuantSpace.dropnan.dropnan-v2","parameters":[],"input_ports":[{"name":"input_data","node_id":"-1348"},{"name":"features","node_id":"-1348"}],"output_ports":[{"name":"data","node_id":"-1348"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='1284,69,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='830,69,200,200'/><node_position Node='-802' Position='1074,178,200,200'/><node_position Node='-809' Position='1054,428,200,200'/><node_position Node='-819' Position='894,610,200,200'/><node_position Node='-850' Position='1337,297,200,200'/><node_position Node='-858' Position='1007,300,200,200'/><node_position Node='-1348' Position='1045,516,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [1]:
    # 本代码由可视化策略环境自动生成 2021年12月6日 22:14
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m10_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df = input_1.read_df()
        prices=[float(x) for x in df['close_0']]
        import talib
        macd, signal, hist = talib.MACD(np.array(prices),12,26,9)#使用talib库计算指标值
        df['macd']=pd.Series(macd,index=df.index)
        df['signal']=pd.Series(signal,index=df.index) 
        #计算昨日macd和signal
        df['macd_1']=df['macd'].shift(1)
        df['signal_1']=df['signal'].shift(1)
        data_1 = DataSource.write_df(df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m10_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:初始化函数,只执行一次
    def m9_initialize_bigquant_run(context):
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m9_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.perf_tracker.position_tracker.positions.items()}
    
        # 记录用于买入股票的可用现金
        cash_for_buy = context.portfolio.cash
        
        # 获取当日符合买入/卖出条件的股票列表
        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]
        # 需要买入的股票:没有持仓且符合买入条件的股票
        stock_to_buy = [i for i in buy_stock if i not in stock_hold_now]
        # 卖出
        for instrument in stock_to_sell:
            # 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
            # 如果返回真值,则可以正常下单,否则会出错
            # 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
            if data.can_trade(context.symbol(instrument)):
                # order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,即卖出全部股票,可参考回测文档
                context.order_target_percent(context.symbol(instrument), 0)
                # 由于是收盘卖出且开盘买入,因此买入时无需更新当日可用现金,如果是收盘买入开盘卖出则需更新可用现金
                # cash_for_buy += stock_hold_now[instrument]
        # 如果当天没有买入的股票,就返回
        if len(stock_to_buy) == 0:
            return
        
        # 买入
        for instrument in stock_to_buy:
            # 针对当日可用现金使用等资金比例下单买入,整百股数下单
            cash = cash_for_buy / len(stock_to_buy)
            if data.can_trade(context.symbol(instrument)):
                current_price = data.current(context.symbol(instrument), 'price')
                amount = math.floor(cash / current_price / 100) * 100
                context.order(context.symbol(instrument), amount)
    # 回测引擎:准备数据,只执行一次
    def m9_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)
    
    m1 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    close_0/adjust_factor_0"""
    )
    
    m2 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2016-01-01'),
        end_date=T.live_run_param('trading_date', '2016-05-01'),
        market='CN_STOCK_A',
        instrument_list='600006.SHA',
        max_count=0
    )
    
    m7 = M.general_feature_extractor.v7(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=100
    )
    
    m10 = M.cached.v3(
        input_1=m7.data,
        run=m10_run_bigquant_run,
        post_run=m10_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m11 = M.input_features.v1(
        features="""
    # #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    
    buy_condition=where((macd_1<signal_1)&(macd>signal_1),1,0)
    sell_condition=where((macd_1>signal_1)&(macd<signal_1),1,0)"""
    )
    
    m8 = M.derived_feature_extractor.v3(
        input_data=m10.data_1,
        features=m11.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False
    )
    
    m3 = M.dropnan.v2(
        input_data=m8.data
    )
    
    m9 = M.trade.v4(
        instruments=m2.data,
        options_data=m3.data,
        start_date='',
        end_date='',
        initialize=m9_initialize_bigquant_run,
        handle_data=m9_handle_data_bigquant_run,
        prepare=m9_prepare_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='000300.HIX'
    )
    
    • 收益率0.44%
    • 年化收益率1.41%
    • 基准收益率-15.39%
    • 阿尔法0.09
    • 贝塔0.16
    • 夏普比率0.04
    • 胜率1.0
    • 盈亏比0.0
    • 收益波动率22.59%
    • 信息比率0.09
    • 最大回撤9.87%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-f6e3c20a8dc6453c9e968325aa89cf98"}/bigcharts-data-end