Ticker selections

[1]:
# If you would like to refresh your data, please execute the bellow codes.

import pandas as pd
import numpy as np
from datetime import datetime
from tqdm import tqdm

from mypo import Loader

DOWNLOAD = False

if DOWNLOAD:
    tickers = pd.read_csv("/app/docs/tutorial/tickers.csv")
    loader = Loader()
    for row in tqdm(tickers.to_dict('record')):
        loader.get(row['Ticker'], expense_ratio=row['ExpenseRatio'] * 0.01)
    loader.save('/app/docs/tutorial/all.bin')
[2]:
loader = Loader.load('/app/docs/tutorial/all.bin')
loader = loader.since(datetime(2005, 1, 1))
market = loader.get_market()
[3]:
tickers = market.get_tickers()
tickers.remove('SHY')
tickers.remove('AGG')
market = market.filter(tickers)
[4]:
pd.set_option('display.max_rows', 200)

summary = loader.summary()
summary = summary.sort_index()
summary
[4]:
established names total_assets volume expense_ratio
AGG 2003-09-29 iShares Core U.S. Aggregate Bond ETF 84365787136 5507550 0.0005
BBH 1999-11-23 VanEck Vectors Biotech ETF 494324384 4925 0.0035
DIA 1998-01-20 SPDR Dow Jones Industrial Average ETF Trust 25793642496 2856683 0.0016
DVY 2003-11-07 iShares Select Dividend ETF 16377193472 937025 0.0039
EEM 2003-04-14 iShares MSCI Emerging Markets ETF 30559182848 32215583 0.0070
EFA 2001-08-27 iShares MSCI EAFE ETF 54545563648 18116250 0.0032
EPP 2001-10-26 iShares MSCI Pacific ex Japan ETF 2342888704 223450 0.0048
EWG 1996-03-18 iShares MSCI Germany ETF 2596413952 3138675 0.0051
EWJ 1996-03-18 iShares MSCI Japan ETF 13236024320 5345900 0.0051
EWM 1996-03-18 iShares MSCI Malaysia ETF 380267392 275083 0.0051
EWS 1996-03-18 iShares MSCI Singapore ETF 687225344 478000 0.0051
EWT 2000-06-23 iShares MSCI Taiwan ETF 6670504448 3766516 0.0059
EWW 1996-03-18 iShares MSCI Mexico ETF 1102177408 2273950 0.0051
EWY 2000-05-12 iShares MSCI South Korea ETF 7541572096 3984233 0.0059
EWZ 2000-07-14 iShares MSCI Brazil ETF 5664168448 27174216 0.0059
EZA 2003-02-07 iShares MSCI South Africa ETF 363515744 370600 0.0059
FEZ 2002-10-21 SPDR EURO STOXX 50 ETF 1917581312 1728550 0.0029
FVD 2003-08-27 First Trust Value Line Dividend Index Fund 11073531904 1407750 0.0070
FXI 2004-10-08 iShares China Large-Cap ETF 4111610368 16967183 0.0074
GLD 2004-11-18 SPDR Gold Shares 61256667136 6901550 0.0040
IBB 2001-02-12 iShares Nasdaq Biotechnology ETF 10739421184 3211016 0.0046
IEF 2002-07-30 iShares 7-10 Year Treasury Bond ETF 13733128192 7309550 0.0015
IEV 2000-07-28 iShares Europe ETF 1614006528 145116 0.0059
IJH 2000-05-26 iShares Core S&P Mid-Cap ETF 61098835968 1314116 0.0007
IJR 2000-05-26 iShares Core S&P Small-Cap ETF 65687891968 3001550 0.0007
ILF 2001-10-26 iShares Latin America 40 ETF 1607567872 1657283 0.0048
IOO 2000-12-08 iShares Global 100 ETF 2973064960 34883 0.0040
IVV 2000-05-19 iShares Core S&P 500 ETF 248764645376 4507483 0.0004
IWM 2000-05-26 iShares Russell 2000 ETF 68774731776 20937900 0.0019
IXC 2001-11-26 iShares Global Energy ETF 1312181760 2375483 0.0046
IXG 2001-11-26 iShares Global Financials ETF 392448096 425383 0.0046
IXJ 2001-11-26 iShares Global Healthcare ETF 2635773184 103633 0.0046
IXN 2001-11-26 iShares Global Tech ETF 5045208576 49583 0.0046
IXP 2001-11-26 iShares Global Comm Services ETF 315172352 11533 0.0046
IYR 2000-06-19 iShares U.S. Real Estate ETF 5353554432 9377175 0.0042
LQD 2002-07-30 iShares iBoxx $ Investment Grade Corporate Bon... 41837125632 17913600 0.0014
OEF 2000-10-27 iShares S&P 100 ETF 6919864320 323800 0.0020
OIH 2001-02-26 VanEck Vectors Oil Services ETF 1194708480 493325 0.0035
PPH 2000-02-01 VanEck Vectors Pharmaceutical ETF 233600000 46475 0.0035
QQQ 1999-03-10 Invesco QQQ Trust 153760448512 52490450 0.0020
RWR 2001-08-27 SPDR Dow Jones REIT ETF 1422119168 87933 0.0025
SHY 2002-07-30 iShares 1-3 Year Treasury Bond ETF 19571066880 3122775 0.0015
SPTM 2000-10-10 SPDR Portfolio S&P 1500 Composite Stock Market... 4653971456 445875 0.0003
SPY 1993-01-29 SPDR S&P 500 ETF Trust 345001230336 99175775 0.0009
SPYG 2000-10-02 SPDR Portfolio S&P 500 Growth ETF 9469054976 1522550 0.0004
SPYV 2000-10-02 SPDR Portfolio S&P 500 Value ETF 8475042304 3269150 0.0004
TIP 2003-12-05 iShares TIPS Bond ETF 26081570816 2258625 0.0019
TLT 2002-07-30 iShares 20+ Year Treasury Bond ETF 14208894976 15786525 0.0015
VAW 2004-01-30 Vanguard Materials Index Fund ETF Shares 3829136896 194300 0.0010
VB 2004-01-30 Vanguard Small-Cap Index Fund ETF Shares 129391697920 591325 0.0005
VBK 2004-01-30 Vanguard Small-Cap Growth Index Fund ETF Shares 35945959424 223525 0.0007
VBR 2004-01-30 Vanguard Small-Cap Value Index Fund ETF Shares 44636930048 877325 0.0007
VCR 2004-01-30 Vanguard Consumer Discretionary Index Fund ETF... 6264308224 108175 0.0010
VDC 2004-01-30 Vanguard Consumer Staples Index Fund ETF Shares 6259133440 124450 0.0010
VDE 2004-09-29 Vanguard Energy Index Fund ETF Shares 5838627328 1247400 0.0010
VFH 2004-01-30 Vanguard Financials Index Fund ETF Shares 10025631744 1219100 0.0010
VGT 2004-01-30 Vanguard Information Technology Index Fund ETF... 47492431872 389200 0.0010
VHT 2004-01-30 Vanguard Health Care Index Fund ETF Shares 16237583360 210850 0.0010
VIS 2004-09-29 Vanguard Industrials Index Fund ETF Shares 5183167488 247425 0.0010
VO 2004-01-30 Vanguard Mid-Cap Index Fund ETF Shares 142442233856 615025 0.0004
VOX 2004-09-29 Vanguard Communication Services Index Fund ETF... 3716337408 134800 0.0010
VPU 2004-01-30 Vanguard Utilities Index Fund ETF Shares 6042439680 260725 0.0010
VTI 2001-06-15 Vanguard Total Stock Market Index Fund ETF Shares 1154018181120 4640375 0.0003
VTV 2004-01-30 Vanguard Value Index Fund ETF Shares 114191532032 2533300 0.0004
VUG 2004-01-30 Vanguard Growth Index Fund ETF Shares 147317587968 688816 0.0004
VV 2004-01-30 Vanguard Large-Cap Index Fund ETF Shares 34485399552 258600 0.0004
VXF 2002-01-04 Vanguard Extended Market Index Fund ETF Shares 106090209280 362275 0.0006
XLB 1998-12-22 Materials Select Sector SPDR Fund 6798151680 6827550 0.0013
XLE 1998-12-22 Energy Select Sector SPDR Fund 20450129920 26723225 0.0013
XLF 1998-12-22 Financial Select Sector SPDR Fund 34379612160 57400300 0.0013
XLI 1998-12-22 Industrial Select Sector SPDR Fund 16734948352 11592925 0.0013
XLK 1998-12-22 Technology Select Sector SPDR Fund 37336981504 7541575 0.0013
XLP 1998-12-22 Consumer Staples Select Sector SPDR Fund 10185106432 16333225 0.0013
XLU 1998-12-22 Utilities Select Sector SPDR Fund 10588869632 12273900 0.0013
XLV 1998-12-22 Health Care Select Sector SPDR Fund 24046927872 7751075 0.0013
XLY 1998-12-22 Consumer Discretionary Select Sector SPDR Fund 18783365120 4094575 0.0013
[5]:
market.get_summary().sort_index()
[5]:
daily return variance sharpe ratio expense ratio
BBH 0.000469 0.000261 1.799338 0.0035
DIA 0.000354 0.000142 2.485636 0.0016
DVY 0.000244 0.000161 1.520327 0.0039
EEM 0.000395 0.000350 1.129411 0.0070
EFA 0.000199 0.000197 1.005975 0.0032
EPP 0.000269 0.000264 1.019724 0.0048
EWG 0.000295 0.000270 1.093817 0.0051
EWJ 0.000203 0.000174 1.165225 0.0051
EWM 0.000085 0.000194 0.438464 0.0051
EWS 0.000236 0.000238 0.990978 0.0051
EWT 0.000364 0.000265 1.374327 0.0059
EWW 0.000333 0.000328 1.015153 0.0051
EWY 0.000480 0.000396 1.212547 0.0059
EWZ 0.000450 0.000616 0.729426 0.0059
EZA 0.000327 0.000509 0.641726 0.0059
FEZ 0.000191 0.000288 0.664141 0.0029
FVD 0.000297 0.000128 2.329348 0.0070
FXI 0.000437 0.000437 0.998962 0.0074
GLD 0.000385 0.000131 2.936049 0.0040
IBB 0.000558 0.000226 2.467879 0.0046
IEF 0.000078 0.000017 4.616812 0.0015
IEV 0.000198 0.000222 0.889878 0.0059
IJH 0.000450 0.000195 2.302109 0.0007
IJR 0.000464 0.000228 2.039541 0.0007
ILF 0.000397 0.000468 0.847993 0.0048
IOO 0.000277 0.000161 1.720624 0.0040
IVV 0.000378 0.000149 2.534072 0.0004
IWM 0.000429 0.000238 1.801028 0.0019
IXC 0.000171 0.000320 0.533886 0.0046
IXG 0.000193 0.000298 0.647012 0.0046
IXJ 0.000354 0.000118 2.989761 0.0046
IXN 0.000533 0.000190 2.805024 0.0046
IXP 0.000194 0.000151 1.285247 0.0046
IYR 0.000304 0.000356 0.855545 0.0042
LQD 0.000052 0.000029 1.791466 0.0014
OEF 0.000362 0.000144 2.506807 0.0020
OIH 0.000070 0.000640 0.109930 0.0035
PPH 0.000229 0.000121 1.894621 0.0035
QQQ 0.000604 0.000180 3.357576 0.0020
RWR 0.000334 0.000408 0.818409 0.0025
SPTM 0.000395 0.000162 2.436931 0.0003
SPY 0.000378 0.000151 2.504204 0.0009
SPYG 0.000461 0.000147 3.135345 0.0004
SPYV 0.000286 0.000154 1.850309 0.0004
TIP 0.000052 0.000015 3.399998 0.0019
TLT 0.000149 0.000080 1.852575 0.0015
VAW 0.000408 0.000248 1.644935 0.0010
VB 0.000449 0.000216 2.082026 0.0005
VBK 0.000515 0.000216 2.381213 0.0007
VBR 0.000387 0.000229 1.687121 0.0007
VCR 0.000519 0.000182 2.847000 0.0010
VDC 0.000334 0.000084 3.962130 0.0010
VDE 0.000264 0.000376 0.700395 0.0010
VFH 0.000298 0.000333 0.895488 0.0010
VGT 0.000592 0.000192 3.093324 0.0010
VHT 0.000451 0.000123 3.665985 0.0010
VIS 0.000414 0.000196 2.106982 0.0010
VO 0.000439 0.000184 2.378992 0.0004
VOX 0.000308 0.000167 1.850033 0.0010
VPU 0.000292 0.000142 2.047491 0.0010
VTI 0.000396 0.000153 2.582378 0.0003
VTV 0.000309 0.000161 1.926668 0.0004
VUG 0.000481 0.000153 3.148496 0.0004
VV 0.000394 0.000150 2.622159 0.0004
VXF 0.000471 0.000205 2.302768 0.0006
XLB 0.000377 0.000236 1.599943 0.0013
XLE 0.000274 0.000379 0.723470 0.0013
XLF 0.000298 0.000392 0.758883 0.0013
XLI 0.000387 0.000188 2.063559 0.0013
XLK 0.000542 0.000186 2.916631 0.0013
XLP 0.000310 0.000082 3.769109 0.0013
XLU 0.000286 0.000144 1.982567 0.0013
XLV 0.000410 0.000118 3.481725 0.0013
XLY 0.000487 0.000180 2.697806 0.0013

Clustering Tickers

[6]:
import matplotlib.pyplot as plt
from mypo import clustering_tickers, evaluate_combinations

CLUSTERS = 3
cluster = clustering_tickers(market, n=CLUSTERS)
corr = market.get_rate_of_change()[cluster.index].corr()

plt.figure()
plt.imshow(corr,interpolation='nearest',vmin=-1,vmax=1,cmap='viridis')
plt.colorbar()
plt.show()

corr
../_images/tutorial_tickers_7_0.png
[6]:
VTI SPTM IXN VIS IYR EWZ VDE FXI VPU VCR ... XLV DIA EEM XLE PPH GLD TIP LQD TLT IEF
VTI 1.000000 0.874032 0.908474 0.935760 0.784825 0.725067 0.781091 0.735558 0.715034 0.928490 ... 0.828526 0.963858 0.846202 0.780647 0.781658 0.050460 -0.169573 0.137284 -0.413045 -0.402994
SPTM 0.874032 1.000000 0.813975 0.855297 0.739293 0.628250 0.672949 0.656638 0.605816 0.844529 ... 0.706793 0.845677 0.739309 0.684152 0.675504 0.021784 -0.180869 0.041749 -0.395530 -0.390168
IXN 0.908474 0.813975 1.000000 0.833987 0.670844 0.675606 0.657394 0.708618 0.591379 0.858729 ... 0.727634 0.870259 0.808089 0.654222 0.683015 0.055647 -0.158921 0.133869 -0.363048 -0.358632
VIS 0.935760 0.855297 0.833987 1.000000 0.752864 0.690452 0.760820 0.689471 0.649736 0.891841 ... 0.740823 0.921489 0.793111 0.754623 0.700288 0.029920 -0.176778 0.109340 -0.420418 -0.411690
IYR 0.784825 0.739293 0.670844 0.752864 1.000000 0.567091 0.565640 0.594224 0.633056 0.765145 ... 0.578187 0.742990 0.683099 0.565272 0.552549 0.050858 -0.098125 0.135713 -0.237456 -0.234246
EWZ 0.725067 0.628250 0.675606 0.690452 0.567091 1.000000 0.691474 0.687005 0.559497 0.645134 ... 0.543694 0.701288 0.834757 0.689225 0.528428 0.180725 -0.057231 0.163644 -0.288678 -0.275663
VDE 0.781091 0.672949 0.657394 0.760820 0.565640 0.691474 1.000000 0.618239 0.584692 0.664342 ... 0.579016 0.760044 0.727975 0.988083 0.568269 0.160815 -0.073870 0.111601 -0.359865 -0.335616
FXI 0.735558 0.656638 0.708618 0.689471 0.594224 0.687005 0.618239 1.000000 0.513776 0.672954 ... 0.566737 0.711172 0.893423 0.618700 0.548833 0.094576 -0.162521 0.111763 -0.318691 -0.341356
VPU 0.715034 0.605816 0.591379 0.649736 0.633056 0.559497 0.584692 0.513776 1.000000 0.605303 ... 0.630757 0.714071 0.614229 0.589604 0.595782 0.104062 -0.051022 0.213702 -0.204574 -0.186291
VCR 0.928490 0.844529 0.858729 0.891841 0.765145 0.645134 0.664342 0.672954 0.605303 1.000000 ... 0.732895 0.884817 0.772409 0.658766 0.689500 0.000638 -0.163802 0.114292 -0.376429 -0.375201
EWT 0.731336 0.644902 0.741142 0.685231 0.585628 0.656549 0.597268 0.771847 0.510484 0.676597 ... 0.554499 0.706733 0.846380 0.596448 0.549467 0.097493 -0.116335 0.129952 -0.286099 -0.296164
XLB 0.873546 0.777528 0.781752 0.870915 0.673261 0.721696 0.780412 0.679643 0.619028 0.797679 ... 0.695497 0.846721 0.798349 0.785268 0.644698 0.181047 -0.134488 0.094961 -0.392961 -0.373050
OEF 0.977826 0.863448 0.907419 0.898531 0.753546 0.699647 0.742480 0.725338 0.699440 0.902390 ... 0.829873 0.963127 0.826142 0.745472 0.786948 0.029568 -0.183504 0.125700 -0.410413 -0.402164
EWY 0.758762 0.678068 0.743557 0.722176 0.613274 0.701187 0.646659 0.797250 0.570034 0.692755 ... 0.593756 0.745861 0.886074 0.644519 0.578538 0.123845 -0.121060 0.123690 -0.323018 -0.327132
SPYV 0.942865 0.868373 0.821193 0.914033 0.773770 0.695982 0.775682 0.691805 0.706766 0.868574 ... 0.793099 0.930133 0.792468 0.780594 0.752271 0.038470 -0.184155 0.106838 -0.434188 -0.415751
VDC 0.830550 0.735073 0.726988 0.770723 0.667648 0.597127 0.595593 0.590934 0.756152 0.768713 ... 0.766400 0.843690 0.687999 0.596527 0.727549 0.027455 -0.156899 0.124303 -0.315616 -0.301876
VAW 0.891212 0.806289 0.795902 0.898644 0.703275 0.739017 0.807997 0.702047 0.632533 0.821398 ... 0.686839 0.854547 0.819193 0.802711 0.652211 0.182821 -0.135516 0.103090 -0.396313 -0.379684
VOX 0.860004 0.754435 0.788908 0.793229 0.687781 0.645314 0.642067 0.649654 0.636605 0.823496 ... 0.698908 0.821510 0.741646 0.633850 0.671293 0.046653 -0.118120 0.141952 -0.314049 -0.308026
IOO 0.947844 0.844992 0.891564 0.885014 0.726631 0.734466 0.760452 0.752664 0.685545 0.866236 ... 0.794026 0.934029 0.861887 0.762687 0.769482 0.100647 -0.164880 0.136741 -0.413661 -0.397665
EZA 0.750110 0.684733 0.704937 0.712499 0.596773 0.750076 0.674883 0.723618 0.554128 0.686011 ... 0.581148 0.728984 0.856404 0.676246 0.578619 0.244017 -0.074989 0.162983 -0.278829 -0.270969
EWM 0.631712 0.549152 0.598847 0.595875 0.502015 0.615533 0.534825 0.641721 0.479535 0.571045 ... 0.500877 0.608729 0.716479 0.531159 0.487845 0.137863 -0.092714 0.134801 -0.266771 -0.261730
IXG 0.889129 0.810851 0.791586 0.870165 0.772634 0.699656 0.713663 0.727478 0.598903 0.827233 ... 0.681775 0.861959 0.811381 0.711124 0.656148 0.036755 -0.166667 0.120974 -0.429966 -0.416618
BBH 0.554814 0.479360 0.515821 0.480494 0.365906 0.349453 0.379052 0.374117 0.366230 0.502179 ... 0.664417 0.510040 0.424125 0.380557 0.597585 -0.003733 -0.090159 0.095332 -0.231169 -0.217097
EWS 0.762792 0.686465 0.722429 0.728062 0.632305 0.689237 0.644613 0.802276 0.565568 0.700506 ... 0.587507 0.748702 0.853226 0.643946 0.567969 0.132756 -0.125129 0.136085 -0.318556 -0.322753
EWW 0.771748 0.671459 0.707072 0.743113 0.633488 0.773270 0.674862 0.702304 0.579400 0.716710 ... 0.602039 0.747738 0.833065 0.676933 0.574887 0.140377 -0.104224 0.129544 -0.322250 -0.311365
OIH 0.697825 0.599992 0.592498 0.695612 0.489357 0.638084 0.930946 0.548991 0.473022 0.597548 ... 0.484762 0.668415 0.652350 0.918825 0.483078 0.153128 -0.059154 0.102210 -0.342869 -0.316507
IXC 0.797679 0.694654 0.690333 0.776555 0.579990 0.730808 0.965400 0.655674 0.599891 0.683255 ... 0.600109 0.784699 0.769985 0.963587 0.593394 0.184852 -0.067246 0.145518 -0.366756 -0.339898
RWR 0.763798 0.723468 0.648666 0.740655 0.980906 0.548719 0.558401 0.577935 0.615912 0.745886 ... 0.565844 0.729905 0.662777 0.558863 0.536763 0.039283 -0.109593 0.113301 -0.239872 -0.237294
ILF 0.789626 0.684480 0.729430 0.756719 0.628640 0.960970 0.748733 0.739767 0.593621 0.711500 ... 0.594684 0.764575 0.885351 0.747313 0.577461 0.171530 -0.089834 0.168066 -0.323494 -0.308311
IEV 0.876540 0.773872 0.809294 0.840219 0.686897 0.727709 0.740804 0.736160 0.635680 0.798877 ... 0.712943 0.857855 0.848202 0.742438 0.706036 0.148232 -0.124386 0.157867 -0.391591 -0.369752
FEZ 0.847863 0.744403 0.785757 0.817423 0.658790 0.694688 0.709199 0.697639 0.607408 0.775538 ... 0.695782 0.831282 0.815006 0.711351 0.682663 0.132800 -0.135793 0.139806 -0.392121 -0.370629
EPP 0.842277 0.743370 0.785044 0.805319 0.682800 0.745849 0.724790 0.808325 0.631454 0.764594 ... 0.661451 0.824595 0.878926 0.723474 0.646479 0.165893 -0.115546 0.169311 -0.360859 -0.351430
EWG 0.838300 0.742657 0.786528 0.811998 0.648098 0.683953 0.693267 0.699103 0.594979 0.772176 ... 0.682749 0.824538 0.816682 0.695671 0.673134 0.149675 -0.124152 0.133581 -0.381388 -0.359533
IXJ 0.851986 0.728575 0.766836 0.765878 0.629721 0.599053 0.616322 0.631887 0.655428 0.757196 ... 0.915313 0.837936 0.721387 0.622187 0.883306 0.055953 -0.137358 0.143945 -0.344828 -0.321371
SPYG 0.961924 0.855916 0.933031 0.889935 0.730525 0.698685 0.719375 0.707404 0.668688 0.913558 ... 0.815243 0.920747 0.815739 0.720258 0.750865 0.054485 -0.165685 0.129386 -0.392576 -0.383592
VFH 0.871361 0.809010 0.733479 0.849786 0.832284 0.609310 0.660742 0.632167 0.574601 0.821058 ... 0.660167 0.842202 0.717865 0.661715 0.632294 -0.032526 -0.182760 0.060880 -0.397797 -0.389474
XLP 0.779692 0.713674 0.681526 0.715782 0.624105 0.547370 0.531370 0.542364 0.715334 0.711268 ... 0.740493 0.803539 0.630082 0.546659 0.698359 0.004216 -0.146086 0.121625 -0.309515 -0.291121
XLU 0.677779 0.578767 0.559107 0.609598 0.603095 0.532731 0.548712 0.490987 0.975538 0.568359 ... 0.613821 0.686350 0.585413 0.562313 0.575193 0.097969 -0.040370 0.215401 -0.188980 -0.167872
VBR 0.927890 0.852215 0.802608 0.928502 0.824409 0.679493 0.763942 0.673594 0.653380 0.893124 ... 0.722219 0.883438 0.777733 0.757126 0.690305 0.029289 -0.158808 0.109085 -0.398947 -0.386901
VGT 0.915742 0.825505 0.969893 0.838387 0.671368 0.653752 0.647079 0.681960 0.589202 0.873575 ... 0.736062 0.870739 0.780454 0.643684 0.683918 0.032490 -0.177478 0.122365 -0.372387 -0.368373
EFA 0.890826 0.773926 0.828456 0.848673 0.697877 0.748433 0.751908 0.772670 0.652582 0.810491 ... 0.721815 0.874509 0.881386 0.752592 0.712399 0.148103 -0.126808 0.172883 -0.385826 -0.367515
IJH 0.955086 0.860779 0.850618 0.938735 0.798729 0.711996 0.788558 0.698944 0.682540 0.909867 ... 0.761442 0.910625 0.810903 0.784720 0.718853 0.058842 -0.155001 0.121090 -0.407496 -0.394522
IJR 0.912721 0.844133 0.804966 0.912820 0.775445 0.660574 0.747930 0.660259 0.620466 0.882760 ... 0.715435 0.864525 0.761894 0.741895 0.681716 0.035478 -0.164171 0.089417 -0.397280 -0.387697
IWM 0.925600 0.852388 0.824353 0.909691 0.781368 0.670654 0.742876 0.685179 0.624304 0.892135 ... 0.735796 0.870033 0.783974 0.737029 0.699598 0.041148 -0.162485 0.102399 -0.396300 -0.388150
IXP 0.851935 0.742692 0.805039 0.773534 0.665932 0.690215 0.663238 0.723875 0.641759 0.789151 ... 0.702450 0.834120 0.806207 0.664266 0.684152 0.109714 -0.128524 0.163995 -0.327326 -0.317243
VXF 0.953729 0.865275 0.861371 0.926894 0.794139 0.703363 0.768589 0.700343 0.645878 0.922840 ... 0.755163 0.890036 0.809665 0.761942 0.715023 0.060397 -0.144070 0.130089 -0.392226 -0.382810
VB 0.946655 0.864702 0.844796 0.929943 0.801617 0.693555 0.764170 0.697074 0.646387 0.913194 ... 0.752398 0.888563 0.802561 0.757580 0.711512 0.052101 -0.154267 0.118908 -0.398163 -0.387903
VO 0.973708 0.874638 0.880296 0.938984 0.804709 0.727311 0.791489 0.725735 0.702429 0.926392 ... 0.782116 0.923300 0.838115 0.789723 0.736773 0.066825 -0.158489 0.137021 -0.405937 -0.395776
VUG 0.969810 0.854806 0.939668 0.888857 0.725760 0.701821 0.723181 0.722210 0.671887 0.919825 ... 0.819385 0.927974 0.827975 0.722605 0.761995 0.058902 -0.162288 0.140279 -0.388548 -0.380866
QQQ 0.917042 0.820528 0.950463 0.825656 0.674572 0.654823 0.635373 0.693532 0.592525 0.886784 ... 0.768920 0.866285 0.789034 0.634884 0.705903 0.025374 -0.175142 0.114686 -0.365431 -0.363531
IVV 0.991728 0.886371 0.909036 0.927810 0.782477 0.719051 0.769882 0.740236 0.716641 0.918692 ... 0.831342 0.969264 0.843685 0.774119 0.785715 0.041026 -0.180844 0.127639 -0.417106 -0.408616
SPY 0.989775 0.866013 0.904788 0.921138 0.772244 0.722311 0.773014 0.738404 0.718358 0.914416 ... 0.834193 0.971903 0.845342 0.777962 0.781229 0.040149 -0.176886 0.146887 -0.414966 -0.404223
VTV 0.970891 0.870057 0.840310 0.930595 0.806886 0.710964 0.789149 0.719834 0.731794 0.884916 ... 0.802740 0.955327 0.823293 0.790707 0.769401 0.034731 -0.185695 0.109618 -0.426319 -0.414990
VBK 0.939363 0.849089 0.867941 0.904195 0.759812 0.687009 0.740288 0.694655 0.622460 0.912409 ... 0.759852 0.867250 0.801270 0.732577 0.711798 0.074775 -0.138765 0.121531 -0.380600 -0.373877
XLK 0.912633 0.818667 0.960110 0.820763 0.667341 0.649366 0.639829 0.683221 0.609564 0.856361 ... 0.743448 0.887378 0.780190 0.643233 0.697042 0.025683 -0.174809 0.123341 -0.363234 -0.361963
XLF 0.854528 0.798389 0.719731 0.824959 0.807808 0.603151 0.639310 0.638698 0.553611 0.798596 ... 0.644869 0.831626 0.717548 0.644368 0.616151 -0.046762 -0.178519 0.072627 -0.393870 -0.389179
IBB 0.735056 0.644489 0.679003 0.650200 0.514323 0.479751 0.513946 0.511734 0.464175 0.688493 ... 0.800864 0.668772 0.580641 0.512318 0.744103 0.014773 -0.111113 0.079199 -0.287478 -0.279722
FVD 0.894281 0.815952 0.778489 0.870826 0.768577 0.660133 0.703810 0.638881 0.768659 0.830670 ... 0.759635 0.878500 0.741254 0.707600 0.718387 0.042653 -0.155467 0.140052 -0.368897 -0.350672
EWJ 0.758448 0.640344 0.724770 0.712239 0.581805 0.625476 0.628079 0.684986 0.563181 0.687203 ... 0.629516 0.752484 0.759283 0.624791 0.611312 0.097977 -0.100758 0.155891 -0.284242 -0.277453
VHT 0.863969 0.746885 0.769765 0.780640 0.618436 0.572511 0.613403 0.600248 0.649295 0.781293 ... 0.967963 0.841434 0.686527 0.613182 0.906777 0.015144 -0.150037 0.124178 -0.350326 -0.331009
DVY 0.904507 0.824354 0.761023 0.879072 0.807667 0.652960 0.728045 0.648213 0.765766 0.841260 ... 0.736650 0.895745 0.748072 0.732437 0.711445 0.018288 -0.153656 0.113892 -0.371184 -0.359732
VV 0.989928 0.879648 0.909814 0.927894 0.780813 0.722037 0.768797 0.735996 0.717165 0.921217 ... 0.827804 0.960151 0.843577 0.770611 0.782193 0.052715 -0.180034 0.132207 -0.417173 -0.408290
XLY 0.913622 0.850196 0.840053 0.874260 0.757579 0.630907 0.644388 0.659515 0.604336 0.975556 ... 0.733649 0.880154 0.758368 0.644730 0.689440 -0.011896 -0.171461 0.086982 -0.376778 -0.374034
XLI 0.919591 0.820748 0.816504 0.975411 0.722421 0.673104 0.742119 0.661686 0.641883 0.864326 ... 0.750165 0.922521 0.770608 0.743656 0.695298 0.033098 -0.172353 0.094181 -0.416830 -0.405857
XLV 0.828526 0.706793 0.727634 0.740823 0.578187 0.543694 0.579016 0.566737 0.630757 0.732895 ... 1.000000 0.821274 0.647944 0.584737 0.899929 0.006520 -0.143230 0.104383 -0.339034 -0.317005
DIA 0.963858 0.845677 0.870259 0.921489 0.742990 0.701288 0.760044 0.711172 0.714071 0.884817 ... 0.821274 1.000000 0.816337 0.764087 0.778409 0.020991 -0.182273 0.135582 -0.411896 -0.403199
EEM 0.846202 0.739309 0.808089 0.793111 0.683099 0.834757 0.727975 0.893423 0.614229 0.772409 ... 0.647944 0.816337 1.000000 0.728888 0.633556 0.164965 -0.135977 0.140473 -0.340844 -0.348218
XLE 0.780647 0.684152 0.654222 0.754623 0.565272 0.689225 0.988083 0.618700 0.589604 0.658766 ... 0.584737 0.764087 0.728888 1.000000 0.573835 0.157892 -0.081652 0.104385 -0.364432 -0.342288
PPH 0.781658 0.675504 0.683015 0.700288 0.552549 0.528428 0.568269 0.548833 0.595782 0.689500 ... 0.899929 0.778409 0.633556 0.573835 1.000000 0.021790 -0.131036 0.112052 -0.315418 -0.295150
GLD 0.050460 0.021784 0.055647 0.029920 0.050858 0.180725 0.160815 0.094576 0.104062 0.000638 ... 0.006520 0.020991 0.164965 0.157892 0.021790 1.000000 0.234096 0.111878 0.137658 0.169814
TIP -0.169573 -0.180869 -0.158921 -0.176778 -0.098125 -0.057231 -0.073870 -0.162521 -0.051022 -0.163802 ... -0.143230 -0.182273 -0.135977 -0.081652 -0.131036 0.234096 1.000000 0.476019 0.687777 0.748269
LQD 0.137284 0.041749 0.133869 0.109340 0.135713 0.163644 0.111601 0.111763 0.213702 0.114292 ... 0.104383 0.135582 0.140473 0.104385 0.112052 0.111878 0.476019 1.000000 0.444578 0.470910
TLT -0.413045 -0.395530 -0.363048 -0.420418 -0.237456 -0.288678 -0.359865 -0.318691 -0.204574 -0.376429 ... -0.339034 -0.411896 -0.340844 -0.364432 -0.315418 0.137658 0.687777 0.444578 1.000000 0.910671
IEF -0.402994 -0.390168 -0.358632 -0.411690 -0.234246 -0.275663 -0.335616 -0.341356 -0.186291 -0.375201 ... -0.317005 -0.403199 -0.348218 -0.342288 -0.295150 0.169814 0.748269 0.470910 0.910671 1.000000

