2

I can't figure out how I can backtest a strategy trading ticker X and ticker Y based on a signal from a synthetic asset created from a combination of ticker X and Y.

The data background is a list of XTS series of tickers. Right now I am solving it by trading the synthetic asset, not the individual assets:

initDate = "1990-01-01"
from = "2010-07-22"
to = "2016-12-31"
initeq = 1000000
NBDG <- lXTS[[1]]
UKPSPIR <- lXTS[[2]]
CoIntV <- list(1, -9.90)
Diff <- NBDG - as.numeric(CoIntV[2])*UKPSPIR
colnames(Diff) <- "Close"

strategy.st <- portfolio.st <- account.st <- "test"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols = list("Diff"), initDate=initDate)
initAcct(account.st, portfolios=portfolio.st)
initOrders(portfolio.st)
strategy(strategy.st, store = TRUE)

Diff <- cbind(Diff, BBands(Diff, maType="SMA", n=12, sd=2))

add.signal(strategy=strategy.st, name="sigCrossover",
           arguments = list(columns=c("Close", "up"),
                            relationship="gt"),
           label="cl.gt.up")

add.signal(strategy=strategy.st, name="sigCrossover",
           arguments = list(columns=c("Close", "dn"),
                            relationship="lt"),
           label="cl.lt.dn")

add.signal(strategy=strategy.st, name="sigCrossover",
           arguments = list(columns=c("Close", "mavg"),
                            relationship="gte"),
           label="mid.cross.frombelow")

add.signal(strategy=strategy.st, name="sigCrossover",
           arguments = list(columns=c("Close", "mavg"),
                            relationship="lte"),
           label="mid.cross.fromabove")

tmp <- applySignals(strategy = strategy.st, mktdata=Diff)

add.rule(stratBBands,name='ruleSignal',
         arguments = list(sigcol="cl.gt.up",
                          sigval=TRUE, 
                          orderqty=-1, 
                          ordertype='market', 
                          orderside=NULL, 
                          threshold=NULL),
         type='enter')

add.rule(stratBBands,name='ruleSignal',
         arguments = list(sigcol="cl.lt.dn",
                          sigval=TRUE, 
                          orderqty=1, 
                          ordertype='market', 
                          orderside=NULL, 
                          threshold=NULL),
         type='enter')

add.rule(stratBBands,name='ruleSignal',
         arguments = list(sigcol="mid.cross.frombelow",
                          sigval=TRUE, 
                          orderqty='all', 
                          ordertype='market', 
                          orderside=NULL, 
                          threshold=NULL),
         type='exit')

add.rule(stratBBands,name='ruleSignal',
         arguments = list(sigcol="mid.cross.fromabove",
                          sigval=TRUE, 
                          orderqty='all', 
                          ordertype='market', 
                          orderside=NULL, 
                          threshold=NULL),
         type='exit')

out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)

updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)

I get the following warnings when doing so:

1: In getInstrument(symbol) :
  instrument Diff not found, please create it first.
2: In getInstrument(Symbol) :
  instrument Diff not found, please create it first.
3: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol),  :
  Instrument Diff  not found, things may break

but I am getting results out.

Does anyone know anything?

mfvas
  • 115
  • 1
  • 11

1 Answers1

0

You have not defined your instruments, which quantstrat expects (although your simulation probably still runs OK). Just as the warning says to you.... Define your synthetic instrument before you run the strategy (before rm.strat in your code above, say).

You also should define your currency (not sure if it is GBP, but by default it is USD, which I assume here):

currency(c("USD"))
spread(primary_id = 'Diff', currency = "USD", members = c('NBDG','UKPSPIR'), memberratio = c(1, -9.90))

Run your code with these changes and the warnings will disappear.

(Also, in your code you post, you have arbitrarily changed from strategy.st to stratBBands at add.rules.)

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67