[关闭]
@Channelchan 2018-04-18T16:34:41.000000Z 字数 9186 阅读 51226

策略组合

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
20140106 0 0 0 0 0 1 0 0 1 0 ... 1 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
20140108 0 1 0 0 1 1 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

5 rows × 456 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
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
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
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
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

5 rows × 456 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
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
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
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
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

5 rows × 456 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 == u'停牌'
  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: 59%
{'ret':                long_ret  long_short_ret  all_sample_ret
t-stat        29.505789        9.897261        7.570006
p-value        0.000000        0.000000        0.000000
skewness       1.933317        0.555441        1.563058
kurtosis      13.585921        3.734487       10.061591
Ann. Ret       0.094616        0.052518        0.014263
Ann. Vol       0.293989        0.056483        0.336234
Ann. IR        0.321837        0.929799        0.042421
occurance  67802.000000      915.000000   256877.000000, 'space':                  long_space  all_sample_space
Up_sp Mean         0.126223          0.128407
Up_sp Std          0.140752          0.144886
Up_sp IR           0.896775          0.886264
Up_sp Pct5         0.000000          0.000000
Up_sp Pct25        0.035608          0.035329
Up_sp Pct50        0.086255          0.086880
Up_sp Pct75        0.167223          0.171021
Up_sp Pct95        0.390235          0.398506
Up_sp Occur    67802.000000     256877.000000
Down_sp Mean      -0.071297         -0.086193
Down_sp Std        0.083802          0.097463
Down_sp IR        -0.850770         -0.884362
Down_sp Pct5      -0.256119         -0.302894
Down_sp Pct25     -0.090581         -0.110774
Down_sp Pct50     -0.044073         -0.054994
Down_sp Pct75     -0.017183         -0.022265
Down_sp Pct95     -0.000542         -0.000447
Down_sp Occur  67802.000000     256877.000000}


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.263947  0.440772  256877    100.0
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/output/returns_report.pdf
Information Analysis
                 ic
IC Mean       0.077
IC Std.       0.147
t-stat(IC)   15.699
p-value(IC)   0.000
IC Skew      -0.177
IC Kurtosis  -0.692
Ann. IR       0.528
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/output/information_report.pdf



<matplotlib.figure.Figure at 0x7fb0bae6e630>

output_13_2.png-259.4kB

output_13_3.png-169.1kB

  1. # 并集绩效 绝对收益
  2. draw_analysis('Union',None)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
{'ret':                long_ret  long_short_ret  all_sample_ret
t-stat        64.492280        9.933217       82.457439
p-value        0.000000        0.000000        0.000000
skewness       1.518563        0.575873        1.205233
kurtosis       8.858423        3.809283        7.216747
Ann. Ret       0.292424        0.052786        0.207705
Ann. Vol       0.415697        0.056565        0.449501
Ann. IR        0.703456        0.933177        0.462078
occurance  67802.000000      915.000000   256877.000000, 'space':                  long_space  all_sample_space
Up_sp Mean         0.126223          0.128407
Up_sp Std          0.140752          0.144886
Up_sp IR           0.896775          0.886264
Up_sp Pct5         0.000000          0.000000
Up_sp Pct25        0.035608          0.035329
Up_sp Pct50        0.086255          0.086880
Up_sp Pct75        0.167223          0.171021
Up_sp Pct95        0.390235          0.398506
Up_sp Occur    67802.000000     256877.000000
Down_sp Mean      -0.071297         -0.086193
Down_sp Std        0.083802          0.097463
Down_sp IR        -0.850770         -0.884362
Down_sp Pct5      -0.256119         -0.302894
Down_sp Pct25     -0.090581         -0.110774
Down_sp Pct50     -0.044073         -0.054994
Down_sp Pct75     -0.017183         -0.022265
Down_sp Pct95     -0.000542         -0.000447
Down_sp Occur  67802.000000     256877.000000}


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.263947  0.440772  256877    100.0
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/output/returns_report.pdf
Information Analysis
                 ic
IC Mean       0.078
IC Std.       0.147
t-stat(IC)   15.799
p-value(IC)   0.000
IC Skew      -0.179
IC Kurtosis  -0.694
Ann. IR       0.531
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/output/information_report.pdf



