I wrote a simple function that combines all indicators from the TTR package, but not all indicators have names that have changed as I specified in the dataframe.
for example, TTR::chaikinVolatility
indicator has not changed its name, as well as some others that I indicated in the comments inside the function
data.frame(
..
..
chaikinVolatility = TTR::chaikinVolatility(HL = HL, n = n), # why colname 'EMA' not chaikinVolatility ?
..
..
)
Here is my complete function
get_all_indicators <- function(p, n= 10){
CLOSE <- quantmod::Cl(p)
OHLC <- quantmod::OHLC(p)
HLC <- quantmod::HLC(p)
HL <- quantmod::HL(p)
VOLUME <- quantmod::Vo(p)
data.frame(
ADX = TTR::ADX(HLC = HLC, n = n),
ALMA = TTR::ALMA(OHLC, n = n),
aroon = TTR::aroon(HL = HL, n = n),
ATR = TTR::ATR(HLC = HLC, n = n),
BBands = TTR::BBands(HLC = HLC, n = n),
CCI = TTR::CCI(HLC = HLC, n = n),
chaikinAD = TTR::chaikinAD(HLC = HLC, volume = VOLUME),
chaikinVolatility = TTR::chaikinVolatility(HL = HL, n = n), # why colname 'EMA' not chaikinVolatility ?
CLV = TTR::CLV(HLC = HLC),
CMF = TTR::CMF(HLC = HLC, n = n, volume = VOLUME),
CMO = TTR::CMO(x = CLOSE, n = n),
CTI = TTR::CTI(price = CLOSE, n = n),
DEMA = TTR::DEMA(x = CLOSE,n = n),
DonchianChannel = TTR::DonchianChannel(HL = HL,n = n),
DPO = TTR::DPO(x = CLOSE, n = n), #---------------------- why colname 'close' not DPO ?
DVI = TTR::DVI(price = CLOSE, n = n),
EMA = TTR::EMA(x = CLOSE, n = n), #---------------------- colname 'EMA.1' not EMA
EVWMA = TTR::EVWMA(price = CLOSE, n = n, volume = VOLUME),
HMA = TTR::HMA(CLOSE, n = n),
keltnerChannels = TTR::keltnerChannels(HLC = HLC, n = n),
KST = TTR::KST(CLOSE, n = n),
MACD = TTR::MACD(x = CLOSE,nFast = n, nSlow = n*2),
MFI = TTR::MFI(HLC = HLC, n = n, volume = VOLUME),
momentum = TTR::momentum(CLOSE, n = n), #-------------------why colname "Close.1" not momentum?
OBV = TTR::OBV(CLOSE,volume = VOLUME),
PBands =TTR::PBands(CLOSE, n = n),
ROC = TTR::ROC(OHLC, n = n),
RSI = TTR::RSI(CLOSE, n),
SAR = TTR::SAR(HL = HL),
SMA = TTR::SMA(x = CLOSE, n = n),
SMI = TTR::SMI(HLC = HLC, n = n, nFast = n, nSlow = n*2),
SNR = TTR::SNR(HLC = HLC, n = n), #-------------------why colname "Close.2" not SNR?
stoch = TTR::stoch(HLC = HLC, nFastK = n, nFastD = n, nSlowD = n*2),
TDI = TTR::TDI(CLOSE,n = n),
TR = TTR::TR(HLC = HLC),
TRIX = TTR::TRIX(price = CLOSE, n = n),
VHF = TTR::VHF(price = CLOSE, n = n),
volatility = TTR::volatility(OHLC = OHLC, n = n),
williamsAD = TTR::williamsAD(HLC = HLC),
WPR = TTR::WPR(HLC = HLC, n = n) #-------------------why colname "Close.3" not WPR?
)
}
Here is the code to fully launch the function
library(xts)
library(quantmod)
library(TTR)
n <- 10000
# make some fake praces
prices <- cumsum(rnorm(n,sd = 0.01)) |>
round(2) |>
cbind(price = _, volume = sample(1:1000,n,T)) |>
xts(Sys.time()-n:1) |>
to.minutes(name = "my_prices") +10000
colnames(prices) <- gsub("^my_prices\\.", "", colnames(prices))
# head(prices)
# chart_Series(prices)
gai <- get_all_indicators(p = prices)
colnames(gai)
colnames(gai)
[1] "ADX.DIp" "ADX.DIn" "ADX.DX" "ADX.ADX"
[5] "ALMA.Open" "ALMA.High" "ALMA.Low" "ALMA.Close"
[9] "aroon.aroonUp" "aroon.aroonDn" "aroon.oscillator" "ATR.tr"
[13] "ATR.atr" "ATR.trueHigh" "ATR.trueLow" "BBands.dn"
[17] "BBands.mavg" "BBands.up" "BBands.pctB" "cci"
[21] "chaikinAD" "EMA" "clv" "CMF"
[25] "cmo" "cti" "DEMA" "DonchianChannel.high"
[29] "DonchianChannel.mid" "DonchianChannel.low" "Close" "DVI.dvi.mag"
[33] "DVI.dvi.str" "DVI.dvi" "EMA.1" "EVWMA"
[37] "HMA" "keltnerChannels.dn" "keltnerChannels.mavg" "keltnerChannels.up"
[41] "KST.kst" "KST.signal" "MACD.macd" "MACD.signal"
[45] "mfi" "Close.1" "obv" "PBands.dn"
[49] "PBands.center" "PBands.up" "ROC.Open" "ROC.High"
[53] "ROC.Low" "ROC.Close" "rsi" "sar"
[57] "SMA" "SMI.SMI" "SMI.signal" "Close.2"
[61] "stoch.fastK" "stoch.fastD" "stoch.slowD" "TDI.tdi"
[65] "TDI.di" "TR.tr" "TR.trueHigh" "TR.trueLow"
[69] "TRIX.TRIX" "TRIX.signal" "VHF" "volatility"
[73] "williamsAD" "Close.3"
What could be wrong with this code?
The functions that you are having problems with return xts
objects rather than simple vectors. When those are coerced into data.frames they keep their special names. You can drop the default names by extracing the core data from the xts
object and explictly unnaming it
data.frame(
chaikinVolatility = unname(coredata(TTR::chaikinVolatility(HL = HL, n = n))),
DPO = unname(coredata(TTR::DPO(x = CLOSE, n = n))),
EMA = unname(coredata(TTR::EMA(x = CLOSE, n = n))),
momentum = unname(coredata(TTR::momentum(CLOSE, n = n))),
SNR = unname(coredata(TTR::SNR(HLC = HLC, n = n))),
WPR = unname(coredata(TTR::WPR(HLC = HLC, n = n)))
)