74 rows × 74 columns

[7]:
from mypo import split_k_folds, select_by_regression
from mypo.optimizer import MaximumDiversificationOptimizer, MeanVarianceOptimizer
from mypo.rebalancer import MonthlyRebalancer
from mypo import Runner

folds = split_k_folds(market, 4, 200)

Traditional Asset Class

[8]:
for i in range(len(folds)):
    selected_tickers = ['SPY', 'TLT', 'GLD']
    fold = folds[i].filter(selected_tickers)
    optimizer = MaximumDiversificationOptimizer(do_re_optimize=True)
    runner = Runner(
        rebalancer=MonthlyRebalancer(optimizer=optimizer),
    )

    runner.run(
        fold=fold,
        verbose=True
    )
    report = runner.report()
    print(report.summary())
    ax = report.history().plot()
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
    ax = report.history_weights().plot.area(stacked=True)
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()

100%|██████████| 984/984 [00:11<00:00, 86.05it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [GLD, SPY, TLT]             1.054379      0.371617        0.85498

   max draw down span
0                 174
../_images/tutorial_tickers_10_2.png
../_images/tutorial_tickers_10_3.png
100%|██████████| 984/984 [00:11<00:00, 88.29it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [GLD, SPY, TLT]             1.108893      1.226537        0.93276

   max draw down span
0                 100
../_images/tutorial_tickers_10_6.png
../_images/tutorial_tickers_10_7.png
100%|██████████| 984/984 [00:11<00:00, 89.38it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [GLD, SPY, TLT]             1.078098      0.874904       0.919568

   max draw down span
0                 295
../_images/tutorial_tickers_10_10.png
../_images/tutorial_tickers_10_11.png
100%|██████████| 984/984 [00:10<00:00, 90.08it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [GLD, SPY, TLT]             1.086937      0.764734        0.84886

   max draw down span
0                 291
../_images/tutorial_tickers_10_14.png
../_images/tutorial_tickers_10_15.png

Select Tickers from clusters

[9]:
for i in range(len(folds)):
    evaluates = evaluate_combinations(folds[i].get_train(), cluster, optimizer=MaximumDiversificationOptimizer(), verbose=True)
    fold = folds[i].filter((list(evaluates['combinations'])[0]))
    optimizer = MaximumDiversificationOptimizer(do_re_optimize=True)
    runner = Runner(
        rebalancer=MonthlyRebalancer(optimizer=optimizer),
    )

    runner.run(
        fold=fold,
        verbose=True
    )
    report = runner.report()
    display(report.summary())
    display(report.annual_summary())
    ax = report.history().plot()
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
    ax = report.history_weights().plot.area(stacked=True)
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
100%|██████████| 414/414 [00:15<00:00, 26.11it/s]
100%|██████████| 984/984 [00:12<00:00, 80.52it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, GLD, EWZ]             1.077544      0.804881       0.869239

   max draw down span
0                 304
../_images/tutorial_tickers_12_2.png
../_images/tutorial_tickers_12_3.png
100%|██████████| 414/414 [00:06<00:00, 64.53it/s]
100%|██████████| 984/984 [00:12<00:00, 80.03it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [RWR, IEF, TIP]             1.068908      0.897267       0.920603

   max draw down span
0                 154
../_images/tutorial_tickers_12_6.png
../_images/tutorial_tickers_12_7.png
100%|██████████| 414/414 [00:05<00:00, 69.71it/s]
100%|██████████| 984/984 [00:12<00:00, 81.95it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, GLD, FEZ]             1.030963      0.206263       0.930154

   max draw down span
0                 312
../_images/tutorial_tickers_12_10.png
../_images/tutorial_tickers_12_11.png
100%|██████████| 414/414 [00:06<00:00, 67.90it/s]
100%|██████████| 984/984 [00:12<00:00, 81.81it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, GLD, XLF]             1.040741      0.433219       0.906438

   max draw down span
0                 289
../_images/tutorial_tickers_12_14.png
../_images/tutorial_tickers_12_15.png

Select Tickers by daily return

[10]:
for i in range(len(folds)):
    df = pd.concat([folds[i].get_train().get_summary(), cluster], axis=1)
    selected_tickers = list(df.groupby('class').idxmax().reset_index(drop=True)['daily return'])
    fold = folds[i].filter(selected_tickers)
    optimizer = MaximumDiversificationOptimizer(do_re_optimize=True)
    runner = Runner(
        rebalancer=MonthlyRebalancer(optimizer=optimizer),
    )

    runner.run(
        fold=fold,
        verbose=True
    )
    report = runner.report()
    display(report.summary())
    display(report.annual_summary())
    ax = report.history().plot()
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
    ax = report.history_weights().plot.area(stacked=True)
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
100%|██████████| 984/984 [00:12<00:00, 80.85it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [ILF, TLT, TIP]             1.050952      0.365127       0.811549

   max draw down span
0                 304
../_images/tutorial_tickers_14_2.png
../_images/tutorial_tickers_14_3.png
100%|██████████| 984/984 [00:12<00:00, 80.38it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, GLD, EWZ]              1.02941      0.161653       0.900265

   max draw down span
0                 338
../_images/tutorial_tickers_14_6.png
../_images/tutorial_tickers_14_7.png
100%|██████████| 984/984 [00:11<00:00, 82.60it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, BBH, LQD]             1.043715      0.415953       0.928769

   max draw down span
0                 327
../_images/tutorial_tickers_14_10.png
../_images/tutorial_tickers_14_11.png
100%|██████████| 984/984 [00:11<00:00, 82.09it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, IXN, TIP]             1.057576      0.715872       0.919638

   max draw down span
0                 165
../_images/tutorial_tickers_14_14.png
../_images/tutorial_tickers_14_15.png

Select Tickers by Sharpe ratio

[11]:
for i in range(len(folds)):
    df = pd.concat([folds[i].get_train().get_summary(), cluster], axis=1)
    selected_tickers = list(df.groupby('class').idxmax().reset_index(drop=True)['sharpe ratio'])
    fold = folds[i].filter(selected_tickers)
    optimizer = MaximumDiversificationOptimizer(do_re_optimize=True)
    runner = Runner(
        rebalancer=MonthlyRebalancer(optimizer=optimizer),
    )

    runner.run(
        fold=fold,
        verbose=True
    )
    report = runner.report()
    display(report.summary())
    display(report.annual_summary())
    ax = report.history().plot()
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
    ax = report.history_weights().plot.area(stacked=True)
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
100%|██████████| 984/984 [00:11<00:00, 83.24it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [TLT, XLU, TIP]              1.01353     -0.083061       0.846498

   max draw down span
0                 269
../_images/tutorial_tickers_16_2.png
../_images/tutorial_tickers_16_3.png
100%|██████████| 984/984 [00:11<00:00, 83.30it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, EWM, LQD]             1.071496      0.988231       0.933239

   max draw down span
0                 141
../_images/tutorial_tickers_16_6.png
../_images/tutorial_tickers_16_7.png
100%|██████████| 984/984 [00:11<00:00, 83.28it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [XLV, GLD, TLT]             1.083471      0.838055       0.876042

   max draw down span
0                 305
../_images/tutorial_tickers_16_10.png
../_images/tutorial_tickers_16_11.png
100%|██████████| 984/984 [00:11<00:00, 83.36it/s]
           tickers  yearly total return  sharpe ratio  max draw down  \
0  [GLD, TLT, IXN]             1.112774      0.935914       0.859415

   max draw down span
0                 286
../_images/tutorial_tickers_16_14.png
../_images/tutorial_tickers_16_15.png

Select Tickers by Regression

[12]:
for i in range(len(folds)):
    selected_tickers = select_by_regression(folds[i].get_train(), threshold=0.8)
    fold = folds[i].filter(selected_tickers)
    optimizer = MaximumDiversificationOptimizer(do_re_optimize=True)
    runner = Runner(
        rebalancer=MonthlyRebalancer(optimizer=optimizer),
    )

    runner.run(
        fold=fold,
        verbose=True
    )
    report = runner.report()
    display(report.summary())
    display(report.annual_summary())
    ax = report.history().plot()
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
    ax = report.history_weights().plot.area(stacked=True)
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
    plt.show()
100%|██████████| 984/984 [00:12<00:00, 76.11it/s]
                tickers  yearly total return  sharpe ratio  max draw down  \
0  [ILF, SPY, TLT, EWG]              1.06894      0.453445       0.837406

   max draw down span
0                 156
../_images/tutorial_tickers_18_2.png
../_images/tutorial_tickers_18_3.png
100%|██████████| 984/984 [00:10<00:00, 90.89it/s]
      tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, SPY]             1.073707       1.13568       0.951708

   max draw down span
0                 119
../_images/tutorial_tickers_18_6.png
../_images/tutorial_tickers_18_7.png
100%|██████████| 984/984 [00:10<00:00, 90.24it/s]
      tickers  yearly total return  sharpe ratio  max draw down  \
0  [IEF, SPY]              1.05523      0.801417       0.954674

   max draw down span
0                 210
../_images/tutorial_tickers_18_10.png
../_images/tutorial_tickers_18_11.png
100%|██████████| 984/984 [00:13<00:00, 73.83it/s]
                tickers  yearly total return  sharpe ratio  max draw down  \
0  [ILF, SPY, EWY, TIP]             1.047854      0.440592        0.85952

   max draw down span
0                 341
../_images/tutorial_tickers_18_14.png
../_images/tutorial_tickers_18_15.png