I have data of 2 stocks on which I calculated 7-day rolling momentum to compare trends.
> dput(dt) structure(list(ticker = c("AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS", "TOPS"), date = structure(c(16072L, 16073L, 16076L, 16077L, 16078L, 16079L, 16080L, 16083L, 16084L, 16085L, 16086L, 16087L, 16091L, 16092L, 16093L, 16094L, 16097L, 16098L, 16099L, 16100L, 16101L, 16104L, 16105L, 16106L, 16107L, 16108L, 16111L, 16112L, 16113L, 16114L, 16115L, 16119L, 16120L, 16121L, 16122L, 16125L, 16126L, 16127L, 16128L, 16129L, 16132L, 16133L, 16134L, 16135L, 16136L, 16139L, 16140L, 16141L, 16142L, 16143L, 16146L, 16147L, 16148L, 16149L, 16150L, 16153L, 16154L, 16155L, 16156L, 16157L, 16160L, 16161L, 16162L, 16163L, 16164L, 16167L, 16168L, 16169L, 16170L, 16171L, 16174L, 16175L, 16176L, 16177L, 16181L, 16182L, 16183L, 16184L, 16185L, 16188L, 16189L, 16190L, 16191L, 16192L, 16195L, 16196L, 16197L, 16198L, 16199L, 16202L, 16203L, 16204L, 16205L, 16206L, 16209L, 16210L, 16211L, 16212L, 16213L, 16217L, 16218L, 16220L, 16072L, 16073L, 16076L, 16077L, 16078L, 16079L, 16083L, 16084L, 16085L, 16086L, 16087L, 16091L, 16092L, 16097L, 16099L, 16100L, 16101L, 16104L, 16105L, 16106L, 16107L, 16108L, 16111L, 16113L, 16119L, 16120L, 16121L, 16122L, 16125L, 16126L, 16128L, 16129L, 16132L, 16133L, 16134L, 16135L, 16136L, 16139L, 16140L, 16141L, 16142L, 16146L, 16147L, 16148L, 16149L, 16150L, 16153L, 16154L, 16155L, 16156L, 16157L, 16160L, 16161L, 16162L, 16164L, 16167L, 16168L, 16169L, 16170L, 16171L, 16174L, 16175L, 16176L, 16177L, 16181L, 16182L, 16183L, 16184L, 16185L, 16188L, 16189L, 16190L, 16191L, 16192L, 16195L, 16196L, 16203L, 16204L, 16205L, 16206L, 16210L, 16211L, 16213L, 16219L, 16220L), class = c("IDate", "Date")), close = c(19.754643, 19.320714, 19.426071, 19.287054, 19.409286, 19.161393, 19.033571, 19.133214, 19.513929, 19.905714, 19.794643, 19.309643, 19.609643, 19.696786, 19.863571, 19.5025, 19.660714, 18.089286, 17.883929, 17.849357, 17.878571, 17.911786, 18.171071, 18.306786, 18.303929, 18.56, 18.8925, 19.141429, 19.14, 19.443929, 19.428214, 19.499643, 19.191786, 18.969643, 18.758929, 18.841071, 18.645, 18.476786, 18.845357, 18.794286, 18.848571, 18.972857, 19.012857, 18.955357, 18.944286, 18.961429, 19.146071, 19.164643, 18.951786, 18.738929, 18.812143, 18.978571, 18.973571, 18.882143, 19.031071, 19.256786, 19.463929, 19.277857, 19.195, 19.173571, 19.169286, 19.344643, 19.376786, 19.2425, 18.993571, 18.695357, 18.694286, 18.94, 18.695714, 18.5575, 18.631429, 18.498568, 18.536071, 18.747857, 18.970357, 18.98925, 18.741071, 20.2775, 20.426429, 21.2175, 21.154643, 21.072857, 21.124286, 21.163571, 21.462857, 21.228929, 21.154643, 20.999643, 20.912232, 21.1725, 21.205714, 21.209643, 21.029286, 21.339643, 21.5925, 21.596786, 21.653929, 21.688214, 21.933214, 22.343929, 22.286071, 22.607143, 10343340000, 1.0773e+10, 1.0584e+10, 1.0773e+10, 1.071e+10, 1.071e+10, 1.0332e+10, 1.0899e+10, 1.071e+10, 11040750000, 11025630000, 1.1781e+10, 1.2537e+10, 11592630000, 10961370000, 1.071e+10, 1.0899e+10, 10649520000, 1.0395e+10, 1.0647e+10, 10451700000, 1.0395e+10, 10370430000, 1.008e+10, 8.505e+09, 9.009e+09, 8819370000, 8.505e+09, 8.379e+09, 8.253e+09, 8.505e+09, 8.631e+09, 8675100000, 9.009e+09, 8.883e+09, 8.82e+09, 9.387e+09, 8820630000, 9370620000, 9.387e+09, 8825040000, 9.009e+09, 9.45e+09, 9.387e+09, 9.261e+09, 9.135e+09, 8.883e+09, 9.135e+09, 9.135e+09, 8.883e+09, 9.261e+09, 9.009e+09, 8695890000, 8.757e+09, 8.946e+09, 8.757e+09, 9.135e+09, 9.135e+09, 8.757e+09, 8.82e+09, 8.82e+09, 8.631e+09, 8.064e+09, 7.875e+09, 6.858e+09, 6.849e+09, 6426450000, 6614910000, 6.291e+09, 6.381e+09, 6335910000, 5.913e+09, 5.733e+09, 5.454e+09, 5255100000, 4.986e+09, 5030100000, 4806180000, 4230090000, 4061160000, 4.176e+09, 4.203e+09, 3.861e+09, 3524400000, 3.258e+09)), row.names = c(NA, -187L), class = c("data.table", "data.frame"))
The code I am using to find the momentum is as follows -
library(data.table) library(dplyr) library(TTR) library(lattice) dt[, mom := round(TTR::momentum(close, n = 7) / 7, 6), by = ticker] %>% na.omit xyplot(mom ~ date, group = ticker, dt, t='l')
However, the scale of momentum is too high for me to make any meaning out of it. See the output of xyplot
. I want to compare the momentum of these two stocks and identify what's going on in these two stocks.
Maybe, I am trying to compare the price change in the wrong way. There might be a better way than momentum.
Any suggestions are welcome.
Thanks!
https://stackoverflow.com/questions/66015034/r-trend-comparison-of-two-stocks February 03, 2021 at 01:51AM
没有评论:
发表评论