【问题标题】:Log chart in blotter function chart.Posn() possible?吸墨纸功能图表中的日志图表。Posn() 可能吗?
【发布时间】:2018-01-15 10:39:23
【问题描述】:

是否可以在blotter的chart.Posn()chart.Reconcile()功能中绘制对数价格图表?我尝试将log.scale = TRUE 添加到函数调用中,但没有成功。底层的chart_Series 函数是否仍然过于“实验性”而无法支持此功能,还是函数调用不正确?

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

更新:我一直在尝试直接使用chart_Series()函数,设置ylog图形参数:

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

但我收到一个错误“对数刻度需要正边界”,尽管数据都是正数。

顺便说一句,GSPC 是标准普尔 500 指数的 OHLCV 时间序列 xts,绘制在 chartSeries()chart_Series() 中,但对于任何一个图表函数都没有对数刻度。

我发现这篇旧帖子不是作为解决方案,而是作为替代方案:

Does chart_Series() work with logarithmic axis?

【问题讨论】:

    标签: r charts quantmod quantstrat blotter


    【解决方案1】:

    我认为chart_Series 无法识别像log.scale 这样的参数。你可以简单地做chart_Series(log(Cl(GSPC))。您还可以对chart.Posn 进行一些基本修改,以将内容放在对数刻度上。以chart.Posn 的源代码为起点。

    这是您可以修改的函数的示例。您显然可以以任何您喜欢的方式进一步修改它。

    # 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
    

    这是原始图表的样子:

    带有对数刻度的新绘图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多