[关闭]
@Channelchan 2018-04-18T16:18:19.000000Z 字数 20278 阅读 51236

参数优化器

目录

  1. 因子参数优化
  2. 事件参数优化
  3. 拓展:外部自定义带参算法优化
  1. from jaqs_fxdayu.data import DataView
  2. import warnings
  3. warnings.filterwarnings("ignore")
  4. dataview_folder = './Factor'
  5. dv = DataView()
  6. dv.load_dataview(dataview_folder)
Dataview loaded successfully.
  1. import numpy as np
  2. def mask_index_member():
  3. df_index_member = dv.get_ts('index_member')
  4. mask_index_member = ~(df_index_member >0) #定义信号过滤条件-非指数成分
  5. return mask_index_member
  6. def limit_up_down():
  7. # 定义可买卖条件——未停牌、未涨跌停
  8. trade_status = dv.get_ts('trade_status')
  9. mask_sus = trade_status == u'停牌'
  10. # 涨停
  11. dv.add_formula('up_limit', '(close - Delay(close, 1)) / Delay(close, 1) > 0.095', is_quarterly=False, add_data=True)
  12. # 跌停
  13. dv.add_formula('down_limit', '(close - Delay(close, 1)) / Delay(close, 1) < -0.095', is_quarterly=False, add_data=True)
  14. can_enter = np.logical_and(dv.get_ts('up_limit') < 1, ~mask_sus) # 未涨停未停牌
  15. can_exit = np.logical_and(dv.get_ts('down_limit') < 1, ~mask_sus) # 未跌停未停牌
  16. return can_enter,can_exit
  1. mask = mask_index_member()
  2. can_enter,can_exit = limit_up_down()
  1. from jaqs_fxdayu.research import Optimizer

'''
:param dataview: 包含了计算公式所需要的所有数据的jaqs.data.DataView对象
:param formula: str 需要优化的公式:如'(open - Delay(close, l1)) / Delay(close, l2)'
:param params: dict 需要优化的参数范围:如{"LEN1":range(1,10,1),"LEN2":range(1,10,1)}
:param name: str (N) 信号的名称
:param price: dataFrame (N) 价格与ret不能同时存在
:param ret: dataFrame (N) 收益
:param high: dataFrame (N) 最高价 用于计算上行收益空间
:param low: dataFrame (N) 最低价 用于计算下行收益空间
:param benchmark_price: dataFrame (N) 基准价格 若不为空收益计算模式为相对benchmark的收益
:param period: int (5) 选股持有期
:param n_quantiles: int (5)
:param mask: 过滤条件 dataFrame (N)
:param can_enter: dataFrame (N) 是否能进场
:param can_exit: dataFrame (N) 是否能出场
:param forward: bool(True) 是否forward return
:param commission: float(0.0008) 手续费率
:param is_event: bool(False) 是否是事件(0/1因子)
:param is_quarterly: bool(False) 是否是季度因子
'''

1. 因子参数优化

  1. price = dv.get_ts('close_adj')
  2. high = dv.get_ts('high_adj')
  3. low = dv.get_ts('low_adj')
  4. price_bench = dv.data_benchmark
  5. optimizer = Optimizer(dataview=dv,
  6. formula='- Correlation(vwap_adj, volume, LEN)',
  7. params={"LEN":range(2,15,1)},
  8. name='divert',
  9. price=price,
  10. high=high,
  11. low=low,
  12. benchmark_price=None,#=None求绝对收益 #=price_bench求相对收益
  13. period=30,
  14. n_quantiles=5,
  15. mask=mask,
  16. can_enter=can_enter,
  17. can_exit=can_exit,
  18. commission=0.0008,#手续费 默认0.0008
  19. is_event=False,#是否是事件(0/1因子)
  20. is_quarterly=False)#是否是季度因子 默认为False

优化目标

target_type:

