Author: 2009, MetaQuotes Software Corp.

This tool is a visual trading indicator designed to provide a dynamic, color-coded representation of market trends on your price chart. Instead of showing a standard, static line, it uses a moving average as its foundation but adds a sophisticated visual layer that changes appearance based on the history of the data and real-time updates.

Core Visual Logic

The primary function of this script is to display a "Color Line." Rather than simply reflecting raw price movements, it tracks an Exponential Moving Average (EMA), which smooths out market noise to help identify the underlying direction of the trend.

What makes this indicator unique is its multi-layered color system:

  • Dynamic Color Cycling: The line does not stay one color indefinitely. The script utilizes a "color scheme" logic that periodically shifts. It cycles through three distinct sets of colors (such as Red, Blue, and Green in the first set, followed by different combinations like Yellow, Pink, and Gray in subsequent sets).
  • Time-Based Transitions: These visual shifts are not random. The script tracks market activity (ticks) and triggers a change to the next color scheme once a specific threshold of movement is reached. This provides a visual cue that indicates the progression of time and data flow on the chart.
  • Historical Mapping: The script also uses a mathematical calculation based on the position of the bars on the chart to assign specific color indexes. This ensures that as you scroll through different parts of the history, the indicator maintains a structured and organized visual layout.

How it Works for the User

When you apply this indicator to your platform:

  1. Trend Identification: It draws a solid line representing the Exponential Moving Average, helping you see the general "flow" of the market.
  2. Visual Evolution: As new data comes in and time passes, the line will automatically transition between different color palettes.
  3. Data Integrity: The script is designed to be efficient; it constantly checks if there is enough historical data available before drawing. If the data is still loading, it waits patiently to ensure that once the line appears, it is accurate and continuous.

In summary, this tool transforms a standard technical indicator into a more expressive visual aid. It allows traders to see trend data while providing a changing color environment that reflects the ongoing flow of market ticks and historical progression.

Indicators Used
Moving average indicator
9 Views
0 Downloads
0 Favorites
ColorLine
//+------------------------------------------------------------------+
//|                                                    ColorLine.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//---- plot ColorLine
#property indicator_label1  "ColorLine"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  Red,Green,Blue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//--- indicator buffers
double ExtColorLineBuffer[];
double ExtColorsBuffer[];
//---
int    ExtMAHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtColorLineBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtColorsBuffer,INDICATOR_COLOR_INDEX);
//--- get MA handle
   ExtMAHandle=iMA(Symbol(),0,10,0,MODE_EMA,PRICE_CLOSE);
  }
//+------------------------------------------------------------------+
//| get color index                                               |
//+------------------------------------------------------------------+
int getIndexOfColor(int i)
  {
   int j=i%300;
   if(j<100) return(0);// first index
   if(j<200) return(1);// second index
   return(2); // third index
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   static int ticks=0,modified=0;
   int        limit;
//--- check data
   int calculated=BarsCalculated(ExtMAHandle);
   if(calculated<rates_total)
     {
      Print("Not all data of ExtMAHandle is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
     {
      //--- copy values of MA into indicator buffer ExtColorLineBuffer
      if(CopyBuffer(ExtMAHandle,0,0,rates_total,ExtColorLineBuffer)<=0) return(0);
      //--- now set line color for every bar
      for(int i=0;i<rates_total;i++)
         ExtColorsBuffer[i]=getIndexOfColor(i);
     }
   else
     {
      //--- we can copy not all data
      int to_copy;
      if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
      else
        {
         to_copy=rates_total-prev_calculated;
         if(prev_calculated>0) to_copy++;
        }
      //--- copy values of MA into indicator buffer ExtColorLineBuffer
      int copied=CopyBuffer(ExtMAHandle,0,0,rates_total,ExtColorLineBuffer);
      if(copied<=0) return(0);

      ticks++;// ticks counting
      if(ticks>=5)//it's time to change color scheme
        {
         ticks=0; // reset counter
         modified++; // counter of color changes
         if(modified>=3)modified=0;// reset counter 
         ResetLastError();
         switch(modified)
           {
            case 0:// first color scheme
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Red);
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Blue);
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,Green);
               //Print("Color scheme"+modified);
               break;
            case 1:// second color scheme
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Yellow);
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Pink);
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,LightSlateGray);
               //Print("Color scheme"+modified);
               break;
            default:// third color scheme
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,LightGoldenrod);
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Orchid);
               PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,LimeGreen);
               //Print("Color scheme"+modified);
           }
        }
      else
        {
         //--- set start position
         limit=prev_calculated-1;
         //--- now we set line color for every bar
         for(int i=limit;i<rates_total;i++)
            ExtColorsBuffer[i]=getIndexOfColor(i);
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments