Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.02 KB | None | 0 0
  1. library(data.table)
  2. library(fasttime)
  3. library(ggplot2)
  4. library(PerformanceAnalytics)
  5. library(xts)
  6.  
  7. #Read data
  8. spy = fread("/path/to/unzipped/tsv")
  9.  
  10. #Convert dates to POSIX format
  11. spy = spy[,date := as.POSIXct(strptime(date, format="%Y-%m-%d %H:%M:%S"), tz="UTC")]
  12.  
  13. #Convert to time series object
  14. spy_xts = as.xts.data.table(spy)
  15.  
  16. #Convert timezone to EST
  17. indexTZ(spy_xts) <- "America/New_York"
  18.  
  19. #Select data points only at 9:30, 10:09, and 10:20 AM
  20. spy_xts =rbind(spy_xts['T09:30/T09:31'], spy_xts['T10:09/T10:11'], spy_xts['T10:20/T10:21'])
  21.  
  22. #Calculate interval returns based on close price
  23. spy_xts$Returns = Return.calculate(spy_xts$close, method="discrete")
  24. spy_xts$Log_Returns = Return.calculate(spy_xts$close, method="log")
  25.  
  26. #Make a matrix of returns at 10:09 and 10:20
  27. rets = cbind(as.vector(spy_xts['T10:09/T10:11']$Log_Returns), as.vector(spy_xts['T10:20/T10:21']$Log_Returns))
  28.  
  29. #Scatter plot of returns at 10:09 vs 10:20
  30. plot(rets)
  31.  
  32. #Pearson and Spearman correlations
  33. cor(rets)
  34. cor(rets, method="spearman")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement