Nextfx / supreme-octo-garbanzo.

VAR project
0 stars 0 forks source link

VAR #1

Open Nextfx opened 6 years ago

Nextfx commented 6 years ago

-- 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

P = 1e6   # 1,000,000 USD example total PF
c = 0.99  # 99% confidence interval
mu = 0.1 # np.mean - mean return

function to calculate returns

def daily_return(prices):
    return prices / prices.shift(1) - 1

example data

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]

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))

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))
Nextfx commented 6 years ago

-- 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

P = 4392541.63797 # 1,000,000 USD example total PF

c = 0.99 # 99% confidence interval mu = 0.9 # np.mean - mean return

function to calculate returns

def daily_return(prices): return prices / prices.shift(1) - 1

example data

''' 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] '''

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.T))

portfolio weights

w = np.array([0.3, 0.4, 0.3])

portfolio volatility (sigma)

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 '''

get list of instruments

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()

get the PF weights

filter for name - result dataframe

CFH = a_sub.filter(like="Etoro",axis=0)

drop unneceserary columns

CFH.drop(CFH.columns[[0,1,2,3,5]],axis = 1, inplace = True)

weights final transformation

CFH.reset_index(inplace = True)

create a new calculated column weights

CFH['Weights'] = CFH['Exposure']/CFH['Exposure'][0]

list of insutrments

instruments = CFH.iloc[1:,1]

list of returns just for the given portfolio

daily_returns = dailyReturns[instruments].copy()

Total Portfolio Exposure

P = CFH.iloc[0,2]

drop unnecssry columns

CFH = np.array([CFH.iloc[1:,[1,3]].set_index('Symbol').values])

w=CFH

convert 3D array to 2D

w=(w.reshape((w.shape[0], -1), order='F').T)

TODO:

mu = 0.9 # MMULT(TRANSPOSE(WEIGHTS),RETURNS) - this is the excel formula - convert to python

mu = w.T.dot(daily_returns)