"First 5 trading days of the year"

Questions about MultiCharts and user contributed studies.
Jonny473
Posts: 68
Joined: 04 Apr 2016
Has thanked: 5 times
Been thanked: 1 time

"First 5 trading days of the year"

Postby Jonny473 » 08 Feb 2017

Hi folks,
probably some of you came accross this strategy „First 5 trading days of the year“ which gives some indication about the direction of the upcoming trading year, at least for the stock market. A simple buy and hold strategy- if those first 5 years are positive- buy on day 6 and hold until end of the year.

As I am still an EL learner I thought to myself lets try to programme this strategy. The first hurdle for myself is to define those first 5 trading days. I cant use the 1st, 2nd or 3rd day of the year as I can only buy at close bar, correct?


So I have to look for the last trading day of a year and buy at the open of the next bar. Issue I had here: Some years do not inherit 31/12, its then 30/12 or even 29/12. As EL counts from left to right in the chart, how can I make sure that if 31/12 exists, the strategy does not use 30/12 or 29/12.
I think I got this to work by:

Code: Select all

var: last.trading.day(true);



if month(date)=12 and DayOfMonth(Date) >= 29 and DayOfweek(Date)=5
then last.trading.day = true;

I used the BarsSinceEntry function to count 5 days from the entry to sell on the close. Actually no buy or sell is needed, moreover a comparison of value from possible entry and exit. If exit(value+5,close)> entry(value,open) then buy the next day open and sell at the end of the year.
I know its not a hard strategy to code. With a little help more users can profit from it.

Code: Select all

inputs: BarToExitOn( 5 ) ;



var: last.trading.day(true);



if month(date)=12 and DayOfMonth(Date) >= 29 and DayOfweek(Date)=5
then last.trading.day = true;


if last.trading.day = true then buy next bar at open;


if BarsSinceEntry = BarToExitOn then Sell this bar close ;
This one does not really work as it keeps on buying. Also somehow I need to compare the values, but dont how yet:

Code: Select all

Value1=Close>Open[5]
Anyone got a clue?

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: "First 5 trading days of the year"

Postby tony » 08 Feb 2017

Depending on how far back you want to test this strategy, the easiest thing to do is likely just figure out the date range for each year (i.e. the first 5 trading days) and then have a conditional statement if date > or < and if true it will run your logic. A little work, but honestly, if not looking to go back too far it's likely the quickest method.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: "First 5 trading days of the year"

Postby tony » 08 Feb 2017

Here's an example of what I was saying above. A tad tedious but if you want to say test the past 10-15 years, you'll like find the first trading days of each year far quicker than spending time writing a script another way.

Code: Select all

if Date = 1170101
OR Date = 1170102
OR Date = 1170103
If the date equals any of the trading days you want (the first 5) then it runs your logic, otherwise it does not.

Jonny473
Posts: 68
Joined: 04 Apr 2016
Has thanked: 5 times
Been thanked: 1 time

Re: "First 5 trading days of the year"

Postby Jonny473 » 08 Feb 2017

Hi TJ, hi Tony,

of course what I could do is and so on:

Code: Select all

if date=1130109 and Close>Open[5] then buy 1 contract this bar close;
if date=1131231 then sell 1 contract this bar close;

if date=1140109 and Close>Open[5] then buy 1 contract this bar close;
if date=1141231 then sell 1 contract this bar close;

if date=1150108 and Close>Open[5] then buy 1 contract this bar close;
if date=1151231 then sell 1 contract this bar close;

if date=1160111 and Close>Open[5] then buy 1 contract this bar close;
if date=1161230 then sell 1 contract this bar close;


if date=1170109 and Close>Open[5] then buy 1 contract this bar close;
This actually is the logic but not really what I thought of as a strategy as I have to "pick" the exact dates in the chart myself.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: "First 5 trading days of the year"

Postby tony » 08 Feb 2017

Hi TJ, hi Tony,

of course what I could do is and so on:

Code: Select all

if date=1130109 and Close>Open[5] then buy 1 contract this bar close;
if date=1131231 then sell 1 contract this bar close;

if date=1140109 and Close>Open[5] then buy 1 contract this bar close;
if date=1141231 then sell 1 contract this bar close;

if date=1150108 and Close>Open[5] then buy 1 contract this bar close;
if date=1151231 then sell 1 contract this bar close;

if date=1160111 and Close>Open[5] then buy 1 contract this bar close;
if date=1161230 then sell 1 contract this bar close;


