Search code examples
rquantmodalgorithmic-tradingquantstrat

object mktdata not found


I believe this is a formatting issue with my indicator. Can someone tell me what im doing wrong here?

#....omitted the portfolio initialization above
#returns change from past day, or NA if one of the values is invalid
changeDaily<-function(x,y){if(is.na(x+y)==T){return(NA)};ifelse(x-y>0,"UP","DOWN")}

#creates column called lagPredict which uses the function changeDaily
add.indicator(strat, name = "changeDaily",arguments = list(HLC = quote(mktdata),Cl(mktdata),Lag(Cl(mktdata))), label='lagPredict')

error:

Error in has.Cl(x) : object 'mktdata' not found

Traceback:

traceback()
3: has.Cl(x)
2: Cl(mktdata)
1: add.indicator(strat, name = "changeDaily", arguments = list(HLC = quote(mktdata), 
       Cl(mktdata), Lag(Cl(mktdata))), label = "lagPredict")

Complete code:

source("forex.functions.R")
startDate <- '2010-01-01' # start of data
endDate <- '2015-05-01' # end of data
symbols<-c("USD/EUR")
portfolio<-acct<-strat<-"simpleLookAhead"

initSetup(symbols,portfolio,acct,strat)
dump<-lapply(symbols,function(x)forex.weeklyOHLC(x))
symbols<-gsub("/","",symbols)
#############################################################
#returns change from past day, or NA if one of the values is invalid
changeDaily<-function(x,y){if(is.na(x+y)==T){return(NA)};ifelse(x-y>0,"UP","DOWN")}

#creates column called lagPredict which uses the function changeDaily to return UP or DOWN in reference to yesterdays price
add.indicator(strat, name = "changeDaily",arguments = list(HLC = quote(mktdata),Cl(mktdata),Lag(Cl(mktdata))), label='lagPredict')

forex.functions.R

library(PerformanceAnalytics)
library(quantmod)
library(lattice)
library(IKTrading)
library(quantstrat)
Sys.setenv(TZ="EST") # set time zone
if (!exists('.blotter')) .blotter <- new.env()
if (!exists('.strategy')) .strategy <- new.env()

forex.weeklyOHLC<-function(ss){
    ss<-getSymbols(ss,src="oanda",from=startDate,to=endDate)
    x<-get(ss)
    #x<-adjustOHLC(x,symbol.name=symbol) #calls get Splits which calls getSymbols which fails bc src != oanda
    x<-to.weekly(x,indexAt='lastof',drop.time=TRUE)
    indexFormat(x)<-'%Y-%m-%d'
    colnames(x)<-gsub("x",ss,colnames(x)) 
    assign(ss,x)    
}

initSetup<-function(symbols,portfolio, acct, strat){
    initDate <- '2009-12-31'
    initEq <- 1e6
    currency("USD")
    stock(symbols, currency="USD", multiplier=1)
    rm.strat(strat) # remove portfolio, account, orderbook if re-run
    initPortf(name=portfolio, symbols, initDate=Sys.Date())
    initAcct(name=acct, portfolios=portfolio,initDate=Sys.Date(), initEq=initEq)
    initOrders(portfolio=portfolio, initDate=Sys.Date())
    strategy(strat, store=TRUE)
}

Solution

  • You need to quote all the objects in the arguments list in the call to add.indicator to prevent them from being evaluated. You also need to specify the correct arguments to pass to your changeDaily function. You pass HLC, but changeDaily does not have a HLC argument.

    Your add.indicator call should look something like this:

    add.indicator(strat, name = "changeDaily",
      arguments = list(x = quote(Cl(mktdata)), y = quote(Lag(Cl(mktdata)))),
      label = 'lagPredict')