[R-SIG-Finance] Quantstrat problem

Bob Schmidt bobschmdt at gmail.com
Sat Mar 9 02:22:05 CET 2013


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.



More information about the R-SIG-Finance mailing list