[R-SIG-Finance] rule delays

Brian G. Peterson brian at braverock.com
Sun May 29 12:17:05 CEST 2016


You can't 'highlight' anything in non-HTML email.  The list only passes 
through standard text emails.

The order book does not contain columns
date     size     days     value     price

so you've done some transformation or aggregation.

If you want someone to help you, I suggest using an actual minimal 
reproducible example.  Keep removing things from your script: options, 
arguments, data, stategy components, etc. until it has *nothing* more 
than necessary to replicate what you believe to be the problem.

Your emails after the first one *add* things, not remove  them. That is 
unlikely to uncover a problem, if one exists.

The 'delay' argument was designed for intraday data, where we have used 
it on backtests for years.

Also, Ilya Kipnis already suggested another approach.  You could add 
another signal which is simply a lag 8 days after your entry signal. (I 
don't think you need sigAND  or sigOR for this, just lag as the signal 
function)

In any event, adding complexity is unlikely to help uncover the solution.

Regards,

Brian

On 05/29/2016 04:56 AM, Stephen Choularton wrote:
> Hi
>
> The more I look the more I see problems.  This is from the orderbook.
>
> date     size     days     value     price
>
>
>
>
>
> 2003-08-07     3261
>      9529.8710891097     2.9223769056
> 2003-08-19     all     9     9631.2529121466     2.9534660877
> 2003-11-11     3073     61     12056.776566913     3.9234547891
> 2003-11-21     all     9     11273.3727012341     3.6685234954
> 2003-12-10     2638     14     9136.2774479317     3.4633348931
> 2003-12-22     all     9     7955.2864672296     3.0156506699
> 2004-01-13     3591     17     11052.4840619585     3.0778290342
> 2004-01-23     all     9     10494.277796203     2.9223831234
> 2004-01-28     3603     4     9969.2747769987     2.7669372126
> 2004-01-30     all     3     10126.0948296706     2.8104620676
> 2004-02-03     3184     3     8710.9401289089     2.7358480304
> 2004-02-09     all     5     8809.9280849192     2.7669372126
> 2004-08-19     2298     139     8716.0387547838     3.792880224
> 2004-08-31     all     9     8873.2132241324     3.8612764248
> 2004-09-17     3469     14     13524.1859650561     3.8985834434
> 2004-09-29     all     9     13265.3498700311     3.8239694062
> 2005-01-06     3969     72     17546.4794820949     4.4208817037
> 2005-01-18     all     9     18484.2660085641     4.6571594882
> 2005-02-21     1830     25     8442.9513786548     4.6136346331
> 2005-03-03     all     9     8306.4076905903     4.539020596
>
> I have highlighted two trades which are not according to that 8 day
> rule. If the highlighting gets lost they are the closing sales on the 30
> Jan and 9 Feb.  I can't figure out why.
>
> However, I also notice that the value of the investments made varies
> widely when I would have expected them to be fairly consistent.
>
> Can anyone throw any light on these problems.
>
> Thanks.
>
>
> ------------------------------------------
> Stephen Choularton PhD, FIoD
>
> On 28/05/2016 5:05 PM, Stephen Choularton wrote:
>> Hi
>>
>> I thought I had found a solution to this by adding the signal:
>>
>> add.signal(strategy.st, name="sigFormula",
>>     arguments = list(columns = c("longEntry"),
>>     formula = "lag(longEntry, 8) == 1", cross=FALSE), label="fixedExit")
>>
>>
>> and a new rule:
>>
>> add.rule(strategy.st, name = "ruleSignal",
>>     arguments = list(sigcol = "fixedExit",
>>     sigval = TRUE,
>>     orderqty = "all",
>>     ordertype = "market",
>>     orderside = "long", TxnFees = txnFees,
>>     replace = FALSE,
>>     prefer = "Open"), type= "exit", path.dep = TRUE)
>>
>> I placed the rule before the old exit one assuming it would then take
>> precedent and because the rule was a sell all one it would do no harm
>> for the other rule to trigger as all = 0 at that stage.
>>
>> It made a difference but I was not sure if it was working properly (ie
>> sell either when the normal sale is triggered or on 8 days whichever
>> is the earlier).  So I temporarily removed the old longExit signal and
>> associated rule.  That should have mean that I only got sales on the
>> 8th trading day after a purchase but this showed up in the order book:
>>
>>            Order.Qty Order.Price        Order.Type Order.Side
>> Order.Threshold Order.Status Order.StatusTime      Prefer Order.Set
>> Txn.Fees Rule              Time.In.Force
>> 2003-08-07 "3261"    "2.92237690558407" "market"   "long"
>> NA              "closed"     "2003-08-08 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2003-08-19 "all"     "2.95346608774812" "market"   "long"
>> NA              "closed"     "2003-08-20 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2003-11-11 "3073"    "3.92345478910284" "market"   "long"
>> NA              "closed"     "2003-11-12 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2003-11-21 "all"     "3.66852349535765" "market"   "long"
>> NA              "closed"     "2003-11-24 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2003-12-10 "2638"    "3.46333489307494" "market"   "long"
>> NA              "closed"     "2003-12-11 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2003-12-22 "all"     "3.01565066991264" "market"   "long"
>> NA              "closed"     "2003-12-23 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2004-01-13 "3591"    "3.07782903424074" "market"   "long"
>> NA              "closed"     "2004-01-14 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2004-01-23 "all"     "2.9223831234205"  "market"   "long"
>> NA              "closed"     "2004-01-26 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2004-01-28 "3603"    "2.76693721260026" "market"   "long"
>> NA              "closed"     "2004-01-29 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> 2004-01-30 "all"     "2.81046206762993" "market"   "long"
>> NA              "closed"     "2004-02-02 00:00:00" "Open" NA "-8"
>> "ruleSignal.rule" ""
>> Please note the last line shows a purchase and sale only two calendar
>> days apart.  They were weekdays.  That is not in accordance with the
>> indicator or rule I wrote.   In fact mktdata shows:
>>
>> 2004-01-28                 1         0         1         0
>> 2004-01-29                 0         0         0         0
>> 2004-01-30                 0         0         0         0
>> 2004-02-02                 0         0         0         0
>> 2004-02-03                 0         0         0         0
>> 2004-02-04                 0         0         0         0
>> 2004-02-05                 0         0         0         0
>> 2004-02-06                 0         0         0         0
>> 2004-02-09                 0         0         0         1
>>
>> with the signals on the correct days.
>>
>> Just a sanity check.  Can anyone tell me where I am going wrong,
>> presumably in writing that rule.
>>
>> -----------------------------------------------------------------------------------------------------------------------------------
>>
>> Stephen Choularton PhD, FIoD
>>
>> On 27/05/2016 4:43 PM, Stephen Choularton wrote:
>>> Hi
>>>
>>> I am trying to implement Harry's Connor's RSI from his book
>>> Quantative Trading ...
>>>
>>> I have it working as is and have varied it to some shares in the ASX.
>>>
>>> However, I want to pursue the idea of a fixed time (8 day) sell rule
>>> that takes place if the normal rule doesn't produce a sale before.
>>>
>>> I added this rule at line 127 of the file 3. strategy.R:
>>>
>>> add.rule(strategy.st, name = "ruleSignal",
>>>     arguments = list(sigcol = "longExit",
>>>     sigval = TRUE,
>>>     orderqty = "all", delay = 691200,
>>>     ordertype = "market",
>>>     orderside = "long", TxnFees = txnFees,
>>>     replace = FALSE,
>>>     prefer = "Open"), type= "exit", path.dep = TRUE)
>>>
>>> but it seems to have no effect.
>>>
>>> I attach all the code. You run it in the numbered order of the files.
>>>
>>> I wonder if anyone can help me make this sort of rule work.
>>>
>>> -----------------------------------------------------------------------------------------------------------------------------------
>>>
>>> Stephen Choularton PhD, FIoD
>>> 0413 545 182
>>
>> _______________________________________________
>> 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.
>>
>
> _______________________________________________
> 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.


-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list