Author: Coded by Witold Wozniak

This script is a specialized technical analysis tool designed for the MetaTrader 5 platform. Based on the "Cybernetics Analysis for Stock and Futures" framework by John F. Ehlers, it aims to provide traders with a visual representation of price trends by calculating specific leading and moving average values.

Core Logic Overview

The primary objective of this script is to transform raw price data into smoother, more readable lines on a chart. It does this by processing the "Mid Price" (the average of the high and low prices) and applying two distinct mathematical filters to create different visual outputs.

Key Components of the Calculation

1. Mid-Price Extraction Instead of relying solely on closing prices, the script first calculates a consistent price point for every candle by finding the halfway mark between the highest and lowest price reached during that period.

2. The "Lead" Component (Blue Line) The script generates a primary signal referred to as "Lead." This is calculated using a recursive formula:

  • It takes the current mid-price and weighs it against the previous bar's mid-price.
  • It also incorporates its own previous value from the prior calculation.
  • By adjusting the Alpha1 parameter, users can change how much "weight" the script gives to current price action versus historical trend data. This creates a smoother curve that attempts to follow price movements with reduced noise.

3. The Net Lead / Filtering (Red Line) To further refine the signal and provide a secondary reference point, the script applies a second layer of smoothing:

  • It takes the "Lead" value generated in the previous step.
  • It blends this value with its own historical data using the Alpha2 parameter.
  • This results in a secondary line (labeled "EMA" in the visual settings) that serves as a smoothed trend indicator, helping traders distinguish between temporary price fluctuations and broader market direction.

4. Visual Customization

  • Shift: The logic includes a horizontal shift feature, allowing users to move the entire indicator forward or backward on the chart to align with specific timeframes or strategies.
  • Dynamic Buffer: The script automatically manages two separate data buffers, ensuring that both the primary lead line and the secondary smoothed line are rendered clearly and accurately as new price data arrives in real-time.

Summary for the User

In simple terms, this tool takes messy market price fluctuations and runs them through two consecutive "filters." The first filter identifies the underlying momentum (the Lead), and the second filter smooths that momentum out further. This provides the trader with a visual representation of the trend that is less reactive to "market noise" while still remaining responsive enough to follow significant price changes.

