I am running a MC simulation on around 500 stocks in order to generate 252 days of stock prices. I am using the function GBM from the sde package.
For this I have a data.frame of 500 stocks that give me the average volume, average return, daily volatility and Price as the example bellow :
Teste=data.frame(matrix(runif(25), nrow=5, ncol=4))
colnames(Teste) <- c("AVG_VOLUME","AVG_RETURN","VOL","PRICE")
AVG_VOLUME AVG_RETURN VOL PRICE
Stock 1 0.7028197 0.09264265 0.002169411 100
Stock 2 0.7154557 0.03314615 0.004839466 100
Stock 3 0.4038030 0.04347487 0.003441471 100
Stock 4 0.5392530 0.06414982 0.004482528 100
Stock 5 0.8720084 0.09615865 0.008081017 100
I have also a function that takes as parameters the data.frame above and the number of simulations required :
Monte_Carlo_Stocks <- function(Dados_Acoes,Numero_Simulacoes){
start.time <- Sys.time()
MC <- matrix(0,nrow = 253,ncol=nrow(Dados_Acoes))
for (n in 1:Numero_Simulacoes){
MC1 <- MC
MC <- apply(Dados_Acoes,1,function(x) {
GBM(x["PRICE"],0,x["VOL"]*100,1,252)
})
MC <- MC/Numero_Simulacoes
MC <- MC+MC1
}
end.time <- Sys.time()
time.taken <- end.time - start.time
print(time.taken)
return(MC)
}
The function takes the data on each line of the matrix and pass it as parameters values to the GBM function, then I average the results in order to return a matrix of the averaged stock price simulation for the 500 stocks in 252 days.
I am using 0 drift, but i can also pass the avarage return as the second parameter on GBM
This is giving me the result that I expect, but I was wondering if there is a more efficient way to do it.
Parameters are in Portuguese :
Dados_Acoes = Stock Data
Numero_Simulacoes = Number of Simulations
Any Toughts ?