[R-SIG-Finance] Writing sell rules with quantstrat

Sergey Pisarenko drseergio at gmail.com
Wed Feb 15 17:35:47 CET 2012


I have put two rules to capture the profit, one for buy and one for sell:

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='osMaxPos', ruletype='enter'), type='enter')

s <- add.rule(strategy=s, name='ruleSignal',
arguments=list(data=quote(mktdata), sigcol='buySig', sigval=TRUE,
orderqty=-1000, ordertype='stoplimit', orderside=NULL, threshold=1.15,
tmult=TRUE, osFUN='osMaxPos'), type='exit')

The max position is set to 1000 using addMaxPos. The sell should
trigger on at least 15% gain. This creates the following transactions:

> p$symbols$BP$txn
           Txn.Qty Txn.Price Txn.Value Txn.Avg.Cost Pos.Qty Pos.Avg.Cost
2009-01-01       0     0.000         0        0.000       0         0.00
2010-07-07    1000    33.190     33190       33.190    1000        33.19
2010-07-09   -1000    38.801    -38801       38.801       0         0.00
2010-07-09    1000    34.050     34050       34.050    1000        34.05
2010-07-14   -1000    42.412    -42412       42.412       0         0.00
2010-07-14    1000    36.180     36180       36.180    1000        36.18
2010-07-27   -1000    42.665    -42665       42.665       0         0.00
2010-07-27    1000    38.000     38000       38.000    1000        38.00

For some reason the sell rule triggers too quick. For example, you can
see that a sell transaction is created on 2010-07-09 @ 38.801.
However, the closing price is not that high at that moment. Here are
the closing prices for the scenario:

2010-07-08   33.74
2010-07-09   34.05
2010-07-12   36.76
2010-07-13   36.88
2010-07-14   36.18
2010-07-15   38.92

Consequently, this also causes multiple buy transactions to trigger. I
would expect the stoplimit orders execute only when the closing price
reaches the stoplimit order.

What am I doing wrong in this case?

Thank you for your help.

/Sergey

On Wed, Feb 15, 2012 at 7:19 AM, Sergey Pisarenko <drseergio at gmail.com> wrote:
> Thank you for useful ideas, Brian. These are helpful and I'll check
> the slides you have mentioned.
>
> --
> Kind Regards,
> Sergey Pisarenko.
>
> On Wed, Feb 15, 2012 at 3:11 AM, Brian G. Peterson <brian at braverock.com> wrote:
>> On Tue, 2012-02-14 at 22:14 -0800, Sergey Pisarenko wrote:
>>> 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) {
>>>   }
>>> }
>> <...>
>>> 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?
>>
>> In your *entry* rule, you enter at market, so you're guaranteed a
>> fill.
>>
>> Your 'exit' rule for the profit taker order is then clear.  Enter it at
>> the same time as a stoplimit for 15% more than your entry signal price.
>> This won't be precisely 15% more than the close price on the next bar,
>> but is likely fine for a backtest (you could also look ahead, but I
>> don't recommend adding these kinds of biases).  It is certainly easier
>> than running all the calculation to check net profit on every bar after
>> entering a position.
>>
>> A timed rule could certainly be done in your 'seller' function, but for
>> efficiency I would probably add an indicator function that would check
>> my entry indicator, and place a new 'signal' one year later.  Then you
>> put an entry rule to exit then if you have a position.
>>
>> If you haven't already done so, find Guy Yollin's lecture slides on
>> quantstrat from UW.  He demonstrates some interesting custom order
>> sizing functions that could also give you ideas.
>>
>> Regards,
>>
>>   - Brian
>>
>>
>>
>>
>> --
>> Brian G. Peterson
>> http://braverock.com/brian/
>> Ph: 773-459-4973
>> IM: bgpbraverock
>>



More information about the R-SIG-Finance mailing list