# Quarterly Percent Change

Dear Team,

Firstly some credits:

1. http://finance.groups.yahoo.com/group/amibroker/message/125846
Original code provided by "Mike", as above and Extra comments, and some changes by Progster
2. Did read through similar query below, but the flow seemed to be closed
https://forum.amibroker.com/t/how-to-get-each-quarter-closing-price-per-year-with-in-the-date-range/22223/12

Now a bit of the code:

``````/*
Original code provided by "Mike", here:

http://finance.groups.yahoo.com/group/amibroker/message/125846

Extra comments, and some changes by Progster

*/

//   Calculation of the value for which the tabular display of %Chg is desired.
//   Change this to whatever calculation you wish.
eq = Close;

//   Make the year and month available (in arrays) for every bar processed
yr = Year();      //   e.g. 2008
mo = Month();      //   i.e. 1 - 12

qtr = (Month() != Ref(Month(), 1) AND (Month() == 3) OR Month() == 6 OR Month() == 9 OR Month() == 12);

//   Create arrays marking new years and new months
YearChange = yr != Ref( yr, -1 );         //   TRUE on first bar of new year
MonChange = mo != Ref( mo, -1 );         //   TRUE on first bar of new month
QtrChange = qtr!= Ref( qtr, -1 );;

FirstYr = 0;
LastYr = 0;

startbar = 0;
endbar = 0;

FirstBar = Status("firstbarinrange");
LastBar = Status("lastbarinrange");

////////////////////////////
////////////////////////////
for ( i = 0; i < BarCount; i++ )
{
if ( FirstBar[ i ] )
{
startbar = i;         //   save the bar index of the first bar in the analysis range
}

if ( LastBar[ i ] )
{
endbar = i;            //   save the bar index of the last bar in the analysis range
break;
}
}

////////////////////////////
// collect yearly / monthly changes in symbol
// into dynamic variables
////////////////////////////

//   Initialize tracking variables
LastYrValue = eq[ startbar ];         //   note: initial equity was set to Close, above
LastMoValue = eq[ startbar ];
LastQtrValue = eq[ startbar ];

MaxYrProfit = MinYrProfit = 0;
MaxMoProfit = MinMoProfit = 0;
MaxQtrProfit = MinQtrProfit = 0;

//   Loop the analysis range (only)
for ( i = startbar +1; i <= endbar; i++ )
{

//   Calculate yearly statistics on year change (and at at end of analysis range)
if ( YearChange[ i ] || i == endbar )
{
// Chg = 100 * ( -1 + eq[ i ] / LastYrValue );      //   percentage change calc
Chg = 100 * ( -1 + eq[ i - 1 ] / LastYrValue );      //   percentage change calc

VarSet( "ChgYear" + yr[ i - 1 ], Chg );         //   save in dynamic variable for each year

// Track max and min yearly profit across years seen
MaxYrProfit = Max( MaxYrProfit, Chg );
MinYrProfit = Min( MinYrProfit, Chg );

if ( FirstYr == 0 )
FirstYr = yr[ i - 1 ];

// LastYr = yr[ i ];
LastYr = yr[ i - 1 ];

// LastYrValue = eq[ i ];
LastYrValue = eq[ i - 1 ];
}

//   Calculate monthly statistics on month change (and at at end of analysis range)
if ( MonChange [ i ] || i == endbar )
{

thisYr = yr[ i - 1];
mon = mo[ i - 1 ];

// Chg = 100 * ( -1 + eq[ i ] / LastMoValue );      //   percentage change calc
Chg = 100 * ( -1 + eq[ i - 1] / LastMoValue );      //   percentage change calc

VarSet( "ChgMon" + yr[ i - 1 ] + "-" + mon, Chg );   //   save in dynamic variable for each month

VarSet( "SumChgMon" + mon, Chg + Nz( VarGet( "SumChgMon" + mon ) ) );
VarSet( "SumMon" + mon, 1 + Nz( VarGet( "SumMon" + mon ) ) );

// Track max and min monthly profit across months seen
MaxMoProfit = Max( MaxMoProfit, Chg );
MinMoProfit = Min( MinMoProfit, Chg );

// LastMoValue = eq[ i ];
LastMoValue = eq[ i - 1 ];
}

if ( QtrChange [ i ] || i == endbar )
{

thisYr = yr[ i - 1];
quarter = qtr[ i - 1 ];

Chg = 100 * ( -1 + eq[ i - 1] / LastQtrValue );      //   percentage change calc

VarSet( "ChgQtr" + yr[ i - 1 ] + "-" + quarter, Chg );   //   save in dynamic variable for each quarter

VarSet( "SumChgQtr" + quarter, Chg + Nz( VarGet( "SumChgQtr" + quarter) ) );
VarSet( "SumQtr" + quarter, 1 + Nz( VarGet( "SumQtr" + quarter) ) );

// Track max and min monthly profit across months seen
MaxQtrProfit = Max( MaxQtrProfit, Chg );
MinQtrProfit = Min( MinQtrProfit, Chg );

LastQtrValue = eq[ i - 1 ];
}

}

