In [583]:
#!/Tsan/bin/python
# -*- coding: utf-8 -*-

In [584]:
# Libraries to use
from __future__ import division 
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime
from sklearn.cluster import KMeans

In [585]:
# Import My own library for factor testing
from SingleFactorTest import factorFilterFunctions as ff
#from config import *

In [586]:
%matplotlib inline

In [587]:
%load_ext line_profiler


The line_profiler extension is already loaded. To reload it, use:
  %reload_ext line_profiler

In [588]:
# make sure that matplotib and seaborn can show Chinese
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']
sns.set_style("darkgrid",{"font.sans-serif":['simhei', 'Arial']})

In [589]:
# Files to use 
filenamePrice = 'Own_Factor_AdjustedPriceForward-1d.csv'
filenameST = 'LZ_GPA_SLCIND_ST_FLAG.csv'
filenameTradeday = 'LZ_GPA_SLCIND_TRADEDAYCOUNT.csv'
filenameStopFlag = 'LZ_GPA_SLCIND_STOP_FLAG.csv'
filenameIndu = 'LZ_GPA_INDU_ZX.csv'
filenameFCAP = 'LZ_GPA_VAL_A_FCAP.csv'
filenameAdjustFactor = 'LZ_GPA_CMFTR_CUM_FACTOR.csv'
filenameHS300 = 'LZ_GPA_INDXQUOTE_CLOSE.csv'
filenameZXIndustry = 'LZ_GPA_INDU_ZX.csv'
filenameZXExplanation = 'LZ_GPA_TMP_INDU_ZX.csv'

In [590]:
# Constants
startTime =  datetime.strptime('20161201', '%Y%m%d')
endTime = datetime.strptime('20170429', '%Y%m%d')

path = ff.data_path
timeStampNum = 2500
thresholdNum = 0.2
HS300Index ='000300.SH' # HS300 index code
ZZ500Index = '000905.SH' #   ZZ500 index code

In [591]:
stDF = pd.read_csv(path+filenameST,infer_datetime_format=True,parse_dates=[0],index_col=0).loc[startTime:endTime]
tradeDayDF = pd.read_csv(path+filenameTradeday,infer_datetime_format=True,parse_dates=[0],index_col=0).loc[startTime:endTime]
stopFlagDF = pd.read_csv(path+filenameStopFlag,infer_datetime_format=True,parse_dates=[0],index_col=0).loc[startTime:endTime]
indusDF = pd.read_csv(path+filenameZXIndustry,infer_datetime_format=True,parse_dates=[0],index_col=0).loc[startTime:endTime]
induExplanation = pd.read_csv(path+filenameZXExplanation,infer_datetime_format=True,parse_dates=[0],encoding='gb2312')

In [670]:
# Uqer factor
filenameUQAD20 = 'Uqer_factor_AD20.csv'    # group9最好,有2%左右的超额收益,其他组都没有超额收益,多空效果很好,有较好的单调性(但是单调性反转,长期是group0最小)(证明类动量/价因子还是效果比较好的)
filenameUQADTM = 'Uqer_factor_ADTM.csv'    # 因子失效中(没有超额收益),失去单调性
filenameUQATR6 = 'Uqer_factor_ATR6.csv'    # group1最好,有0.2%左右的超额收益,其他组都没有超额收益,多空效果很好,有较好的单调性
filenameUQAroon = 'Uqer_factor_Aroon.csv'   # 因子失效中(几乎没有超额收益),失去单调性 
filenameUQBias10 = 'Uqer_factor_BIAS10.csv' # 因子失效中(几乎没有超额收益),失去单调性 
filenameUQCCI10 = 'Uqer_factor_CCI10.csv'    #  因子失效中(几乎没有超额收益),失去单调性 
filenameUQKDJ_K = 'Uqer_factor_KDJ_K.csv'    # 因子失效中(几乎没有超额收益),失去单调性 
filenameROC6 = 'Uqer_factor_ROC6.csv'        # 无效因子 
filenameCMO = 'Uqer_factor_CMO.csv'          # 因子失去单调性
filenameRVI = 'Uqer_factor_RVI.csv'           # 无效银子
filenameRSI = 'Uqer_factor_RSI.csv'           # 价格波动率策略目前都处于失效中?(大概)

In [593]:
# Own factor
filenameOWNILLIQ ='Own_Factor_ILLQ-1d.csv'
filenameSharpe = 'Own_Factor_sharpe_ratio_20D.csv'

filenameTOV20 = 'Own_Factor_Turnover_Volatility_20D.csv' # group0 最好,有年化 10%的收益率,单调性很好,多空效果也很好,
 #但是市值单调递减,group0市值最大,可能选到了上证50(大市值股票交易比较稳定,因此换手率波动较小)
filenameADJTOV20 = 'Own_Factor_ADJ_Turnover_Volatility_20D.csv' # 单调性较好,但是没有超额收益 
filenameTOVD20 = 'Own_Factor_Turnover_Volatility_deviation_20D.csv' # 无单调性,无超额收益
filenameADJTOVD20 = 'Own_Factor_ADJ_Turnover_Volatility_Deviation_20D.csv' # 无单调性,无超额收益
filenameMOM1M = 'LZ_GPA_DERI_Momentum_1M.csv' # 一月反转

In [594]:
# yield/growth
filenameYOYGR = 'LZ_GPA_FIN_IND_QFA_YOYGR.csv'
filenameYOYROE = 'LZ_GPA_FIN_IND_YOYROE.csv'
filenameROE = 'LZ_GPA_FIN_IND_ROE.csv'

In [595]:
# value
filenamePS = 'LZ_GPA_VAL_PS.csv'  # group0 最好,有年化 5%的收益率,单调性很好,多空效果也很好
filenamePB ='LZ_GPA_VAL_PB.csv'  # group0 最好,有年化 12%的收益率,单调性很好,多空效果也很好 (神级因子)
filenamePE ='LZ_GPA_VAL_PE.csv'  #  group0 最好,有年化 10%的收益率,单调性不是特别完美,但不错,多空效果也很好

In [596]:
#
filenameROE = 'LZ_GPA_FIN_IND_ROE.csv'
filenameRoeVol250 = 'Own_Factor_ROE_Volatility_250D.csv'

In [671]:
sololist = [filenameRSI]

In [672]:
sparedata = pd.read_csv(path+sololist[0],infer_datetime_format=True,parse_dates=[0],index_col=0)

In [673]:
# adjust the columns name of the Uqer data same as the own factor
if 'Uqer' in sololist[0]: 
    uqercolumnList = sparedata.columns.tolist()
    uqercolumnName = [x.split('.')[0] for x in uqercolumnList]
    newcolumnList = stDF.columns.tolist()
    newcolumnName = [x.split('.')[0] for x in newcolumnList]
    columndict = dict(zip(newcolumnName,newcolumnList))
    finalcolumns = []
    for stk in uqercolumnName:
        if stk in newcolumnName:
            stk = columndict[stk]
        else:
            pass
        finalcolumns.append(stk)
    sparedata.columns = finalcolumns
    addNanColumns = list(set(newcolumnList) - set(sparedata.columns))
    addData = pd.DataFrame(index = sparedata.index,columns = addNanColumns,dtype =float)
    sparedata = pd.concat([sparedata,addData],axis=1)
    sparedata = sparedata[newcolumnList]

