[R-SIG-Finance] quantstrat - stop loss and stop trailing:

Stephen Choularton stephen at organicfoodmarkets.com.au
Sat Feb 26 20:30:01 CET 2011


Hi Brian

Thanks for that.

Still trying to make sure I have a handle on this.   I am working on the 
macd.R demo example but have started from 31/12/2010 to make the data 
easier to eyeball.

Obviously I am trying to see the stop work  and I notice that a 95% 
threshold should do this in August when it produces a stop figure of 
$255.30375 and it can be seen below the closing price falls below this 
on 2010-08-11

2010-08-02    260.44    262.59   259.62     261.85    15287700        
261.85  0.33850430
2010-08-03    261.01    263.26   259.42     261.93    14916200        
261.93  0.42744640
2010-08-04    262.84    264.28   260.31     262.98    15013400        
262.98  0.52431485
2010-08-05    261.73    263.18   260.55     261.70    10324900        
261.70  0.55460413
2010-08-06    259.78    261.49   257.63     260.09    15889200        
260.09  0.52246936
2010-08-09    261.48    262.15   259.57     261.75    10826000        
261.75  0.54227647
2010-08-10    259.85    260.45   257.55     259.41    16140000        
259.41  0.47975631
2010-08-11    255.40    255.69   249.81     250.19    22144800        
250.19  0.14151831
2010-08-12    246.69    253.10   246.12     251.79    19104300        
251.79 -0.07671837
2010-08-13    251.65    251.88   249.09     249.10    12673900        
249.10 -0.33112468
2010-08-16    247.58    250.01   246.62     247.64    11372500        
247.64 -0.57338811

The order book looks like this:


 > getOrderBook(portfolio.st)
$macd
$macd$AAPL
            Order.Qty Order.Price Order.Type  Order.Side Order.Threshold
2009-12-31 "0"       NA          "init"      "long"     "0"
2010-03-08 "-100"    "213.603"   "stoplimit" "long"     "0.975"
2010-03-08 "100"     "219.08"    "market"    "long"     NA
2010-07-15 "-100"    "251.45"    "market"    "long"     NA
2010-08-02 "-100"    "255.30375" "stoplimit" "long"     "0.975"
2010-08-02 "100"     "261.85"    "market"    "long"     NA
2010-08-16 "-100"    "247.64"    "market"    "long"     NA
2010-09-14 "-100"    "261.3585"  "stoplimit" "long"     "0.975"
2010-09-14 "100"     "268.06"    "market"    "long"     NA
            Order.Status Order.StatusTime Order.Set Txn.Fees
2009-12-31 "closed"     "2009-12-31"     "1"       "0"
2010-03-08 "replaced"   "2010-08-02"     NA        "0"
2010-03-08 "closed"     "2010-03-09"     NA        "0"
2010-07-15 "closed"     "2010-07-16"     NA        "0"
2010-08-02 "replaced"   "2010-09-14"     NA        "0"
2010-08-02 "closed"     "2010-08-03"     NA        "0"
2010-08-16 "closed"     "2010-08-17"     NA        "0"
2010-09-14 "open"       NA               NA        "0"
2010-09-14 "closed"     "2010-09-15"     NA        "0"

but the actual transactions like this:


 > getTxns(Portfolio=portfolio.st, Symbol=stock.str)
            Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
2009-12-31       0      0.00        0         0         0.00
2010-03-08     100    219.08        0     21908       219.08
2010-07-15    -100    251.45        0    -25145       251.45
2010-08-02     100    261.85        0     26185       261.85
2010-08-16    -100    247.64        0    -24764       247.64
2010-09-14     100    268.06        0     26806       268.06
            Net.Txn.Realized.PL
2009-12-31                   0
2010-03-08                   0
2010-07-15                3237
2010-08-02                   0
2010-08-16               -1421
2010-09-14

Can anyone tell me what I am doing wrong?



Stephen Choularton Ph.D., FIoD


On 24/02/2011 9:55 PM, Brian G. Peterson wrote:
> On 02/24/2011 03:31 AM, Stephen Choularton wrote:
>> Hi and thanks.
>>
>>   I had noticed these but I am having difficulty in understand the
>> 'mechanics'.
>>
>>   I notice the column names in mktdata are:
>>
>> AAPL.Open     AAPL.High     AAPL.Low     AAPL.Close     AAPL.Volume
>> AAPL.Adjusted macd     signal     signal.gt.zero     signal.lt.zero
>>
>>
>> when I have stoplimit live.     I can see where everything but 'signal'
>> comes from.  Can anyone advise what puts it there ?  I was guessing
>> add.signal calls did this.
>
> It would be more appropriate to say that these columns are added when 
> applySignals is called inside applyStrategy, and in turn executes the 
> signal function sigThreshold.  For porting to a moving average 
> strategy, you'd probably want to use sigCrossover-based signals 
> instead, because the value of the MA is not known beforehand.
>
> As should be obvious from the demo code and the documentation, the 
> columns you asked about are are the label= parameters from your signals.
>
> In the indicator function, we do not specify a label, because the 
> MACD() function from TTR adds the labels 'macd' and 'signal' to its 
> output.
>
> In the signal generators, we specify a label as 'signal.gt.zero' and 
> 'signal.lt.zero' when we define the signals with add.signal so that we 
> know what the 'sigcol' will be for our later rules.  These labels aer 
> arbitrary, and were chosen to indicate what the signal stands for, but 
> could just as easily have been called 'blue' and 'brown'.
>
> As the documents describe, what you are doing up front with all the 
> add.* functions is just building the strategy object, nothing is 
> 'added to mktdata' until you've called applyStrategy on a portfolio 
> (which in turn calls applyIndicators and applySignals in a vectorized 
> way and applyRules in a path-dependent manner).
>
>   - Brian
>
>
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 10.0.1204 / Virus Database: 1435/3464 - Release Date: 02/23/11
>
>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: macd.R
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110227/e4d16e3f/attachment.pl>


More information about the R-SIG-Finance mailing list