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

Sergey Pisarenko drseergio at gmail.com
Thu Feb 16 06:19:34 CET 2012


I have developed a rather clumsy solution but which works for my goals:

seller <- function(data, timestamp, portfolio, symbol) {
  posn <- getPosQty(portfolio, symbol, timestamp)
  if (posn != 0) {
    pos         <- getPos(portfolio, symbol, timestamp)
    holdPeriod  <- as.POSIXlt(timestamp) - index(pos)

    orderprice <- as.vector(Cl(data[timestamp]))
    qty        <- as.vector(pos[, 1])

    if (holdPeriod > maxHold) {
      addOrder(portfolio=portfolio, symbol=symbol,
timestamp=timestamp, qty=-qty, price=as.numeric(orderprice),
ordertype='market', side='long')
    }

    buy_price   <- as.vector(pos[, 2])
    close_price <- as.vector(Cl(data[timestamp]))
    profit      <- (close_price - buy_price) / buy_price

    if (profit > prfTarget) {
      addOrder(portfolio=portfolio, symbol=symbol,
timestamp=timestamp, qty=-qty, price=as.numeric(orderprice),
ordertype='market', side='long')
    }
  }
}

...

s <- add.rule(strategy=s, name='seller',
arguments=list(data=quote(mktdata)), type='exit')

I am not yet concerned about performance and efficiency. I am still on
my way of making a whole strategy to execute correctly and completely.

/Sergey

On Wed, Feb 15, 2012 at 8:24 PM, Sergey Pisarenko <drseergio at gmail.com> wrote:
> I also realized a logic fault. The stop-limit order I had created used
> the buy signal which is obviously not correct. The sell should occur
> anytime price reaches a certain point regardless of the buy signal.
>
> I will re-implement the same thing using a combination of a rule and
> an indicator (separate column with price that is 15% higher than
> purchase price).
>
> Thanks for your help!
>
> /Sergey
>
> On Wed, Feb 15, 2012 at 8:16 PM, Sergey Pisarenko <drseergio at gmail.com> wrote:
>> Brian,
>>
>> I think this has to do with the fact that stop-limit order triggers
>> whenever the price is below the threshold. In other words, if I buy
>> shares for 30 and set a stop-limit order at 50 the stop-limit order
>> will trigger next day because the price is already below 50. It seems
>> that the stop-limit in its current implementation is not designed for
>> the case of gains. Instead, it works only for losses. When I set
>> threshold to say 0.9 (10% loss) I see that it behaves as expected and
>> the order is executed only when the price goes down 10%.
>>
>> /Sergey
>>
>> On Wed, Feb 15, 2012 at 9:30 AM, Sergey Pisarenko <drseergio at gmail.com> wrote:
>>> Thanks, that's a good idea. I have checked the high prices and I see
>>> that the price does not reach that level. To further troubleshoot I
>>> have changed the profit target to 50% (threshold=1.5). With that in
>>> place I still got very similar results:
>>>
>>>> p<-getPortfolio('p')
>>>> p$symbols$B$txn
>>>           Txn.Qty Txn.Price Txn.Value Txn.Avg.Cost Pos.Qty Pos.Avg.Cost
>>> 2009-01-01       0      0.00         0         0.00       0         0.00
>>> 2010-07-07    1000     33.19     33190        33.19    1000        33.19
>>> 2010-07-09   -1000     50.61    -50610        50.61       0         0.00
>>> 2010-07-09    1000     34.05     34050        34.05    1000        34.05
>>> 2010-07-14   -1000     55.32    -55320        55.32       0         0.00
>>> 2010-07-14    1000     36.18     36180        36.18    1000        36.18
>>> 2010-07-27   -1000     55.65    -55650        55.65       0         0.00
>>> 2010-07-27    1000     38.00     38000        38.00    1000        38.00
>>>
>>> Prices of 50 are nowhere close to the prices for that time period:
>>>                   Open   High    Low      Close  Volume          Adjusted
>>> 2010-07-07   32.73   33.22  32.19    33.19  54388300       31.95
>>> 2010-07-08   33.79   33.90  32.95    33.74  47070000       32.48
>>> 2010-07-09   33.28   34.17  32.12    34.05  48454800       32.78
>>> 2010-07-12   35.64   37.00  35.42    36.76  72391600       35.39
>>> 2010-07-13   37.72   37.76  35.71    36.88  85975400       35.51
>>> 2010-07-14   36.23   36.85  35.70    36.18  45008600       34.83
>>> 2010-07-15   36.68   39.81  36.52    38.92  75737600       37.47
>>> 2010-07-16   38.52   38.54  37.07    37.10  50505300       35.72
>>> 2010-07-19   36.00   36.08  34.58    35.75  55491300       34.42
>>>
>>> /Sergey
>>>
>>> On Wed, Feb 15, 2012 at 8:41 AM, Brian G. Peterson <brian at braverock.com> wrote:
>>>> On Wed, 2012-02-15 at 08:35 -0800, Sergey Pisarenko wrote:
>>>>> 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.
>>>>>
>>>> I'll look at it more closely when I have time, but if the High goes
>>>> through your limit, you will execute at the limit.
>>>>
>>>> Regards,
>>>>
>>>>   - Brian
>>>>>
>>>> --
>>>> Brian G. Peterson
>>>> http://braverock.com/brian/
>>>> Ph: 773-459-4973
>>>> IM: bgpbraverock
>>>>



More information about the R-SIG-Finance mailing list