In [ ]:


In [674]:
priceData =  pd.read_csv(path+ filenamePrice ,infer_datetime_format=True,parse_dates=[0],index_col=0).loc[startTime:endTime]
benchMarkData = pd.read_csv(path+filenameHS300,infer_datetime_format=True,parse_dates=[0],index_col=0).loc[startTime:endTime][ZZ500Index]

In [675]:
LFCAPDF = np.log10(ff.getData(thresholdNum, startTime, endTime,filename = filenameFCAP))

In [676]:
LFCAPDF


Out[676]:
000005.SZ 600601.SH 600602.SH 600651.SH 600652.SH 600653.SH 600654.SH 000004.SZ 000002.SZ 000001.SZ ... 603577.SH 002832.SZ 300579.SZ 603058.SH 600996.SH 603298.SH 002833.SZ 002835.SZ 300582.SZ 603218.SH
LZ_GPA_VAL_A_FCAP-d
2016-12-01 5.832980 6.042117 5.962139 5.956489 5.961952 5.888543 6.168674 5.540260 7.418016 7.147551 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-02 5.820563 6.033378 5.949452 5.944518 5.947681 5.880628 6.157182 5.535352 7.409560 7.145283 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-05 5.818157 6.028049 5.947732 5.946033 5.944833 5.872567 6.151205 5.539116 7.393675 7.141171 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-06 5.817553 6.028942 5.949023 5.944518 5.946733 5.867451 6.154663 5.545422 7.391626 7.142546 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-07 5.821761 6.031609 5.952872 5.946788 5.950196 5.874596 6.167115 5.557964 7.387500 7.142088 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-08 5.816343 6.025360 5.948163 5.939559 5.944197 5.869505 6.161951 5.552142 7.380534 7.143916 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-09 5.817553 6.024460 5.946438 5.938022 5.937792 5.867451 6.161499 5.552142 7.381761 7.149807 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-12 5.795245 5.998469 5.927898 5.907729 5.917312 5.843112 6.120265 5.518759 7.353744 7.143003 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-13 5.796515 5.996551 5.921080 5.913877 5.917650 5.842022 6.119269 5.526116 7.349994 7.139330 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-14 5.790771 5.994626 5.921080 5.914284 5.916973 5.845283 6.119269 5.533773 7.335245 7.138407 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-15 5.794608 5.996551 5.927898 5.917121 5.921687 5.845283 6.119269 5.556366 7.336023 7.131421 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-16 5.801557 5.999424 5.928799 5.920341 5.922021 5.850665 6.119269 5.566943 7.338544 7.131421 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-19 5.803433 5.997511 5.921538 5.923538 5.917988 5.846365 6.119269 5.562329 7.311417 7.129067 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-20 5.799043 6.013511 5.921080 5.915908 5.921352 5.843112 6.119269 5.561341 7.295272 7.124798 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-21 5.805922 6.025360 5.925637 5.917121 5.924024 5.846365 6.119269 5.573053 7.298465 7.127175 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-22 5.807161 6.017191 5.932385 5.926710 5.925686 5.855981 6.119269 5.567821 7.301213 7.126226 ... 4.668662 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-23 5.797148 6.018107 5.918783 5.912246 5.916973 5.848520 6.119269 5.553957 7.294631 7.123365 ... 4.710104 5.002134 4.583879 4.460591 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-26 5.797781 6.015355 5.918783 5.908965 5.919000 5.846365 6.119269 5.559755 7.302055 7.125274 ... 4.751542 5.043548 4.625312 4.502272 5.430784 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-27 5.797781 6.015355 5.917861 5.909788 5.918325 5.846365 6.119269 5.568016 7.317954 7.123365 ... 4.792853 5.084988 4.666705 4.543455 5.472054 5.198844 4.686387 4.681051 4.848522 5.149595
2016-12-28 5.797148 6.015355 5.913688 5.908965 5.918663 5.844199 6.119269 5.564102 7.313470 7.122408 ... 4.834301 5.126381 4.708081 4.584895 5.513391 5.240150 4.686387 4.681051 4.848522 5.149595
2016-12-29 5.793971 6.010730 5.915548 5.906488 5.916973 5.840929 6.119269 5.573341 7.310387 7.122887 ... 4.875694 5.167805 4.749427 4.626202 5.554707 5.281621 4.727889 4.722511 4.889951 5.190964
2016-12-30 5.790771 6.004171 5.923365 5.908141 5.917650 5.840929 6.119269 5.571122 7.299946 7.124321 ... 4.917025 5.209212 4.790848 4.667476 5.596146 5.323067 4.769232 4.764004 4.931310 5.232399
2017-01-03 5.812703 6.009799 5.924730 5.913062 5.924357 5.845283 6.119269 5.566748 7.303734 7.127175 ... 4.958404 5.234369 4.832253 4.708761 5.637560 5.364493 4.810580 4.805379 4.972749 5.273839
2017-01-04 5.824000 6.015355 5.926995 5.916312 5.926349 5.850665 6.119269 5.569184 7.306241 7.127175 ... 4.999746 5.209998 4.873669 4.750056 5.630784 5.405870 4.852013 4.846838 5.014197 5.315214
2017-01-05 5.821515 6.018107 5.930147 5.917121 5.923024 5.849594 6.119269 5.566650 7.307904 7.127649 ... 5.041196 5.186898 4.915083 4.791331 5.603480 5.395549 4.893369 4.888185 5.055577 5.291302
2017-01-06 5.825237 6.015355 5.927447 5.919137 5.922356 5.846365 6.119269 5.561934 7.301844 7.125750 ... 5.069743 5.156805 4.956457 4.832804 5.567732 5.359093 4.934846 4.929536 5.096924 5.255347
2017-01-09 5.827702 6.020841 5.931491 5.914284 5.922021 5.848520 6.119269 5.552445 7.302265 7.189770 ... 5.024034 5.165437 4.997823 4.874099 5.560265 5.349943 4.976238 4.970942 5.138337 5.248971
2017-01-10 5.824000 6.019931 5.925184 5.919940 5.920681 5.850665 6.119269 5.554862 7.300580 7.189770 ... 5.016047 5.183874 5.039255 4.915603 5.564512 5.345719 5.017659 5.012289 5.179759 5.235826
2017-01-11 5.814606 6.013511 5.923820 5.912654 5.920681 5.844199 6.119269 5.546754 7.296765 7.189295 ... 4.999111 5.169690 5.031004 4.956935 5.538121 5.317849 5.059026 5.053681 5.147311 5.219380
2017-01-12 5.810152 6.012586 5.918783 5.913470 5.914597 5.837635 6.119269 5.542642 7.296765 7.189770 ... 4.998094 5.163216 5.063709 4.998364 5.526068 5.303119 5.100373 5.007935 5.101599 5.206070
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2017-03-16 5.797168 6.003226 5.916937 5.906902 5.892607 5.829850 6.119269 5.507932 7.314902 7.206986 ... 5.190670 5.209212 5.150511 5.128827 5.585619 5.412035 5.263826 5.072441 5.158000 5.309577
2017-03-17 5.794523 5.998469 5.916937 5.901073 5.887203 5.828726 6.119269 5.506587 7.320380 7.197298 ... 5.195706 5.189151 5.130269 5.104908 5.583244 5.400366 5.241562 5.076983 5.136980 5.324338
2017-03-20 5.791863 5.997511 5.916937 5.914690 5.886115 5.828726 6.119269 5.512162 7.312034 7.194490 ... 5.187225 5.203812 5.142577 5.113169 5.588686 5.412763 5.248239 5.105122 5.153797 5.332860
2017-03-21 5.800450 5.993659 5.916937 5.911428 5.878785 5.826470 6.119269 5.518213 7.313061 7.194021 ... 5.171462 5.219464 5.145072 5.115702 5.592199 5.417538 5.254815 5.102537 5.160682 5.360662
2017-03-22 5.794523 5.987817 5.916937 5.901910 5.864889 5.825338 6.119269 5.519195 7.310799 7.190244 ... 5.212885 5.235919 5.154667 5.122107 5.588216 5.427777 5.269938 5.100215 5.162263 5.358795
2017-03-23 5.791863 5.984866 5.916937 5.899813 5.857965 5.820778 6.119269 5.510942 7.318562 7.192137 ... 5.221493 5.232881 5.139753 5.108488 5.593829 5.418688 5.254089 5.078747 5.156104 5.336732
2017-03-24 5.793195 5.988796 5.916937 5.903163 5.850534 5.823064 6.119269 5.505126 7.319371 7.191664 ... 5.249489 5.245567 5.149589 5.108773 5.588451 5.442561 5.277574 5.082933 5.156104 5.342839
2017-03-27 5.789857 5.989773 5.916937 5.904829 5.847367 5.820778 6.119269 5.499002 7.313513 7.189295 ... 5.251624 5.240337 5.159567 5.104044 5.583006 5.434472 5.284101 5.087270 5.154600 5.326526
2017-03-28 5.785817 5.979902 5.916937 5.900233 5.852502 5.818480 6.119269 5.501848 7.314945 7.188344 ... 5.251482 5.230094 5.141763 5.086849 5.560014 5.413199 5.269705 5.069853 5.135094 5.305983
2017-03-29 5.783783 5.975890 5.916937 5.906074 5.842170 5.813848 6.119269 5.498088 7.309604 7.187867 ... 5.247486 5.224674 5.113275 5.052711 5.540229 5.385132 5.247092 5.042449 5.096121 5.280237
2017-03-30 5.772770 5.965695 5.916937 5.897706 5.823687 5.805620 6.119269 5.486960 7.299566 7.186435 ... 5.201721 5.210640 5.098367 5.031611 5.535471 5.381082 5.243385 5.029059 5.082351 5.298167
2017-03-31 5.774856 5.967753 5.916937 5.899813 5.820317 5.809165 6.119269 5.475178 7.300623 7.190718 ... 5.155990 5.217292 5.095518 5.024401 5.537328 5.369825 5.267832 5.023429 5.080688 5.289295
2017-04-05 5.781739 5.979902 5.916937 5.910609 5.831999 5.818480 6.119269 5.475178 7.300623 7.192608 ... 5.122810 5.212561 5.108565 5.039373 5.544155 5.378255 5.296281 5.034508 5.088707 5.298078
2017-04-06 5.782422 5.978903 5.916937 5.914284 5.828693 5.819631 6.119269 5.475178 7.305032 7.192137 ... 5.117428 5.228112 5.116940 5.051086 5.541804 5.381238 5.300508 5.036022 5.090921 5.309664
2017-04-07 5.784462 5.988796 5.916937 5.912654 5.828278 5.820778 6.119269 5.475178 7.305241 7.192137 ... 5.133751 5.232474 5.093422 5.035509 5.532538 5.369022 5.301594 5.038390 5.076622 5.292030
2017-04-10 5.777623 5.979902 5.916937 5.913470 5.810048 5.817327 6.119269 5.475178 7.301045 7.191191 ... 5.139027 5.233558 5.054536 4.996707 5.511710 5.332445 5.280546 5.028291 5.031465 5.256237
2017-04-11 5.796508 5.983878 5.916937 5.905244 5.819894 5.816170 6.119269 5.475191 7.303148 7.189770 ... 5.160217 5.237263 5.071882 5.038033 5.519224 5.345719 5.309196 5.013541 5.043559 5.268786
2017-04-12 5.805651 5.979902 5.916937 5.900653 5.813927 5.815010 6.119269 5.475191 7.309810 7.188344 ... 5.160217 5.237665 5.058578 5.037529 5.510867 5.332970 5.302823 5.000503 5.032530 5.249673
2017-04-13 5.800450 5.980899 5.916937 5.898971 5.813927 5.817327 6.119269 5.475191 7.308154 7.188344 ... 5.152882 5.244908 5.068557 5.029905 5.511990 5.334367 5.297232 5.004468 5.033593 5.246353
2017-04-14 5.787842 5.973870 5.916937 5.888742 5.806570 5.812682 6.119269 5.475191 7.301887 7.186435 ... 5.132258 5.232339 5.063483 5.019352 5.501484 5.317305 5.278796 4.993895 5.024071 5.235102
2017-04-17 5.781739 5.969802 5.916937 5.876052 5.790565 5.804432 6.119269 5.475191 7.300201 7.187390 ... 5.086469 5.212064 5.042418 4.973595 5.495114 5.322351 5.270016 4.951192 5.000959 5.229784
2017-04-18 5.778311 5.966725 5.916937 5.876052 5.772534 5.799646 6.119269 5.475191 7.298295 7.184997 ... 5.041196 5.174908 5.045323 4.970666 5.493360 5.318030 5.275966 4.947105 5.000243 5.225356
2017-04-19 5.769973 5.958413 5.916937 5.863436 5.780486 5.789914 6.119269 5.475191 7.299566 7.178226 ... 5.054812 5.185691 5.058578 4.986817 5.503491 5.344189 5.284703 4.958994 5.011274 5.247160
2017-04-20 5.767863 5.954197 5.916937 5.863436 5.780950 5.786207 6.119269 5.475191 7.301677 7.178714 ... 5.054700 5.181212 5.042576 4.977662 5.500621 5.345210 5.264772 4.948692 5.006218 5.246050
2017-04-21 5.769973 5.956310 5.916937 5.866171 5.777230 5.787446 6.119269 5.475191 7.302728 7.181141 ... 5.031641 5.185011 5.041472 4.977662 5.499178 5.339219 5.261213 4.976547 5.005511 5.243923
2017-04-24 5.757157 5.940204 5.916937 5.867984 5.761539 5.768478 6.119269 5.475191 7.297446 7.179200 ... 4.998222 5.194883 5.042339 4.962759 5.484784 5.319114 5.262086 4.949878 4.985382 5.220989
2017-04-25 5.752799 5.941296 5.916937 5.862063 5.762989 5.769769 6.119269 5.475191 7.299989 7.182591 ... 5.016901 5.202434 5.043441 4.976697 5.486275 5.320556 5.262640 4.960537 4.988483 5.225250
2017-04-26 5.752799 5.940204 5.916937 5.856524 5.789205 5.763277 6.119269 5.475191 7.295955 7.182108 ... 5.046352 5.217573 5.040523 4.978625 5.489832 5.325030 5.264299 4.974314 4.998521 5.231250
2017-04-27 5.741710 5.938010 5.916937 5.856524 5.791018 5.760653 6.119269 5.475191 7.282964 7.181141 ... 5.059809 5.226809 5.051693 4.998732 5.497441 5.340252 5.290011 4.995082 5.012947 5.245646
2017-04-28 5.743205 5.941296 5.916937 5.860685 5.791470 5.765885 6.119269 5.475191 7.277212 7.182108 ... 5.045782 5.238803 5.050070 4.987948 5.492774 5.344018 5.296427 4.996148 5.010576 5.249171

