[R-SIG-Finance] Multi Asset portfolio failing at applyStrategy with 'data' must be of a vector type, was 'NULL'

golam sakline golam.sakline at gmail.com
Fri Aug 12 12:28:35 CEST 2016


Hi Joshua,

I have found the problem and it is not within the code. There was a problem
with one of the CSV files with "NA" data cells.

Thanks & Regards

Golam

On Wed, Aug 10, 2016 at 3:41 AM, Joshua Ulrich <josh.m.ulrich at gmail.com>
wrote:

> 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:
> > RT is the function you wrote here...
> >
> > http://stackoverflow.com/questions/5554220/r-
> statistics-average-true-range-trailing-stop-indicator
> >
> > RT <- function(x)
> > {
> >   x$stopLongATR <- -3.5*ATR(HLC(x),5)[,"atr"]
> >   x$stopShortATR <- 3.5*ATR(HLC(x),5)[,"atr"]
> >
> >
> >   x$CR <- Cl(x)
> >   x$x.lagCl <- lag(Cl(x))
> >
> >   for(i in 6:NROW(x)) {
> >     trail1 <- coredata(x$CR[i-1])
> >
> >     if(Cl(x)[i] > trail1 && x$x.lagCl[i] > trail1) {
> >       x$CR[i] <- max(trail1,coredata(Cl(x)[i]+x$stopLongATR[i]))
> >     } else
> >       if(Cl(x)[i] < trail1 && x$x.lagCl[i] < trail1) {
> >         x$CR[i] <- min(trail1,coredata(Cl(x)[i]+x$stopShortATR[i]))
> >       } else
> >         if(Cl(x)[i] > trail1) {
> >           x$CR[i] <- coredata(Cl(x)[i]+x$stopLongATR[i])
> >         } else {
> >           x$CR[i] <- coredata(Cl(x)[i]+x$stopShortATR[i])
> >         }
> >   }
> >   x$CR
> > }
> >
> 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
>
> >
> > On Mon, Aug 8, 2016 at 4:58 PM, Joshua Ulrich <josh.m.ulrich at gmail.com>
> > wrote:
> >>
> >> On Mon, Aug 8, 2016 at 7:48 AM, golam sakline <golam.sakline at gmail.com>
> >> wrote:
> >> > Hi All,
> >> >
> >> > I have an error at applyStrategy that is failing with "Error in
> array(x,
> >> > c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 'data'
> must
> >> > be
> >> > of a vector type, was 'NULL'" .
> >> > I believe the problem is occurring at add.indicator function "RT" that
> >> > takes in OHLC(mktdata) data and outputs an univariate series.
> >> > This error doesn't occur when I replace RT with MACD function that
> takes
> >> > in
> >> > Cl(mktdata) rather than OHLC.
> >> > This error doesn't occur when I am using RT function with a single
> asset
> >> > in
> >> > the symbol i.e. the length(symbol) = 1.
> >> >
> >> > What am I doing wrong or missing here?
> >> >
> >> It is nearly impossible for anyone to help you because your example
> >> uses data (CSVs on your local hard drive) and functions (in
> >> functions.R) that no one else has access to.  And you suspect the
> >> error is related to one of those functions...
> >>
> >> > Thanks in advance. Much appreciated.
> >> >
> >> > G
> >> >
> >> >
> >> > ############################################################
> ##############
> >> > rm(list = ls(all = TRUE))
> >> > setwd("/Users/mm/Documents/R")
> >> > library(lattice)
> >> > library(timeSeries)
> >> > library(timeDate)
> >> > library(PerformanceAnalytics)
> >> > library(quantmod)
> >> > library(xts)
> >> > library(blotter)
> >> > library(TTR)
> >> > library(quantstrat)
> >> > library(FinancialInstrument)
> >> > source("functions.R")
> >> >
> >> > setwd("/Users/mm/Documents/R/UAE")
> >> > options("getSymbols.warning4.0" = FALSE)
> >> > #sessionInfo()
> >> >
> >> > startDate = "2015-07-01"
> >> > symbols <- c("DFMC", "ARTC", "EMAARMALLS", "DUBAIPARKS")
> >> > Sys.setenv(TZ="UTC")
> >> >
> >> > getSymbols("DFM", src="csv",
> >> > col.names=c("Open","High","Low","Close","Volume"))
> >> > getSymbols(symbols, src="csv",
> >> > col.names=c("Open","High","Low","Close","Volume"))
> >> >
> >> >
> >> > initDate <- "2015-07-01"
> >> > initEq <- 100000
> >> > tradeSize <- initEq/length(symbols)
> >> > currency("USD")
> >> > stock(symbols, currency="USD",multiplier=1)
> >> >
> >> > myTheme<-chart_theme()
> >> > myTheme$col$dn.col<- 'lightblue'
> >> > myTheme$col$dn.border <-  'lightgray'
> >> > myTheme$col$up.border <-  'lightgray'
> >> >
> >> >
> >> > par(mfrow=c(2,2))
> >> > for(symbol in symbols)
> >> > {
> >> >   plot(chart_Series(get(symbol),name=symbol))
> >> > }
> >> > par(mfrow=c(1,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, initDate=initDate)
> >> > initAcct(qs.strategy,portfolios=qs.strategy, initDate=initDate,
> >> > initEq=initEq)
> >> > initOrders(portfolio=qs.strategy,initDate=initDate)
> >> > strategy(qs.strategy,store=TRUE)
> >> >
> >> > add.indicator("AD26", name = "RT",
> >> >               arguments = list(x=quote(OHLC(mktdata))),label= "RT")
> >> >
> >> > summary(getStrategy(qs.strategy))
> >> >
> >> >
> >> > ############################################################
> #############
> >> >
> >> > 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")
> >> >
> >> >
> >> > ############################################################
> #############
> >> >
> >> > summary(getStrategy(qs.strategy))
> >> >
> >> >
> >> > ############################################################
> #############
> >> >
> >> > 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
> >> > )
> >> >
> >> > summary(getStrategy(qs.strategy))
> >> >
> >> > #enable.rule(qs.strategy, type="chain", label ="StopLoss")
> >> >
> >> > applyStrategy(strategy=qs.strategy , portfolios=qs.strategy,
> >> > verbose=TRUE)
> >> >
> >> > updatePortf(qs.strategy)
> >> > updateAcct(qs.strategy)
> >> > updateEndEq(qs.strategy)
> >> >
> >> > checkBlotterUpdate("AD26", "AD26")
> >> >
> >> > ##################################################################
> >> >
> >> > OUTPUT:
> >> >
> >> <snip>
> >> >> applyStrategy(strategy=qs.strategy , portfolios=qs.strategy,
> >> >> verbose=TRUE)
> >> > *Error in array(x, c(length(x), 1L), if (!is.null(names(x)))
> >> > list(names(x),
> >> >  : *
> >> > *  'data' must be of a vector type, was 'NULL'*
> >> > *In addition: Warning messages:*
> >> > *1: In min(j, na.rm = TRUE) :*
> >> > *  no non-missing arguments to min; returning Inf*
> >> > *2: In max(j, na.rm = TRUE) :*
> >> > *  no non-missing arguments to max; returning -Inf*
> >> > *Called from: array(x, c(length(x), 1L), if (!is.null(names(x)))
> >> > list(names(x), *
> >> > *    NULL) else NULL)*
> >> > Browse[1]>
> >> >> updatePortf(qs.strategy)
> >> > [1] "AD26"
> >> >> updateAcct(qs.strategy)
> >> > [1] "AD26"
> >> >> updateEndEq(qs.strategy)
> >> > [1] "AD26"
> >> >>
> >> >> checkBlotterUpdate("AD26", "AD26")
> >> > [1] TRUE
> >> >>
> >> >> sessionInfo()
> >> > R version 3.2.4 (2016-03-10)
> >> > Platform: x86_64-apple-darwin13.4.0 (64-bit)
> >> > Running under: OS X 10.11.6 (El Capitan)
> >> >
> >> > locale:
> >> > [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
> >> >
> >> > attached base packages:
> >> > [1] stats     graphics  grDevices utils     datasets  methods   base
> >> >
> >> > other attached packages:
> >> >  [1] quantstrat_0.9.1739           foreach_1.4.3
> >> > blotter_0.9.1741
> >> >  [4] FinancialInstrument_1.2.0     quantmod_0.4-5
> >> > TTR_0.23-1
> >> >
> >> >  [7] PerformanceAnalytics_1.4.3541 xts_0.9-7
> >> > zoo_1.7-13
> >> >
> >> > [10] timeSeries_3022.101.2         timeDate_3012.100
> >> > lattice_0.20-33
> >> >
> >> > loaded via a namespace (and not attached):
> >> > [1] rsconnect_0.4.2.2 tools_3.2.4       codetools_0.2-14  grid_3.2.4
> >> >  iterators_1.0.8
> >> >
> >> >         [[alternative HTML version deleted]]
> >> >
> >> > _______________________________________________
> >> > R-SIG-Finance at r-project.org mailing list
> >> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >> > -- Subscriber-posting only. If you want to post, subscribe first.
> >> > -- Also note that this is not the r-help list where general R
> questions
> >> > should go.
> >>
> >>
> >>
> >> --
> >> Joshua Ulrich  |  about.me/joshuaulrich
> >> FOSS Trading  |  www.fosstrading.com
> >> R/Finance 2016 | www.rinfinance.com
> >
> >
>
>
>
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com
> R/Finance 2016 | www.rinfinance.com
>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list