0

I tried to run the following code

library(quantstrat)
library(blotter)
search()

currency("USD")
stock("SPY", currency = "USD", multiplier = 1)

ls(envir = FinancialInstrument:::.instrument)

ls(all=T)

initDate <- '1997-12-31'
startDate <- '1998-01-01'
endDate <- '2014-06-30'
initEq <- 1e6

Sys.setenv(TZ = "UTC")

options("getSymbols.yahoo.warning"=FALSE)

getSymbols('SPY', from = startDate, to = endDate, index.class = "POSIXct", adjust = T)

SPY$SMA10m <- SMA(Cl(SPY), 10)

#rm.strat(qs.strategy)

qs.strategy <- "qsFaber"

initPortf(qs.strategy, 'SPY', initDate = initDate)
initAcct(qs.strategy, portfolios = qs.strategy, initDate = initDate, initEq = initEq)
initOrders(portfolio = qs.strategy, initDate = initDate)
strategy(qs.strategy, store = TRUE)

strat <- getStrategy(qs.strategy)

add.indicator(
    strategy = qs.strategy,
    name = "SMA",
    arguments = list(
                x = quote(Cl(mltdata)), 
                n=10),
  label = "SMA10"
)


add.signal(
  qs.strategy,
  name = "sigCrossover",
  arguments = list(
    columns = c("Close", "SMA10"),
    relationship = "gt"),
  label = "Cl.gt.SMA"
)

add.signal(
  qs.strategy,
  name = "sigCrossover",
  arguments = list(
    columns = c("Close", "SMA10"),
    relationship = "lt"),
  label = "Cl.lt.SMA"
)

add.rule(
  qs.strategy,
  name = "ruleSignal",
  arguments = list(
    sigcol = "Cl.gt.SMA",
    sigval = TRUE,
    orderqty = 900,
    ordertype = "market",
    orderside = "long"),
  type = "enter"
)

add.rule(
  qs.strategy,
  name = "ruleSignal",
  arguments = list(
    sigcol = "Cl.lt.SMA",
    sigval = TRUE,
    orderqty = "all",
    ordertype = "market",
    orderside = "long"),
  type = "exit"
)

applyStrategy(strategy = qs.strategy, portfolios = qs.strategy)

I got the following error message:

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

Could anyone explain why am I getting this error? Any feed or reply is greatly appreciated!

boniface316
  • 489
  • 3
  • 17

2 Answers2

1

Here:

add.indicator(
    strategy = qs.strategy,
    name = "SMA",
    arguments = list(
                x = quote(Cl(mltdata)), 
                n=10),
  label = "SMA10"
)

it should be mktdata. But even then you are not specifying it. Therefore, try to modify it to x = quote(Cl(SPY)).

AK88
  • 2,946
  • 2
  • 12
  • 31
0

In your 'add.indicator' function, you've named the closing price (Cl) of mltdata in the arguments list. However, no where else in the script is 'mltdata' defined. I do see that you downloaded SPY, so perhaps you need to update the example script to reflect the data you are working with.

~ Justin

Justin
  • 1,360
  • 12
  • 15