How to color an indicator using AFL for Amibroker

This function is very simple and sometimes useful. You can use this function if you need to color an indicator with a color when the indicator is increasing and with another color if the indicator is falling.

The AFL (Amibroker Formula Language) function code is:

function ColorIndicator( Indicator, UpColor, DnColor )
{
    CI = Null;

    for ( i = 1; i < BarCount; i++ )
        if ( Indicator[i] > Indicator[i-1] )
            CI[i] = UpColor;
        else
            if ( Indicator[i] < Indicator[i-1] )
                CI[i] = DnColor;
            else
                CI[i] = CI[i-1];

    return CI;
}

For example you can use it to color the SuperTrend indicator that I coded in this post:

#include_once ".\Formulas\Custom\Functions\SuperTrend.afl";
#include_once ".\Formulas\Custom\Functions\ColorIndicator.afl";

ST = SuperTrend( 10, 3 );

ColorArray = ColorIndicator(ST, colorGreen, colorRed);

Plot( ST, "SuperTrend", ColorArray);

You need to customize the #include_once paths, the result is something like this:

Example of SuperTrend Indicator
Example of SuperTrend Indicator

You can download the script here

Leave a Reply

Your email address will not be published. Required fields are marked *