Skip to content
Prev 13988 / 15274 Next

Multi Asset portfolio failing at applyStrategy with 'data' must be of a vector type, was 'NULL'

Please keep your replies on-list, so others can follow/contribute.
On Mon, Aug 8, 2016 at 9:59 AM, golam sakline <golam.sakline at gmail.com> wrote:
You still need to provide data, and _all_ the relevant functions.
osFixedDollar is still missing.  I assume the function you used is a
version of the function from Guy Yollin's presentations:

osFixedDollar <- function(timestamp, orderqty, portfolio, symbol, ruletype, ...)
{
  ClosePrice <- as.numeric(Cl(mktdata[timestamp,]))
  orderqty <- round(tradeSize/ClosePrice,-2)
  return(orderqty)
}


Your strategy runs fine for me using SPY data from Yahoo Finance, and
the latest version of quantstrat, blotter, and xts from GitHub. Here's
a version with many extraneous components removed.


require(quantstrat)

symbols <- c("SPY")
Sys.setenv(TZ="UTC")

getSymbols(symbols)

initEq <- 100000
tradeSize <- initEq/length(symbols)
currency("USD")
stock(symbols, currency="USD",multiplier=1)

if(exists('.strategy')) rm.strat(qs.strategy)
if(!exists('.blotter')) .blotter <- new.env()
if(!exists('.strategy')) .strategy <- new.env()

qs.strategy <- "AD26"
initPortf(qs.strategy, symbols)
initAcct(qs.strategy,portfolios=qs.strategy, initEq=initEq)
initOrders(portfolio=qs.strategy)
strategy(qs.strategy,store=TRUE)

add.indicator("AD26", name = "RT",
              arguments = list(x=quote(OHLC(mktdata))),label= "RT")

add.signal(qs.strategy, name="sigCrossover",
arguments=list(columns=c("Close", "RT"), relationship="gte"), label="Buy")

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

add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="Buy", sigval=TRUE,
                          prefer ="open",
                          replace = FALSE,
                          orderqty = 10,
                          osFUN = "osFixedDollar",
                          ordertype='market',
                          orderside='long',
                          TxnFees=-5,
                          orderset ="ocolong"
         ),
         type='enter',
         label = 'LE'
)

add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="Sell", sigval=TRUE,
                          replace = TRUE,
                          prefer ="open",
                          orderqty="all",
                          ordertype='market',
                          orderside='long',
                          TxnFees=-5,
                          orderset = "ocolong"
         ),
         type='exit',
         label = "LX"
)

add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="Buy", sigval=TRUE,
                          replace =FALSE,
                          orderqty="all",
                          ordertype='stoplimit',
                          orderside='long',
                          tmult = TRUE,
                          threshold = quote(stopLossPercent),
                          TxnFees=-5,
                          orderset = "ocolong"
         ),
         type='chain',
         parent = "LE",
         label = "StopLossLong",
         enabled = FALSE
)

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

updatePortf(qs.strategy)
updateAcct(qs.strategy)
updateEndEq(qs.strategy)



Here's my sessionInfo() output:
R> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] quantstrat_0.9.1739           foreach_1.4.2
[3] blotter_0.10.2                PerformanceAnalytics_1.4.3541
[5] FinancialInstrument_1.2.0     quantmod_0.4-5
[7] TTR_0.23-1                    xts_0.10-0
[9] zoo_1.7-13

loaded via a namespace (and not attached):
[1] codetools_0.2-14 grid_3.3.1       iterators_1.0.7  boot_1.3-18
[5] lattice_0.20-33