target:

  1. ret_best = optimizer.enumerate_optimizer(target_type="top_quantile_ret",#优化目标类型
  2. target="Ann. IR",#优化目标
  3. in_sample_range=[20140101,20160101],#样本内范围 默认为None,在全样本上优化
  4. ascending=False)#是否按优化目标升序排列(从小到大)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
  1. # 样本内最优绩效
  2. print(ret_best[0]["signal_name"])
  3. print(ret_best[0]["ret"])
  4. print(ret_best[0]["ic"])
  5. print(ret_best[0]["space"])
divert{'LEN': 12}
             long_ret   short_ret  long_short_ret  top_quantile_ret  \
t-stat       5.110799   -4.669524        3.005345         33.349300   
p-value      0.000000    0.000000        0.002790          0.000000   
skewness    -0.575172    0.309078       -1.131889          0.675351   
kurtosis    -0.018146   -0.320506        4.012586          3.707588   
Ann. Ret     0.262372   -0.254147        0.026183          0.308727   
Ann. Vol     0.398883    0.422892        0.067692          0.534983   
Ann. IR      0.657766   -0.600973        0.386791          0.577079   
occurance  488.000000  488.000000      488.000000      26941.000000   

           bottom_quantile_ret     tmb_ret  all_sample_ret  
t-stat               23.930533    4.042585       68.882384  
p-value               0.000000    0.000060        0.000000  
skewness              0.729822   -0.948665        0.951901  
kurtosis              2.493816    3.201947        4.295649  
Ann. Ret              0.232219    0.078149        0.299248  
Ann. Vol              0.564872    0.150204        0.563383  
Ann. IR               0.411100    0.520286        0.531163  
occurance         27335.000000  488.000000   135662.000000  
                return_ic  upside_ret_ic  downside_ret_ic
IC Mean      4.960313e-02       0.007377     6.425467e-02
IC Std.      1.375747e-01       0.136801     1.358379e-01
t-stat(IC)   7.964903e+00       1.191293     1.044946e+01
p-value(IC)  1.177710e-14       0.234119     3.385348e-23
IC Skew     -2.779783e-01      -0.167767     2.016567e-01
IC Kurtosis  3.472020e-01       0.873831     3.616037e-01
Ann. IR      3.605542e-01       0.053927     4.730247e-01
               long_space  short_space  long_short_space  top_quantile_space  \
Up_sp Mean       0.156935     0.113816          0.270751            0.163129   
Up_sp Std        0.091596     0.105440          0.113461            0.157858   
Up_sp IR         1.713338     1.079438          2.386298            1.033390   
Up_sp Pct5       0.044771     0.022594          0.127558            0.008423   
Up_sp Pct25      0.087302     0.044775          0.176277            0.054945   
Up_sp Pct50      0.135787     0.075946          0.242004            0.118632   
Up_sp Pct75      0.216750     0.122701          0.354439            0.222801   
Up_sp Pct95      0.346134     0.356583          0.474459            0.468291   
Up_sp Occur    488.000000   488.000000        488.000000        26941.000000   
Down_sp Mean    -0.108474    -0.168088         -0.276562           -0.105026   
Down_sp Std      0.103840     0.102438          0.118263            0.116824   
Down_sp IR      -1.044627    -1.640874         -2.338534           -0.899011   
Down_sp Pct5    -0.339163    -0.370044         -0.480706           -0.366672   
Down_sp Pct25   -0.109113    -0.233223         -0.369367           -0.133969   
Down_sp Pct50   -0.068448    -0.138006         -0.239328           -0.063891   
Down_sp Pct75   -0.046176    -0.092070         -0.185622           -0.026318   
Down_sp Pct95   -0.022139    -0.043186         -0.136297           -0.003715   
Down_sp Occur  488.000000   488.000000        488.000000        26941.000000   

               bottom_quantile_space   tmb_space  all_sample_space  
