How to identify and define trading days

Hello everybody

I am new to AmiBroker, doing the first steps since a week or two.
For my testing, I would like to compare how a trading model behaves when I trade daily, once per week, once per month or once per quarter. The trading decisions shall be made with daily data.

To trade on Wednesday (= once per week), my code looks as follows:

// Darf diesen Monat getradet werden?
// Switch to S&P symbol to calculate broad-markettiming
SetForeign( "A0AET0" );
// now we can calculate,based on S&P 500
MarketIsUp = C > MA( C, 200 ); // here Crepresents closing price of S&P 500
//
// now go back to originaldata (current symbol)
RestorePriceArrays();
//
NumPos =10;
SetOption( "MaxOpenPositions", NumPos );
PositionSize = -100 /NumPos;
PositionScore = (Close / MA (Close, 256)) + (Close / MA (Close,128)) + (Close / MA (Close, 64));

// SumRSL = (Close / MA(Close, 256)) + (Close / MA (Close, 128)) + (Close / MA (Close, 64));
SumRSL = Close / MA (Close, 128) ;
Tradeday = DayofWeek() ==3;

Bcond1 = Close > MA (Close, 200 );
Bcond2 = Close > MA ( Close, 100 ) ;
// Bcond3 = SumRSL > 3.4 ;
Bcond3 = SumRSL > 1.1;
Bcond4 = MarketIsUp ;
Bcond5 = Tradeday;

Buy = Bcond1 AND Bcond2 AND Bcond3 AND Bcond4 AND Bcond5;

Scond1 = C < HHV( C - 3* ATR(10), 15 );
Scond2 = Tradeday; 

Sell = Scond1 AND Scond2;

What I could not find out yet and where I would like to get support from you, dear forum member, is the following:

  1. In case Wednesday is a holiday as it could happen in the Christmas week, which options exist to define the tradingday e.g. as second tradingday in the week?
  2. To apply the same methodology to define the second trading day per month and the second tradingday per quarter as "tradingday", which options exist?

I am looking for a simple and elegant solution.

Thank you a lot.

Hi Reti

Here's one way to do it. First identify the first bar of each of the periods, then use BarsSince to count forward to the next bar, which will be the second trading day of that period (assuming you are using Daily periodicity).

NewWeek 	= ROC(DayOfWeek(), 1) < 0;
NewMonth 	= ROC(Month(), 1) != 0;
NewQuarter 	= ROC(Month() % 3 == 0, 1) < 0;

PeriodStart 	= NewWeek;
//PeriodStart 	= NewMonth;
//PeriodStart 	= NewQuarter;

SecondTradingDay = BarsSince(PeriodStart) == 1;
1 Like

FYI, using Ref() is way faster than using ROC(). NewWeek and NewMonth below being around 30% faster. NewQuarter around 10% faster.

mth = Month();
dow = DayOfWeek();
mth_mod3 = mth % 3;

NewWeek = dow < Ref(dow,-1);
NewMonth = mth != Ref(mth,-1);
NewQuarter = mth_mod3 * !Ref(mth_mod3,-1);
4 Likes

Good morning MacAllan, good morning fxshrat

Thank you so much for supporting me. Very elegant solutions! This is exactly what I am looking for and this will bring me a big step forward.

Have a great day and stay safe.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.