// Downloaded From https://www.WiseStockTrader.com
/*
   MonthlyTable_01.afl

   Exploration code to produce a month-by-month tabular presentation of percentage changes
   in a value.

   In this code the value is designated as "eq" (for equity), but is simply set to the 
   price Close for demonstration purposes.
   
   Original code provided by "Mike", here:

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

   Extra comments, _TRACE, 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

//   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

FirstYr = 0;
LastYr = 0;

startbar = 0;
endbar = 0;

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

////////////////////////////
// SKIP non-trading bars
////////////////////////////
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 ];

MaxYrProfit = MinYrProfit = 0;
MaxMoProfit = MinMoProfit = 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

      _TRACE( "Calculations for " ) ;
      _TRACE( "Year: " +  NumToStr(thisYr, 1.0) ) ;
      _TRACE( "Month: " +  NumToStr(mon, 1.0) ) ;
      _TRACE( "LastMoValue: " +  NumToStr(LastMoValue, 1.2) ) ;
      // _TRACE( "eq[" + NumToStr(i - 1, 1.0) + "]: " +  NumToStr(eq[ i - 1], 1.2) ) ;
      _TRACE( "ThisMoValue: " +  NumToStr(eq[ i - 1], 1.2) ) ;
      _TRACE( "Chg: " +  NumToStr(Chg, 1.2) ) ;
      _TRACE( "---------------------------" ) ;

        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 ];
    }
}

////////////////////////////
// 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;
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 );
    Annual[ index ] = VarGet( "ChgYear" + y );

    index++;
}

Filter = Years;

SetOption("NoDefaultColumns", True);
AddColumn(Years, "Year", 4.0);
AddColumn(Jan, "Jan%", 1.2);
AddColumn(Feb, "Feb%", 1.2);
AddColumn(Mar, "Mar%", 1.2);
AddColumn(Apr, "Apr%", 1.2);
AddColumn(May, "May%", 1.2);
AddColumn(Jun, "Jun%", 1.2);
AddColumn(Jul, "Jul%", 1.2);
AddColumn(Aug, "Aug%", 1.2);
AddColumn(Sep, "Sep%", 1.2);
AddColumn(Oct, "Oct%", 1.2);
AddColumn(Nov, "Nov%", 1.2);
AddColumn(Dec, "Dec%", 1.2);
AddColumn(Annual, "Yr. Profit%", 1.2);
//AddColumn(AddSummaryRows,(2) ); ===cnb found no solution hence blocked as adviced bellow.
/*
- You will need the most recent beta release for the AddSummaryRows
function to work (just remove this line if you are NOT up to that
release). http://finance.groups.yahoo.com/group/amibroker/message/125846
*/