我正在尝试应用我在R中编码的这种策略,并且遇到了2个难以调试的错误。我可以获取有关如何解决这些错误并可能使我的代码更高效的帮助吗?在两个ETF上使用它,但在这一部分上专注于1以获取帮助。我只是以FYI开始R。
这是使用20天和10天渠道以及快速和慢速EMA过滤器对Donchian的策略进行编码。
下面编码的代码删除了我的许多笔记和其他不必要的区域,例如图表和内容。
这些错误的概括性表述促使我扫描了看起来缺少的内容(即EMA1
)。尽管我相信我为此创建了“对象”,但显然我不是。与colnames
错误相同...我不知道从哪里开始。
library(TTR)
library(IBrokers)
library(xts)
library(blotter)
currency("USD")
stock("VTI",currency="USD",multiplier=1)
stock("BND",currency="USD",multiplier=1)
initDate <-'2007-12-31'
startDate <-'2008-01-01'
endDate <-'2016-12-31'
initEq <- 1e6
Sys.setenv(TZ = "UTC")
getSymbols('VTI',
from = startDate,
to = endDate,
index.class = "POSIXct",
adjust = T
)
#not sure if needed
VTI = to.monthly(VTI,
indexAt = 'endof',
drop.time=FALSE
)
#HLVTI <- cbind(VTI$VTI.High,VTI$VTI.Low) incase it is needed below
#ACTUAL STRATEGY - initializing name to strategy
D.VTI <- "VTI Don"
#giving name D.VTI to portoflio and account.
initPortf(D.VTI, 'VTI', initDate = initDate) #portfolio startes at inital date
initAcct(D.VTI, portfolios = D.VTI, initDate = initDate, initEq=initEq)
initOrders(portfolio = D.VTI, initDate = initDate)
strategy(D.VTI, store = TRUE)
strat <- getStrategy(D.VTI) #retrieve strategy object
class(strat) #check class
summary(strat) #check what is inside the strategu itself. Check periodically.
#####
#Indicators. DCH 20-10, EMA 25-360 or EMA1-12 in months
add.indicator(strategy = D.VTI,
name = "DonchianChannel",
arguments = list(HL = quote(HLC(VTI)[,2:3]),
n = 10),
label = "DCH10")
add.indicator(strategy = D.VTI,
name = "DonchianChannel",
arguments = list(HL = quote(HLC(VTI)[,2:3]),
n = 20),
label = "DCH20")
add.indicator(strategy = D.VTI,
name = "EMA",
arguments = list(x = quote(Cl(VTI)), n=1),
label = "EMA1")
add.indicator(strategy = D.VTI,
name = "EMA",
arguments = list(x = quote(Cl(VTI)), n=12),
label = "EMA12")
summary(getStrategy(D.VTI))
#Signals
add.signal(D.VTI, name = "sigComparison",
arguments = list(columns = c("EMA1", "EMA12"), relationship = "gte"),
label = "EMA1.gte.EMA12")
add.signal(D.VTI, name = "sigComparison",
arguments = list(columns = c("EMA1", "EMA12"), relationship = "lte"),
label = "EMA1.lte.EMA12")
add.signal(D.VTI, name = "sigCrossover",
arguments = list(columns = c("Close", "high.DCH10"), relationship = "eq"),
label = "Close.eq.high.DCH10")
add.signal(D.VTI, name = "sigCrossover",
arguments = list(columns = c("Close", "low.DCH10"), relationship = "eq"),
label = "Close.eq.low.DCH10")
#Garrett and Brian's suggestion
add.signal(D.VTI, name="sigFormula",
arguments = list(columns=c("EMA1","EMA12","high.DCH20", "Close"),
formula = "(EMA1 > EMA12) & (CLose > high.DCH20)",
label="trigger",
cross=TRUE),
label="EMAFilterLong" #Close.gt.high.DCH20"
)
add.signal(D.VTI, name="sigFormula",
arguments = list(columns=c("EMA1","EMA12","high.DCH20", "Close"),
formula = "(EMA1 < EMA12) & (CLose < low.DCH20)",
label="trigger",
cross=TRUE),
label="EMAFilterShort" #Close.lt.low.DCH20
)
summary(getStrategy(D.VTI))
add.rule(D.VTI, name = 'ruleSignal',
arguments = list(sigcol = "EMAFilterLong",
sigval=TRUE,
orderqty=100,
ordertype='market',
orderside='long'),
type='enter'
)
add.rule(D.VTI, name = 'ruleSignal',
arguments = list(sigcol = "Close.eq.low.DCH10",
sigval=TRUE,
orderqty='all',
ordertype='market',
orderside='long'),
type='exit')
add.rule(D.VTI, name = 'ruleSignal',
arguments = list(sigcol = "EMAFilterShort",
sigval=TRUE,
orderqty='100',
ordertype='market',
orderside='short'),
type='enter')
add.rule(D.VTI, name = 'ruleSignal',
arguments = list(sigcol = "Close.eq.high.DCH10",
sigval=TRUE,
orderqty='all',
ordertype='market',
orderside='short'),
type='exit')
summary(getStrategy(D.VTI))
applyStrategy(strategy = D.VTI, portfolios = D.VTI)
除了执行该策略并能够分析结果之外,我没有其他期望。相反,我正在检索此错误和一些警告。一旦套用applyStrategy,就会出现完整错误:
Error in eval(parse(text = formula), as.list(data)) :
object 'EMA1' not found
In addition: Warning message:
In match.names(columns, colnames(data)) :
all columns not located in EMA1 EMA12 for VTI.Open VTI.High VTI.Low VTI.Close VTI.Volume VTI.Adjusted high.DCH10 mid.DCH10 low.DCH10 high.DCH20 mid.DCH20 low.DCH20 EMA.EMA1 EMA.EMA12 EMA1.gte.EMA12
Error in `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val))) :
attempt to set 'colnames' on an object with less than two dimensions.