Supplying an argument to TA
in chartSeries
inside a function only works when TA
´s are in .GlobalEnv$
. Taking .GlobalEnv$
out, it gets this error:
Error in addTA(cChart.env$I1, col = "red", on = 1) :
object 'cChart.env' not found
It seems that chartSeries
is looking for TA
´s only in global environment.
#lib
library(quantmod)
#function
left <- function(x, n) if(n>0) substr(x, 1, n) else substr(x, 1-n, nchar(x))
cChart=function(chart,dt_venc,nSem,diasPos=0){
dt_ini=dt_venc-(7*nSem)
dt_fim=dt_ini+4
dt_fim=dt_fim+diasPos
dt_ini=dt_ini-120
price <- Cl(chart)
#it only worked with .GlobalEnv$cChart.env
.GlobalEnv$cChart.env=list( # cChart.env=list(...) does not work
I1 = SMA(price,10)
,I2 = SMA(price,20)
,I3 = SMA(price,50)
,I4 = SMA(price,100)
)
#it only worked with .GlobalEnv$cChart.env
TA=list("addTA(cChart.env$I1,col='red',on=1)"
,"addTA(cChart.env$I2,col='blue',on=1)"
,"addTA(cChart.env$I3,col='#009900',on=1)"
,"addTA(cChart.env$I4,col='black',on=1)"
,"addBBands(22)")
assign(deparse(substitute(chart)),chart)
chartSeries(chart,type='candlesticks',theme=chartTheme('white',up.col='#009900',dn.col='darkred')
,subset=paste0(dt_ini,'/',dt_fim)
,major.ticks='weeks'
,TA=TA)
print(deparse(substitute(chart)))
}
#input
getSymbols('PETR4.SA')
hj=Sys.Date()
p4=PETR4.SA[paste0('2017-01-01/',hj)]
colnames(p4)=left(colnames(p4),-9)
p4=p4[complete.cases(p4) & p4$Volume > 0,]
#routine
dt_venc=as.date('2018-01-15')
cChart(p4,dt_venc,nSem=2,diasPos=0)
Is there an option for TA
´s environment?
I've simplified your code, because it distracts from your core problem. Here are two options for solving your problem.
This alternative approach might be new, so I've added an example of
theme
andpars
which you could modify for your own purposes as a little bit of an extra starter.