[R-SIG-Finance] Writing sell rules with quantstrat
Sergey Pisarenko
drseergio at gmail.com
Wed Feb 15 07:14:12 CET 2012
Hi,
I have been working with quantstrat and for the most part have been
able to re-write my existing code to fit within the framework. I see
that "buy" signals work and "enter" rules are triggered.
I currently do not grasp how to implement sell rules. I would like to
create 2 sell rules:
1. when profit for a specific position reaches 15%
2. when a position is kept for a year
>From what I have read from the examples the correct method to use is
"add.rule". Here's something I have already:
seller <- function(data, timestamp, portfolio, symbol) {
posn <- getPosQty(portfolio, symbol, timestamp)
if (posn != 0) {
}
}
...
s <- strategy('s')
s <- add.indicator(strategy=s, name='SMA',
arguments=list(x=quote(Cl(mktdata)), n=shortSMA), label='SMA5')
s <- add.signal(strategy=s, name='buySig',
arguments=list(data=quote(mktdata)), label='buySig')
s <- add.rule(strategy=s, name='ruleSignal',
arguments=list(data=quote(mktdata), sigcol='buySig', sigval=TRUE,
orderqty=1000, ordertype='market', orderside=NULL, threshold=NULL,
osFUN='sizer'), type='enter')
s <- add.rule(strategy=s, name='seller',
arguments=list(data=quote(mktdata)), type='exit')
...
It is clear that the code must go in the "seller" function. I also
understand that sell order is created by using "addOrder" function. I
am not sure how to check current profit inside of the function,
though.
What would be the correct approach to check the current profit for a
position inside of the rule function?
Appreciate your help.
--
Kind Regards,
Sergey Pisarenko.
More information about the R-SIG-Finance
mailing list