import seaborn as sns
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
# Import modules for API calls
import requests
import io
import pandas as pd
import requests
import json
from datetime import datetime
# Import module for plotting
import seaborn as sns
## JHU Vaccination Rates (Taken From: https://github.com/owid/covid-19-data/tree/master/public/data)
url = 'https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv'
download = requests.get(url).content
covid = pd.read_csv(io.StringIO(download.decode('utf-8')), parse_dates=['date'])
covid.tail()
sns.scatterplot('date', 'new_cases', data=covid, hue='continent')
/shared-libs/python3.7/py/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
FutureWarning
!pip install yfinance --upgrade --no-cache-dir
Requirement already satisfied: yfinance in /root/venv/lib/python3.7/site-packages (0.1.63)
Requirement already satisfied: pandas>=0.24 in /root/venv/lib/python3.7/site-packages (from yfinance) (0.25.3)
Requirement already satisfied: requests>=2.20 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from yfinance) (2.26.0)
Requirement already satisfied: lxml>=4.5.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from yfinance) (4.6.3)
Requirement already satisfied: numpy>=1.15 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from yfinance) (1.19.5)
Requirement already satisfied: multitasking>=0.0.7 in /root/venv/lib/python3.7/site-packages (from yfinance) (0.0.9)
Requirement already satisfied: python-dateutil>=2.6.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from pandas>=0.24->yfinance) (2.8.2)
Requirement already satisfied: pytz>=2017.2 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from pandas>=0.24->yfinance) (2021.1)
Requirement already satisfied: six>=1.5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from python-dateutil>=2.6.1->pandas>=0.24->yfinance) (1.16.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from requests>=2.20->yfinance) (1.26.6)
Requirement already satisfied: charset-normalizer~=2.0.0 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from requests>=2.20->yfinance) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from requests>=2.20->yfinance) (2021.5.30)
Requirement already satisfied: idna<4,>=2.5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from requests>=2.20->yfinance) (3.2)
WARNING: You are using pip version 21.2.3; however, version 21.2.4 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
import yfinance as yf
# find the symbol (i.e., google the instrument + 'yahoo finance') to any data series you are interested at
# e.g., market/sector index ETF for your chosen country and various asset classes (e.g., Comex Gold's symbol is 'GC=F')
# e.g., SPY (https://finance.yahoo.com/quote/SPY/)
symbols_list = [ 'SPY', 'ETH-USD', 'ADA-USD', 'BTC-USD','USDT-USD', 'ICLN','TAN','PBW', 'FMAT', 'LIT', 'QCLN', 'SKYY','BUG','SOXX', 'IYW','VGT','VHT','IHI','XLV','VBR','VBK']
start = dt.datetime(2017,11,1) #chosen date - 1M after ADA listing
end = dt.datetime(2021,7,15)
data = yf.download(symbols_list, start=start, end=end)
[*********************100%***********************] 21 of 21 completed
# filter column adjusted close
df = data['Adj Close']
df = df.ffill()
df.head()
!pip install PyPortfolioOpt==1.2.1
Requirement already satisfied: PyPortfolioOpt==1.2.1 in /root/venv/lib/python3.7/site-packages (1.2.1)
Requirement already satisfied: pandas<0.26.0,>=0.25.3 in /root/venv/lib/python3.7/site-packages (from PyPortfolioOpt==1.2.1) (0.25.3)
Requirement already satisfied: cvxpy<2.0.0,>=1.0.28 in /root/venv/lib/python3.7/site-packages (from PyPortfolioOpt==1.2.1) (1.1.15)
Requirement already satisfied: scipy in /shared-libs/python3.7/py/lib/python3.7/site-packages (from PyPortfolioOpt==1.2.1) (1.7.1)
Requirement already satisfied: numpy in /shared-libs/python3.7/py/lib/python3.7/site-packages (from PyPortfolioOpt==1.2.1) (1.19.5)
Requirement already satisfied: scs>=1.1.6 in /root/venv/lib/python3.7/site-packages (from cvxpy<2.0.0,>=1.0.28->PyPortfolioOpt==1.2.1) (2.1.4)
Requirement already satisfied: osqp>=0.4.1 in /root/venv/lib/python3.7/site-packages (from cvxpy<2.0.0,>=1.0.28->PyPortfolioOpt==1.2.1) (0.6.2.post0)
Requirement already satisfied: ecos>=2 in /root/venv/lib/python3.7/site-packages (from cvxpy<2.0.0,>=1.0.28->PyPortfolioOpt==1.2.1) (2.0.7.post1)
Requirement already satisfied: qdldl in /root/venv/lib/python3.7/site-packages (from osqp>=0.4.1->cvxpy<2.0.0,>=1.0.28->PyPortfolioOpt==1.2.1) (0.1.5.post0)
Requirement already satisfied: python-dateutil>=2.6.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from pandas<0.26.0,>=0.25.3->PyPortfolioOpt==1.2.1) (2.8.2)
Requirement already satisfied: pytz>=2017.2 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from pandas<0.26.0,>=0.25.3->PyPortfolioOpt==1.2.1) (2021.1)
Requirement already satisfied: six>=1.5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from python-dateutil>=2.6.1->pandas<0.26.0,>=0.25.3->PyPortfolioOpt==1.2.1) (1.16.0)
WARNING: You are using pip version 21.2.3; however, version 21.2.4 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns
from pypfopt import cla
from pypfopt.plotting import Plotting
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices
from matplotlib.ticker import FuncFormatter
import seaborn as sns
# Check NaN values in the data
nullin_df = pd.DataFrame(df,columns=symbols_list)
print(nullin_df.isnull().sum())
# Calculate portfolio mean return
mu = expected_returns.mean_historical_return(df)
print(mu)
# Calculate portfolio return variance
sigma = risk_models.sample_cov(df)
print(sigma)
SPY 0
ETH-USD 0
ADA-USD 0
BTC-USD 0
USDT-USD 0
ICLN 0
TAN 0
PBW 0
FMAT 0
LIT 0
QCLN 0
SKYY 0
BUG 730
SOXX 0
IYW 0
VGT 0
VHT 0
IHI 0
XLV 0
VBR 0
VBK 0
dtype: int64
ADA-USD 1.469338
BTC-USD 0.513810
BUG 0.282682
ETH-USD 0.709595
FMAT 0.090161
ICLN 0.208555
IHI 0.160443
IYW 0.203576
LIT 0.178875
PBW 0.287512
QCLN 0.262543
SKYY 0.186792
SOXX 0.224990
SPY 0.126502
TAN 0.294429
USDT-USD 0.003219
VBK 0.135079
VBR 0.090189
VGT 0.204449
VHT 0.118505
XLV 0.112072
dtype: float64
ADA-USD BTC-USD BUG ETH-USD FMAT ICLN \
ADA-USD 1.730157 0.445596 0.056399 0.632215 0.038316 0.041898
BTC-USD 0.445596 0.445445 0.051995 0.416474 0.026246 0.032516
BUG 0.056399 0.051995 0.069689 0.057744 0.034971 0.055429
ETH-USD 0.632215 0.416474 0.057744 0.704103 0.034399 0.037303
FMAT 0.038316 0.026246 0.034971 0.034399 0.044233 0.035951
ICLN 0.041898 0.032516 0.055429 0.037303 0.035951 0.061754
IHI 0.026419 0.018416 0.036671 0.025368 0.029350 0.029927
IYW 0.041822 0.030349 0.051637 0.040106 0.033898 0.038551
LIT 0.037622 0.026866 0.049824 0.038707 0.041446 0.048393
PBW 0.050528 0.038055 0.071174 0.046111 0.047204 0.070116
QCLN 0.046187 0.037353 0.068576 0.042035 0.044474 0.064832
SKYY 0.039962 0.029714 0.052638 0.038621 0.028627 0.036621
SOXX 0.049480 0.037369 0.058478 0.046442 0.042551 0.048085
SPY 0.034321 0.022560 0.036743 0.030661 0.032407 0.031529
TAN 0.048385 0.038290 0.070662 0.045881 0.041933 0.075457
USDT-USD -0.005165 0.001467 -0.001938 -0.000648 -0.001205 -0.001582
VBK 0.038339 0.028146 0.049504 0.035963 0.035786 0.040703
VBR 0.038308 0.025940 0.037526 0.033871 0.042078 0.037471
VGT 0.041765 0.030309 0.051418 0.039961 0.034885 0.039021
VHT 0.026341 0.017822 0.032647 0.025107 0.027650 0.027548
XLV 0.023933 0.015256 0.028716 0.022824 0.026482 0.024881
IHI IYW LIT PBW ... SKYY SOXX \
ADA-USD 0.026419 0.041822 0.037622 0.050528 ... 0.039962 0.049480
BTC-USD 0.018416 0.030349 0.026866 0.038055 ... 0.029714 0.037369
BUG 0.036671 0.051637 0.049824 0.071174 ... 0.052638 0.058478
ETH-USD 0.025368 0.040106 0.038707 0.046111 ... 0.038621 0.046442
FMAT 0.029350 0.033898 0.041446 0.047204 ... 0.028627 0.042551
ICLN 0.029927 0.038551 0.048393 0.070116 ... 0.036621 0.048085
IHI 0.035453 0.034862 0.032278 0.036138 ... 0.030930 0.038567
IYW 0.034862 0.051111 0.041981 0.049204 ... 0.043717 0.056721
LIT 0.032278 0.041981 0.073284 0.063919 ... 0.037531 0.052863
PBW 0.036138 0.049204 0.063919 0.100949 ... 0.047671 0.064043
QCLN 0.036252 0.049140 0.061761 0.089050 ... 0.046432 0.064283
SKYY 0.030930 0.043717 0.037531 0.047671 ... 0.044626 0.047365
SOXX 0.038567 0.056721 0.052863 0.064043 ... 0.047365 0.078369
SPY 0.028875 0.036586 0.034866 0.039601 ... 0.031091 0.041411
TAN 0.035899 0.048356 0.062054 0.094391 ... 0.046187 0.061885
USDT-USD -0.001220 -0.001851 -0.001342 -0.000874 ... -0.001592 -0.001848
VBK 0.032667 0.040778 0.041239 0.053861 ... 0.038919 0.048276
VBR 0.029280 0.034259 0.041391 0.051067 ... 0.029607 0.043252
VGT 0.035435 0.050232 0.042710 0.049873 ... 0.043299 0.056427
VHT 0.029923 0.031816 0.029469 0.033318 ... 0.027848 0.035370
XLV 0.028700 0.029676 0.027322 0.029269 ... 0.025202 0.032614
SPY TAN USDT-USD VBK VBR VGT \
ADA-USD 0.034321 0.048385 -0.005165 0.038339 0.038308 0.041765
BTC-USD 0.022560 0.038290 0.001467 0.028146 0.025940 0.030309
BUG 0.036743 0.070662 -0.001938 0.049504 0.037526 0.051418
ETH-USD 0.030661 0.045881 -0.000648 0.035963 0.033871 0.039961
FMAT 0.032407 0.041933 -0.001205 0.035786 0.042078 0.034885
ICLN 0.031529 0.075457 -0.001582 0.040703 0.037471 0.039021
IHI 0.028875 0.035899 -0.001220 0.032667 0.029280 0.035435
IYW 0.036586 0.048356 -0.001851 0.040778 0.034259 0.050232
LIT 0.034866 0.062054 -0.001342 0.041239 0.041391 0.042710
PBW 0.039601 0.094391 -0.000874 0.053861 0.051067 0.049873
QCLN 0.038453 0.086067 -0.000592 0.051504 0.047167 0.049770
SKYY 0.031091 0.046187 -0.001592 0.038919 0.029607 0.043299
SOXX 0.041411 0.061885 -0.001848 0.048276 0.043252 0.056427
SPY 0.030979 0.037285 -0.001591 0.033064 0.033605 0.036780
TAN 0.037285 0.112364 -0.001003 0.050215 0.043557 0.048721
USDT-USD -0.001591 -0.001003 0.007079 -0.001330 -0.001415 -0.001796
VBK 0.033064 0.050215 -0.001330 0.042881 0.039218 0.041188
VBR 0.033605 0.043557 -0.001415 0.039218 0.048157 0.035346
VGT 0.036780 0.048721 -0.001796 0.041188 0.035346 0.050134
VHT 0.027291 0.032263 -0.001546 0.029998 0.028147 0.032024
XLV 0.026153 0.028475 -0.001560 0.027149 0.026504 0.029918
VHT XLV
ADA-USD 0.026341 0.023933
BTC-USD 0.017822 0.015256
BUG 0.032647 0.028716
ETH-USD 0.025107 0.022824
FMAT 0.027650 0.026482
ICLN 0.027548 0.024881
IHI 0.029923 0.028700
IYW 0.031816 0.029676
LIT 0.029469 0.027322
PBW 0.033318 0.029269
QCLN 0.032469 0.028690
SKYY 0.027848 0.025202
SOXX 0.035370 0.032614
SPY 0.027291 0.026153
TAN 0.032263 0.028475
USDT-USD -0.001546 -0.001560
VBK 0.029998 0.027149
VBR 0.028147 0.026504
VGT 0.032024 0.029918
VHT 0.029364 0.028294
XLV 0.028294 0.027910
[21 rows x 21 columns]
/root/venv/lib/python3.7/site-packages/pypfopt/risk_models.py:69: UserWarning: The covariance matrix is non positive semidefinite. Amending eigenvalues.
"The covariance matrix is non positive semidefinite. Amending eigenvalues."
/root/venv/lib/python3.7/site-packages/pypfopt/risk_models.py:88: UserWarning: Could not fix matrix. Please try a different risk model.
warnings.warn("Could not fix matrix. Please try a different risk model.")
# Note max sharpe ratio is the tangency portfolio
# weight bounds in negative allows shorting of stocks
# Thus, we put (0,1) as no shorting is allowed in this scenario
ef1 = EfficientFrontier(mu, sigma, weight_bounds=(0,1))
# optional constraints possible, read pypfopt documentation.
sharpe_portfolio1=ef1.max_sharpe(risk_free_rate=0.008)
sharpe_portfolio1_wt=ef1.clean_weights()
print(sharpe_portfolio1_wt)
Plotting.plot_weights(sharpe_portfolio1_wt)
print(sharpe_portfolio1)
# weight bounds in negative allows shorting of stocks without boundaries, i.e. possible 100% long and 100% short
# Thus, we put (-1,1) as shorting is allowed in this scenario
ef2 = EfficientFrontier(mu, sigma, weight_bounds=(-1,1))
# optional constraints possible, read pypfopt documentation.
sharpe_portfolio2=ef2.max_sharpe(risk_free_rate=0.008)
sharpe_portfolio2_wt=ef2.clean_weights()
print(sharpe_portfolio2_wt)
Plotting.plot_weights(sharpe_portfolio2_wt)
print(sharpe_portfolio2)
ef3 = EfficientFrontier(mu, sigma, weight_bounds=(-0.3,1.3)) #130long-30short capital neutral strategy
# optional constraints possible, read pypfopt documentation.
sharpe_portfolio3=ef3.max_sharpe(risk_free_rate=0.008)
sharpe_portfolio3_wt=ef3.clean_weights()
print(sharpe_portfolio3_wt)
Plotting.plot_weights(sharpe_portfolio3_wt)
print(sharpe_portfolio3)
{'ADA-USD': 0.13629, 'BTC-USD': 0.0, 'BUG': 0.49283, 'ETH-USD': 0.02062, 'FMAT': 0.0, 'ICLN': 0.0, 'IHI': 0.1363, 'IYW': 0.0, 'LIT': 0.0, 'PBW': 0.0, 'QCLN': 0.0, 'SKYY': 0.0, 'SOXX': 0.0, 'SPY': 0.0, 'TAN': 0.07452, 'USDT-USD': 0.13945, 'VBK': 0.0, 'VBR': 0.0, 'VGT': 0.0, 'VHT': 0.0, 'XLV': 0.0}
/root/venv/lib/python3.7/site-packages/cvxpy/expressions/expression.py:556: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 1 times so far.
warnings.warn(msg, UserWarning)
{'ADA-USD': 0.1362858621506734, 'BTC-USD': 0.0, 'BUG': 0.4928280208376269, 'ETH-USD': 0.0206185745267703, 'FMAT': 0.0, 'ICLN': 0.0, 'IHI': 0.1362982225416865, 'IYW': 0.0, 'LIT': 0.0, 'PBW': 0.0, 'QCLN': 0.0, 'SKYY': 0.0, 'SOXX': 0.0, 'SPY': 0.0, 'TAN': 0.074523116569368, 'USDT-USD': 0.139446203373875, 'VBK': 0.0, 'VBR': 0.0, 'VGT': 0.0, 'VHT': 0.0, 'XLV': 0.0}
{'ADA-USD': 0.08043, 'BTC-USD': -0.02716, 'BUG': 1.0, 'ETH-USD': 0.01594, 'FMAT': -0.23394, 'ICLN': -0.17662, 'IHI': 0.6579, 'IYW': -0.24418, 'LIT': 0.00983, 'PBW': 0.39257, 'QCLN': -0.25834, 'SKYY': -0.50127, 'SOXX': 0.05323, 'SPY': -0.12202, 'TAN': -0.00286, 'USDT-USD': 0.63246, 'VBK': -1.0, 'VBR': 0.1492, 'VGT': 0.57836, 'VHT': -1.0, 'XLV': 0.99649}
/root/venv/lib/python3.7/site-packages/cvxpy/expressions/expression.py:556: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 2 times so far.
warnings.warn(msg, UserWarning)
{'ADA-USD': 0.0804275836675468, 'BTC-USD': -0.0271629419549462, 'BUG': 1.0, 'ETH-USD': 0.0159423090962306, 'FMAT': -0.2339420535209436, 'ICLN': -0.1766185064952192, 'IHI': 0.6579005275053906, 'IYW': -0.2441849771320331, 'LIT': 0.0098321771805435, 'PBW': 0.392568956549131, 'QCLN': -0.2583391381798173, 'SKYY': -0.5012719032604429, 'SOXX': 0.0532269968357435, 'SPY': -0.1220248701659908, 'TAN': -0.0028619978048277, 'USDT-USD': 0.6324553612285586, 'VBK': -1.0, 'VBR': 0.1492010212920862, 'VGT': 0.5783585499135115, 'VHT': -1.0, 'XLV': 0.996492905245478}
{'ADA-USD': 0.1066, 'BTC-USD': -0.0081, 'BUG': 0.58102, 'ETH-USD': 0.02231, 'FMAT': -0.3, 'ICLN': -0.14991, 'IHI': 0.68401, 'IYW': -0.3, 'LIT': 0.01641, 'PBW': 0.52131, 'QCLN': -0.238, 'SKYY': -0.3, 'SOXX': -0.00075, 'SPY': -0.13364, 'TAN': -0.00032, 'USDT-USD': 0.48529, 'VBK': -0.3, 'VBR': -0.3, 'VGT': 0.62524, 'VHT': -0.3, 'XLV': 0.28853}
/root/venv/lib/python3.7/site-packages/cvxpy/expressions/expression.py:556: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 3 times so far.
warnings.warn(msg, UserWarning)
{'ADA-USD': 0.1066002044534036, 'BTC-USD': -0.0081038534179148, 'BUG': 0.5810246200633017, 'ETH-USD': 0.0223134485078406, 'FMAT': -0.3, 'ICLN': -0.1499089799799419, 'IHI': 0.6840114715636939, 'IYW': -0.3, 'LIT': 0.0164086639986033, 'PBW': 0.5213109338601681, 'QCLN': -0.2380017266766518, 'SKYY': -0.3, 'SOXX': -0.0007515180052277, 'SPY': -0.1336382478469071, 'TAN': -0.0003180873813856, 'USDT-USD': 0.4852860655017812, 'VBK': -0.3, 'VBR': -0.3, 'VGT': 0.6252392652564762, 'VHT': -0.3, 'XLV': 0.2885277401027598}
ef1.portfolio_performance(verbose=True)
Expected annual return: 39.8%
Annual volatility: 27.5%
Sharpe Ratio: 1.38
ef2.portfolio_performance(verbose=True)
Expected annual return: 33.7%
Annual volatility: 18.6%
Sharpe Ratio: 1.71
ef3.portfolio_performance(verbose=True)
Expected annual return: 39.9%
Annual volatility: 23.8%
Sharpe Ratio: 1.59
latest_prices = get_latest_prices(df)
# Allocate Portfolio Value in $ as required to show number of shares/stocks to buy,
# also bounds for shorting will affect allocation
# Maximum Sharpe Portfolio Allocation $1000000
da = DiscreteAllocation(sharpe_portfolio3_wt, latest_prices, total_portfolio_value=1000000)
allocation, leftover = da.greedy_portfolio()
print(allocation)
print("Leftover Fund value for the maximum Sharpe portfolio is ${:.2f}".format(leftover))
{'IHI': 3376, 'VGT': 458, 'BUG': 5963, 'PBW': 1894, 'USDT-USD': 145708, 'XLV': 673, 'ADA-USD': 26165, 'ETH-USD': 3, 'LIT': 63, 'FMAT': -838, 'IYW': -379, 'SKYY': -370, 'VBK': -138, 'VBR': -228, 'VHT': -155, 'QCLN': -481, 'ICLN': -852, 'SPY': -40, 'SOXX': -1, 'TAN': -1}
Leftover Fund value for the maximum Sharpe portfolio is $1701.60
max_sharpe_cla = cla.CLA(mu, sigma)
max_sharpe_cla.max_sharpe()
Plotting.plot_efficient_frontier(max_sharpe_cla, show_assets="True")
sharpe_portfolio3_wt_list = list(sharpe_portfolio3_wt.values())
ret_data = df.pct_change()[1:]
weighted_returns = (sharpe_portfolio3_wt_list * ret_data)
portfolio_ret = pd.DataFrame(weighted_returns.sum(axis=1))
ret_data = ret_data.merge(portfolio_ret, on="Date", how="left")
ret_data = ret_data.rename(columns={0: "portfolio_ret"})
ret_data.head()
ret_data['cumulative_portfolio_ret'] = (ret_data['portfolio_ret'] + 1).cumprod()
ret_data['cumulative_spy_ret'] = (ret_data['SPY'] + 1).cumprod()
ret_data.tail()
sns.scatterplot('Date', 'cumulative_portfolio_ret', data=ret_data,label="portfolio ret")
sns.scatterplot('Date', 'cumulative_spy_ret', data=ret_data,label="spy_ret")
/shared-libs/python3.7/py/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
FutureWarning
/shared-libs/python3.7/py/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
FutureWarning
#May use add objective to ensure minimum zero weighting to individual stocks
min_vol_portfolio=ef3.min_volatility()
min_vol_portfolio_wt=ef3.clean_weights()
print(min_vol_portfolio_wt)
Plotting.plot_weights(min_vol_portfolio_wt)
{'ADA-USD': 0.33241, 'BTC-USD': -0.03965, 'BUG': 1.3, 'ETH-USD': 0.08105, 'FMAT': -0.3, 'ICLN': -0.3, 'IHI': 1.24882, 'IYW': -0.3, 'LIT': -0.3, 'PBW': 0.81569, 'QCLN': -0.3, 'SKYY': -0.3, 'SOXX': -0.1006, 'SPY': -0.3, 'TAN': 0.23092, 'USDT-USD': -0.3, 'VBK': -0.3, 'VBR': -0.3, 'VGT': 0.73136, 'VHT': -0.3, 'XLV': -0.3}
# Allocate Portfolio Value in $ as required to show number of shares/stocks to buy,
# also bounds for shorting will affect allocation
# Maximum Sharpe Portfolio Allocation $1000000
da = DiscreteAllocation(min_vol_portfolio_wt, latest_prices, total_portfolio_value=1000000)
allocation, leftover = da.greedy_portfolio()
print(allocation)
print("Leftover Fund value for the maximum Sharpe portfolio is ${:.2f}".format(leftover))
{'BUG': 9375, 'IHI': 4331, 'PBW': 2083, 'VGT': 376, 'ADA-USD': 57329, 'TAN': 581, 'ETH-USD': 9, 'FMAT': -522, 'ICLN': -1062, 'IYW': -236, 'LIT': -306, 'QCLN': -378, 'SKYY': -230, 'SPY': -55, 'USDT-USD': -24063, 'VBK': -86, 'VBR': -141, 'VHT': -96, 'XLV': -186, 'SOXX': -18}
Leftover Fund value for the maximum Sharpe portfolio is $4161.18
min_vol_cla = cla.CLA(mu, sigma)
min_vol_cla.min_volatility()
Plotting.plot_efficient_frontier(min_vol_cla, show_assets="True")
df = df.stack()
df.head()
df = df.reset_index()
df = df.rename(columns={"level_1": "ticker", 0: "price"})
df.head()
df = df.set_index('Date')
df['ret'] = df.groupby('ticker').pct_change()
sns.scatterplot('Date', 'price', data=df, hue='ticker')
/shared-libs/python3.7/py/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
FutureWarning
!pip install autoviz
!pip install xlrd==2.0.1
!pip install wordcloud==1.8.1
Collecting autoviz
Downloading autoviz-0.0.84-py3-none-any.whl (44 kB)
|████████████████████████████████| 44 kB 3.2 MB/s
Requirement already satisfied: seaborn in /shared-libs/python3.7/py/lib/python3.7/site-packages (from autoviz) (0.11.2)
Requirement already satisfied: ipython in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from autoviz) (7.27.0)
Collecting statsmodels
Downloading statsmodels-0.12.2-cp37-cp37m-manylinux1_x86_64.whl (9.5 MB)
|████████████████████████████████| 9.5 MB 337 kB/s
Requirement already satisfied: matplotlib in /shared-libs/python3.7/py/lib/python3.7/site-packages (from autoviz) (3.4.3)
Collecting jupyter
Downloading jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB)
Collecting xgboost
Downloading xgboost-1.4.2-py3-none-manylinux2010_x86_64.whl (166.7 MB)
|████████████████████████████████| 166.7 MB 22 kB/s
Requirement already satisfied: pandas in /root/venv/lib/python3.7/site-packages (from autoviz) (0.25.3)
Requirement already satisfied: scikit-learn in /shared-libs/python3.7/py/lib/python3.7/site-packages (from autoviz) (0.24.2)
Requirement already satisfied: pickleshare in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (0.7.5)
Requirement already satisfied: pexpect>4.3 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (4.8.0)
Requirement already satisfied: setuptools>=18.5 in /root/venv/lib/python3.7/site-packages (from ipython->autoviz) (57.4.0)
Requirement already satisfied: decorator in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (5.0.9)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (3.0.20)
Requirement already satisfied: matplotlib-inline in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (0.1.3)
Requirement already satisfied: backcall in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (0.2.0)
Requirement already satisfied: traitlets>=4.2 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (5.1.0)
Requirement already satisfied: jedi>=0.16 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (0.17.2)
Requirement already satisfied: pygments in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipython->autoviz) (2.10.0)
Requirement already satisfied: parso<0.8.0,>=0.7.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jedi>=0.16->ipython->autoviz) (0.7.1)
Requirement already satisfied: ptyprocess>=0.5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from pexpect>4.3->ipython->autoviz) (0.7.0)
Requirement already satisfied: wcwidth in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython->autoviz) (0.2.5)
Collecting qtconsole
Downloading qtconsole-5.1.1-py3-none-any.whl (119 kB)
|████████████████████████████████| 119 kB 43.7 MB/s
Requirement already satisfied: nbconvert in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jupyter->autoviz) (6.1.0)
Requirement already satisfied: ipykernel in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jupyter->autoviz) (6.3.1)
Collecting jupyter-console
Downloading jupyter_console-6.4.0-py3-none-any.whl (22 kB)
Requirement already satisfied: notebook in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jupyter->autoviz) (7.0.0.dev0)
Collecting ipywidgets
Downloading ipywidgets-7.6.5-py2.py3-none-any.whl (121 kB)
|████████████████████████████████| 121 kB 52.5 MB/s
Requirement already satisfied: jupyter-client<8.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipykernel->jupyter->autoviz) (7.0.2)
Requirement already satisfied: importlib-metadata<5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipykernel->jupyter->autoviz) (4.8.1)
Requirement already satisfied: argcomplete>=1.12.3 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipykernel->jupyter->autoviz) (1.12.3)
Requirement already satisfied: tornado<7.0,>=4.2 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipykernel->jupyter->autoviz) (6.1)
Requirement already satisfied: debugpy<2.0,>=1.0.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipykernel->jupyter->autoviz) (1.4.1)
Requirement already satisfied: ipython-genutils in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipykernel->jupyter->autoviz) (0.2.0)
Requirement already satisfied: typing-extensions>=3.6.4 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from importlib-metadata<5->ipykernel->jupyter->autoviz) (3.10.0.2)
Requirement already satisfied: zipp>=0.5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from importlib-metadata<5->ipykernel->jupyter->autoviz) (3.5.0)
Requirement already satisfied: jupyter-core>=4.6.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel->jupyter->autoviz) (4.7.1)
Requirement already satisfied: python-dateutil>=2.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel->jupyter->autoviz) (2.8.2)
Requirement already satisfied: nest-asyncio>=1.5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel->jupyter->autoviz) (1.5.1)
Requirement already satisfied: pyzmq>=13 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel->jupyter->autoviz) (22.2.1)
Requirement already satisfied: entrypoints in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel->jupyter->autoviz) (0.3)
Requirement already satisfied: six>=1.5 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from python-dateutil>=2.1->jupyter-client<8.0->ipykernel->jupyter->autoviz) (1.16.0)
Collecting jupyterlab-widgets>=1.0.0
Downloading jupyterlab_widgets-1.0.2-py3-none-any.whl (243 kB)
|████████████████████████████████| 243 kB 51.9 MB/s
Collecting widgetsnbextension~=3.5.0
Downloading widgetsnbextension-3.5.1-py2.py3-none-any.whl (2.2 MB)
|████████████████████████████████| 2.2 MB 37.7 MB/s
Requirement already satisfied: nbformat>=4.2.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from ipywidgets->jupyter->autoviz) (5.1.3)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from nbformat>=4.2.0->ipywidgets->jupyter->autoviz) (3.2.0)
Requirement already satisfied: attrs>=17.4.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->jupyter->autoviz) (21.2.0)
Requirement already satisfied: pyrsistent>=0.14.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->jupyter->autoviz) (0.18.0)
Requirement already satisfied: jinja2 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from notebook->jupyter->autoviz) (3.0.1)
Requirement already satisfied: terminado>=0.8.3 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from notebook->jupyter->autoviz) (0.12.1)
Requirement already satisfied: argon2-cffi in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from notebook->jupyter->autoviz) (21.1.0)
Requirement already satisfied: prometheus-client in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from notebook->jupyter->autoviz) (0.11.0)
Requirement already satisfied: Send2Trash>=1.5.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from notebook->jupyter->autoviz) (1.8.0)
Requirement already satisfied: cffi>=1.0.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from argon2-cffi->notebook->jupyter->autoviz) (1.14.6)
Requirement already satisfied: pycparser in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from cffi>=1.0.0->argon2-cffi->notebook->jupyter->autoviz) (2.20)
Requirement already satisfied: MarkupSafe>=2.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from jinja2->notebook->jupyter->autoviz) (2.0.1)
Requirement already satisfied: numpy>=1.16 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib->autoviz) (1.19.5)
Requirement already satisfied: cycler>=0.10 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib->autoviz) (0.10.0)
Requirement already satisfied: pyparsing>=2.2.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from matplotlib->autoviz) (2.4.7)
Requirement already satisfied: pillow>=6.2.0 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib->autoviz) (8.3.2)
Requirement already satisfied: kiwisolver>=1.0.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib->autoviz) (1.3.2)
Requirement already satisfied: bleach in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from nbconvert->jupyter->autoviz) (4.1.0)
Requirement already satisfied: pandocfilters>=1.4.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from nbconvert->jupyter->autoviz) (1.4.3)
Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from nbconvert->jupyter->autoviz) (0.5.4)
Requirement already satisfied: jupyterlab-pygments in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from nbconvert->jupyter->autoviz) (0.1.2)
Requirement already satisfied: mistune<2,>=0.8.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from nbconvert->jupyter->autoviz) (0.8.4)
Requirement already satisfied: testpath in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from nbconvert->jupyter->autoviz) (0.5.0)
Requirement already satisfied: defusedxml in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from nbconvert->jupyter->autoviz) (0.7.1)
Requirement already satisfied: webencodings in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from bleach->nbconvert->jupyter->autoviz) (0.5.1)
Requirement already satisfied: packaging in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from bleach->nbconvert->jupyter->autoviz) (21.0)
Requirement already satisfied: pytz>=2017.2 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from pandas->autoviz) (2021.1)
Collecting qtpy
Downloading QtPy-1.11.1-py2.py3-none-any.whl (58 kB)
|████████████████████████████████| 58 kB 7.2 MB/s
Requirement already satisfied: joblib>=0.11 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from scikit-learn->autoviz) (1.0.1)
Requirement already satisfied: threadpoolctl>=2.0.0 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from scikit-learn->autoviz) (2.2.0)
Requirement already satisfied: scipy>=0.19.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from scikit-learn->autoviz) (1.7.1)
Collecting patsy>=0.5
Downloading patsy-0.5.1-py2.py3-none-any.whl (231 kB)
|████████████████████████████████| 231 kB 58.2 MB/s
Installing collected packages: widgetsnbextension, qtpy, jupyterlab-widgets, qtconsole, patsy, jupyter-console, ipywidgets, xgboost, statsmodels, jupyter, autoviz
Successfully installed autoviz-0.0.84 ipywidgets-7.6.5 jupyter-1.0.0 jupyter-console-6.4.0 jupyterlab-widgets-1.0.2 patsy-0.5.1 qtconsole-5.1.1 qtpy-1.11.1 statsmodels-0.12.2 widgetsnbextension-3.5.1 xgboost-1.4.2
WARNING: You are using pip version 21.2.3; however, version 21.2.4 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
Collecting xlrd==2.0.1
Downloading xlrd-2.0.1-py2.py3-none-any.whl (96 kB)
|████████████████████████████████| 96 kB 4.0 MB/s
Installing collected packages: xlrd
Successfully installed xlrd-2.0.1
WARNING: You are using pip version 21.2.3; however, version 21.2.4 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
Collecting wordcloud==1.8.1
Downloading wordcloud-1.8.1-cp37-cp37m-manylinux1_x86_64.whl (366 kB)
|████████████████████████████████| 366 kB 23.4 MB/s
Requirement already satisfied: pillow in /shared-libs/python3.7/py/lib/python3.7/site-packages (from wordcloud==1.8.1) (8.3.2)
Requirement already satisfied: matplotlib in /shared-libs/python3.7/py/lib/python3.7/site-packages (from wordcloud==1.8.1) (3.4.3)
Requirement already satisfied: numpy>=1.6.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from wordcloud==1.8.1) (1.19.5)
Requirement already satisfied: cycler>=0.10 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib->wordcloud==1.8.1) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib->wordcloud==1.8.1) (1.3.2)
Requirement already satisfied: python-dateutil>=2.7 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from matplotlib->wordcloud==1.8.1) (2.8.2)
Requirement already satisfied: pyparsing>=2.2.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from matplotlib->wordcloud==1.8.1) (2.4.7)
Requirement already satisfied: six in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from cycler>=0.10->matplotlib->wordcloud==1.8.1) (1.16.0)
Installing collected packages: wordcloud
Successfully installed wordcloud-1.8.1
WARNING: You are using pip version 21.2.3; however, version 21.2.4 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.