if date=1170109 and Close>Open[5] then buy 1 contract this bar close;
This actually is the logic but not really what I thought of as a strategy as I have to "pick" the exact dates in the chart myself.
If you just want to run a quick backtest to see if the strategy has merit, then I would do what I propose. May take you an hour to look up 10-15 years of dates and the write into a script versus hours to figure out another way. At least this will be a quick and dirty test to see if the strategy warrants more time or not. If you do go the route I suggest, then save yourself a lot of lines of code and write something like this

Code: Select all

If date = 1170101
or date = 1170102
or date = // put down every date you want to run

Then Begin // now if the date matches above the following code is run

enter your code

end;
This will save you a lot of time, lines of code.

Jonny473
Posts: 68
Joined: 04 Apr 2016
Has thanked: 5 times
Been thanked: 1 time

Re: "First 5 trading days of the year"

Postby Jonny473 » 08 Feb 2017

Actually the "strategy" is just comparing the Open of date = 1170101 or date = 1170102 or date =1170103 (whatever is the first trading day in a new year) with the close price five days(bars) later thats it...how am I going to get this to work properly...actually I would have to "add" 5 bars to whatever the first trading day of the year is, use this close value and compare to the open price [5]....

Jonny473
Posts: 68
Joined: 04 Apr 2016
Has thanked: 5 times
Been thanked: 1 time

Re: "First 5 trading days of the year"

Postby Jonny473 » 08 Feb 2017

The CalcDate(RefDate,DaysChange) function can add/subtract days, but only if the RefDate is explicit. In our case its not....

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: "First 5 trading days of the year"

Postby ABC » 09 Feb 2017

Jonny473,

if I understand you correctly it should be enough to compare the open of a year to the close on day 5 and then buy/sell according to that.
You can get the new year with:

"if Year( Date ) <> Year( Date[1] )"

Then you can use a counter to increment on each day until day 5, where you compare the Close to the value stored in the variable yearOpen.
The below would work under the condition that you use daily bars.

Code: Select all

Variables:
dayCounter ( 0 ),
yearOpen ( -999999 );

//check for a new year
if Year( Date ) <> Year( Date[1] ) then
begin
yearOpen = Open ;
dayCounter = 1 ;
end
//same year
else
begin
dayCounter += 1 ; //increment the day counter

//on the fifth day of a year, check if the direction is up or down until then
//check for yearOpen <> -999999 to ensure that there was at least one new year
if dayCounter = 5 and yearOpen <> -999999 then
begin
//your buy /sell code in here
end ;
end ;
Getting the exit on the last day of the year can be a bit trickier as you want to make sure that this day is not a weekend (if it can fall on a holiday you would need a holiday list that you maintain).

Regards,

ABC

Jonny473
Posts: 68
Joined: 04 Apr 2016
Has thanked: 5 times
Been thanked: 1 time

Re: "First 5 trading days of the year"

Postby Jonny473 » 13 Feb 2017

Thanks ABC, so for those interested the code for SPY since 1994 is like this:

Code: Select all

Variables:
dayCounter ( 0 ),
last.trading.day(true),
yearOpen ( -999999 );

//check for a new year
if Year( Date ) <> Year( Date[1] ) then
begin
yearOpen = Open ;
dayCounter = 1 ;
end
//same year
else
begin
dayCounter += 1 ; //increment the day counter

//on the fifth day of a year, check if the direction is up or down until then
//check for yearOpen <> -999999 to ensure that there was at least one new year
if dayCounter = 5 and yearOpen <> -999999 then
begin
if close>open[5] then buy 100 contract next bar open;//your buy /sell code in here
end ;
end ;


if date=(941230) then sell this bar close;
if date=(951229) then sell this bar close;
if date=(961231) then sell this bar close;
if date=(971231) then sell this bar close;
if date=(981231) then sell this bar close;
if date=(991231) then sell this bar close;
if date=(1001231) then sell this bar close;
if date=(1011231) then sell this bar close;
if date=(1021230) then sell this bar close;
if date=(1031231) then sell this bar close;
if date=(1041231) then sell this bar close;
if date=(1051231) then sell this bar close;
if date=(1061229) then sell this bar close;
if date=(1071231) then sell this bar close;
if date=(1081231) then sell this bar close;
if date=(1091231) then sell this bar close;
if date=(1101231) then sell this bar close;
if date=(1111230) then sell this bar close;
if date=(1121231) then sell this bar close;
if date=(1131231) then sell this bar close;
if date=(1141231) then sell this bar close;
if date=(1151231) then sell this bar close;
Of course depending on the underlying and exchange last year trading dates can vary.


Return to “MultiCharts”