<matplotlib.figure.Figure at 0x7fb0b8cae438>

output_14_2.png-263.2kB
output_14_3.png-169kB

  1. # 交集绩效 相对收益
  2. draw_analysis('Intersection',dv.data_benchmark)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
{'ret':                long_ret  long_short_ret  all_sample_ret
t-stat        17.138726        9.045112        7.570006
p-value        0.000000        0.000000        0.000000
skewness       1.962324        1.218376        1.563058
kurtosis      13.327837        4.170159       10.061591
Ann. Ret       0.128300        0.067791        0.014263
Ann. Vol       0.266718        0.079690        0.336234
Ann. IR        0.481034        0.850675        0.042421
occurance  10241.000000      913.000000   256877.000000, 'space':                  long_space  all_sample_space
Up_sp Mean         0.114700          0.128407
Up_sp Std          0.126737          0.144886
Up_sp IR           0.905027          0.886264
Up_sp Pct5         0.000000          0.000000
Up_sp Pct25        0.033609          0.035329
Up_sp Pct50        0.078645          0.086880
Up_sp Pct75        0.151099          0.171021
Up_sp Pct95        0.357282          0.398506
Up_sp Occur    10241.000000     256877.000000
Down_sp Mean      -0.058394         -0.086193
Down_sp Std        0.068206          0.097463
Down_sp IR        -0.856142         -0.884362
Down_sp Pct5      -0.215423         -0.302894
Down_sp Pct25     -0.074737         -0.110774
Down_sp Pct50     -0.035432         -0.054994
Down_sp Pct75     -0.013426         -0.022265
Down_sp Pct95      0.000000         -0.000447
Down_sp Occur  10241.000000     256877.000000}


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.039867  0.195648  256877    100.0
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/output/returns_report.pdf
Information Analysis
                 ic
IC Mean       0.039
IC Std.       0.085
t-stat(IC)   13.624
p-value(IC)   0.000
IC Skew       0.138
IC Kurtosis  -0.016
Ann. IR       0.458
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/output/information_report.pdf



<matplotlib.figure.Figure at 0x7fb0b8b08550>

output_15_2.png-259.9kB
output_15_3.png-166.8kB

  1. # 交集绩效 绝对收益
  2. draw_analysis('Intersection',None)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
{'ret':                long_ret  long_short_ret  all_sample_ret
t-stat        31.493033        9.084742       82.457439
p-value        0.000000        0.000000        0.000000
skewness       1.742077        1.225597        1.205233
kurtosis       9.888569        4.196702        7.216747
Ann. Ret       0.323998        0.068093        0.207705
Ann. Vol       0.366548        0.079696        0.449501
Ann. IR        0.883917        0.854402        0.462078
occurance  10241.000000      913.000000   256877.000000, 'space':                  long_space  all_sample_space
Up_sp Mean         0.114700          0.128407
Up_sp Std          0.126737          0.144886
Up_sp IR           0.905027          0.886264
Up_sp Pct5         0.000000          0.000000
Up_sp Pct25        0.033609          0.035329
Up_sp Pct50        0.078645          0.086880
Up_sp Pct75        0.151099          0.171021
Up_sp Pct95        0.357282          0.398506
Up_sp Occur    10241.000000     256877.000000
Down_sp Mean      -0.058394         -0.086193
Down_sp Std        0.068206          0.097463
Down_sp IR        -0.856142         -0.884362
Down_sp Pct5      -0.215423         -0.302894
Down_sp Pct25     -0.074737         -0.110774
Down_sp Pct50     -0.035432         -0.054994
Down_sp Pct75     -0.013426         -0.022265
Down_sp Pct95      0.000000         -0.000447
Down_sp Occur  10241.000000     256877.000000}


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.039867  0.195648  256877    100.0
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/output/returns_report.pdf
Information Analysis
                 ic
IC Mean       0.039
IC Std.       0.084
t-stat(IC)   13.699
p-value(IC)   0.000
IC Skew       0.135
IC Kurtosis  -0.012
Ann. IR       0.461
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/output/information_report.pdf



<matplotlib.figure.Figure at 0x7fb0b8c65b70>

output_16_2.png-263.8kB
output_16_3.png-167kB

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