

Hi everyone!! I am new in R and I want to create a simple R function for estimating historical-VaR.  In y_IBM returns, there are 2300 observations. For evaluation I take the next 2000 observations,
then I abandon the latest 300 observations. Firstly, I use the window which has the fix
length and contains the observations from 1 to 2000 to estimate the VaR. At first I  take 2000 obs. and reorder these series in ascending order, from smallest return to largest return. Each ordered return is assigned an index value (1, 2, ...). At the 99% confidence level, the daily VaR under historical simulation method equals the return corresponding to the index number calculated as follows:
(1-0.99)*2000 (the number of our window) =20. The return corresponding to index 20 is the daily historical simulation VaR.
I repeat the first step except the window changes the observations from 2 to 2001. Such a process provides 300 one-step ahead VaR.
My function is:



VaR_foc <- function (returns, value = 1000, p = 0.01, n=251) { 
T = length(returns)
x_foc = vector(length=n)
N = T-(n+1)
m=sort(returns[1:N])
op = as.integer(N*p) # p % smallest
for (i in 2:n) {
g= returns[i:(N+i)] 
ys = sort(g) # sort returns
x_foc[[1]] = -m[op]*value     # VaR number
x_foc[i] = -ys[op]*value 
}
return(x_foc)
}
VaR_foc (returns=y_IBM)

But the fucntion doesn't work,  can smbd help me wh

	[[alternative HTML version deleted]]

