如何根据未来一段时间收益率与最大回撤对股票进行标注

策略分享
标签: #<Tag:0x00007fcf6b1872b0>

(iQuant) #1
克隆策略
In [1]:
class conf:
    start_date = '2015-01-01'
    end_date = '2017-01-01'
    instruments = D.instruments(start_date=start_date,end_date=end_date)[:2]  # 便于运算,拿两只股票举例

    label_expr = [
    
    # 标注逻辑  区间收益率/区间最大回撤
    # 分子为未来五天的收益率,分母为未来5天的最大回撤。最大回撤式子为:最后一天收盘价/期间内最高价
    '(shift(close, -5)/close - 1) / (shift(close, -5) / shift(ts_max(close, 5), -5))',   # 分子,之所以是-5,是因为要把数据向前漂移
    # 极值处理:用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)',
     
           ]

m1 = M.advanced_auto_labeler.v1(
    instruments=conf.instruments, start_date=conf.start_date, end_date=conf.end_date,
    label_expr=conf.label_expr, benchmark='000300.SHA',cast_label_int=False)
[2018-01-19 21:12:03.631151] INFO: bigquant: advanced_auto_labeler.v1 开始运行..
[2018-01-19 21:12:03.761612] INFO: advanced_auto_labeler: load history data: 846 rows
[2018-01-19 21:12:03.762877] INFO: advanced_auto_labeler: start labeling ..
[2018-01-19 21:12:03.929902] INFO: bigquant: advanced_auto_labeler.v1 运行完成[0.298736s].
In [2]:
# 标注结果
m1.data.read_df()
Out[2]:
m:high m:amount instrument date m:close m:low label
0 1156.754150 4.565388e+09 000001.SZA 2015-01-05 1138.280273 1108.437622 2.0
1 1885.197021 9.700711e+09 000002.SZA 2015-01-05 1838.344482 1753.270264 0.0
2 1164.570068 3.453446e+09 000001.SZA 2015-01-06 1121.227295 1104.885010 2.0
3 1848.208252 4.839616e+09 000002.SZA 2015-01-06 1770.531738 1732.309937 1.0
4 1124.780029 2.634796e+09 000001.SZA 2015-01-07 1099.911255 1087.121582 4.0
5 1787.793091 3.772151e+09 000002.SZA 2015-01-07 1754.503174 1726.145142 2.0
6 1106.306030 2.128003e+09 000001.SZA 2015-01-08 1062.963257 1058.700073 8.0
7 1771.764648 3.629554e+09 000002.SZA 2015-01-08 1675.593750 1659.565186 7.0
8 1127.622192 3.835378e+09 000001.SZA 2015-01-09 1071.489746 1045.199829 7.0
9 1753.270264 4.521978e+09 000002.SZA 2015-01-09 1658.332275 1638.604858 7.0
10 1069.358154 2.293105e+09 000001.SZA 2015-01-12 1049.463135 1030.278564 3.0
11 1642.303711 3.180963e+09 000002.SZA 2015-01-12 1617.644531 1572.025024 2.0
12 1058.700073 1.204987e+09 000001.SZA 2015-01-13 1043.068237 1038.094482 3.0
13 1649.701538 2.187988e+09 000002.SZA 2015-01-13 1611.479736 1599.150146 3.0
14 1080.016235 1.889297e+09 000001.SZA 2015-01-14 1052.305298 1044.489380 5.0
15 1639.837891 2.160268e+09 000002.SZA 2015-01-14 1615.178589 1586.820557 6.0
16 1090.674194 1.868796e+09 000001.SZA 2015-01-15 1090.674194 1045.199829 2.0
17 1705.184814 3.265536e+09 000002.SZA 2015-01-15 1697.786987 1602.848999 6.0
18 1109.858765 2.403346e+09 000001.SZA 2015-01-16 1092.095337 1078.595093 3.0
19 1697.786987 2.892247e+09 000002.SZA 2015-01-16 1666.963013 1650.934448 8.0
20 1035.252319 3.016203e+09 000001.SZA 2015-01-19 982.672607 982.672607 8.0
21 1590.519409 4.483885e+09 000002.SZA 2015-01-19 1500.513306 1500.513306 13.0
22 999.014954 2.064281e+09 000001.SZA 2015-01-20 982.672607 963.488159 7.0
23 1553.530640 3.604938e+09 000002.SZA 2015-01-20 1522.706543 1486.950684 10.0
24 1037.384033 2.758193e+09 000001.SZA 2015-01-21 1024.594360 976.988342 5.0
25 1627.508179 4.554004e+09 000002.SZA 2015-01-21 1609.013794 1491.882568 6.0
26 1031.699707 1.801436e+09 000001.SZA 2015-01-22 1016.067871 1006.120361 5.0
27 1682.991455 4.326298e+09 000002.SZA 2015-01-22 1674.360718 1592.985352 4.0
28 1039.515625 2.108747e+09 000001.SZA 2015-01-23 1023.173218 1016.067871 5.0
29 1737.241699 4.561753e+09 000002.SZA 2015-01-23 1716.281372 1639.837891 3.0
... ... ... ... ... ... ... ...
804 999.393738 7.232221e+08 000001.SZA 2016-12-05 991.013062 985.775146 7.0
805 3449.688477 1.757025e+09 000002.SZA 2016-12-05 3385.953125 3300.972168 1.0
806 997.298584 5.715852e+08 000001.SZA 2016-12-06 994.155823 989.965515 6.0
807 3403.214844 9.034705e+08 000002.SZA 2016-12-06 3370.019043 3339.479248 1.0
808 994.155823 4.660855e+08 000001.SZA 2016-12-07 993.108215 985.775146 6.0
809 3368.691406 1.230396e+09 000002.SZA 2016-12-07 3338.151367 3210.680176 0.0
810 1000.441345 6.380585e+08 000001.SZA 2016-12-08 997.298584 987.870300 5.0
811 3336.823486 9.442771e+08 000002.SZA 2016-12-08 3285.038330 3279.727051 1.0
812 1021.392944 1.460778e+09 000001.SZA 2016-12-09 1010.917114 993.108215 4.0
813 3342.134766 1.193736e+09 000002.SZA 2016-12-09 3294.333008 3235.908936 1.0
814 1023.488159 1.206663e+09 000001.SZA 2016-12-12 995.203430 988.917908 5.0
815 3290.349609 1.865280e+09 000002.SZA 2016-12-12 3088.520264 3063.291504 1.0
816 995.203430 6.077700e+08 000001.SZA 2016-12-13 986.822754 977.394470 5.0
817 3091.176025 1.098532e+09 000002.SZA 2016-12-13 3061.963867 2902.624756 0.0
818 996.250977 5.647875e+08 000001.SZA 2016-12-14 984.727600 984.727600 5.0
819 3040.718506 9.315382e+08 000002.SZA 2016-12-14 2959.721191 2947.770752 2.0
820 985.775146 7.677518e+08 000001.SZA 2016-12-15 969.013855 964.823486 6.0
821 3003.539551 7.924791e+08 000002.SZA 2016-12-15 2965.032471 2905.280518 2.0
822 973.204163 3.669253e+08 000001.SZA 2016-12-16 969.013855 964.823486 5.0
823 3020.801270 6.607972e+08 000002.SZA 2016-12-16 2982.294189 2937.148193 1.0
824 966.918701 4.538997e+08 000001.SZA 2016-12-19 963.775940 960.633179 6.0
825 2921.214355 1.497453e+09 000002.SZA 2016-12-19 2801.710205 2793.743164 5.0
826 963.775940 5.801181e+08 000001.SZA 2016-12-20 954.347656 951.204956 6.0
827 2788.431885 1.473706e+09 000002.SZA 2016-12-20 2699.467529 2675.566895 9.0
828 959.585571 3.382014e+08 000001.SZA 2016-12-21 959.585571 954.347656 6.0
829 2748.597168 1.085459e+09 000002.SZA 2016-12-21 2719.385010 2682.205811 8.0
830 959.585571 3.115468e+08 000001.SZA 2016-12-22 957.490417 954.347656 6.0
831 2757.891846 1.023482e+09 000002.SZA 2016-12-22 2736.646729 2668.927734 7.0
832 957.490417 3.481404e+08 000001.SZA 2016-12-23 951.204956 950.157349 7.0
833 2744.613770 6.629325e+08 000002.SZA 2016-12-23 2695.484131 2688.844971 7.0

834 rows × 7 columns