rchartsquantmodquantstratblotter

Log chart in blotter function chart.Posn() possible?


Is it possible to draw a log price chart in the chart.Posn() or chart.Reconcile() functions of blotter? I tried adding log.scale = TRUE to the function call without success. Is the underlying chart_Series function still too "experimental" to support this functionality or is the function call not correct?

chart.Posn(Portfolio = portfolio.st, Symbol = "GSPC", log.scale = TRUE)

Update: I have been trying to use the chart_Series() function directly, setting the ylog graphical parameter:

par(ylog=TRUE)
chart_Series(Cl(GSPC))

But I receive an error "log scale needs positive bounds" despite the data being all positive.

Btw, GSPC is an OHLCV time-series xts of the S&P 500 that plots in chartSeries() and chart_Series(), but just not with log-scale for either charting functions.

I found this old post not as a solution but as an alternative:

Does chart_Series() work with logarithmic axis?


Solution

  • I don't think there is any parameter like log.scale that chart_Series recognises. You could simply do chart_Series(log(Cl(GSPC)). You could also do some basic modifications to chart.Posn to put things on the log scale. Use as a starting point the source code for chart.Posn.

    Here is an example of a modified function you could make. You can obviously modify it further in any way you please.

    # We need an example.  So,
    # Source this code from the directory containing quantstrat, or at least source the macd.R demo in quantstrat.
    source("demo/macd.R")
    
    
    log.chart.Posn <- function(Portfolio, Symbol, Dates = NULL, env = .GlobalEnv) {
      pname<-Portfolio
      Portfolio<-getPortfolio(pname)
      x <- get(Symbol, env)
      Prices <- log(x)
      chart_Series(Prices)
      #browser()
      if(is.null(Dates)) Dates<-paste(first(index(Prices)),last(index(Prices)),sep='::')
    
      #scope the data by Dates
      Portfolio$symbols[[Symbol]]$txn<-Portfolio$symbols[[Symbol]]$txn[Dates]
      Portfolio$symbols[[Symbol]]$posPL<-Portfolio$symbols[[Symbol]]$posPL[Dates]
    
      Trades = Portfolio$symbols[[Symbol]]$txn$Txn.Qty
    
      Buys = log(Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades>0)])
      Sells = log(Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades<0)])
    
      Position = Portfolio$symbols[[Symbol]]$txn$Pos.Qty
    
      if(nrow(Position)<1) stop ('no transactions/positions to chart')
    
      if(as.POSIXct(first(index(Prices)))<as.POSIXct(first(index(Position)))) Position<-rbind(xts(0,order.by=first(index(Prices)-1)),Position)
      Positionfill = na.locf(merge(Position,index(Prices)))
    
      CumPL = cumsum(Portfolio$symbols[[Symbol]]$posPL$Net.Trading.PL)
      if(length(CumPL)>1)
        CumPL = na.omit(na.locf(merge(CumPL,index(Prices))))
      else
        CumPL = NULL
    
      if(!is.null(CumPL)) {
        CumMax <- cummax(CumPL)
        Drawdown <- -(CumMax - CumPL)
        Drawdown<-rbind(xts(-max(CumPL),order.by=first(index(Drawdown)-1)),Drawdown)
      } else {
        Drawdown <- NULL
      }
    
      if(!is.null(nrow(Buys)) && nrow(Buys) >=1 ) (add_TA(Buys,pch=2,type='p',col='green', on=1));
      if(!is.null(nrow(Sells)) && nrow(Sells) >= 1) (add_TA(Sells,pch=6,type='p',col='red', on=1));
      if(nrow(Position)>=1) {
        (add_TA(Positionfill,type='h',col='blue', lwd=2))
        (add_TA(Position,type='p',col='orange', lwd=2, on=2))
      }
      if(!is.null(CumPL))  (add_TA(CumPL, col='darkgreen', lwd=2))
      if(!is.null(Drawdown)) (add_TA(Drawdown, col='darkred', lwd=2, yaxis=c(0,-max(CumMax))))
      plot(current.chob())
    
    
    
    }
    
    log.chart.Posn(Portfolio = portfolio.st, Sym = "AAPL", Dates = NULL, env = .GlobalEnv)
    add_MACD() # Simply added to make the plot almost identical to what is in demo/macd.R
    

    This is what the original chart looks like: enter image description here

    New plot, with log scales: enter image description here