Skip to content

Instantly share code, notes, and snippets.

View lumos-consulting's full-sized avatar

lumos-consulting

View GitHub Profile
import pandas as pd
from newsapi import NewsApiClient
api = NewsApiClient(api_key='your key')
def news_api(company, date):
all_articles = api.get_everything(
q = company,
language = 'en',
from_param = date,
to = date,
back = pd.DataFrame({"MinVar":ret_1, "MaxSharpe":ret_2})
back = pd.concat([back, ind_ret], join = "outer", axis = 1)
back.drop(back.tail(1).index,inplace=True)
back.interpolate(method = "linear", inplace = True)
fig = px.line(back, x = back.index, y = back.columns, title = "Portfolio Performance (2018-2020)")
fig.update_xaxes(title_text='Date')
fig.update_yaxes(title_text='Cumulative Return in %')
weights_minvar_exp = list(raw_weights_minvar_exp.values())
weights_maxsharpe_exp = list(raw_weights_maxsharpe_exp.values())
ret_1 = test.dot(weights_minvar_exp).add(1).cumprod().subtract(1).multiply(100)
ret_2 = test.dot(weights_maxsharpe_exp).add(1).cumprod().subtract(1).multiply(100)
ind_ret = indices["2018-10-23":]["ATX_TR"].pct_change().add(1).cumprod().subtract(1).multiply(100)
ef = EfficientFrontier(mu, Sigma)
raw_weights_maxsharpe_exp = ef.max_sharpe(risk_free_rate=0.009)
plot_weights(raw_weights_maxsharpe_exp)
ef.portfolio_performance(verbose = True, risk_free_rate = 0.009)
ef = EfficientFrontier(mu, Sigma)
raw_weights_minvar_exp = ef.min_volatility()
plot_weights(raw_weights_minvar_exp)
ef.portfolio_performance(verbose = True, risk_free_rate = 0.009)
ret_ef = np.arange(0, 0.879823, 0.01)
vol_ef = []
for i in np.arange(0, 0.879823, 0.01):
ef = EfficientFrontier(mu, Sigma)
ef.efficient_return(i)
vol_ef.append(ef.portfolio_performance()[1])
ef = EfficientFrontier(mu, Sigma)
ef.min_volatility()
min_vol_ret = ef.portfolio_performance()[0]
!pip install PyPortfolioOpt
from pypfopt import risk_models
from pypfopt import expected_returns
from pypfopt.expected_returns import ema_historical_return
from pypfopt.risk_models import exp_cov
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt.plotting import plot_efficient_frontier
from pypfopt.plotting import plot_weights
from pypfopt.cla import CLA
atx_comps_returns = atx_comps.pct_change()
atx_comps_rets_cumprod = atx_comps_returns.add(1).cumprod().sub(1)*100
fig = px.line(atx_comps_rets_cumprod, x=atx_comps_rets_cumprod.index, y=atx_comps_rets_cumprod.columns, title='Cumulative Returns of ATX Stocks (2010-2020)')
fig.update_xaxes(title_text='Date')
fig.update_yaxes(title_text='Cumulative Return in %')
fig.show()
indices_rets = indices.pct_change()
indices_cumrets = indices_rets.add(1).cumprod().sub(1)*100
fig = px.line(indices_cumrets, x=indices_cumrets.index, y=indices_cumrets.columns, title='Cumulative Returns of Indices (2010-2020)')
fig.update_xaxes(title_text='Date')
fig.update_yaxes(title_text='Cumulative Return in %')
fig.show()
atx = pd.read_csv("indices/atx_tr.csv", delimiter = ";", decimal = ".", names = ["Date", "ATX_TR"], usecols = [0,1], header = 0, index_col = "Date", parse_dates=True)
sp500 = pd.read_csv("indices/^GSPC.csv", names = ["Date", "SP500"], parse_dates=True, usecols = [0,1], index_col = "Date", header = 1)
dax = pd.read_csv("indices/^GDAXI.csv", names = ["Date", "DAX"], parse_dates=True, usecols = [0,1], index_col = "Date", header = 1)
nasdaq = pd.read_csv("indices/^IXIC.csv", names = ["Date", "NASDAQ"], parse_dates=True, usecols = [0,1], index_col = "Date", header = 1)
indices = pd.concat([atx, sp500, dax, nasdaq], join = "outer", axis = 1)['2010-10-19':]
indices.head()