Buy N days before next ex-dividend date
Author: jgimene1
Creation Date: 3/3/2021 7:31 PM
profile picture

jgimene1

#1
I am setting up a rule to buy at market open N days before next ex-dividend date.

I used entry at market with condition next ex-dividend date is within N days

It works up to 4 days, then if I use 5 or 6 days it does the same as 4 days.

See attached.

How can I correct this?
profile picture

Cone

#2
Are you using sliders/parameters or hard-coding the N days?
Please show us the code for the rule.
profile picture

jgimene1

#3
using sliders/parameters. note that the slider does not work (created other ticket for this issue)

CODE:
Please log in to see this code.
profile picture

Eugene

#4
CODE:
Please log in to see this code.

Somehow the step is set to 20 so you have 6, 26, 46 days etc. That's why 4 days is equal to 5 or 6 - all the way up to 26. You would want to change the step value in the code like this:
CODE:
Please log in to see this code.
profile picture

jgimene1

#5
We tried with Cone your code slider1 = CreateParameter("Days",6,1,20,1); but did not work. Cone came up with this one slider1 = CreateParameter("Days",7,1,10,1) that still does not allow for more than 4 days but all other functions I need do work, So we just need to be able to use a number of days higher than 4 with no limit up to 50 (example, 5, 6, 7, 8, 9, 10).

Please try to resolve this on Friday and if it works, I will purchase your product to take advantage of the special offer.

Thanks a lot for your help.
profile picture

Eugene

#6
Everything already works correctly. Important: it takes to click "Reset" after making changes to the parameter slider code.

In the example below I initialized the slider like this:
CODE:
Please log in to see this code.

As you can see, entries takes place up to 14 bars (~20 days) prior to the dividend event:

profile picture

Cone

#7
The thing we got hung up on was why it wasn't seemingly working past "4" days.. And this is because the value is returned calendar days to the event. In other words, you'll get the same result for 4, 5, and 6 because the weekends are included. Here's a picture of the number of days to the dividend event:

profile picture

Eugene

#8
Right, the rule says "Days" not "Bars" (and I stressed "14 bars (~20 days)"). But being Fidelity compiled code, it cannot be modified in WL6 and will have to stay as is.
profile picture

Cone

#9
It's not a problem Eugene. We were mostly hung up on why the 4, 5, and 6 all returned the same result. Now we know!
profile picture

jgimene1

#10
I tried slider1 = CreateParameter("Bars",20,1,20,1); bar set a 10 for entry 7 bars before ex div and it works but it does not take all the trades for MMM on 7 years backtest the last entry is for ex div 2/15/2018, so 11/22/2017 and prior are missing. See screenshot.

For APPL, all the trades are there and all are correct.

But it does not work on all symbols, attached XOM is not taking all the trades BUT with the same settings, it changes the 7 bars entry rule (see attached on entry 7 days and other 5 days).
profile picture

Eugene

#11
1. "Error" entry = no problem, talking about calendar days and not bars here.
2. "Missing" trade = not enough capital, switch to Raw Profit mode to verify.
profile picture

jgimene1

#12
1. It does not work with days (as you wrote due to the weekends). It does work with bars but there are errors on XOM (previous trades missing screenshot sent and VZ attached here) please send a solution/code that works to verify that backtest has no errors and move on by purchasing your product.

2. Already on raw profit mode, see screenshot, only trades for 2018 (one missing) up to 2021, should be 7 years, missing 2017/2016/2015/2014. Dates with trades vary from one symbol to another. VZ screenshot, 2017 missing one trade, 2018 3 trades, 2019 3 trades, 2020 complete, should be 4 trades per year.

Thanks.

John
profile picture

Eugene

#13
No missing trades on MMM in my test in 2017-2018.

CODE:
Please log in to see this code.

Just added a time-based exit:
CODE:
Please log in to see this code.



profile picture

jgimene1

#14
The exit worked nothing to change.

slider1 = CreateParameter("Days", 7, 1, 20, 1) seems to work but many trades missing, see attached
profile picture

Cone

#15
Okay, I get it. We need to make a different rule for "Trading Days Before" the event. I'll take care of this in the ticket.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).