7 Views
0 Downloads
0 Favorites
leading_v1
//+------------------------------------------------------------------+
//|                                                      Leading.mq5 |
//|                                                                  |
//| Leading                                                          |
//|                                                                  |
//| Algorithm taken from book                                        |
//|     "Cybernetics Analysis for Stock and Futures"                 |
//| by John F. Ehlers                                                |
//|                                                                  |
//|                                              contact@mqlsoft.com |
//|                                          http://www.mqlsoft.com/ |
//+------------------------------------------------------------------+
//--- àâòîðñòâî èíäèêàòîðà
#property copyright "Coded by Witold Wozniak"
//--- àâòîðñòâî èíäèêàòîðà
#property link      "www.mqlsoft.com"
//--- íîìåð âåðñèè èíäèêàòîðà
#property version   "1.00"
//--- îòðèñîâêà èíäèêàòîðà â ãëàâíîì îêíå
#property indicator_chart_window 
//--- äëÿ ðàñ÷åòà è îòðèñîâêè èíäèêàòîðà èñïîëüçîâàíî äâà áóôåðà
#property indicator_buffers 2
//--- èñïîëüçîâàíî âñåãî äâà ãðàôè÷åñêèõ ïîñòðîåíèÿ
#property indicator_plots   2
//+----------------------------------------------+
//|  Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà 1            |
//+----------------------------------------------+
//--- îòðèñîâêà èíäèêàòîðà 1 â âèäå ëèíèè
#property indicator_type1   DRAW_LINE
//--- â êà÷åñòâå öâåòà áû÷åé ëèíèè èíäèêàòîðà èñïîëüçîâàí ñèíèé öâåò
#property indicator_color1  clrBlue
//--- ëèíèÿ èíäèêàòîðà 1 - íåïðåðûâíàÿ êðèâàÿ
#property indicator_style1  STYLE_SOLID
//--- òîëùèíà ëèíèè èíäèêàòîðà 1 ðàâíà 1
#property indicator_width1  1
//--- îòîáðàæåíèå áû÷åé ìåòêè èíäèêàòîðà
#property indicator_label1  "Lead"
//+----------------------------------------------+
//|  Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà 2            |
//+----------------------------------------------+
//--- îòðèñîâêà èíäèêàòîðà 2 â âèäå ëèíèè
#property indicator_type2   DRAW_LINE
//--- â êà÷åñòâå öâåòà ìåäâåæüåé ëèíèè èíäèêàòîðà èñïîëüçîâàí êðàñíûé öâåò
#property indicator_color2  clrRed
//--- ëèíèÿ èíäèêàòîðà 2 - íåïðåðûâíàÿ êðèâàÿ
#property indicator_style2  STYLE_SOLID
//--- òîëùèíà ëèíèè èíäèêàòîðà 2 ðàâíà 1
#property indicator_width2  1
//--- îòîáðàæåíèå ìåäâåæüåé ìåòêè èíäèêàòîðà
#property indicator_label2  "EMA"
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà                 |
//+----------------------------------------------+
input double Alpha1 = 0.25; // 1 êîýôôèöèåíò èíäèêàòîðà
input double Alpha2 = 0.33; // 2 êîýôôèöèåíò èíäèêàòîðà 
input int Shift=0;          // Ñäâèã èíäèêàòîðà ïî ãîðèçîíòàëè â áàðàõ 
//+----------------------------------------------+
//--- Îáúÿâëåíèå öåëûõ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
int min_rates_total;
//--- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå â äàëüíåéøåì
//--- áóäóò èñïîëüçîâàíû â êà÷åñòâå èíäèêàòîðíûõ áóôåðîâ
double NetLeadBuffer[],EMABuffer[];
//+------------------------------------------------------------------+
//| Ïîëó÷åíèå ñðåäíåãî îò öåíîâûõ òàéìñåðèé                          |
//+------------------------------------------------------------------+   
double Get_Price(const double  &High[],const double  &Low[],int bar)
  {
//---
   return((High[bar]+Low[bar])/2);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
void OnInit()
  {
//--- èíèöèàëèçàöèÿ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
   min_rates_total=2;
//--- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
   SetIndexBuffer(0,NetLeadBuffer,INDICATOR_DATA);
//--- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà 1 ïî ãîðèçîíòàëè íà Shift
   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//--- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà 1 íà min_rates_total
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//--- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
   SetIndexBuffer(1,EMABuffer,INDICATOR_DATA);
//--- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà 2 ïî ãîðèçîíòàëè íà Shift
   PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//--- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà 2 íà min_rates_total+1
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
//--- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- èíèöèàëèçàöèè ïåðåìåííîé äëÿ êîðîòêîãî èìåíè èíäèêàòîðà
   string shortname;
   StringConcatenate(shortname,
                     "Leading(",DoubleToString(Alpha1,4),", ",DoubleToString(Alpha2,4),", ",Shift,")");
//--- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//--- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷åòà
   if(rates_total<min_rates_total) return(0);
//--- îáúÿâëåíèÿ ëîêàëüíûõ ïåðåìåííûõ 
   int first,bar;
   double Lead;
//--- îáúÿâëåíèÿ ñòàòè÷åñêèõ ïåðåìåííûõ äëÿ õðàíåíèÿ äåéñòâèòåëüíûõ çíà÷åíèé êîýôèöèåíòîâ
   static double Lead_;
//--- ðàñ÷åò ñòàðòîâîãî íîìåðà first äëÿ öèêëà ïåðåñ÷åòà áàðîâ
   if(prev_calculated>rates_total || prev_calculated<=0) // ïðîâåðêà íà ïåðâûé ñòàðò ðàñ÷åòà èíäèêàòîðà
    {
      first=0; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà âñåõ áàðîâ
    }
   else first=prev_calculated-1; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà íîâûõ áàðîâ
//--- âîññòàíàâëèâàåì çíà÷åíèÿ ïåðåìåííûõ
   Lead=Lead_;
//--- îñíîâíîé öèêë ðàñ÷åòà èíäèêàòîðà
   for(bar=first; bar<rates_total; bar++)
     {
      //--- çàïîìèíàåì çíà÷åíèÿ ïåðåìåííûõ ïåðåä ïðîãîíàìè íà òåêóùåì áàðå
      if(rates_total!=prev_calculated && bar==rates_total-1)
         Lead_=Lead;

      if(bar>min_rates_total)
        {
         Lead=2.0*Get_Price(high,low,bar)+(Alpha1-2.0)*Get_Price(high,low,bar-1)+(1.0-Alpha1)*Lead;
         NetLeadBuffer[bar] = Alpha2 * Lead + (1 - Alpha2) * NetLeadBuffer[bar-1];
         EMABuffer[bar]=0.5 * Get_Price(high,low,bar) + 0.5 * EMABuffer[bar-1];
        }
      else
        {
         Lead=Get_Price(high,low,bar);
         NetLeadBuffer[bar]=Lead;
         EMABuffer[bar]=Lead;
        }
     }
//---     
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments