I have a data frame something like this.
Date IOC.NS RELIANCE.NS BPCL.NS HINDPETRO.NS EXIDEIND.NS portfolio
0 2002-07-02 0.001000 -0.016930 0.001264 0.009413 0.055980 0.010145
1 2002-07-03 -0.000500 0.003931 -0.003787 0.015961 0.077108 0.018543
2 2002-07-04 -0.000500 -0.004289 0.007964 -0.006884 0.008949 0.001048
3 2002-07-05 0.005001 -0.013858 0.022266 0.012620 0.033259 0.011858
4 2002-07-08 -0.003235 0.032472 -0.001405 0.010356 0.072961 0.022230
... ... ... ... ... ... ... ...
5006 2022-08-22 -0.011781 -0.002774 -0.013599 -0.064657 -0.036829 -0.025928
5007 2022-08-23 -0.003506 0.015422 0.000150 -0.017028 0.012962 0.001600
5008 2022-08-24 -0.007037 -0.002664 -0.012436 -0.018767 0.009597 -0.006261
5009 2022-08-25 0.012757 -0.002917 0.000759 0.013451 0.001584 0.005127
5010 2022-08-26 0.006298 -0.003704 0.002881 0.003733 0.020563 0.005954
I scraped the daily closed data of these stocks and then calculated the pct_change() in python. And then made a equal-weighted portfolio.
import pandas as pd
import numpy as np
import datetime as dt
from datetime import timedelta, datetime
from pandas_datareader import data as pdr
from scipy.stats import norm, t
import matplotlib.pyplot as plt
import statistics
def getData(stocks, start, end):
stockData = pdr.get_data_yahoo(stocks, start=start, end=end)
stockData = stockData['Close']
returns_pctchange = stockData.pct_change()
return returns_pctchange
stockList = ['IOC', 'RELIANCE', 'BPCL', 'HINDPETRO', 'EXIDEIND']
stocks = [stock+'.NS' for stock in stockList]
endDate = dt.datetime.now()
startDate = endDate - dt.timedelta(days=8000)
returns = getData(stocks, start=startDate, end=endDate)
returns = returns.dropna()
weights = np.array([1/len(stocks) for n in stocks])
returns['portfolio'] = returns.dot(weights)
returns.reset_index(inplace=True)
After getting the returns dataframe, I wanted to calculate the monthly Value at Risk(VaR) and Expected Tail Risk(ETL) of the portfolio column using the R package PerformanceAnalytics.
This was the code I had written to perform that.
df = read.csv('C:/Users/VaR and CVaR/returns')
ss = data.frame(`Month/Year`=unique(format(as.Date(df$Date),"%b/%Y")),
`Monthly Expected Shortfall`=(tapply(df$portfolio,sub("-..$","",df$Date),FUN=PerformanceAnalytics::ETL)),
check.names=F,row.names=NULL)
This basically group pct_change() of portfolio column of each day of a particular month and group them and then apply the ETL function from PerformanceAnalytics hence I get back the ETL of each month.
But I got one error like this.
ES calculation produces unreliable result (risk over 100%) for column: 1 : 4.05194174741596
When I searched about this problem, in many places, it was suggested to use the log returns, which is the same as pct_change(). Reference
I could not understand where my mistake was and how to overcome the error it showed.
Please help me with this.