99 rows × 3021 columns


In [677]:
endOfWeekList = sorted(list(set(LFCAPDF.iloc[LFCAPDF.resample('W').size().cumsum().sub(1)].index)))

In [678]:
endOfWeekList


Out[678]:
[Timestamp('2016-12-02 00:00:00'),
 Timestamp('2016-12-09 00:00:00'),
 Timestamp('2016-12-16 00:00:00'),
 Timestamp('2016-12-23 00:00:00'),
 Timestamp('2016-12-30 00:00:00'),
 Timestamp('2017-01-06 00:00:00'),
 Timestamp('2017-01-13 00:00:00'),
 Timestamp('2017-01-20 00:00:00'),
 Timestamp('2017-01-26 00:00:00'),
 Timestamp('2017-02-03 00:00:00'),
 Timestamp('2017-02-10 00:00:00'),
 Timestamp('2017-02-17 00:00:00'),
 Timestamp('2017-02-24 00:00:00'),
 Timestamp('2017-03-03 00:00:00'),
 Timestamp('2017-03-10 00:00:00'),
 Timestamp('2017-03-17 00:00:00'),
 Timestamp('2017-03-24 00:00:00'),
 Timestamp('2017-03-31 00:00:00'),
 Timestamp('2017-04-07 00:00:00'),
 Timestamp('2017-04-14 00:00:00'),
 Timestamp('2017-04-21 00:00:00'),
 Timestamp('2017-04-28 00:00:00')]

