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')
!pip install yfinance --upgrade --no-cache-dir
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)
# filter column adjusted close
df = data['Adj Close']
df = df.ffill()
df.head()
!pip install PyPortfolioOpt==1.2.1
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)
# 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)
ef1.portfolio_performance(verbose=True)
ef2.portfolio_performance(verbose=True)
ef3.portfolio_performance(verbose=True)
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))
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")
#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)
# 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))
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')
!pip install autoviz
!pip install xlrd==2.0.1
!pip install wordcloud==1.8.1