Up_sp Mean                  0.166828    0.281121          0.167836  
Up_sp Std                   0.169810    0.110843          0.172231  
Up_sp IR                    0.982437    2.536211          0.974487  
Up_sp Pct5                  0.005584    0.135413          0.007070  
Up_sp Pct25                 0.050753    0.183756          0.052442  
Up_sp Pct50                 0.115835    0.278703          0.118884  
Up_sp Pct75                 0.229334    0.349264          0.227149  
Up_sp Pct95                 0.498775    0.489607          0.492549  
Up_sp Occur             27335.000000  488.000000     135662.000000  
Down_sp Mean               -0.114959   -0.274749         -0.108013  
Down_sp Std                 0.121342    0.120116          0.117617  
Down_sp IR                 -0.947400   -2.287362         -0.918343  
Down_sp Pct5               -0.391534   -0.495471         -0.376228  
Down_sp Pct25              -0.149821   -0.359070         -0.139779  
Down_sp Pct50              -0.071857   -0.240467         -0.066070  
Down_sp Pct75              -0.030352   -0.181938         -0.027373  
Down_sp Pct95              -0.004729   -0.129625         -0.004144  
Down_sp Occur           27335.000000  488.000000     135662.000000  
  1. from jaqs_fxdayu.research import SignalDigger
  2. from jaqs_fxdayu.research.signaldigger.analysis import analysis
  3. import matplotlib.pyplot as plt
  4. obj = SignalDigger()
  5. def draw_analysis(signal_data,period):
  6. obj.signal_data = signal_data
  7. obj.period = period
  8. obj.create_full_report()
  9. plt.show()

全样本可视化

  1. draw_analysis(optimizer.all_signals[ret_best[0]["signal_name"]], period=30)
Value of signals of Different Quantiles Statistics
               min       max      mean       std  count    count %
quantile                                                          
1        -3.062545 -0.054923 -0.790034  0.111886  51786  20.144786
2        -0.910108  0.444112 -0.605170  0.154872  51418  20.001634
3        -0.852226  0.664258 -0.439884  0.198861  51393  19.991909
4        -0.772911  0.801471 -0.239247  0.232510  51418  20.001634
5        -0.644317  0.987630  0.146103  0.293025  51054  19.860038
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/returns_report.pdf
Information Analysis
                 ic
IC Mean       0.059
IC Std.       0.133
t-stat(IC)   13.244
p-value(IC)   0.000
IC Skew      -0.084
IC Kurtosis   0.237
Ann. IR       0.445
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/information_report.pdf



<matplotlib.figure.Figure at 0x7f9a7c4107b8>

output_13_2.png-435.8kB
output_13_3.png-161kB

样本内可视化

  1. draw_analysis(optimizer.all_signals[ret_best[0]["signal_name"]].loc[20140101:20160101], period=30)
Value of signals of Different Quantiles Statistics
               min       max      mean       std  count    count %
quantile                                                          
1        -2.080126 -0.054923 -0.802377  0.108587  27335  20.149342
2        -0.910108  0.444112 -0.629030  0.159256  27131  19.998968
3        -0.852226  0.664258 -0.467972  0.211465  27124  19.993808
4        -0.772911  0.801471 -0.269276  0.249928  27131  19.998968
5        -0.644317  0.964493  0.119820  0.308311  26941  19.858914
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/returns_report.pdf
Information Analysis
                ic
IC Mean      0.050
IC Std.      0.138
t-stat(IC)   7.965
p-value(IC)  0.000
IC Skew     -0.278
IC Kurtosis  0.347
Ann. IR      0.361
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/information_report.pdf



<matplotlib.figure.Figure at 0x7f9a7f534dd8>

output_15_2.png-407.1kB
output_15_3.png-139.3kB

样本外可视化

  1. draw_analysis(optimizer.all_signals[ret_best[0]["signal_name"]].loc[20160101:], period=30)
Value of signals of Different Quantiles Statistics
               min       max      mean       std  count    count %