In [679]:
# calculate correlation between two factors
if  'Uqer' in sololist[0]:
    factor1 = sparedata.loc[startTime:endTime]
else:
    factor1 = pd.read_csv(path+sololist[0],infer_datetime_format=True,parse_dates=[0],index_col=0).loc[startTime:endTime]
#correlationDF = ff.showCorrelation(factor1,LFCAPDF, endOfWeekList, filterdic = None).astype(float)
#correlationDF.describe()

In [680]:
activeReturnData = ff.calcReturn(priceData, endOfWeekList, benchmark = benchMarkData,activeReturn = True,logReturn = False)

In [681]:
# Generate the useles stock list
filterdict={}
for i in endOfWeekList:
    suspendList = ff.GetSTNewSuspend(i,stDF,tradeDayDF,stopFlagDF)
    filterdict[i] = suspendList
    print i, len(filterdict[i])


2016-12-02 00:00:00 391
2016-12-09 00:00:00 392
2016-12-16 00:00:00 404
2016-12-23 00:00:00 413
2016-12-30 00:00:00 404
2017-01-06 00:00:00 408
2017-01-13 00:00:00 416
2017-01-20 00:00:00 422
2017-01-26 00:00:00 428
2017-02-03 00:00:00 432
2017-02-10 00:00:00 445
2017-02-17 00:00:00 448
2017-02-24 00:00:00 457
2017-03-03 00:00:00 449
2017-03-10 00:00:00 446
2017-03-17 00:00:00 448
2017-03-24 00:00:00 451
2017-03-31 00:00:00 444
2017-04-07 00:00:00 448
2017-04-14 00:00:00 456
2017-04-21 00:00:00 451
2017-04-28 00:00:00 453

In [682]:
### Calculate the return of each group of given factor(solo factor)
totalGroupDict ={}
factorData = ff.getData(thresholdNum, startTime, endTime,availableData = factor1)

for date in endOfWeekList:
    factorDataTemp = factorData.loc[:date].tail()
    factorDataTemp = factorDataTemp[list(set(factorDataTemp.columns.tolist())- set(filterdict[date]))]
    #print factorDataTemp
    totalGroupDict[date] = ff.getStockGroup(factorDataTemp,groupNum=10,Mean_Num=1)

In [683]:
factorData


