[关闭]
@Channelchan 2018-10-11T10:03:35.000000Z 字数 9356 阅读 74726

策略组合

Section_5 如何对不同选股方案进行组合(股票多策略)?

读取选股结果

  1. import pandas as pd
  2. strategy1 = pd.read_excel('divert_opt_quantile_5.xlsx').set_index("trade_date")
  3. strategy2 = pd.read_excel('equal_weight_quantile_5.xlsx').set_index("trade_date")
  4. strategy1.head()
000001.SZ 000002.SZ 000008.SZ 000009.SZ 000012.SZ 000024.SZ 000027.SZ 000039.SZ 000046.SZ 000060.SZ ... 601992.SH 601997.SH 601998.SH 603000.SH 603160.SH 603288.SH 603699.SH 603858.SH 603885.SH 603993.SH
trade_date
20140103 1 0 0 0 0 1 0 0 1 0 ... 0 0 1 0 0 0 0 0 0 1
20140106 0 0 0 0 0 1 0 0 1 0 ... 1 0 0 0 0 0 0 0 0 0
20140107 0 1 0 0 1 1 0 0 1 0 ... 0 0 0 0 0 0 0 0 0 0
20140108 0 1 0 0 1 1 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
20140109 0 0 0 0 0 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0

5 rows × 466 columns

为不同的选股方案设置不同的权重偏好,从而控制组合选股结果。以下以等权取交并集为例

  1. import numpy as np
  2. combined_result = 1*strategy1.replace(np.nan,0)+1*strategy2.replace(np.nan,0)

取交集

  1. Intersection = combined_result[combined_result==2].fillna(0).replace(2,1)
  2. Intersection.head()
000001.SZ 000002.SZ 000008.SZ 000009.SZ 000012.SZ 000024.SZ 000027.SZ 000039.SZ 000046.SZ 000060.SZ ... 601992.SH 601997.SH 601998.SH 603000.SH 603160.SH 603288.SH 603699.SH 603858.SH 603885.SH 603993.SH
trade_date
20140103 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20140106 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20140107 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20140108 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20140109 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

5 rows × 466 columns

取并集

  1. Union = combined_result[combined_result>0].fillna(0)
  2. Union[Union>0] = 1
  3. Union.head()
000001.SZ 000002.SZ 000008.SZ 000009.SZ 000012.SZ 000024.SZ 000027.SZ 000039.SZ 000046.SZ 000060.SZ ... 601992.SH 601997.SH 601998.SH 603000.SH 603160.SH 603288.SH 603699.SH 603858.SH 603885.SH 603993.SH
trade_date
20140103 1.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20140106 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20140107 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20140108 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20140109 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

5 rows × 466 columns

测试策略组合效果

  1. from jaqs_fxdayu.data import DataView
  2. import warnings
  3. import numpy as np
  4. warnings.filterwarnings("ignore")
  5. dataview_folder = '../Factor'
  6. dv = DataView()
  7. dv.load_dataview(dataview_folder)
  8. def mask_index_member():
  9. df_index_member = dv.get_ts('index_member')
  10. mask_index_member = ~(df_index_member >0) #定义信号过滤条件-非指数成分
  11. return mask_index_member
  12. def limit_up_down():
  13. # 定义可买卖条件——未停牌、未涨跌停
  14. trade_status = dv.get_ts('trade_status')
  15. mask_sus = trade_status == 0
  16. # 涨停
  17. dv.add_formula('up_limit', '(close - Delay(close, 1)) / Delay(close, 1) > 0.095', is_quarterly=False, add_data=True)
  18. # 跌停
  19. dv.add_formula('down_limit', '(close - Delay(close, 1)) / Delay(close, 1) < -0.095', is_quarterly=False, add_data=True)
  20. can_enter = np.logical_and(dv.get_ts('up_limit') < 1, ~mask_sus) # 未涨停未停牌
  21. can_exit = np.logical_and(dv.get_ts('down_limit') < 1, ~mask_sus) # 未跌停未停牌
  22. return can_enter,can_exit
  23. mask = mask_index_member()
  24. can_enter,can_exit = limit_up_down()
