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?