Out[683]:
000005.SZ 600601.SH 600651.SH 600652.SH 600653.SH 000004.SZ 000002.SZ 000001.SZ 000009.SZ 600603.SH ... 300573.SZ 603990.SH 002826.SZ 002827.SZ 603036.SH 603928.SH 002829.SZ 002830.SZ 300571.SZ 603708.SH
2016-12-01 35.1852 46.4286 40.7895 48.3871 55.1020 29.4926 50.3546 79.3651 41.5929 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-02 26.7606 20.0000 28.0172 40.3226 42.3077 12.2850 47.4388 73.5294 34.4000 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-05 26.0274 20.4082 31.0811 42.3729 36.6667 17.2370 35.3516 66.6667 56.0000 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-06 24.6575 25.5814 25.2381 45.7627 35.4839 25.4522 24.1150 70.6667 45.8647 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-07 32.0513 30.4348 23.7864 50.0000 44.6154 36.5188 24.5495 67.1429 58.1699 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-08 24.3902 23.5294 13.3005 37.2881 37.8788 29.6000 22.9958 62.9032 56.6667 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-09 24.3902 25.0000 16.2651 36.0656 28.3333 31.2802 24.5243 65.1515 55.5556 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-12 17.8571 16.6667 5.2863 29.3333 22.6667 24.9518 19.3656 51.8987 43.5897 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-13 18.5841 7.4627 11.4894 24.6445 23.2877 32.7655 15.0338 36.1111 45.5446 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-14 12.1739 7.4627 14.0000 24.7619 18.8406 39.1945 15.1618 34.2466 44.0191 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-15 18.8679 10.2941 20.2312 26.5116 20.0000 57.4212 16.3445 28.7356 47.7833 46.0377 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-16 27.9279 15.1515 22.9050 24.7619 24.6377 59.9826 4.8000 31.2500 48.0392 47.8102 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-19 27.9279 13.4328 26.6304 16.2562 21.1268 59.4128 3.7795 25.0000 48.5437 49.6296 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-20 31.9588 35.1351 28.6550 25.7485 22.7273 61.3738 3.6364 23.8095 51.0204 56.1290 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-21 40.3846 48.1481 28.2353 30.7229 29.5082 64.0664 6.7241 31.2500 48.6631 65.2985 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-22 41.9048 42.6966 37.8947 30.3030 41.5385 59.3489 8.9501 27.8481 50.5495 66.1818 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-23 32.4561 41.3793 30.0000 21.6667 30.7692 48.3526 8.8435 26.1905 41.5584 62.1160 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-26 35.8491 43.3735 31.5789 26.9461 32.2581 53.1301 14.9228 26.1905 50.2793 70.8455 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-27 34.6154 43.9024 32.8502 30.2013 33.3333 56.1633 24.0429 12.0000 52.6316 56.3855 ... 100.0000 100.0000 100.0000 100.0000 100.0000 86.4334 100.0000 100.0000 100.0000 77.9395
2016-12-28 52.1739 66.6667 51.1278 52.2727 51.2821 71.4706 30.1923 14.5161 77.5862 50.7901 ... 100.0000 100.0000 100.0000 85.9764 86.2675 77.0370 100.0000 100.0000 100.0000 77.9395
2016-12-29 44.1558 63.1579 42.7419 48.9130 48.7805 71.0159 29.2910 19.6429 63.2812 57.0732 ... 100.0000 100.0000 87.2995 81.9973 78.4662 74.3902 100.0000 100.0000 100.0000 71.7046
2016-12-30 50.0000 58.0645 44.0945 51.0870 44.7368 69.4328 32.1721 23.2143 60.4478 62.1762 ... 100.0000 100.0000 82.7232 80.9489 77.3281 72.0449 88.6808 99.0812 100.0000 68.1637
2017-01-03 50.7246 60.6061 46.2121 54.0816 50.0000 56.7568 34.0637 40.4255 61.8705 56.6092 ... 100.0000 100.0000 82.9665 80.3036 77.3609 71.2000 88.4870 99.0409 100.0000 66.2313
2017-01-04 55.2632 62.3188 46.2121 56.3107 50.0000 51.6547 33.9321 40.4255 65.8065 49.6667 ... 100.0000 100.0000 80.3384 76.7611 74.1532 67.2076 88.0623 98.8698 100.0000 61.5066
2017-01-05 50.6494 65.7143 43.6508 57.4257 53.8462 53.2641 47.7212 46.5116 57.8947 47.7124 ... 100.0000 100.0000 77.7671 72.7263 69.0530 59.0415 84.8519 98.7894 100.0000 53.2840
2017-01-06 59.2105 51.7857 53.5714 51.6129 53.8462 50.4213 54.7692 52.6316 59.0604 33.3333 ... 100.0000 90.4143 70.9568 62.7287 60.3713 49.7152 75.9617 92.1549 100.0000 42.6685
2017-01-09 55.0725 44.8980 47.1074 46.5116 52.6316 34.7889 52.8846 48.5714 50.4065 33.3333 ... 100.0000 88.8798 66.7183 57.5325 55.1146 41.3915 74.1896 86.4430 100.0000 32.9443
2017-01-10 49.3151 53.6585 42.3423 41.1765 41.9355 40.0304 49.5114 51.5152 51.9685 29.9270 ... 94.0440 87.3893 63.3675 48.0855 48.7997 28.6105 75.3816 84.3700 100.0000 19.6440
2017-01-11 50.0000 44.6809 50.5376 59.3220 43.3333 44.0536 51.7007 60.7143 42.0382 28.0822 ... 87.6404 82.0662 54.9970 33.2798 41.4647 9.0680 66.1144 78.3529 94.5138 6.2500
2017-01-12 44.8718 46.6667 56.3218 40.8451 38.2353 35.4059 51.7007 56.0000 27.3973 8.8235 ... 86.7056 77.6083 46.1695 24.6237 33.6569 12.1896 64.3882 72.8787 94.3230 8.2312
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2017-03-16 52.1739 45.2830 54.9133 41.2281 48.7179 48.3019 65.2893 55.5556 46.9880 71.7143 ... 67.1388 75.4777 42.8082 77.1535 14.5902 69.7744 80.1681 80.3982 63.0865 48.5197
2017-03-17 48.6111 40.3509 46.8208 27.4336 46.1538 42.4547 69.5489 33.9286 38.5417 68.4713 ... 59.5477 68.1332 47.0514 77.6093 28.9110 62.3656 85.1717 80.8027 60.5966 45.9502
2017-03-20 51.4706 41.0714 45.2381 33.3333 48.6486 38.2289 63.5739 33.9286 39.7959 69.9088 ... 65.1290 71.8172 49.4611 78.9408 49.0915 65.7702 87.1815 88.1618 60.4204 49.6350
2017-03-21 52.1739 41.8182 53.5211 29.2453 50.0000 51.6704 67.3759 35.1852 27.9070 64.6875 ... 67.7046 72.3338 51.3825 77.0229 74.6900 70.8089 84.2416 90.4528 65.4268 58.8997
2017-03-22 35.3846 34.4828 46.0606 13.1783 45.7143 50.1149 64.6048 24.5614 24.7423 66.8622 ... 69.9258 75.5167 50.9872 68.5927 78.8615 66.4619 82.2622 91.5522 70.6418 54.9249
2017-03-23 37.0968 6.8182 48.1013 11.9718 30.3030 42.6326 68.3077 29.5082 25.0000 80.4487 ... 57.3803 67.1402 47.3571 65.2050 68.0483 58.2538 85.3176 67.4089 67.8911 56.9378
2017-03-24 33.8983 17.5000 51.5337 11.2583 32.3529 48.0088 63.6042 30.5085 30.6818 83.1956 ... 63.5553 69.6548 47.9781 66.6024 67.8391 56.1605 77.8865 67.2725 77.4023 58.8138
2017-03-27 33.3333 23.5294 59.0604 10.9677 16.6667 36.3436 61.8557 30.0000 32.9268 83.1956 ... 68.4665 73.5749 57.9974 76.9248 69.4380 59.9388 78.1161 66.6019 73.0052 59.1060
2017-03-28 32.2581 19.0476 55.3459 13.2075 20.8333 40.4255 61.5917 26.6667 39.0805 69.4253 ... 60.8835 57.3964 54.8512 76.9248 59.9887 52.6174 70.6157 54.8892 72.3153 56.0440
2017-03-29 31.2500 18.1818 55.9006 11.6667 19.2308 35.5967 54.9342 21.0526 31.5789 75.3275 ... 48.9177 46.6733 39.1730 55.1656 54.1693 35.4214 52.8379 45.6169 63.2794 38.9706
2017-03-30 26.6667 15.3846 52.6316 8.5202 15.6250 30.4577 30.4511 20.0000 26.3158 56.6794 ... 42.3221 43.0362 30.3030 43.1460 59.7864 31.6842 44.1225 42.2962 54.9607 33.4437
2017-03-31 31.0811 18.8679 52.6316 8.7156 25.8065 29.1246 35.3909 26.1538 30.0971 55.6180 ... 42.0742 44.5984 35.2282 37.7222 59.8483 25.2304 47.6306 36.8805 53.2183 33.8090
2017-04-05 35.4430 30.6452 52.6316 17.0833 36.1111 29.1246 35.6846 26.1538 32.7103 48.7544 ... 43.2930 42.1226 26.6546 22.4385 60.6728 26.3337 53.1840 39.2233 51.5468 29.0588
2017-04-06 38.1579 32.7586 59.6386 17.5966 38.8889 29.1246 34.0426 37.7778 37.6344 43.7383 ... 50.7279 45.8341 31.6492 24.7212 63.8610 38.2423 51.3201 46.8019 50.3128 32.8590
2017-04-07 42.6667 43.2836 48.1752 17.7489 40.5405 29.1246 41.5385 43.5897 32.5581 38.5841 ... 42.7481 41.4129 27.6254 19.6012 56.6208 34.8371 44.8937 43.0805 51.2708 26.4892
2017-04-10 26.3889 40.2778 51.9084 16.1417 39.4737 29.1246 36.1905 42.5000 43.2990 42.1667 ... 27.1267 32.1193 19.8957 14.5663 52.5289 27.7487 36.9557 35.2730 42.7296 20.1087
2017-04-11 51.6484 47.1429 53.1250 26.6667 39.4737 29.1246 41.1483 48.5714 54.6392 42.4497 ... 24.7070 26.9065 16.4855 22.1905 43.6410 26.4010 38.5877 21.4960 41.6667 23.8347
2017-04-12 60.3960 46.4789 50.7463 27.1186 42.8571 29.1246 39.4089 38.2353 62.5000 35.7782 ... 24.1874 27.5034 17.7388 21.6520 42.2889 25.7282 35.3965 30.6936 42.8651 11.3014
2017-04-13 55.1402 44.1176 46.1538 29.4931 42.8571 29.1246 36.7150 39.3939 56.3636 31.7774 ... 22.4450 24.0445 20.5521 20.8558 39.5326 33.4059 32.5866 25.0126 39.8280 12.3130
2017-04-14 48.7603 39.1892 37.3333 28.3186 40.5405 29.1246 36.5385 40.6250 46.6165 23.3871 ... 20.5590 14.0669 18.6538 19.8426 38.2003 33.9978 0.2906 24.5666 44.2274 12.7381
2017-04-17 47.5806 42.6471 33.3333 20.4819 35.7143 29.1246 33.0144 46.8750 42.3077 22.9249 ... 19.6647 14.5172 19.6078 15.6601 36.9817 33.8852 0.3201 21.9761 42.6717 12.6627
2017-04-18 46.8254 43.2836 26.7516 19.4656 35.7143 29.1246 35.9375 41.6667 43.6508 14.9897 ... 22.0610 17.6349 22.6757 19.4534 38.0430 45.6215 0.3560 24.7907 49.0148 17.4603
2017-04-19 48.3607 44.6154 25.9259 29.0598 34.8837 29.1246 50.0000 31.9149 47.0085 16.8981 ... 29.9732 26.1079 38.4848 30.2084 36.5302 37.6019 0.6146 31.4531 64.8466 28.3247
2017-04-20 45.9016 40.2985 23.5669 30.3965 27.9070 29.1246 51.6129 17.9487 49.1803 22.6852 ... 28.8521 22.2743 41.8338 31.4914 41.3553 31.1828 0.6928 42.3849 69.5818 24.8252
2017-04-21 42.6087 29.8246 12.4088 19.8068 13.8889 29.1246 53.1250 20.0000 36.4407 23.3890 ... 20.9577 15.5238 37.2032 25.9280 37.9631 24.9255 0.7914 37.0911 60.0512 19.6147
2017-04-24 36.3636 23.9437 9.0909 17.6724 8.0000 29.1246 39.0244 18.6047 26.8750 21.9239 ... 16.3185 11.5527 26.4355 25.9566 28.2129 16.4286 1.1633 17.9015 63.7660 16.2539
2017-04-25 33.3333 12.9032 8.5106 18.8034 8.0000 29.1246 42.8571 30.0000 27.7778 30.9795 ... 19.5586 19.1660 27.5085 30.1489 32.5605 12.1795 1.6815 18.5644 61.8891 18.2283
2017-04-26 36.0000 14.8148 6.6225 40.4858 7.6923 29.1246 43.1034 30.6122 21.4765 23.0964 ... 33.3333 29.4211 34.5895 50.3981 27.2792 14.7789 2.2408 29.8443 73.5890 37.1373
2017-04-27 15.1786 7.6923 7.6336 35.5263 7.5472 29.1246 29.0179 31.2500 13.1250 31.7016 ... 34.3475 29.6069 34.7651 47.9358 30.1152 28.3744 4.0943 37.3081 70.5224 38.2129
2017-04-28 5.0000 13.7255 14.7287 38.1395 14.2857 29.1246 15.1376 36.1702 6.0403 35.2804 ... 42.2244 30.9286 37.1593 54.5172 36.2188 31.7444 9.2778 41.0327 71.5827 44.4906

