Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Exploration -Monthly Percent Change Table for Amibroker (AFL)
cnbondre
about 14 years ago
Amibroker (AFL)

Rating:
5 / 5 (Votes 2)
Tags:
amibroker, exploration

Hi All,

I want to share how “Exploration” function can be used for different purpose in Amibroker.

“Here is some AFL code that runs as an Exploration and let’s you create a table of monthly percentage changes in a calculation.

Starting from this posting by Mike on the AB Yahoo List. I ( Progster ) have expanded the code comments, and altered it slightly to:

Calculate end-of-month to end-of-month."

For further information check the above link.

cnbondre

Similar Indicators / Formulas

General Market Exploration by XeL
Submitted by xel over 12 years ago
miftha indicator
Submitted by coolpace over 14 years ago
Deel - Average Dollar Price Volatility
Submitted by kaiji almost 15 years ago
DIX50,20,10
Submitted by morgen over 14 years ago
Volume Spike Exploration
Submitted by ngocleasing over 14 years ago

Indicator / Formula

Copy & Paste Friendly
/*
   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
*/

4 comments

1. arm

THANKS FOR SHARING. IS IT POSSIBLE THAT THE STOCKS ARE HIGHLIGHTED THAN JUST FIGURES

THANKS ARM

2. cnbondre

Hi,
arm,
Yes this is a very good suggestion. Unfortunately so far I am not competent enough to write codes to that level. Let us hope some of the members here will be able to add/modify the code to show the tickers instead of only figures;I wish as well.

3. analystbank

At line no. 159 the default parametre is set to true to disable the default column value like symbol, date etc. If we set it to false, it gives some error, kindly suggest, what to do

SetOption(“NoDefaultColumns”, True);

4. edgainbc

To add columns for the Ticker name and the Full Name of equities,
just add the following two lines to the top of the add column statements:

AddTextColumn(Name(), “Name”, 6.0);
AddTextColumn(FullName(), “FullName”, 20.0);

Leave Comment

Please login here to leave a comment.

Back