Dear R experts:

 

I am a newcomer and learning R with quantstrat for strategy testing such as
Bollinger band. 

(Most codes are from the sample of the demo in the package.)

I encountered a problem here and hope you can help me out from the error
below.

 

My process is

1.     Reading a data file, instead of using data from yahoo

##my own code

tmp <- read.table("AAPL.dat", sep = ",")  #dat file is in below

f <- function(x) as.POSIXct(x, origin ="1970-01-01")

symbolData <- read.zoo(tmp, index = 1, FUN = f)

colnames(symbolData) <- c("Open","High","Low","Close","Volume",
"Adjust","V8","V9") 

#here is one line of the symbolData

> symbolData 
                      Open   High    Low  Close Volume  Adjust V8   V9
2011-06-16 14:45:00 326.57 326.80 326.56 326.65    353 326.688  0  249

 

2.     Pass it to add.Indicators, and applySignals

dataSrc <- list(HLC = quote(HLC(symbolData)), maType='SMA') 

  stratBBands <- add.indicator(strategy = stratBBands, name = "BBands",
arguments = dataSrc) #use my file instead of mktdata

.

### modified by my own code

out<-try(applyStrategy(strategy=stratBBands  , portfolios=port.st, mktData =
symbolData,  parameters=list(sd=SD,n=N) ) )

 

The problem is:

out<-try(applyStrategy(strategy=stratBBands  , portfolios=port.st, mktData =
symbolData,  parameters=list(sd=SD,n=N) ) )

 

Error in if (inherits(sret$indicators, "xts") & nrow(mktdata) ==
nrow(sret$indicators)) { : 

  argument is of length zero

 

Another basic questions, how to set a breakpoint and trace line by line? Any
new tool?

 

Thanks

Richard

 

 

 

 

 

Main.R

###############lib needed##############

library(quantstrat)

library(quantmod)

library(chron)

symbols = c("AAPL")    #,"AAPL", "FB"

timeFrame <- 15

 

###my own code

tmp <- read.table("AAPL.dat", sep = ",")

f <- function(x) as.POSIXct(x, origin ="1970-01-01")

symbolData <- read.zoo(tmp, index = 1, FUN = f)

colnames(symbolData) <- c("Open","High","Low","Close","Volume",
"Adjust","V8","V9") 

#barChart(symbolData)

 

  initDate='2010-05-28'

  initEq=100000

  port.st<-'backTest11' #use a string here for easier changing of parameters
and re-trying

    

  #############     bollinger bands ###########################

  SD = 2 # how many standard deviations, traditionally 2

  N = 20 # how many periods for the moving average, traditionally 20

    

  stratBBands <- strategy("bbands")

  

  #one indicator

###my own code

 dataSrc <- list(HLC = quote(HLC(symbolData)), maType='SMA') 

  stratBBands <- add.indicator(strategy = stratBBands, name = "BBands",
arguments = dataSrc) #use my file instead of mktdata

  

  #add signals:

  stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments =
list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")

  stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments =
list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")

  stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments =
list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")

  

  # lets add some rules

  stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-0, ordertype='market',
orderside=NULL, threshold=NULL),type='enter')

  stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100,
ordertype='market', orderside=NULL, threshold=NULL),type='enter')

  stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments =
list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market',
orderside=NULL, threshold=NULL),type='exit')

  

  ####################end of bollinger bands ##################

  

  ##############initialize##############

  currency("USD")

  

#symbols = c("BIDU")

for(symbol in symbols){ # establish trade-able instruments

    stock(symbol, currency="USD",multiplier=1)

    #getSymbols(symbol, from=initDate) #not using server data, use my own

  }

  

 

  initPortf(port.st, symbols=symbols, initDate=initDate)

  initAcct(port.st, portfolios=port.st, initDate=initDate)

  initOrders(portfolio=port.st, initDate=initDate)

  

  print("initialize done")

  

  ################ Process the indicators and generate
trades########################

start_t<-Sys.time()

### modified by my own code

  out<-try(applyStrategy(strategy=stratBBands  , portfolios=port.st, mktData
= symbolData,  parameters=list(sd=SD,n=N) ) )

  end_t<-Sys.time()

  print("Strategy Loop:")

  print(end_t-start_t)

  

  # look at the order book

  #print(getOrderBook(port.st))

  

  start_t<-Sys.time()

 
updatePortf(Portfolio=port.st,Dates=paste('::',as.Date(Sys.time()),sep=''))

  end_t<-Sys.time()

  print("trade blotter portfolio update:")

  print(end_t-start_t)

  

  # hack for new quantmod graphics, remove later

  themelist<-chart_theme()

  themelist$col$up.col<-'lightgreen'

  themelist$col$dn.col<-'red'

  

    for(symbol in symbols){

    dev.new()

    chart.Posn(Portfolio=port.st,Symbol=symbol,theme=themelist)

plot(add_BBands(on=1,sd=SD,n=N))

  }

  

AAPL.dat

1308231000,327.05,327.48,326.70,327.19,1903,326.908,0,495

1308231060,327.18,327.26,326.27,326.58,896,326.747,0,651

1308231120,326.58,326.80,326.40,326.40,818,326.557,0,510

1308231180,326.42,326.46,325.75,325.89,1054,325.955,0,627

1308231240,325.86,326.20,325.82,326.12,662,325.943,0,430

1308231300,326.16,326.55,326.03,326.44,687,326.31,0,376

1308231360,326.41,326.86,326.35,326.80,751,326.586,0,566

1308231420,326.84,327.20,326.67,327.04,994,326.959,0,603

1308231480,327.04,327.09,326.43,326.89,632,326.808,0,445

1308231540,326.88,326.91,326.45,326.68,521,326.665,0,370

1308231600,326.63,326.80,326.50,326.71,581,326.644,0,402

1308231660,326.71,327.20,326.71,327.20,647,326.996,0,346

1308231720,327.14,327.36,327.05,327.15,708,327.229,0,418

1308231780,327.10,327.19,326.55,326.66,525,326.902,0,339

 

 

 


	[[alternative HTML version deleted]]