////////////////////////////
// Transfer dynamic variable values into arrays and add to exploration.
////////////////////////////

Years = 0;
Jan = Feb = Mar = Apr = May = Jun = Jul = Aug = Sep = Oct = Nov = Dec = 0;
MarQ = JunQ = SepQ = DecQ = 0;
Annual = 0;
index = startbar;

for ( y = FirstYr; y <= LastYr; y++ )
{
Years[ index ] = y;
Jan[ index ] = VarGet( "ChgMon" + y + "-" + 1 );
Feb[ index ] = VarGet( "ChgMon" + y + "-" + 2 );
Mar[ index ] = VarGet( "ChgMon" + y + "-" + 3 );
Apr[ index ] = VarGet( "ChgMon" + y + "-" + 4 );
May[ index ] = VarGet( "ChgMon" + y + "-" + 5 );
Jun[ index ] = VarGet( "ChgMon" + y + "-" + 6 );
Jul[ index ] = VarGet( "ChgMon" + y + "-" + 7 );
Aug[ index ] = VarGet( "ChgMon" + y + "-" + 8 );
Sep[ index ] = VarGet( "ChgMon" + y + "-" + 9 );
Oct[ index ] = VarGet( "ChgMon" + y + "-" + 10 );
Nov[ index ] = VarGet( "ChgMon" + y + "-" + 11 );
Dec[ index ] = VarGet( "ChgMon" + y + "-" + 12 );

MarQ[ index ] = VarGet( "ChgQtr" + y + "-" + 3 );
JunQ[ index ] = VarGet( "ChgQtr" + y + "-" + 6 );
SepQ[ index ] = VarGet( "ChgQtr" + y + "-" + 9 );
DecQ[ index ] = VarGet( "ChgQtr" + y + "-" + 12 );

Annual[ index ] = VarGet( "ChgYear" + y );

index++;
}

Filter = Years;

SetOption("NoDefaultColumns", True);
SetSortColumns(-4);
SetSortColumns(-5);
SetSortColumns(-6);
SetSortColumns(-7);
SetSortColumns(-8);
SetSortColumns(-9);
SetSortColumns(-10);
SetSortColumns(-11);
SetSortColumns(-12);
SetSortColumns(-13);
SetSortColumns(-14);
SetSortColumns(-15);

``````

Am bit stuck up on following issues:

1. Monthly change (in percentage) for latest month is getting calculated on day prior i.e. Ref(-1). Similarly the yearly changes are reflected a bit off the target.
2. Quarterly is a AND + OR condition. Seems it's not able to pick up the right stuff. Hence there is some logical flaw wherein it doesn't differentiate between Month = 3 or Month =6 etc..

You've got parentheses wrong with qtr variable assignment. Should be:

``````mo = Month();
qtr = mo != Ref( mo, 1) AND ( mo == 3 OR mo == 6 OR mo == 9 OR mo == 12);
``````
1 Like

Thanks. OK have corrected the same.
Well the issue still remains the same, as far as output is concerned.

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