Open Nextfx opened 6 years ago
""" Created on Sun Feb 11 11:11:44 2018
@author: EZ """
import numpy as np from scipy.stats import norm import pandas as pd
c = 0.99 # 99% confidence interval mu = 0.9 # np.mean - mean return
def daily_return(prices): return prices / prices.shift(1) - 1
''' daily_returns_BTC = ((daily_return(pd.read_csv("BTC-USD.csv")["Adj Close"]).dropna()).tolist())
daily_returns_XRP = (daily_return(pd.read_csv("XRP-USD.csv")["Adj Close"]).dropna()).tolist()
daily_returns_ETH = (daily_return(pd.read_csv("ETH-USD.csv")["Adj Close"]).dropna()).tolist()
daily_returns = [daily_returns_BTC, daily_returns_XRP, daily_returns_ETH] '''
corr_matrix = np.corrcoef(daily_returns)
cov_matrix = np.cov(np.array(daily_returns.T))
w = np.array([0.3, 0.4, 0.3])
sigma = np.sqrt(w.T.dot(cov_matrix).dot(w))
def var_cov_var(P, c, mu, sigma): alpha = norm.ppf(1 - c, mu, sigma) return P - P*(alpha + 1) ''' Variance-Covariance calculation of daily Value-at-Risk using confidence level c, with mean of returns mu and standard deviation of returns sigma, on a portfolio of value P.
'''
var = var_cov_var(P, c, mu, sigma) print ("Value-at-Risk: $%0.3f" % abs(var))
############################################## in development ################################################### ''' 0.01,0.03, 0.00,0.02, 0.16,0.03, 0.00,0.24, 0.00,0.51 '''
def PF_instruments(returns,instruments):
dailyReturns = pd.read_excel("C:/Users/vseykov/Desktop/R - All reports/GARCH_Volatilities.xlsm",
sheetname = 'Daily Return')
instr =["GER30","SPX500","US30","UK100","ESP35","FRA40","HKG33","JPN225","AUS200","EUSTX50"]
return dailyReturns[instr].copy()
CFH = a_sub.filter(like="Etoro",axis=0)
CFH.drop(CFH.columns[[0,1,2,3,5]],axis = 1, inplace = True)
CFH.reset_index(inplace = True)
CFH['Weights'] = CFH['Exposure']/CFH['Exposure'][0]
instruments = CFH.iloc[1:,1]
daily_returns = dailyReturns[instruments].copy()
P = CFH.iloc[0,2]
CFH = np.array([CFH.iloc[1:,[1,3]].set_index('Symbol').values])
w=CFH
w=(w.reshape((w.shape[0], -1), order='F').T)
mu = 0.9 # MMULT(TRANSPOSE(WEIGHTS),RETURNS) - this is the excel formula - convert to python
mu = w.T.dot(daily_returns)
-- coding: utf-8 --
""" Created on Sun Feb 11 11:11:44 2018
@author: EZ """
import numpy as np from scipy.stats import norm import pandas as pd
function to calculate returns
example data
create the correlation matrix - not used
corr_matrix = np.corrcoef(daily_returns)
create covariance matrix - to obtain sigma
cov_matrix = np.cov(np.array(daily_returns))
portfolio weights
w = np.array([0.3, 0.4, 0.3])
portfolio volatility (sigma)
sigma = np.sqrt(w.T.dot(cov_matrix).dot(w))