Dataview loaded successfully.
  1. dv.append_df(field_name="Union",df=Union,is_quarterly=False)
  2. dv.append_df(field_name="Intersection",df=Intersection,is_quarterly=False)
  1. from jaqs_fxdayu.research import SignalDigger
  2. import matplotlib.pyplot as plt
  3. from jaqs_fxdayu.research.signaldigger.analysis import analysis
  4. obj = SignalDigger(output_folder='./output',
  5. output_format='pdf')
  6. def draw_analysis(signal="Union",benchmark_price=None):
  7. obj.process_signal_before_analysis(signal=dv.get_ts(signal),
  8. price=dv.get_ts("close_adj"),
  9. high=dv.get_ts("high_adj"), # 可为空
  10. low=dv.get_ts("low_adj"),# 可为空
  11. n_quantiles=1,# quantile分类数
  12. mask=mask,# 过滤条件
  13. can_enter=can_enter,# 是否能进场
  14. can_exit=can_exit,# 是否能出场
  15. period=30,# 持有期
  16. benchmark_price=benchmark_price, # 基准价格 可不传入,持有期收益(return)计算为绝对收益
  17. commission = 0.0008,
  18. )
  19. print(analysis(obj.signal_data,is_event=True,period=30))
  20. obj.create_full_report()
  21. plt.show()
  1. # 并集绩效 相对收益
  2. draw_analysis('Union',dv.data_benchmark)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 56%
{'ret':                long_ret  long_short_ret  all_sample_ret
t-stat        25.043401       10.539699       -2.652237
p-value        0.000000        0.000000        0.008000
skewness       1.888463        0.589888        1.495793
kurtosis      12.693454        4.120623        9.228879
Ann. Ret       0.079830        0.055447       -0.004995
Ann. Vol       0.296356        0.056910        0.341326
Ann. IR        0.269372        0.974292       -0.014635
occurance  69724.000000      945.000000   264923.000000, 'space':                  long_space  all_sample_space
Up_sp Mean         0.127073          0.130108
Up_sp Std          0.138914          0.143171
Up_sp IR           0.914762          0.908764
Up_sp Pct5         0.004752          0.004664
Up_sp Pct25        0.037918          0.038332
Up_sp Pct50        0.087050          0.088657
Up_sp Pct75        0.167585          0.172473
Up_sp Pct95        0.386359          0.395787
Up_sp Occur    69724.000000     264923.000000
Down_sp Mean      -0.120225         -0.152314
Down_sp Std        0.221141          0.253327
Down_sp IR        -0.543658         -0.601254
Down_sp Pct5      -1.000800         -1.000800
Down_sp Pct25     -0.103364         -0.133069
Down_sp Pct50     -0.049375         -0.063733
Down_sp Pct75     -0.021008         -0.027531
Down_sp Pct95     -0.004117         -0.005335
Down_sp Occur  69724.000000     264923.000000}


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.263186  0.440363  264923    100.0
Figure saved: E:\2018_Course\HighSchool\Final\5_因子研发工具实操Richard\output\returns_report.pdf
Information Analysis
                 ic
IC Mean       0.080
IC Std.       0.145
t-stat(IC)   16.884
p-value(IC)   0.000
IC Skew      -0.199
IC Kurtosis  -0.626
Ann. IR       0.549
Figure saved: E:\2018_Course\HighSchool\Final\5_因子研发工具实操Richard\output\information_report.pdf


<matplotlib.figure.Figure at 0x2daadb9da20>

output_13_2.png-242kB

output_13_3.png-168.5kB

  1. # 并集绩效 绝对收益
  2. draw_analysis('Union',None)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 56%
{'ret':                long_ret  long_short_ret  all_sample_ret
t-stat        64.362194       10.539699       79.707240
p-value        0.000000        0.000000        0.000000
skewness       1.503716        0.589888        1.187669
kurtosis       8.716077        4.120623        6.949213
Ann. Ret       0.286846        0.055447        0.198000
Ann. Vol       0.414341        0.056910        0.450174
Ann. IR        0.692293        0.974292        0.439831
occurance  69724.000000      945.000000   264923.000000, 'space':                  long_space  all_sample_space
Up_sp Mean         0.127073          0.130108
Up_sp Std          0.138914          0.143171
Up_sp IR           0.914762          0.908764
Up_sp Pct5         0.004752          0.004664
Up_sp Pct25        0.037918          0.038332
Up_sp Pct50        0.087050          0.088657
Up_sp Pct75        0.167585          0.172473
Up_sp Pct95        0.386359          0.395787
Up_sp Occur    69724.000000     264923.000000
Down_sp Mean      -0.120225         -0.152314
Down_sp Std        0.221141          0.253327
Down_sp IR        -0.543658         -0.601254
Down_sp Pct5      -1.000800         -1.000800
Down_sp Pct25     -0.103364         -0.133069
Down_sp Pct50     -0.049375         -0.063733
Down_sp Pct75     -0.021008         -0.027531
Down_sp Pct95     -0.004117         -0.005335
Down_sp Occur  69724.000000     264923.000000}


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.263186  0.440363  264923    100.0
Figure saved: E:\2018_Course\HighSchool\Final\5_因子研发工具实操Richard\output\returns_report.pdf
Information Analysis
                 ic
IC Mean       0.080
IC Std.       0.145
t-stat(IC)   16.884
p-value(IC)   0.000
IC Skew      -0.199
IC Kurtosis  -0.626
Ann. IR       0.549
Figure saved: E:\2018_Course\HighSchool\Final\5_因子研发工具实操Richard\output\information_report.pdf


