atr出错


(luckychan) #1
# 标注数据为:经过平均真实波幅调整后的收益率 
label_expr = ['return / ATR * 20', 'where(label > {0}, {0}, where(label < -{0}, -{0}, label)) + {0}'.format(15)]   


# 标注函数
m = M.fast_auto_labeler.v6(
    # instrument表示股票池  start_date和end_date决定了股票时间段
    instruments=['000001.SZA', '600519.SHA'], start_date='2017-01-01', end_date='2017-02-01',
    # label_expr表示标注过程   hold_days为持仓时间
    label_expr=label_expr, hold_days=5,
    # 如果涉及相对收益率,还要传入比较基准  sell_at和buy_at表示以什么价格计算持仓期间的收益率
    benchmark='000300.SHA', sell_at='close', buy_at='open')

克隆的代码,想测试一下自定义标注,但出现上图的错误,请问是ATR没定义还是什么原因。怎样解决。


(iQuant) #2
  1. atr要小写
  2. M.fast_auto_labeler.v7采取的是v7版
克隆策略
In [6]:
label_expr = [
    # 将百分比收益乘以100
    'return / atr',  # atr是小写
    # where 将分数限定在[-20, 20]区间,+20将分数调整到 [0, 40] 区间,如果想改变标注区间,修改具体数值即可
    'where(label > {0}, {0}, where(label < -{0}, -{0}, label)) + {0}'.format(20)
]

# 标注函数
m = M.fast_auto_labeler.v7(  # 这里是版本V7
    # instrument表示股票池  start_date和end_date决定了股票时间段
    instruments=['000001.SZA', '600519.SHA'], start_date='2017-01-01', end_date='2017-02-01',
    # label_expr表示标注过程   hold_days为持仓时间
    label_expr=label_expr, hold_days=5,
    # 如果涉及相对收益率,还要传入比较基准  sell_at和buy_at表示以什么价格计算持仓期间的收益率
    benchmark='000300.SHA', sell_at='open', buy_at='open')
[2018-02-01 20:30:56.712272] INFO: bigquant: fast_auto_labeler.v7 开始运行..
[2018-02-01 20:30:56.718844] INFO: bigquant: 命中缓存
[2018-02-01 20:30:56.727847] INFO: bigquant: fast_auto_labeler.v7 运行完成[0.015568s].

(jove) #3

标注数据为:经过平均真实波幅调整后的收益率

label_expr = [‘return / atr * 20’, ‘where(label > {0}, {0}, where(label < -{0}, -{0}, label)) + {0}’.format(15)]
holding_days =5

这里的atr是不是和收益率一样,和holding_days联系,表示未来5天的收益率和未来5天的atr ?
这里的atr是个绝对差值还是百分数或者小数?

示范例子中return / atr * 20,这个20的数字一般怎么得到,凭经验还是有一定的规律?

这里的"return"因子在因子库和表达式引擎文档里都没有找到描述,在什么文档里能找到相关注释?
因子库里只有“return_0”这样的因子,其它因子是否都可以像return和holding_days这样构建未来n天的值(我指的是在标注模块里使用)?
比如,volume,holding_days =5表示未来5天的volume.


(allan001) #4

可视化m2模块,这种表达报错!!

"# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
shift(close, -5) / shift(open, -1) / shift(ta_atr_0, -5)

修改为这样,还报错!
shift(close, -5) / shift(open, -1) / shift(ta_atr, -5)

怎么正确写这个柿子?


(小Q) #5

@allan001 atr的计算可以直接使用表达式引擎单独计算。

 mean(max(high-low,high-shift(close,1),shift(close,1)-low),14) 
克隆策略

    {"Description":"实验创建于2018/2/5","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-13:instruments","SourceOutputPortId":"-5:data"}],"ModuleNodes":[{"Id":"-5","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000333.SZA\n600000.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":"-5"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-13","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/data_history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/big_expr.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -5) / shift(open, -1)/shift(mean(max(high-low,high-shift(close,1),shift(close,1)-low),14),5) # 计算atr用到的长度为14,5日前的atr值。\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na_label","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"cast_label_int","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-13"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-13","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"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='-5' Position='244,106,200,200'/><NodePosition Node='-13' Position='258,229,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 [3]:
    # 本代码由可视化策略环境自动生成 2018年2月5日 10:18
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date='2017-01-01',
        end_date='2018-01-01',
        market='CN_STOCK_A',
        instrument_list="""000333.SZA
    600000.SHA""",
        max_count=0
    )
    
    m2 = M.advanced_auto_labeler.v2(
        instruments=m1.data,
        label_expr="""# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 https://bigquant.com/docs/data_history_data.html
    #   添加benchmark_前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/big_expr.html>`_
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(close, -5) / shift(open, -1)/shift(mean(max(high-low,high-shift(close,1),shift(close,1)-low),14),5) # 计算atr用到的长度为14,5日前的atr值。
    
    # 极值处理:用1%和99%分位的值做clip
    clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    all_wbins(label, 20)
    
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=True,
        user_functions={}
    )
    
    [2018-02-05 10:20:15.323746] INFO: bigquant: instruments.v2 开始运行..
    [2018-02-05 10:20:15.327021] INFO: bigquant: 命中缓存
    [2018-02-05 10:20:15.328643] INFO: bigquant: instruments.v2 运行完成[0.004945s].
    [2018-02-05 10:20:15.339165] INFO: bigquant: advanced_auto_labeler.v2 开始运行..
    [2018-02-05 10:20:15.371196] INFO: 自动数据标注: 加载历史数据: 488 行
    [2018-02-05 10:20:15.373032] INFO: 自动数据标注: 开始标注 ..
    [2018-02-05 10:20:15.564418] INFO: bigquant: advanced_auto_labeler.v2 运行完成[0.225237s].
    
    In [5]:
    m2.data.read_df().head()
    
    Out[5]:
    m:high m:low instrument date m:open m:close m:amount label
    0 127.194565 124.689087 000333.SZA 2017-02-06 124.856117 125.231941 896997440.0 8
    1 155.507416 154.395325 600000.SHA 2017-02-06 155.229385 154.395325 225037392.0 15
    2 125.983582 123.352837 000333.SZA 2017-02-07 125.273697 123.812172 965184256.0 9
    3 155.136719 154.117294 600000.SHA 2017-02-07 154.302643 154.487991 246037888.0 15
    4 125.566002 123.519867 000333.SZA 2017-02-08 123.812172 124.772606 763569856.0 9