first, I imported the data into R using quantmod.
getSymbols("QQQ")
QQQ <- xts::last(QQQ, "1 year")
QQQ
then, I used the MACD function to get the MACD for my etf.
macd_QQQ <- MACD(QQQ[,"QQQ.Close"], nFast = 12, nSlow = 26, nSig = 9, percent = FALSE)
macd_QQQ
I also obtained the ADX.
adx_QQQ <- ADX(QQQ, n =14)
adx_QQQ
Then I specified which values of the overall functions I wanted
adx <- adx_QQQ$ADX
adx
macd <- macd_QQQ$macd
macd
Currently, I am stuck here
macd_indicator <- function(macd)
if (macd > 0) {
signal <- "buy"
} else {
signal <- "sell"
}
The code has no problem running, but I am confused as to why buy and sell signals are not being produced in accordance with the values of macd.
Any help is appreciated
I wouldn't bet my money on this though.
library(tidyquant) # It uses Quantmod
library(tidyverse)
library(lubridate)
tq_get("QQQ", from = today() %m-% years(1)) %>%
tq_mutate(select = close,
mutate_fun = MACD) %>%
mutate(signal = if_else(macd > 0, "BUY", "SELL")) %>%
ggplot() +
aes(x = date, y = close, col = signal) +
geom_point() +
theme_light()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.