99 rows × 2678 columns


In [684]:
ReturnDF = pd.DataFrame(index=endOfWeekList[:-1],columns=totalGroupDict.values()[0].keys(),data=None, dtype =float)
sizeDistribution = pd.DataFrame(index=endOfWeekList[:-1],columns=totalGroupDict.values()[0].keys(),data=None, dtype =float)
for group in ReturnDF.columns.tolist():
    for time in ReturnDF.index:
        ReturnDF.loc[time][group] = activeReturnData.loc[time][totalGroupDict[time][group]].mean()
        sizeDistribution.loc[time][group] = LFCAPDF.loc[time][totalGroupDict[time][group]].quantile()
ReturnDF.sort_index(axis=1,inplace=True)
sizeDistribution.sort_index(axis=1,inplace=True)

In [685]:
# show size distribution(quantile plot)
fig = plt.figure(figsize=(16,10))
# Add a subplot
ax = fig.add_subplot(111)
sizeDistribution.median().plot(kind='bar',ax = ax, fontsize =13,title ='Size Distribution of each group',alpha =0.8)
ax.set_title(ax.get_title(),alpha=0.7, fontsize=25)


Out[685]:
<matplotlib.text.Text at 0x62322a58>

In [686]:
# Calc spearman correlation to investigate monotonicity
referSeries = pd.Series(index = ReturnDF.columns, data=range(len(ReturnDF.columns)))
monoDF = pd.DataFrame(index = ReturnDF.index, columns=['Spearman_Cor'], dtype = float)
for date in ReturnDF.index:
    monoDF.loc[date] = ReturnDF.loc[date].corr(referSeries,method='spearman')

In [687]:
# Plot
fig = plt.figure(figsize=(18,14))
# Add a subplot
ax = fig.add_subplot(111)
monoDF.plot(figsize=(22,14),ax=ax,fontsize =13,title ='Monotonicity')
ax.set_title(ax.get_title(),alpha=0.7, fontsize=30)


Out[687]:
<matplotlib.text.Text at 0x58269898>

In [688]:
# long top group short bot group 
sortGroups = ReturnDF[['group_0','group_9']].cumsum().iloc[-1].sort_values(ascending = False).index
top, bot = sortGroups[0], sortGroups[-1]

# Add another column
ReturnDF['top_bot_comb'] = ReturnDF[top] - ReturnDF[bot]

In [689]:
ReturnDF


Out[689]:
group_0 group_1 group_2 group_3 group_4 group_5 group_6 group_7 group_8 group_9 top_bot_comb
2016-12-02 0.005757 0.000134 -0.003690 0.000473 -0.000673 0.000234 -0.003519 -0.000732 -0.002690 -0.005619 -0.011376
2016-12-09 -0.023254 -0.012912 -0.011157 -0.011422 -0.016245 -0.004743 0.001444 0.004889 0.005469 0.012617 0.035871
2016-12-16 -0.002192 0.002198 0.001696 0.000640 0.003766 -0.000406 0.002810 0.000663 0.001569 -0.009521 -0.007329
2016-12-23 0.002112 0.004041 0.004323 0.007749 0.007927 0.009709 0.004568 0.004324 0.004661 -0.003301 -0.005413
2016-12-30 0.000737 0.000444 0.000018 -0.000927 -0.001555 0.001671 0.001438 -0.000214 -0.001569 -0.011786 -0.012523
2017-01-06 -0.019811 -0.019128 -0.012359 -0.009850 -0.010501 -0.003337 -0.007286 -0.006767 -0.004772 -0.001392 0.018419
2017-01-13 -0.014764 -0.013646 -0.016554 -0.015186 -0.013486 -0.015996 -0.013245 -0.002728 -0.003446 0.011738 0.026503
2017-01-20 0.003237 0.005115 0.001193 0.002937 0.000520 -0.003936 0.000376 -0.000197 -0.005501 -0.010101 -0.013338
2017-01-26 0.003152 0.000046 -0.001105 -0.001186 -0.001854 -0.000651 -0.002610 -0.001221 -0.000108 -0.004393 -0.007545
2017-02-03 0.013617 0.011766 0.011518 0.009881 0.006973 0.004997 0.002841 0.007022 0.009363 -0.000190 -0.013807
2017-02-10 -0.002581 -0.001146 -0.001624 -0.001870 -0.000671 -0.001270 -0.000829 -0.001293 0.001393 0.004262 0.006843
2017-02-17 0.005342 0.007835 0.010249 0.002076 0.004283 0.002840 -0.005328 -0.004534 -0.009328 -0.015913 -0.021255
2017-02-24 0.000473 -0.001538 0.001060 0.001329 -0.004478 -0.003192 -0.002737 -0.005495 -0.007470 -0.002146 -0.002619
2017-03-03 0.006602 0.007951 0.005573 0.005198 0.012006 0.006156 0.006793 0.005055 -0.000661 0.003180 -0.003422
2017-03-10 -0.000963 0.003145 0.001189 -0.002791 0.002780 -0.000110 0.002591 0.002187 0.005759 0.004536 0.005499
2017-03-17 -0.012116 -0.007366 -0.008572 -0.005333 -0.005504 -0.009505 -0.001383 0.005365 0.005229 -0.006137 0.005979
2017-03-24 -0.006858 -0.009368 -0.006711 -0.010182 -0.010000 -0.015219 -0.016785 -0.024519 -0.026240 -0.013624 -0.006766
2017-03-31 0.004182 0.001851 0.000569 0.001841 -0.003090 -0.001410 -0.004675 -0.003564 -0.000823 -0.006349 -0.010531
2017-04-07 -0.036023 -0.027274 -0.028024 -0.021919 -0.017841 -0.016448 -0.010536 -0.010326 -0.011186 0.020479 0.056502
2017-04-14 -0.023014 -0.015541 -0.015434 -0.012264 -0.015504 -0.015708 -0.007461 -0.004181 -0.012303 -0.021745 0.001269
2017-04-21 0.002927 0.008626 0.003234 0.000168 0.000853 -0.005290 -0.002873 -0.002189 -0.003622 0.022170 0.019244

In [690]:
# Net worth plot 
netWorthDF = (ReturnDF.astype(float)+1).cumprod()
fig = plt.figure(figsize=(18,14))
# Add a subplot
ax = fig.add_subplot(111)
netWorthDF.plot(figsize=(22,14),ax=ax,color=sns.color_palette("Paired",11),title ='Net Worth',fontsize =13)
ax.set_title(ax.get_title(),alpha=0.7, fontsize=30, ha='right')


Out[690]:
<matplotlib.text.Text at 0x58269780>

In [691]:
# Calc spearman correlation to investigate monotonicity
referSeries = pd.Series(index = netWorthDF.iloc[:,:-1].columns, data=range(len(netWorthDF.iloc[:,:-1].columns)))
monoDF = pd.DataFrame(index = netWorthDF.index, columns=['Spearman_Cor'], dtype = float)
for date in netWorthDF.index:
    monoDF.loc[date] = netWorthDF.iloc[:,:-1].loc[date].corr(referSeries,method='spearman')

In [692]:
# Plot
fig = plt.figure(figsize=(18,14))
# Add a subplot
ax = fig.add_subplot(111)
monoDF.plot(figsize=(22,14),ax=ax,fontsize =13,title ='Monotonicity')
ax.set_title(ax.get_title(),alpha=0.7, fontsize=30)


Out[692]:
<matplotlib.text.Text at 0x644f3fd0>

In [693]:
# basic indicator
annualizedReturn = (1+ReturnDF.mean())**50 - 1
annualizedVol = ReturnDF.std()* np.sqrt(50)
sharpeRatio = annualizedReturn / annualizedVol 
print 'Annual Return:','\n',annualizedReturn,'\n\n','Annual Volatility:','\n',annualizedVol,'\n\n','Sharpe Ratio:''\n',sharpeRatio


Annual Return: 
group_0        -0.199860
group_1        -0.122403
group_2        -0.142785
group_3        -0.134624
group_4        -0.138035
group_5        -0.157014
group_6        -0.125830
group_7        -0.087565
group_8        -0.125561
group_9        -0.076135
top_bot_comb    0.153892
dtype: float64 

Annual Volatility: 
group_0         0.088196
group_1         0.072549
group_2         0.067400
group_3         0.056191
group_4         0.059022
group_5         0.052688
group_6         0.042047
group_7         0.048260
group_8         0.055904
group_9         0.080525
top_bot_comb    0.134673
dtype: float64 

Sharpe Ratio:
group_0        -2.266101
group_1        -1.687174
group_2        -2.118469
group_3        -2.395854
group_4        -2.338720
group_5        -2.980044
group_6        -2.992631
group_7        -1.814449
group_8        -2.246012
group_9        -0.945479
top_bot_comb    1.142703
dtype: float64