quantile                                                          
1        -3.062545 -0.240942 -0.776234  0.113892  24451  20.139695
2        -0.906725  0.089250 -0.578515  0.145263  24287  20.004613
3        -0.826314  0.291378 -0.408492  0.178606  24269  19.989786
4        -0.744365  0.490119 -0.205701  0.206260  24287  20.004613
5        -0.616987  0.987630  0.175469  0.271961  24113  19.861293
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/returns_report.pdf
Information Analysis
                 ic
IC Mean       0.071
IC Std.       0.126
t-stat(IC)   11.197
p-value(IC)   0.000
IC Skew       0.272
IC Kurtosis  -0.239
Ann. IR       0.561
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/information_report.pdf



<matplotlib.figure.Figure at 0x7f9a7f65b470>

output_17_2.png-426.1kB

output_17_3.png-139.3kB

  1. # 以持有期mean_ic为最优化目标
  2. ic_best = optimizer.enumerate_optimizer(target_type="return_ic",#优化目标类型
  3. target = "IC Mean",
  4. in_sample_range=[20140101,20160101],#样本内范围 默认为None,在全样本上优化
  5. ascending=False)
  1. print(ic_best[0]["signal_name"])
  2. print(ic_best[0]["ic"])
  3. print(ic_best[0]["ret"])
  4. print(ic_best[0]["space"])
divert{'LEN': 12}
                return_ic  upside_ret_ic  downside_ret_ic
IC Mean      4.960313e-02       0.007377     6.425467e-02
IC Std.      1.375747e-01       0.136801     1.358379e-01
t-stat(IC)   7.964903e+00       1.191293     1.044946e+01
p-value(IC)  1.177710e-14       0.234119     3.385348e-23
IC Skew     -2.779783e-01      -0.167767     2.016567e-01
IC Kurtosis  3.472020e-01       0.873831     3.616037e-01
Ann. IR      3.605542e-01       0.053927     4.730247e-01
             long_ret   short_ret  long_short_ret  top_quantile_ret  \
t-stat       5.110799   -4.669524        3.005345         33.349300   
p-value      0.000000    0.000000        0.002790          0.000000   
skewness    -0.575172    0.309078       -1.131889          0.675351   
kurtosis    -0.018146   -0.320506        4.012586          3.707588   
Ann. Ret     0.262372   -0.254147        0.026183          0.308727   
Ann. Vol     0.398883    0.422892        0.067692          0.534983   
Ann. IR      0.657766   -0.600973        0.386791          0.577079   
occurance  488.000000  488.000000      488.000000      26941.000000   

           bottom_quantile_ret     tmb_ret  all_sample_ret  
t-stat               23.930533    4.042585       68.882384  
p-value               0.000000    0.000060        0.000000  
skewness              0.729822   -0.948665        0.951901  
kurtosis              2.493816    3.201947        4.295649  
Ann. Ret              0.232219    0.078149        0.299248  
Ann. Vol              0.564872    0.150204        0.563383  
Ann. IR               0.411100    0.520286        0.531163  
occurance         27335.000000  488.000000   135662.000000  
               long_space  short_space  long_short_space  top_quantile_space  \
Up_sp Mean       0.156935     0.113816          0.270751            0.163129   
Up_sp Std        0.091596     0.105440          0.113461            0.157858   
Up_sp IR         1.713338     1.079438          2.386298            1.033390   
Up_sp Pct5       0.044771     0.022594          0.127558            0.008423   
Up_sp Pct25      0.087302     0.044775          0.176277            0.054945   
Up_sp Pct50      0.135787     0.075946          0.242004            0.118632   
Up_sp Pct75      0.216750     0.122701          0.354439            0.222801   
Up_sp Pct95      0.346134     0.356583          0.474459            0.468291   
Up_sp Occur    488.000000   488.000000        488.000000        26941.000000   
Down_sp Mean    -0.108474    -0.168088         -0.276562           -0.105026   
Down_sp Std      0.103840     0.102438          0.118263            0.116824   
Down_sp IR      -1.044627    -1.640874         -2.338534           -0.899011   
Down_sp Pct5    -0.339163    -0.370044         -0.480706           -0.366672   
Down_sp Pct25   -0.109113    -0.233223         -0.369367           -0.133969   
Down_sp Pct50   -0.068448    -0.138006         -0.239328           -0.063891   
Down_sp Pct75   -0.046176    -0.092070         -0.185622           -0.026318   
Down_sp Pct95   -0.022139    -0.043186         -0.136297           -0.003715   
Down_sp Occur  488.000000   488.000000        488.000000        26941.000000   

               bottom_quantile_space   tmb_space  all_sample_space  
