How Many Trading Days In A Year?

Sometimes it is useful to know the number of trading days in a year e.g. to annualize returns from a system that trades on a daily basis.

On other occasions, index data can contain errors, and the number of daily bars, in a year, can be compared to the exchange calendar, for the relevant index, to check on this failing.

To count the number of trading days in a calendar year (for any equity market):

a) download the .afl file attached to this post and save it in Program Files/AmiBroker/Formulas/Custom,

b) open AmiBroker and display any index, for the market of interest, in a chart e.g. ^DJI;
c) find p-barsinyear in the Charts list and right click on it, then Insert it as an indicator, (it will be plotted in a new pane in the bottom section of the Chart window);
d) click in the Chart to position the Selector Line in the BarsInYear chart, just after the end of the year being measured (where the cumulative line becomes a constant in the upper section of the chart);
e) read the BarsInYear total from the Chart Title (highlighted in yellow).

(Note that this calculation requires daily bars and also that it is set to calculate the number of trading days for the last full year at the time of writing i.e. 2006. To calculate the number of trading days for a different year e.g. 2007, change the 106 to 107 in both of the DateNum() number sequences).

1
2
3
4
5
6
7
8
9
10
/*P_BarsInYear*/
 
InYearFlag = IIf(DateNum() >= 1060101 AND DateNum() <= 1061231,1,0);
InYear = Cum(InYearFlag);
Plot(InYear,"BarsInYear",colorBlack,styleLine);
 
// Logic.
// line 3 returns one if a bar is within the calendar range, or zero if not, and assigns it to the variable InYearFlag;
// line 4 cumulates the ones from line 3 and assigns the result to the InYear variable;
// line 5 plots the cumulated result in a chart;

BIY001

Attached Files:

Right click and Save Target As to save .afl files to the desktop at Program Files/AmiBroker/Formulas/Custom and access them as formulas within AmiBroker Charts.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Plotting Trade-Lines

A useful application is to plot straight lines between entry- and exit-signals, giving you the ability to view at a glance the location and magnitude of profits and losses of your trading system. The LineArray() function enables you to draw straight lines from one event or condition to another. In the chart below, which shows a reversal trading system, note how the lines begin and end at the exact trade prices, green being long and red being short. This gives you a quick impression of the profitability and location of individual trades:

Chart with Trade-Lines

Other applications would be plotting of custom ZigZag lines, price channels, trendlines, breakouts, etc.

There are two afl versions listed below, since I believe many of us use the first method, I decided to show them both for educational purposes.

The first one shows how you should NOT plot LineArrays. This method calls Plot() repeatedly and plots each LineArray segment as it is calculated. This is very resource consuming (executes slow) and may trigger a Warning 502 when you display a lot of data. Do not use this version.

The second version shows how Tomasz (Thanks TJ!) combined the individual LineArray segments inside the loop and then plots them with a single Plot() statement outside the loop. This code executes much faster and will never trigger Warning 502. The technique is simple but shows a clever way to combine array segments. Study it :-) it will come in handy one day!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// This version is only listed to show you how it should NOT be programmed
// Dummy system to generate some signals
Buy = Cross( MACD(), Signal() );
BuyPrice = Open; 	// Substitute your own prices
Sell = Cross( Signal(), MACD() );
SellPrice = Close; 	// Substitute your own prices
PlotShapes( IIf( Buy, shapeSmallUpTriangle, shapeNone ), colorBrightGreen, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorRed, 0, SellPrice, 0 );
Plot( C, "", 1, 128 );
// Plot the Trade Lines
Sig = Buy OR Sell;
y0 = 0;
y1 = C[0];
TPrice = C;
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status( "LastVisibleBar" );
 
for ( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++ )
{
    if ( Buy[b] )
    {
        Co = colorRed;
        TPrice[b] = BuyPrice[b];
    }
 
    if ( Sell[b] )
    {
        Co = colorBrightGreen;
        TPrice[b] = SellPrice[b];
    }
 
    if ( Sig[b] )
    {
        x0 = y0;
        x1 = y1;
        y0 = b;
        y1 = TPrice[b];
        Plot( LineArray( x0, x1, y0, y1 ), "", Co, 1 );
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Improved version
// Dummy system to generate some signals
Buy = Cross( MACD(), Signal() );
BuyPrice = O; // Substitute your own prices
Sell = Cross( Signal(), MACD() );
SellPrice = C; // Substitute your own prices
PlotShapes( IIf( Buy, shapeUpTriangle, shapeNone ), colorBrightGreen, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownTriangle, shapeNone ), colorRed, 0, SellPrice, 0 );
Plot( C, "", 1, 128 );
 
// Plot the Trade Lines
Sig = Buy OR Sell;
y0 = 0;
y1 = C[0];
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status( "LastVisibleBar" );
CombinedColor = colorWhite;
CombinedLine = Null;
 
for ( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++ )
{
 
    if ( Buy[b] )
    {
        Co = colorRed;
        TPrice[b] = BuyPrice[b];
    }
    else if ( Sell[b] )
    {
        Co = colorBrightGreen;
        TPrice[b] = SellPrice[b];
    }
 
    if ( Sig[b] )
 
    {
 
        x0 = y0;
 
        x1 = y1;
 
        y0 = b;
 
        y1 = TPrice[b];
 
        La = LineArray( x0, x1, y0, y1 );
 
        CombinedLine = IIf( IsNull( la ), CombinedLine, la );
        CombinedColor = IIf( IsNull( La ), CombinedColor, Co );
    }
}
 
Plot( CombinedLine, "", CombinedColor );

Edited by Al Venosa

1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 4.33 out of 5)
Loading ... Loading ...
« Previous Page