In [694]:
copyReturn = ReturnDF.copy()
copyReturn[copyReturn > copyReturn.mean()] =0
downsideRisk = ReturnDF.std(skipna = True) * np.sqrt(12)
downsideRisk


Out[694]:
group_0         0.043207
group_1         0.035542
group_2         0.033019
group_3         0.027528
group_4         0.028915
group_5         0.025812
group_6         0.020599
group_7         0.023642
group_8         0.027387
group_9         0.039449
top_bot_comb    0.065976
dtype: float64

In [695]:
sortinoRatio = annualizedReturn / downsideRisk
sortinoRatio


Out[695]:
group_0        -4.625659
group_1        -3.443930
group_2        -4.324307
group_3        -4.890516
group_4        -4.773892
group_5        -6.082990
group_6        -6.108682
group_7        -3.703729
group_8        -4.584653
group_9        -1.929950
top_bot_comb    2.332533
dtype: float64

In [696]:
# Max drawdown
maxdd = netWorthDF.copy()
maxdd.iloc[0] = 0
for date in netWorthDF.index[1:]:
    maxdd.loc[date] = 1-netWorthDF.loc[date]/netWorthDF.loc[:date].max()
maxddInfo = pd.concat([maxdd.max(),maxdd.idxmax()],axis=1)
maxddInfo.columns = ['Max_drawdown','Time']
maxddInfo


Out[696]:
Max_drawdown Time
group_0 0.098678 2017-04-14
group_1 0.062581 2017-04-14
group_2 0.063081 2017-04-14
group_3 0.060123 2017-04-14
group_4 0.061308 2017-04-14
group_5 0.075686 2017-04-21
group_6 0.061580 2017-04-21
group_7 0.046944 2017-04-21
group_8 0.063834 2017-04-21
group_9 0.061420 2017-04-14
top_bot_comb 0.059694 2017-03-31

In [697]:
# calmar Ratio
calmarRatio = annualizedReturn/ maxddInfo['Max_drawdown']
calmarRatio


Out[697]:
group_0        -2.025378
group_1        -1.955906
group_2        -2.263500
group_3        -2.239138
group_4        -2.251494
group_5        -2.074544
group_6        -2.043372
group_7        -1.865319
group_8        -1.966981
group_9        -1.239586
top_bot_comb    2.578002
dtype: float64

In [698]:
# show the difference of a risk factor through different market capitalzation size
# capdata should not contain Nan value
# Return: DICTIONARY, the KEY is the date and the Value is the tuple of the groups
# Inputs: 
# capdata: DATAFRAME ,the LFCAP DATA
# datelist: LIST, the datelist of the end month
def getGroupsbyCap(capdata, datelist):
    capdict ={}
    for date in datelist:
        capdataindice = capdata.loc[date]
        lower = capdataindice.quantile(1/3)
        upper = capdataindice.quantile(2/3)
        smallcap = capdataindice[capdataindice<=lower].index
        midcap = capdataindice[(lower<capdataindice) & (capdataindice<=upper)].index
        hugecap = capdataindice[capdataindice>upper].index
        capdict[date] = (smallcap,midcap,hugecap)
    return capdict

In [699]:
# show the difference of a risk factor through different industry
# capdata should not contain Nan value
# Return: DICTIONARY, the KEY is the date and the Value is the DICTIONARY of the groups of each industry on that day
# Inputs: 
# datelist: LIST, the datelist of the end month
# grouplable: LIST, the industry label,usually 3 of them is constant and other 3 is randomed  alike [2,3,6,15,18,25]
# industryDF: DATAFRAME,the industry dataframe(could )
def getIndustryDict(datelist,grouplabel,industryDF):
    industrydict = {}
    industryDF = industryDF.loc[datelist]
    for date in datelist:
        industryDFindice = industryDF.loc[date]
        industrydict[date] = {label:industryDFindice[industryDFindice == label].index for label in grouplabel}
    return industrydict

In [700]:
# industry number
grouplabel = np.random.choice(29,10,replace=False)
grouplabel


Out[700]:
array([ 6,  2, 11,  8,  3,  1, 16, 28, 26, 10])

In [701]:
wholeIndList = np.array(range(29))
wholeIndDict = getIndustryDict(endOfWeekList,wholeIndList,indusDF)

In [702]:
multindexList = [endOfWeekList,wholeIndList]
induReturnDF  = pd.DataFrame(data=None, columns=totalGroupDict.values()[0].keys(),\
                             index=pd.MultiIndex.from_product(multindexList,names=['time','industry']),dtype=float)

In [703]:
groupNumberThrottle = 10
for i,j in wholeIndDict.iteritems():
    print i
    factorIndice = factorData.loc[:i].tail()
    factorIndice = factorIndice[list(set(factorIndice.columns.tolist())- set(filterdict[date]))]  # Remove ST ,new and suspend stk
    for ind, stk in j.iteritems():
        intersection = list(set(factorIndice.columns.tolist()) & set(stk))
        if len(intersection) < groupNumberThrottle:
            induReturnDF.loc[i,ind] = 0
            continue
        else:
            stkgroup = ff.getStockGroup(factorIndice[intersection], groupNum=10, Mean_Num=1)
            for p,q in stkgroup.iteritems():
                try:
                    induReturnDF.loc[i,ind][p] = activeReturnData.loc[i][q].mean()
                except:
                    induReturnDF.loc[i,ind][p] = np.NaN


2017-01-13 00:00:00
2017-01-20 00:00:00
2016-12-30 00:00:00
2016-12-02 00:00:00
2017-01-26 00:00:00
2017-02-17 00:00:00
2017-03-17 00:00:00
2017-03-24 00:00:00
2017-03-31 00:00:00
2016-12-09 00:00:00
2017-04-28 00:00:00
2016-12-16 00:00:00
2017-02-10 00:00:00
2017-04-14 00:00:00
2017-04-21 00:00:00
2017-01-06 00:00:00
2017-03-03 00:00:00
2016-12-23 00:00:00
2017-03-10 00:00:00
2017-02-24 00:00:00
2017-02-03 00:00:00
2017-04-07 00:00:00

In [704]:
finalReturn = induReturnDF.mean(level = 'time')
finalReturn.sort_index(axis=1,inplace=True)

In [705]:
sortGroups = finalReturn[['group_0','group_9']].cumsum().iloc[-1].sort_values(ascending = False).index
top, bot = sortGroups[0], sortGroups[-1]

# Add another column
finalReturn['top_bot_comb'] = finalReturn[top] - finalReturn[bot]

In [706]:
indNetWorth = (finalReturn+1).cumprod()
fig = plt.figure(figsize=(14,9))
# Add a subplot
ax = fig.add_subplot(111)
indNetWorth.plot(figsize=(22,14),ax=ax,color=sns.color_palette("Paired",11),title ='Net Worth',fontsize =13)
ax.set_title(ax.get_title(),alpha=0.7, fontsize=30, ha='right')


Out[706]:
<matplotlib.text.Text at 0x57a2ba90>