Up_sp Mean                  0.166828    0.281121          0.167836  
Up_sp Std                   0.169810    0.110843          0.172231  
Up_sp IR                    0.982437    2.536211          0.974487  
Up_sp Pct5                  0.005584    0.135413          0.007070  
Up_sp Pct25                 0.050753    0.183756          0.052442  
Up_sp Pct50                 0.115835    0.278703          0.118884  
Up_sp Pct75                 0.229334    0.349264          0.227149  
Up_sp Pct95                 0.498775    0.489607          0.492549  
Up_sp Occur             27335.000000  488.000000     135662.000000  
Down_sp Mean               -0.114959   -0.274749         -0.108013  
Down_sp Std                 0.121342    0.120116          0.117617  
Down_sp IR                 -0.947400   -2.287362         -0.918343  
Down_sp Pct5               -0.391534   -0.495471         -0.376228  
Down_sp Pct25              -0.149821   -0.359070         -0.139779  
Down_sp Pct50              -0.071857   -0.240467         -0.066070  
Down_sp Pct75              -0.030352   -0.181938         -0.027373  
Down_sp Pct95              -0.004729   -0.129625         -0.004144  
Down_sp Occur           27335.000000  488.000000     135662.000000  
  1. excel_data = optimizer.all_signals[ret_best[0]["signal_name"]][optimizer.all_signals[ret_best[0]["signal_name"]]['quantile']==5]["quantile"].unstack().replace(np.nan, 0).replace(5, 1)
  2. print (excel_data.head())
  3. excel_data.to_excel('./divert_opt_quantile_5.xlsx')
symbol      000001.SZ  000002.SZ  000008.SZ  000009.SZ  000012.SZ  000024.SZ  \
trade_date                                                                     
20140103          1.0        0.0        0.0        0.0        0.0        1.0   
20140106          0.0        0.0        0.0        0.0        0.0        1.0   
20140107          0.0        1.0        0.0        0.0        1.0        1.0   
20140108          0.0        1.0        0.0        0.0        1.0        1.0   
20140109          0.0        0.0        0.0        0.0        0.0        1.0   

symbol      000027.SZ  000039.SZ  000046.SZ  000060.SZ    ...      601992.SH  \
trade_date                                                ...                  
20140103          0.0        0.0        1.0        0.0    ...            0.0   
20140106          0.0        0.0        1.0        0.0    ...            1.0   
20140107          0.0        0.0        1.0        0.0    ...            0.0   
20140108          0.0        0.0        0.0        0.0    ...            0.0   
20140109          0.0        0.0        0.0        0.0    ...            1.0   

symbol      601997.SH  601998.SH  603000.SH  603160.SH  603288.SH  603699.SH  \
trade_date                                                                     
20140103          0.0        1.0        0.0        0.0        0.0        0.0   
20140106          0.0        0.0        0.0        0.0        0.0        0.0   
20140107          0.0        0.0        0.0        0.0        0.0        0.0   
20140108          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   

symbol      603858.SH  603885.SH  603993.SH  
trade_date                                   
20140103          0.0        0.0        1.0  
20140106          0.0        0.0        0.0  
20140107          0.0        0.0        0.0  
20140108          0.0        0.0        0.0  
20140109          0.0        0.0        0.0  

[5 rows x 456 columns]

