[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
Mon Aug 8 14:48:10 CEST 2016
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?
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:
> rm(list = ls(all = TRUE))
> setwd("/Users/mm/Documents/R")
> library(lattice)
> library(timeSeries)
Loading required package: timeDate
> library(timeDate)
> library(PerformanceAnalytics)
Loading required package: xts
Loading required package: zoo
Attaching package: ‘zoo’
The following object is masked from ‘package:timeSeries’:
time<-
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Package PerformanceAnalytics (1.4.3541) loaded.
Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson, GPL-2 | GPL-3
http://r-forge.r-project.org/projects/returnanalytics/
Attaching package: ‘PerformanceAnalytics’
The following objects are masked from ‘package:timeDate’:
kurtosis, skewness
The following object is masked from ‘package:graphics’:
legend
Warning message:
package ‘zoo’ was built under R version 3.2.5
> library(quantmod)
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
> library(xts)
> library(blotter)
Loading required package: FinancialInstrument
> library(TTR)
> library(quantstrat)
Loading required package: foreach
foreach: simple, scalable parallel programming from Revolution Analytics
Use Revolution R for scalability, fault tolerance and more.
http://www.revolutionanalytics.com
> 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"))
[1] "DFM"
> getSymbols(symbols, src="csv",
col.names=c("Open","High","Low","Close","Volume"))
[1] "DFMC" "ARTC" "EMAARMALLS" "DUBAIPARKS"
>
>
> initDate <- "2015-07-01"
> initEq <- 100000
> tradeSize <- initEq/length(symbols)
> currency("USD")
[1] "USD"
> stock(symbols, currency="USD",multiplier=1)
[1] "DFMC" "ARTC" "EMAARMALLS" "DUBAIPARKS"
>
> 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)
Error in is.strategy(name) : object 'qs.strategy' not found
> if(!exists('.blotter')) .blotter <- new.env()
> if(!exists('.strategy')) .strategy <- new.env()
>
> qs.strategy <- "AD26"
> initPortf(qs.strategy, symbols, initDate=initDate)
[1] "AD26"
> initAcct(qs.strategy,portfolios=qs.strategy, initDate=initDate,
initEq=initEq)
[1] "AD26"
> initOrders(portfolio=qs.strategy,initDate=initDate)
> strategy(qs.strategy,store=TRUE)
>
> add.indicator("AD26", name = "RT",
+ arguments = list(x=quote(OHLC(mktdata))),label= "RT")
[1] "AD26"
>
> summary(getStrategy(qs.strategy))
Length Class Mode
name 1 -none- character
assets 0 -none- NULL
indicators 1 -none- list
signals 0 -none- list
rules 1 -none- list
constraints 0 -none- NULL
init 0 -none- list
wrapup 0 -none- list
call 3 -none- call
>
>
#############################################################################################################
>
> add.signal(qs.strategy, name="sigCrossover",
arguments=list(columns=c("Close", "RT"), relationship="gte"), label="Buy")
[1] "AD26"
>
> add.signal(qs.strategy, name="sigCrossover",
arguments=list(columns=c("Close", "RT"), relationship="lt"), label="Sell")
[1] "AD26"
>
>
##############################################################################################################
>
> summary(getStrategy(qs.strategy))
Length Class Mode
name 1 -none- character
assets 0 -none- NULL
indicators 1 -none- list
signals 2 -none- list
rules 1 -none- list
constraints 0 -none- NULL
init 0 -none- list
wrapup 0 -none- list
call 3 -none- call
>
>
##############################################################################################################
>
> 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'
+ )
[1] "AD26"
>
> 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"
+ )
[1] "AD26"
>
> 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
+ )
[1] "AD26"
>
> summary(getStrategy(qs.strategy))
Length Class Mode
name 1 -none- character
assets 0 -none- NULL
indicators 1 -none- list
signals 2 -none- list
rules 4 -none- list
constraints 0 -none- NULL
init 0 -none- list
wrapup 0 -none- list
call 3 -none- call
>
> #enable.rule(qs.strategy, type="chain", label ="StopLoss")
>
> 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]]
More information about the R-SIG-Finance
mailing list