错误指定的时间序列参数无效 - 在回测 R 脚本中
Error invalid time series parameters specified - in backtesting R script
我已经创建了这个回测脚本并执行了它,但是有一个小问题。
library(quantmod)
library(PerformanceAnalytics)
tickers = 'AMZN'
symbol = getSymbols(tickers,from="2014-01-01",auto.assign=F)
head(symbol)
prices=Cl(symbol)
sma.fast=SMA(prices, 10)
sma.slow=SMA(prices, 20)
buy.signal = ifelse((sma.fast> sma.slow), 1, NA)
sell.signal=ifelse((sma.fast< sma.slow), -1, NA)
position=rowSums(cbind(buy.signal,sell.signal),na.rm=TRUE)
myReturn <- lag(position) * dailyReturn(symbol)
charts.PerformanceSummary(cbind(dailyReturn(symbol),myReturn))
Performance <- function(x) {
cumRetx = Return.cumulative(x)
annRetx = Return.annualized(x, scale=252)
sharpex = SharpeRatio.annualized(x, scale=252)
winpctx = length(x[x > 0])/length(x[x != 0])
annSDx = sd.annualized(x, scale=252)
DDs <- findDrawdowns(x)
maxDDx = min(DDs$return)
maxLx = max(DDs$length)
Perf = c(cumRetx, annRetx, sharpex, winpctx, annSDx, maxDDx, maxLx)
names(Perf) = c("Cumulative Return", "Annual Return","Annualized Sharpe Ratio",
"Win %","Annualized Volatility", "Maximum Drawdown", "Max Length Drawdown")
return(Perf)
}
cbind(STRAT=Performance(myReturn),BMK=Performance(dailyReturn(symbol)))
执行此脚本时显示此错误
Error in `[.xts`(x, x > 0) : invalid time series parameters specified
我能够在问题行中归零,并且在计算时 winpctx = length(x[x > 0])/length(x[x != 0])
出现了这个问题。
如何解决这个错误。
P.S。 Performance() 取自网站
问题出在这一行:
myReturn <- lag(position) * dailyReturn(symbol)
position
只是一个向量(不是 xts 对象)所以 lag.default
被调度并且 lag.default
只是更改 tsp
属性(如果没有则添加一个'不存在)。这使得 myReturn
成为格式错误的 xts 对象。
> str(lag(position))
atomic [1:422] 0 0 0 0 0 0 0 0 0 0 ...
- attr(*, "tsp")= num [1:3] 0 421 1
> str(myReturn)
Error in `[.xts`(x, 1, ) : invalid time series parameters specified
解决这个问题,其他一切正常。
signals <- merge(buy.signal, sell.signal)
position <- xts(rowSums(signals, na.rm=TRUE), index(signals))
myReturn <- lag(position) * dailyReturn(symbol)
# set 1st obs to 0 (findDrawdowns complains if there are NA in the series)
myReturn[1] <- 0
我已经创建了这个回测脚本并执行了它,但是有一个小问题。
library(quantmod)
library(PerformanceAnalytics)
tickers = 'AMZN'
symbol = getSymbols(tickers,from="2014-01-01",auto.assign=F)
head(symbol)
prices=Cl(symbol)
sma.fast=SMA(prices, 10)
sma.slow=SMA(prices, 20)
buy.signal = ifelse((sma.fast> sma.slow), 1, NA)
sell.signal=ifelse((sma.fast< sma.slow), -1, NA)
position=rowSums(cbind(buy.signal,sell.signal),na.rm=TRUE)
myReturn <- lag(position) * dailyReturn(symbol)
charts.PerformanceSummary(cbind(dailyReturn(symbol),myReturn))
Performance <- function(x) {
cumRetx = Return.cumulative(x)
annRetx = Return.annualized(x, scale=252)
sharpex = SharpeRatio.annualized(x, scale=252)
winpctx = length(x[x > 0])/length(x[x != 0])
annSDx = sd.annualized(x, scale=252)
DDs <- findDrawdowns(x)
maxDDx = min(DDs$return)
maxLx = max(DDs$length)
Perf = c(cumRetx, annRetx, sharpex, winpctx, annSDx, maxDDx, maxLx)
names(Perf) = c("Cumulative Return", "Annual Return","Annualized Sharpe Ratio",
"Win %","Annualized Volatility", "Maximum Drawdown", "Max Length Drawdown")
return(Perf)
}
cbind(STRAT=Performance(myReturn),BMK=Performance(dailyReturn(symbol)))
执行此脚本时显示此错误
Error in `[.xts`(x, x > 0) : invalid time series parameters specified
我能够在问题行中归零,并且在计算时 winpctx = length(x[x > 0])/length(x[x != 0])
出现了这个问题。
如何解决这个错误。
P.S。 Performance() 取自网站
问题出在这一行:
myReturn <- lag(position) * dailyReturn(symbol)
position
只是一个向量(不是 xts 对象)所以 lag.default
被调度并且 lag.default
只是更改 tsp
属性(如果没有则添加一个'不存在)。这使得 myReturn
成为格式错误的 xts 对象。
> str(lag(position))
atomic [1:422] 0 0 0 0 0 0 0 0 0 0 ...
- attr(*, "tsp")= num [1:3] 0 421 1
> str(myReturn)
Error in `[.xts`(x, 1, ) : invalid time series parameters specified
解决这个问题,其他一切正常。
signals <- merge(buy.signal, sell.signal)
position <- xts(rowSums(signals, na.rm=TRUE), index(signals))
myReturn <- lag(position) * dailyReturn(symbol)
# set 1st obs to 0 (findDrawdowns complains if there are NA in the series)
myReturn[1] <- 0