2. 事件参数优化

  1. event_opt = Optimizer(dataview=dv,
  2. formula="(Ts_Mean(close_adj, SHORT)>=Ts_Mean(close_adj, LONG))&&(Delay(Ts_Mean(close_adj, SHORT)<Ts_Mean(close_adj, LONG), 1))",
  3. params={'SHORT':range(5,11,1),'LONG':range(30,61,5)},
  4. name='cross',
  5. price=price,
  6. high=high,
  7. low=low,
  8. benchmark_price=None,#=None求绝对收益 #=price_bench求相对收益
  9. period=30,
  10. n_quantiles=1,
  11. mask=mask,
  12. can_enter=can_enter,
  13. can_exit=can_exit,
  14. commission=0.0008,#手续费 默认0.0008
  15. is_event=True,#是否是事件(0/1因子)
  16. is_quarterly=False)#是否是季度因子 默认为False
  1. event_best = event_opt.enumerate_optimizer(target_type="long_ret",
  2. target="Ann. IR",
  3. in_sample_range=[20140101,20160101],#样本内范围 默认为None,在全样本上优化
  4. ascending=False)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 58%
  1. # 事件样本内最优绩效 ps:事件没有ic分析结果
  2. # 可以进一步尝试优化space,辅以更精细的择时捕捉事件收益
  3. print(event_best[0]["signal_name"])
  4. print(event_best[0]["ret"])
  5. print(event_best[0]["space"])
cross{'SHORT': 10, 'LONG': 55}
              long_ret  long_short_ret  all_sample_ret
t-stat       12.373617       -2.253417       69.704793
p-value       0.000000        0.024880        0.000000
skewness      1.005173        0.410379        0.929952
kurtosis      4.215504        2.859697        4.188103
Ann. Ret      0.456521       -0.047507        0.311164
Ann. Vol      0.496016        0.136064        0.569344
Ann. IR       0.920375       -0.349156        0.546531
occurance  1459.000000      337.000000   131218.000000
                long_space  all_sample_space
Up_sp Mean        0.165738          0.169461
Up_sp Std         0.165765          0.173734
Up_sp IR          0.999838          0.975401
Up_sp Pct5        0.004526          0.006992
Up_sp Pct25       0.049561          0.052758
Up_sp Pct50       0.118951          0.120412
Up_sp Pct75       0.234501          0.229488
Up_sp Pct95       0.475594          0.496829
Up_sp Occur    1459.000000     131218.000000
Down_sp Mean     -0.081863         -0.109031
Down_sp Std       0.090621          0.119049
Down_sp IR       -0.903356         -0.915854
Down_sp Pct5     -0.291374         -0.379734
Down_sp Pct25    -0.104825         -0.141476
Down_sp Pct50    -0.053169         -0.066069
Down_sp Pct75    -0.023307         -0.027116
Down_sp Pct95    -0.003920         -0.004029
Down_sp Occur  1459.000000     131218.000000

可视化

  1. # 全样本
  2. draw_analysis(event_opt.all_signals[event_best[0]["signal_name"]], period=30)
Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.012066  0.109179  252621    100.0
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/returns_report.pdf
Information Analysis
                ic
IC Mean     -0.006
IC Std.      0.066
t-stat(IC)  -2.326
p-value(IC)  0.020
IC Skew     -0.083
IC Kurtosis  0.033
Ann. IR     -0.091
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/information_report.pdf



<matplotlib.figure.Figure at 0x7f9a7c54fd30>

output_26_2.png-261.2kB

output_26_3.png-142.3kB

  1. # 样本内
  2. draw_analysis(event_opt.all_signals[event_best[0]["signal_name"]].loc[20140101:20160101], period=30)
Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.011119  0.104859  131218    100.0
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/returns_report.pdf
Information Analysis
                ic
IC Mean     -0.004
IC Std.      0.068
t-stat(IC)  -1.135
p-value(IC)  0.257
IC Skew     -0.098
IC Kurtosis  0.562
Ann. IR     -0.062
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/information_report.pdf



