Skip to content

quantstrat

3 messages · Stephen Choularton, Brian G. Peterson

#
Hi

I'm trying to money the bbands demo to by it on the S&P/ ASX 200.

  I think the Symbol is ^AXJO and I can get the info I want using it.

  I have amended the code as follows:

# bbands



  require(quantstrat)

  try(rm("order_book.bbands",pos=.strategy),silent=TRUE)

  try(rm("account.bbands","portfolio.bbands",pos=.blotter),silent=TRUE)

  try(rm("account.st","portfolio.st","stock.str","stratBBands","initDate","initEq",'start_t','end_t'),silent=TRUE)

  # some things to set up here
  stock.str='^AXJO' # what are we trying it on

  # we'll pass these
  SD = 2 # how many standard deviations, traditionally 2

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

  currency('AUD')

  stock(stock.str,currency='AUD',multiplier=1)

  initDate='2006-12-31'

  initEq=1000000

  portfolio.st='bbands'

  account.st='bbands'

  initPortf(portfolio.st,symbols=stock.str, initDate=initDate)

  initAcct(account.st,portfolios='bbands', initDate=initDate)

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

  stratBBands <- strategy("bbands")

  #one indicator
  stratBBands <- add.indicator(strategy = stratBBands, name = "BBands", 
arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'))

  #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=-100, 
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')

  #alternately, to exit at the opposite band, the rules would be...
  #stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = 
list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 
'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
  #stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = 
list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 
'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')

  #TODO add thresholds and stop-entry and stop-exit handling to test

  getSymbols(stock.str,from=initDate)

  start_t<-Sys.time()

  out<-try(applyStrategy(strategy=stratBBands , 
portfolios='bbands',mktdata='AXJO', 
parameters=list(sd=SD,n=N),verbose=TRUE) )


  # look at the order book
  #getOrderBook('bbands')
  end_t<-Sys.time()

  print("strat execution time:")

  print(end_t-start_t)
Time difference of 32.271 secs

  start_t<-Sys.time()

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

  end_t<-Sys.time()

  print("updatePortf execution time:")

  print(end_t-start_t)

  chart.Posn(Portfolio='bbands',Symbol=stock.str)

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

  ###############################################################################
  # R (http://r-project.org/) Quantitative Strategy Model Framework
  #
  # Copyright (c) 2009-2010
  # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and 
Joshua Ulrich
  #
  # This library is distributed under the terms of the GNU Public 
License (GPL)
  # for full details see the file COPYING
  #
  # $Id: bbands.R 374 2010-08-17 18:43:35Z braverock $
  #
  ###############################################################################


I get my prices with thus


 >  getSymbols(stock.str,from=initDate)
[1] "AXJO"


but note the hat has been dropped!

and just afterwards I get thIs:

rategy=stratBBands , portfolios='bbands', 
parameters=list(sd=SD,n=N),verbose=TRUE) )
Error in get(symbol) : object '^AXJO' not found

I've looked at the notes for applyStrategy and I've tried specifically 
referring to AXJO like this:
 >
 >  out<-try(applyStrategy(strategy=stratBBands , 
portfolios='bbands',mktdata='AXJO', 
parameters=list(sd=SD,n=N),verbose=TRUE) )
Error in get(symbol) : object '^AXJO' not found

but I still get the message.

Can anyone advise me how to resolve this?
#
On 01/08/2011 03:54 PM, Stephen Choularton wrote:
try this:

stock.str <- getSymbols(stock.str,from=initDate+1)

which should drop the hat for you.

the hat isn't a valid R variable name.

Also note, per the blotter documentation, that your first trade should 
occur *after* the initDate.

Cheers,

   - Brian
#
Thanks.

I still got some problems with that but in the end I used

  stock.str='AXJO'

but when I got the data referred to it correctly with:

  getSymbols('^AXJO',from=initDate)

and it all seemed to work.

Couple of questions though:

1.  First initDate+1.  I tried this but R didn't want to do the 
addition.  Do you mean that I should increment it outside and then use 
it such as:

nextDate='2007-01-01'
getSymbols('^AXJO',from=nextDate)

2.  I turned to the MACROSS code.  I edited it to refer to the aussie SPI:


  #########################################################################################################################################################################
  #A simple moving average strategy to evaluate trade efficiency
  #checks on SMA of 50 days and SMA of 200 days
  #Author: R. Raghuraman("raghu"), Brian Peterson
  #########################################################################################################################################################################

  require(quantstrat)

  try(rm("order_book.macross",pos=.strategy),silent=TRUE)

  try(rm("account.macross","portfolio.macross",pos=.blotter),silent=TRUE)

  try(rm("account.st","portfolio.st","stock.str","stratMACROSS","initDate","initEq",'start_t','end_t'),silent=TRUE)

  stock.str='AXJO' # what are we trying it on

  currency('AUD')

  stock(stock.str,currency='AUD',multiplier=1)

  initDate='1999-12-31'

  initEq=1000000

  portfolio.st='macross'

  account.st='macross'

  initPortf(portfolio.st,symbols=stock.str, initDate=initDate)

  initAcct(account.st,portfolios=portfolio.st, initDate=initDate)

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

  stratMACROSS<- strategy(portfolio.st)

  stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", 
arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" )

  stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", 
arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200")

  stratMACROSS <- add.signal(strategy = 
stratMACROSS,name="sigCrossover",arguments = 
list(columns=c("ma50","ma200"),relationship="gt"),label="ma50.gt.ma200")

  stratMACROSS <- add.signal(strategy = 
stratMACROSS,name="sigCrossover",arguments = 
list(column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")

  stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', 
arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, 
ordertype='market', orderside='long'),type='enter')

  stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', 
arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100, 
ordertype='market', orderside='long'),type='exit')

  # if you want a long/short Stops and Reverse MA cross strategy, you'd 
add two more rules for the short side:

  stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', 
arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100, 
ordertype='market', orderside='short'),type='enter')

  stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', 
arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, 
ordertype='market', orderside='short'),type='exit')

  getSymbols('^AXJO',from=initDate)

  start_t<-Sys.time()

  out<-try(applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st))

  end_t<-Sys.time()

  print(end_t-start_t)

  start_t<-Sys.time()

  updatePortf(Portfolio='macross',Dates=paste('::',as.Date(Sys.time()),sep=''))
  end_t<-Sys.time()

  print("trade blotter portfolio update:")

  print(end_t-start_t)

  chart.Posn(Portfolio='macross',Symbol=stock.str)

  add_SMA(n=50 , on=1,col='blue')

  add_SMA(n=200, on=1)

  ###############################################################################
  # R (http://r-project.org/) Quantitative Strategy Model Framework
  #
  # Copyright (c) 2009-2010
  # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and 
Joshua Ulrich
  #
  # This library is distributed under the terms of the GNU Public 
License (GPL)
  # for full details see the file COPYING
  #
  # $Id: maCross.R 374 2010-08-17 18:43:35Z braverock $
  #
  ###############################################################################


Basically that was to refer to the AXJO and to put the AUD in.  Also I 
wanted to run a short rule as well so I removed the comment outs from 
those two lines.  I got some problems:

a. As you can see I get some warnings:


 >  out<-try(applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st))
[1] "2003-06-12 AXJO 100 @ 3084.8"
[1] "2008-01-22 AXJO -100 @ 5186.8"
[1] "2009-06-17 AXJO 100 @ 3904.1"
[1] "2010-06-07 AXJO -100 @ 4325.9"
[1] "2010-11-02 AXJO 100 @ 4701.4"
Warning messages:
1: In match.names(columns, colnames(data)) :
   all columns not located in ma50 ma200 for AXJO.Open AXJO.High 
AXJO.Low AXJO.Close AXJO.Volume AXJO.Adjusted ma50 ma200 ma50.gt.ma200
2: In max(i) : no non-missing arguments to max; returning -Inf
3: In ruleOrderProc(portfolio = portfolio, symbol = symbol, mktdata = 
mktdata,  :
   orderQty of-100would cross through zero, reducing qty to0
4: In ruleOrderProc(portfolio = portfolio, symbol = symbol, mktdata = 
mktdata,  :
   orderQty of-100would cross through zero, reducing qty to0
5: In ruleOrderProc(portfolio = portfolio, symbol = symbol, mktdata = 
mktdata,  :
   orderQty of-100would cross through zero, reducing qty to0
6: In ruleOrderProc(portfolio = portfolio, symbol = symbol, mktdata = 
mktdata,  :
   orderQty of-100would cross through zero, reducing qty to0
7: In min(dindex[dindex > curIndex]) :
   no non-missing arguments to min; returning Inf

I am trying to make sense of them:

1. I cannot find any object called data so I'm a bit at a loss there.
3 - 6 Obviously these are to do with short sales violating some rule, 
but I cannot see such a rule so how do I make sure that shorts are allowed?

b.  A little later I get this:


 >  
updatePortf(Portfolio='macross',Dates=paste('::',as.Date(Sys.time()),sep=''))
Error in get(FXrate.str) : object 'AUDUSD' not found
Error in get(FXrate.str) : object 'USDAUD' not found
[1] "macross"
Warning message:
In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol),  :
   Exchange RateUSDAUD not found for symbol,',Symbol,' using currency 
multiplier of 1
 >

but again I cannot find any reference left to the USD.  I tried to 
specify AUD in getSymbols like this:

   getSymbols('^AXJO',currency='AUD', from=initDate)

but it made no difference.

Finally, is there anywhere I can read more/see more examples of 
add.indicator, add.rules other than ?add.indicator,  ?add.rules.

Thanks



Stephen Choularton Ph.D., FIoD

9999 2226
0413 545 182
On 09/01/2011 9:05 AM, Brian G. Peterson wrote: