


Sent from my Verizon Wireless 4G LTE Smartphone

-------- Original message --------
From: r-sig-finance-request@r-project.org 
Date:  
To: r-sig-finance@r-project.org 
Subject: R-SIG-Finance Digest, Vol 106, Issue 8 
 
Send R-SIG-Finance mailing list submissions to
r-sig-finance@r-project.org

To subscribe or unsubscribe via the World Wide Web, visit
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
or, via email, send a message with subject or body 'help' to
r-sig-finance-request@r-project.org

You can reach the person managing the list at
r-sig-finance-owner@r-project.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of R-SIG-Finance digest..."


Today's Topics:

   1. Quantstrat problem (Robert Schien)
   2. Re: Quantstrat problem (Bob Schmidt)
   3. Re: Quantstrat problem (Bob Schmidt)


----------------------------------------------------------------------

Message: 1
Date: Fri, 8 Mar 2013 20:21:33 +0100
From: Robert Schien <robsch@robsch.eu>
To: r-sig-finance@r-project.org
Subject: [R-SIG-Finance] Quantstrat problem
Message-ID: <20130308192133.GA13572@robsch.eu>
Content-Type: text/plain; charset=us-ascii


Hello,

I am trying to get familiar with the quantstrat package. Therefore
I am following the documentation file quantstrat_I.pdf available at
r-programming.org.

I am trying the following code:


library(blotter)
library(quantstrat)
currency("USD")
stock("AMZN",currency="USD",multiplier=1)
getSymbols('AMZN', from='2005-01-01', to='2012-08-31', adjust=T)
qs.strategy <- "qsAMZN"
initPortf(qs.strategy,'AMZN', initDate='2004-12-31')
initAcct(qs.strategy,portfolios=qs.strategy, initDate='2004-12-31', initEq=1e6)
initOrders(portfolio=qs.strategy,initDate='2004-12-31')
strat<- strategy(qs.strategy)
strat <- add.indicator(strategy = strat, name = "SMA",
arguments = list(x = quote(Cl(mktdata)), n=7), label="SMA7")
strat <- add.indicator(strategy = strat, name = "SMA",
arguments = list(x = quote(Cl(mktdata)), n=30), label="SMA30")
strat <- add.signal(strat,name="sigCrossover",
arguments = list(columns=c("SMA7","SMA30"),relationship="gt"),
label="SMA7.gt.SMA30")
strat <- add.signal(strat,name="sigCrossover",
arguments = list(columns=c("Close","SMA7"),relationship="gt"),
label="cl.gt.SMA7")
strat <- add.signal(strat,name="sigCrossover",
arguments = list(columns=c("Close","SMA7"),relationship="lt"),
label="Cl.lt.SMA7")
strat <- add.rule(strat, name='sigFormula',
arguments = list(formula=" (SMA7 > SMA30) & (AMZN.Close > SMA30) ", orderqty=1000,
ordertype='market', orderside='long', pricemethod='market'),
type='enter', path.dep=TRUE)
out <- applyStrategy(strategy=strat , portfolios=qs.strategy)
updatePortf(Portfolio=qs.strategy,Dates=paste('::',as.Date(Sys.time()),sep=''))
updateAcct(name=qs.strategy,Dates=index(AMZN))
dummy <- sapply(X=index(AMZN),FUN=updateEndEq,Account=qs.strategy)

The last command produces an error:

Error in if (!all(i <= 0)) stop("only zeros may be mixed with negative subscripts") : 
  missing value where TRUE/FALSE needed


Did I overlook something?

Thanks in advance.

Robert



------------------------------

Message: 2
Date: Fri, 8 Mar 2013 17:22:05 -0800 (PST)
From: Bob Schmidt <bobschmdt@gmail.com>
To: r-sig-finance@r-project.org
Subject: Re: [R-SIG-Finance] Quantstrat problem
Message-ID: <1362792125762-4660771.post@n4.nabble.com>
Content-Type: text/plain; charset=us-ascii

Hi Robert,

I'm learning like you and thought figuring this out would be fun.  I'm not
sure about the last error.  It might have been due to the script not making
any trades.  I put the sigFormula back into an add.signal command from the
add.rule command.  Then I mangled the rest of it with some rule commands I
made up.  I tried to preserve the formula you were using.  It is completely
messed up from what you are trying to do, no doubt.  But it does make some
trades and shows a plot.

