joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
809 stars 223 forks source link

addVo() and pretty scale #276

Open jsramseyer opened 4 years ago

jsramseyer commented 4 years ago

Description

Chartseries adds (by default) a second volume chart, equivalent to "addVo ()"

The volume chart scale is represented in a friendly way (x thousand, x one hundred thousand, etc.)

When we add the Volume average to this graph, it is plotted without friendly scaling (x unit) and is outside the plot area.

Expected behavior

he expected behavior is to represent "SMA (Vo (VALE3.SA), n = 20)" on the same scale as the variable that created the second graph, in this case "Vo ()".

Minimal, reproducible example

# incorrect behavior of function Vo() in second graph
fim <- Sys.Date()
inicio <- Sys.Date() - 730

getSymbols("VALE3.SA", from= inicio, to = fim)
chartSeries(VALE3.SA, theme = "white", TA='addEMA(n=21); addVo(); addTA(SMA(Vo(VALE3.SA), n=20), on=2)', subset = 'last 6 months')

# correct behavior, add function in second graph, but not Vo() function
chartSeries(VALE3.SA, theme = "white", TA= 'addEMA(n=21,col="red"); addOBV(); addTA(EMA(OBV(Cl(VALE3.SA), Vo(VALE3.SA)), n=10), on =2)', subset = 'last 6 months')

Session Info

[Insert your sessionInfo() output]
helgasoft commented 4 years ago

You may get the desired result with the newer charting functions. Try this:

d <- na.omit(getSymbols("VALE3.SA", from=Sys.Date()-300, to=Sys.Date(), auto.assign=FALSE))
rm(g)  # remove graphics chob in case it exists already
g <- chart_Series(d, TA='add_EMA(n=21, on=1); add_Vo()', name='vale3.sa')
g <- add_TA(SMA(Vo(d), n=20), col='blue', on=2)
plot(g)
joshuaulrich commented 4 years ago

I agree that this is a bug in chartSeries() and/or addTA(), but I'm not sure it's worth the effort to try and fix. Especially when chart_Series() works.