Finding seasonal pattterns

Hi,

I would like to test for the presence of certain seasonal patterns in stock data. For example are Tuesdays up days? I tried the following, which is meant to simple count up Tuesdays, but it clearly doesn't work. Any trailheads or code examples around?

Thanks
John

monday = 0;
tuesday = 0;
wednesday = 0;
thursday = 0;
friday = 0;

tuesday += iif (DayOfWeek() == 2 AND ROC(Close, 1) > 0, 1, 0);

Filter=1;
AddColumn(monday, "monday");
AddColumn(tuesday, "tuesday");
AddColumn(wednesday, "wednesday");
AddColumn(thursday, "thursday");
AddColumn(friday, "friday");

You can use code as follows (Cumulative sum function)

upday = ROC( Close, 1 ) > 0;

dw = DayOfWeek();

monday = Cum( dw == 1 AND upday );
tuesday = Cum( dw == 2 AND upday );
wednesday = Cum( dw == 3 AND upday );
thursday = Cum( dw == 4 AND upday );
friday = Cum( dw == 5 AND upday );

Filter=Status("lastbarinrange");
AddColumn(monday, "monday");
AddColumn(tuesday, "tuesday");
AddColumn(wednesday, "wednesday");
AddColumn(thursday, "thursday");
AddColumn(friday, "friday");
4 Likes

Thanks Tomasz. Cum() is perfect. I would like to aggregate the results over a list of stocks. Is this the correct method?

Best, John

// init vars
monday = 0.0;
tuesday = 0.0;
wednesday = 0.0;
thursday = 0.0;
friday = 0.0;
// calc vals
upday = ROC( Close, 1 ) > 0;
dw = DayOfWeek();
// aggregate by day
monday = Cum(dw == 1 AND upday);
tuesday = Cum(dw == 2 AND upday);
wednesday = Cum(dw == 3 AND upday);
thursday = Cum(dw == 4 AND upday);
friday = Cum(dw == 5 AND upday);
// columns for scan
Filter=Status("lastbarinrange");
AddColumn(monday, "monday");
AddColumn(tuesday, "tuesday");
AddColumn(wednesday, "wednesday");
AddColumn(thursday, "thursday");
AddColumn(friday, "friday");
// aggregate over list
Buy = 0; // required by scan
AddToComposite(monday, "~days", "Open");
AddToComposite(tuesday, "~days", "High");
AddToComposite(wednesday, "~days", "Low");
AddToComposite(thursday, "~days", "Close");
AddToComposite(friday, "~days", "Volume");
// plot it
Plot(Foreign("~days", "Open"), "monday", colorRed, styleLine);
Plot(Foreign("~days", "High"), "tuesday", colorBlue, styleLine);
Plot(Foreign("~days", "Low"), "Wednesday", colorGreen, styleLine);
Plot(Foreign("~days", "Close"), "Thursday", colorBlack, styleLine);
Plot(Foreign("~days", "Volume"), "Friday", colorOrange, styleLine);
// That's all folks!
1 Like

John, to aggregate values by column directly in exploration you can use this http://www.amibroker.com/guide/afl/addsummaryrows.html

5 Likes

Ah, another great feature!

Thanks again,

 John
1 Like

@BBands I thought you may have had a different intention. And depending on what you are looking to do with that data, I thought to mention the SUM function ( Calculates a cumulative sum of the ARRAY for the specified number of lookback periods (including today)) .

http://www.amibroker.com/guide/afl/sum.html

An example of gathering data on a Watch List for 2500 daily bars (~ 10 years) instead of using the CUM function (Calculates a cumulative sum of the ARRAY from the first period in the chart).

///@link https://forum.amibroker.com/t/finding-seasonal-pattterns/12940

upday = ROC( Close, 1 ) > 0;
dw = DayOfWeek();

tuesday = Cum( dw == 2 AND upday ); // TJ original calculation
TuesdayUp = dw == 2 AND upday; // identify an Up Tuesday

// lets look at about 10 years of daily bars
Periods = Param("Periods", 2500, 1, 10000, 1); // Adjustable look back period

totTuesUp = Sum(TuesdayUp, Periods);

// on a WL with different listing dates and data lengths
NumberOfTuesdays = Sum(dw == 2 AND C !=0, Periods);

pctTuesUp = (totTuesUp/NumberOfTuesdays)*100;

Filter = Status("lastbarinrange");
AddtextColumn(FullName(), "Company");
AddColumn(tuesday, "original Calculation", 1.2, colorDefault, colorRose);
AddColumn(totTuesUp, "# Tuesdays Up", 1.0);
AddColumn(pctTuesUp, "% Tuesday Up", 1.2, colorDefault, colorLightYellow);
AddColumn(NumberOfTuesdays, "# of Tuesdays", 1.0);

Producing these kinds of calculations,
image

13 Likes