[R-SIG-Finance] quanstrat exit rules
Brian G. Peterson
brian at braverock.com
Tue Apr 18 16:44:46 CEST 2017
Change side to long in your exit rule.
You're not going short, you're selling a long position. Those are
different things. Going short in an equity requires a margin account
and a short locate at least, and will incur interest charges on the
borrow.
In any case, your strategy is trying to enter long, and then exit your
long position. Both the entry and exit orders are on the same
side='long' of the market.
See the demo script rsi.R in the demo directory for a long/short
version of the same idea.
The other thing that is likely tripping you up is rule order. Rules
are evaluated in a specific order, as described in the documentation
for add.rule and other places. exits are evaluated before entries.
Regards,
Brian
--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
On Tue, 2017-04-18 at 10:31 -0400, Jon Golenbock wrote:
> Hi, trying to get a handle on quantstrat and was hoping somebody
> could
> explain the what I'm doing wrong.
>
> The strategy itself is simple enough (and just for testing purposes -
> buy
> when RSI goes below 30, sell back out when RSI goes above 70.
>
> What is unintended, is that short trades (exit) are processed before
> long
> trades (entry) ever are. Why is this happening?
>
> Also, I was playing around with order sizing - it seems like one
> should be
> able to specify "all" for exit trades, but that doesn't seem to work
> for me
> when I make the substitution in the below code... the issues may be
> related, though, so I am putting this on the back burner for now.
>
> Any help appreciated, and thanks in advance.
>
> Jon
>
> ----
>
> rm(list=ls(.blotter), envir=.blotter)
> rm(list=ls(.strategy), envir=.strategy)
> .blotter <- new.env()
> .strategy <- new.env()
>
>
>
>
> initEq <- 5000
> n <- 14
> nSMA <- 200
> days_count <- 100
> initDate <- Sys.Date()-days(days_count + 1)
> .orderqty = 100
>
>
>
>
>
> symbols <- getSymbols(c("AAPL","QQQ"))
>
>
> currency("USD")
> Sys.setenv(TZ="UTC")
> stock(symbols, currency="USD", multiplier=1)
>
> strategy.st <- 'teststrat'
> portfolio.st <- 'teststrat'
> account.st <- 'teststrat'
> initPortf(portfolio.st, symbols = symbols,
> initDate=initDate,currency='USD')
> initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
> initEq=initEq, currency='USD')
> initOrders(portfolio=portfolio.st, initDate=initDate)
>
>
>
> strategy(name = strategy.st,
> store = TRUE)
>
>
>
>
>
>
>
> add.indicator(
> strategy = strategy.st,
> name = 'RSI',
> arguments = list(price=quote(Cl(mktdata)),n=n),
> label = "rsi")
>
>
> add.signal(
> strategy = strategy.st,
> name = 'sigThreshold',
> arguments=list(
> threshold = 30,
> column = "rsi",
> relationship="lt",
> cross = TRUE),
> label = "RSI_long"
> )
>
> add.signal(
> strategy = strategy.st,
> name = 'sigThreshold',
> arguments=list(
> threshold = 70,
> column = "rsi",
> relationship="gt",
> cross = TRUE),
> label = "RSI_short"
> )
>
>
>
> add.rule(
> strategy=strategy.st,
> name='ruleSignal',
> arguments=list(
> sigcol='RSI_long',
> sigval = TRUE,
> orderside = 'long',
> ordertype='market',
> orderqty=.orderqty,
> replace=FALSE
> ),
> type='enter',
> label='Enterlong',
> path.dep = TRUE
> )
>
> add.rule(
> strategy=strategy.st,
> name='ruleSignal',
> arguments=list(
> sigcol='RSI_short',
> sigval = TRUE,
> orderside = 'short',
> ordertype='market',
> orderqty=75,
> replace=TRUE
> ),
> type="exit",
> label='Exitlong',
> path.dep = TRUE
>
> )
>
>
> t1<-Sys.time()
> out<-applyStrategy(strategy=strategy.st,portfolios = portfolio.st)
> t2<-Sys.time()
> print(t2-t1)
> get.orderbook(portfolio.st)
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R
> questions should go.
More information about the R-SIG-Finance
mailing list