Guest User

Untitled

a guest
Aug 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #Setup
  2. rm(list = ls(all = TRUE))
  3. set.seed(1)
  4. nsims <- 1000
  5. library(quantmod)
  6. library(PerformanceAnalytics)
  7. #Load Data
  8. getSymbols('^GSPC',from='1900-01-01')
  9. spyReturns <- dailyReturn(GSPC$GSPC.Adjusted, type = "arithmetic")
  10.  
  11. #Make 1000 random position vectors
  12. randomPositions <- sample(c(0,1),nsims*length(spyReturns),TRUE)
  13. randomPositions <- matrix(randomPositions,ncol=nsims)
  14.  
  15. #Determine trades and calculate trade costs
  16. trades <- apply(randomPositions,2,Lag,1) != randomPositions
  17. trades[1,] <- trades[1,]==1
  18. trades <- ifelse(trades,0.001,0) #Assume the commision is 0.5%
  19.  
  20. #Determine dailty returns of random strats
  21. randomStrats <- randomPositions*matrix(rep(spyReturns,nsims),ncol=nsims)
  22. randomStrats <- randomStrats-trades
  23. randomStrats.return <- apply(randomStrats,2,Return.annualized,scale=252)
  24. hist(randomStrats.return, freq=FALSE)
  25.  
  26. #Plot some of the strategies
  27. library(PerformanceAnalytics)
  28. charts.PerformanceSummary(na.omit(cbind(spyReturns,randomStrats)[,1:15]),colorset=redfocus,)
  29.  
  30. #Same plot, removes buy&hold
  31. #charts.PerformanceSummary(na.omit(cbind(spyReturns,randomStrats)[,2:15]),colorset=redfocus,)
Add Comment
Please, Sign In to add comment