<matplotlib.figure.Figure at 0x2dab19ae630>

output_14_2.png-248.1kB
output_14_3.png-168.5kB

  1. # 交集绩效 相对收益
  2. draw_analysis('Intersection',dv.data_benchmark)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 56%
{'ret':                long_ret  long_short_ret  all_sample_ret
t-stat        14.201482        9.433016       -2.652237
p-value        0.000000        0.000000        0.008000
skewness       1.822887        1.195934        1.495793
kurtosis      11.829903        4.223381        9.228879
Ann. Ret       0.105866        0.068869       -0.004995
Ann. Vol       0.271663        0.078937        0.341326
Ann. IR        0.389695        0.872453       -0.014635
occurance  10714.000000      944.000000   264923.000000, 'space':                  long_space  all_sample_space
Up_sp Mean         0.114795          0.130108
Up_sp Std          0.124444          0.143171
Up_sp IR           0.922460          0.908764
Up_sp Pct5         0.004971          0.004664
Up_sp Pct25        0.036105          0.038332
Up_sp Pct50        0.077915          0.088657
Up_sp Pct75        0.150222          0.172473
Up_sp Pct95        0.353808          0.395787
Up_sp Occur    10714.000000     264923.000000
Down_sp Mean      -0.105363         -0.152314
Down_sp Std        0.212464          0.253327
Down_sp IR        -0.495909         -0.601254
Down_sp Pct5      -0.390329         -1.000800
Down_sp Pct25     -0.086105         -0.133069
Down_sp Pct50     -0.040817         -0.063733
Down_sp Pct75     -0.016932         -0.027531
Down_sp Pct95     -0.003529         -0.005335
Down_sp Occur  10714.000000     264923.000000}


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.040442  0.196994  264923    100.0
Figure saved: E:\2018_Course\HighSchool\Final\5_因子研发工具实操Richard\output\returns_report.pdf
Information Analysis
                 ic
IC Mean       0.039
IC Std.       0.083
t-stat(IC)   14.532
p-value(IC)   0.000
IC Skew       0.114
IC Kurtosis   0.105
Ann. IR       0.473
Figure saved: E:\2018_Course\HighSchool\Final\5_因子研发工具实操Richard\output\information_report.pdf


<matplotlib.figure.Figure at 0x2dab23f67f0>

output_15_2.png-244kB
output_15_3.png-167.5kB

  1. # 交集绩效 绝对收益
  2. draw_analysis('Intersection',None)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 56%
{'ret':                long_ret  long_short_ret  all_sample_ret
t-stat        30.968482        9.433016       79.707240
p-value        0.000000        0.000000        0.000000
skewness       1.699205        1.195934        1.187669
kurtosis       9.505349        4.223381        6.949213
Ann. Ret       0.310223        0.068869        0.198000
Ann. Vol       0.365058        0.078937        0.450174
Ann. IR        0.849790        0.872453        0.439831
occurance  10714.000000      944.000000   264923.000000, 'space':                  long_space  all_sample_space
Up_sp Mean         0.114795          0.130108
Up_sp Std          0.124444          0.143171
Up_sp IR           0.922460          0.908764
Up_sp Pct5         0.004971          0.004664
Up_sp Pct25        0.036105          0.038332
Up_sp Pct50        0.077915          0.088657
Up_sp Pct75        0.150222          0.172473
Up_sp Pct95        0.353808          0.395787
Up_sp Occur    10714.000000     264923.000000
Down_sp Mean      -0.105363         -0.152314
Down_sp Std        0.212464          0.253327
Down_sp IR        -0.495909         -0.601254
Down_sp Pct5      -0.390329         -1.000800
Down_sp Pct25     -0.086105         -0.133069
Down_sp Pct50     -0.040817         -0.063733
Down_sp Pct75     -0.016932         -0.027531
Down_sp Pct95     -0.003529         -0.005335
Down_sp Occur  10714.000000     264923.000000}


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.040442  0.196994  264923    100.0
Figure saved: E:\2018_Course\HighSchool\Final\5_因子研发工具实操Richard\output\returns_report.pdf
Information Analysis
                 ic
IC Mean       0.039
IC Std.       0.083
t-stat(IC)   14.532
p-value(IC)   0.000
IC Skew       0.114
IC Kurtosis   0.105
Ann. IR       0.473
Figure saved: E:\2018_Course\HighSchool\Final\5_因子研发工具实操Richard\output\information_report.pdf


<matplotlib.figure.Figure at 0x2dab1b21898>

output_16_2.png-246.5kB
output_16_3.png-167.5kB

  1. Intersection.to_excel('./Intersection.xlsx')
  2. Union.to_excel('./Union.xlsx')
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注