<matplotlib.figure.Figure at 0x7f9a7f59e898>

output_27_2.png-249.8kB

output_27_3.png-119.5kB

  1. # 样本外
  2. draw_analysis(event_opt.all_signals[event_best[0]["signal_name"]].loc[20160101:], period=30)
Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.013089  0.113655  121403    100.0
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/returns_report.pdf
Information Analysis
                ic
IC Mean     -0.008
IC Std.      0.065
t-stat(IC)  -2.204
p-value(IC)  0.028
IC Skew     -0.073
IC Kurtosis -0.674
Ann. IR     -0.125
Figure saved: /home/xinger/Desktop/qtc/多因子选股模型/information_report.pdf



<matplotlib.figure.Figure at 0x7f9a7f3b14a8>

output_28_2.png-258.4kB

output_28_3.png-133.1kB

3. 拓展:外部自定义带参算法优化

  1. import pandas as pd
  2. import talib as ta
  3. slp_opt = Optimizer(dataview=dv,
  4. price=price,
  5. period=30,
  6. n_quantiles=5,
  7. mask=mask,
  8. can_enter=can_enter,
  9. can_exit=can_exit,
  10. commission=0.0008,#手续费 默认0.0008
  11. is_event=False,#是否是事件(0/1因子)
  12. is_quarterly=False)#是否是季度因子 默认为False
  13. signals_dict = {}
  14. for param in range(2,9,1):
  15. slope_df = pd.DataFrame({name: -ta.LINEARREG_SLOPE(value.values, param) for name, value in price.iteritems()}, index=price.index)
  16. dv.append_df(slope_df,'SLOPE_PARAM='+str(param))
  17. signals_dict['SLOPE_PARAM='+str(param)] = slp_opt.cal_signal(dv.get_ts('SLOPE_PARAM='+str(param)))
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 59%
  1. slp_opt.all_signals = signals_dict
  1. # 以持有期mean_ic为最优化目标
  2. slp_best = slp_opt.enumerate_optimizer(target_type="return_ic",#优化目标类型
  3. target = "IC Mean",
  4. in_sample_range=[20140101,20160101],#样本内范围 默认为None,在全样本上优化
  5. ascending=False)
  1. # 样本内绩效
  2. print(slp_best[0]["signal_name"])
  3. print(slp_best[0]["ic"])
  4. print(slp_best[0]["ret"])
SLOPE_PARAM=8
                return_ic
IC Mean      4.299888e-02
IC Std.      1.617011e-01
t-stat(IC)   5.831988e+00
p-value(IC)  1.007307e-08
IC Skew      5.618541e-02
IC Kurtosis  2.089043e-01
Ann. IR      2.659157e-01
             long_ret   short_ret  long_short_ret  top_quantile_ret  \
t-stat       3.819634   -1.719541        4.581976         32.283237   
p-value      0.000150    0.086160        0.000010          0.000000   
skewness    -0.248871    0.321936        0.350289          0.931792   
kurtosis    -0.149910   -0.288344        1.808498          4.240505   
Ann. Ret     0.210085   -0.095689        0.052531          0.312823   
Ann. Vol     0.424275    0.428817        0.088438          0.555819   
Ann. IR      0.495163   -0.223147        0.593990          0.562815   
occurance  481.000000  480.000000      481.000000      26542.000000   

           bottom_quantile_ret     tmb_ret  all_sample_ret  
t-stat               18.988489    5.454760       69.090547  
p-value               0.000000    0.000000        0.000000  
skewness              0.834395    0.135840        0.941367  
kurtosis              3.770243    4.601257        4.245985  
Ann. Ret              0.185419    0.133563        0.303882  
Ann. Vol              0.564128    0.188879        0.566108  
Ann. IR               0.328682    0.707135        0.536791  
occurance         26924.000000  481.000000   133636.000000  
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注