吸墨纸功能 chart.Posn() 中的日志图表可能吗?
Log chart in blotter function chart.Posn() possible?
blotter的chart.Posn()
或chart.Reconcile()
函数中是否可以绘制对数价格图表?我尝试将 log.scale = TRUE
添加到函数调用但没有成功。底层 chart_Series
函数是否仍然 "experimental" 无法支持此功能,或者函数调用是否不正确?
chart.Posn(Portfolio = portfolio.st, Symbol = "GSPC", log.scale = TRUE)
更新:我一直在尝试直接使用chart_Series()
函数,设置ylog图形参数:
par(ylog=TRUE)
chart_Series(Cl(GSPC))
但是我收到一个错误 "log scale needs positive bounds" 尽管数据都是正数。
顺便说一句,GSPC 是标准普尔 500 指数的 OHLCV 时间序列 xts,绘制在 chartSeries()
和 chart_Series()
中,但两个图表函数都没有对数刻度。
我发现这个旧的 post 不是解决方案而是替代方案:
Does chart_Series() work with logarithmic axis?
我不认为 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
这是原始图表的样子:
新情节,对数刻度:
blotter的chart.Posn()
或chart.Reconcile()
函数中是否可以绘制对数价格图表?我尝试将 log.scale = TRUE
添加到函数调用但没有成功。底层 chart_Series
函数是否仍然 "experimental" 无法支持此功能,或者函数调用是否不正确?
chart.Posn(Portfolio = portfolio.st, Symbol = "GSPC", log.scale = TRUE)
更新:我一直在尝试直接使用chart_Series()
函数,设置ylog图形参数:
par(ylog=TRUE)
chart_Series(Cl(GSPC))
但是我收到一个错误 "log scale needs positive bounds" 尽管数据都是正数。
顺便说一句,GSPC 是标准普尔 500 指数的 OHLCV 时间序列 xts,绘制在 chartSeries()
和 chart_Series()
中,但两个图表函数都没有对数刻度。
我发现这个旧的 post 不是解决方案而是替代方案:
Does chart_Series() work with logarithmic axis?
我不认为 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
这是原始图表的样子:
新情节,对数刻度: