joshuaulrich / quantmod

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

Error when zooming chart on chart.Posn #81

Open ignl opened 8 years ago

ignl commented 8 years ago

Ok finally found time to create reproducible example on this error which I seem to encounter randomly. On most periods zoom_Chart works but sometimes I get this error:

Error in plot.window(c(1, 61), c(NaN, NaN)) : need finite 'ylim' values In addition: Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf

And here is my reproducible example:

library(blotter);

currency("USD");

stock("SPY",currency="USD",multiplier=1);

initDate <- '2006-01-01';
startDate <- '2006-01-01';
endDate <- '2016-03-16';
initEq <- 10000;

Sys.setenv(TZ = "UTC");

getSymbols('SPY', from=startDate, to=endDate, index.class="POSIXct", adjust=T)

addTxn("TEST", Symbol='SPY', TxnDate=startDate, TxnPrice=SPY[1,]$SPY.Close, TxnQty = 100, TxnFees=0, verbose = TRUE) 

chart.Posn("TEST", Symbol = 'SPY')
zoom_Chart("2008-01::2008-03")
joshuaulrich commented 8 years ago

Your example does not run. You didn't initialize the portfolio. Even if I do that, the chart.Posn call throws an error.

R> chart.Posn("TEST", Symbol = 'SPY')
Error in chart.Posn("TEST", Symbol = "SPY") : 
  no transactions/positions to chart

It might be because there's only one transaction... or because you didn't call updatePortf. This also may be a bug with blotter, if you can't replicate this issue independent of chart.Posn.

ignl commented 8 years ago

OK fixed it on empty environment. Not sure if its quantmod or blotter issue.

library(blotter);

currency("USD");

stock("SPY",currency="USD",multiplier=1);

initDate <- '2006-01-01';
startDate <- '2006-01-01';
endDate <- '2016-03-16';
initEq <- 10000;

suppressWarnings(try(rm(list=c("TEST", paste0("account.TEST"), paste0("portfolio.TEST")), pos=.GlobalEnv$.blotter), silent=TRUE))
initPortf("TEST", symbols="SPY", initDate=initDate)
initAcct("TEST", portfolios="TEST", initDate=initDate, initEq=initEq)

Sys.setenv(TZ = "UTC");

getSymbols('SPY', from=startDate, to=endDate, index.class="POSIXct", adjust=T)

addTxn("TEST", Symbol='SPY', TxnDate='2006-01-03', TxnPrice=SPY[1,]$SPY.Close, TxnQty = 100, TxnFees=0, verbose = TRUE) 

chart.Posn("TEST", Symbol = 'SPY')
zoom_Chart("2008-01::2008-03")
ignl commented 7 years ago

I came back to work a bit with quantmod and remembered about this error as I am still getting it. Maybe you have taken a look at it? It basically fails when you try to zoom into a date range that does not have any transactions.