Best regards,

Bob

library(blotter)
library(quantstrat)
try(rm("account.qsAMZN","portfolio.qsAMZN",pos=.blotter),silent=TRUE)
try(rm("order_book.qsAMZN",pos=.strategy),silent=TRUE)
currency("USD")
initDate='2009-12-31'
stock("AMZN",currency="USD",multiplier=1)
getSymbols('AMZN', from=initDate, to='2010-08-31', adjust=T)
qs.strategy <- "qsAMZN"
initPortf(qs.strategy,'AMZN', initDate=initDate)
initAcct(qs.strategy,portfolios=qs.strategy, initDate=initDate, initEq=1e6)
initOrders(portfolio=qs.strategy,initDate=initDate)
strat<- strategy(qs.strategy)
strat <- add.indicator(strategy = strat, name = "SMA",
                       arguments = list(x = quote(Cl(mktdata)), n=7),
                       label="SMA7")
strat <- add.indicator(strategy = strat, name = "SMA",
                       arguments = list(x = quote(Cl(mktdata)), n=30),
                       label="SMA30")
# changed this to use it for the exit rule since the sigFormula didn't need
it
strat <- add.signal(strat,name="sigCrossover",
                    arguments = list(columns=c("SMA7","SMA30"),
                    relationship="lt"), label="SMA7.lt.SMA30")
strat <- add.signal(strat,name="sigCrossover",
                    arguments = list(columns=c("Close","SMA7"),
                    relationship="gt"), label="cl.gt.SMA7")
strat <- add.signal(strat,name="sigCrossover",
                    arguments = list(columns=c("Close","SMA7"),
                    relationship="lt"), label="Cl.lt.SMA7")
# strat <- add.rule(strat, name='sigFormula',
#                   arguments = list(formula=" (SMA7 > SMA30) & (AMZN.Close
> SMA30) ", orderqty=1000,
#                                    ordertype='market', orderside='long',
pricemethod='market'),
#                   type='enter', path.dep=TRUE)

strat <- add.signal(strat, name="sigFormula",
                 arguments = list(columns=c("Close","SMA7","SMA30"),
                        formula = "(SMA7 > SMA30) & (AMZN.Close > SMA30)",
                        label="buysignal", cross=TRUE),
                 label="buysignal")
strat <- add.rule(strategy = strat,name='ruleSignal',
         arguments = list(sigcol="buysignal",sigval=TRUE, orderqty=100,
                    ordertype='market', orderside='long', type='enter'))
strat <- add.rule(strategy = strat,name='ruleSignal',
        arguments = list(sigcol="SMA7.lt.SMA30",sigval=TRUE, orderqty='all',
                    ordertype='market', orderside='long'),type='exit')

out <- applyStrategy(strategy=strat , portfolios=qs.strategy)
updatePortf(Portfolio=qs.strategy)
updateAcct(name=qs.strategy)
#dummy <- sapply(X=index(AMZN),FUN=updateEndEq,Account=qs.strategy)
updateEndEq(qs.strategy)
chart.Posn(Portfolio = 'qsAMZN', Symbol = 'AMZN')




--
View this message in context: http://r.789695.n4.nabble.com/Quantstrat-problem-tp4660746p4660771.html
Sent from the Rmetrics mailing list archive at Nabble.com.



------------------------------

Message: 3
Date: Fri, 8 Mar 2013 18:02:06 -0800 (PST)
From: Bob Schmidt <bobschmdt@gmail.com>
To: r-sig-finance@r-project.org
Subject: Re: [R-SIG-Finance] Quantstrat problem
Message-ID: <1362794526837-4660773.post@n4.nabble.com>
Content-Type: text/plain; charset=us-ascii

Doh!  It still doesn't like that dummy line at the end even when it makes
trades.



--
View this message in context: http://r.789695.n4.nabble.com/Quantstrat-problem-tp4660746p4660773.html
Sent from the Rmetrics mailing list archive at Nabble.com.



------------------------------

_______________________________________________
R-SIG-Finance mailing list
R-SIG-Finance@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-finance


End of R-SIG-Finance Digest, Vol 106, Issue 8
*********************************************

	[[alternative HTML version deleted]]

