2pbIdeal3MA_nk

Author: Copyright � 2009, Nikolay Kositsin
2pbIdeal3MA_nk

This script functions as a sophisticated technical analysis tool designed to generate a "smoothed" moving average line on a trading chart. Unlike standard moving averages that simply take a basic average of past prices, this indicator uses a multi-layered mathematical approach to filter out market noise and provide a cleaner view of price trends.

Core Logic Overview

The primary logic revolves around a three-stage smoothing process. Instead of calculating one single average, the script performs three consecutive "Ideal Moving Average" operations in a chain. Each stage takes the output of the previous calculation as its input, creating a refined, high-quality curve that reacts to price movements with significant stability.

How it Works

  1. The Smoothing Engine: The script utilizes a specific mathematical formula (referred to as "IdealMA") that calculates the difference between consecutive price points. It then uses these differences to weight the current value against previous results. This allows the indicator to adjust its "smoothness" dynamically based on the volatility of the market.
  2. The Three-Layer Chain:
    • Layer 1: The script first processes the raw closing prices of the market.
    • Layer 2: It takes the result of Layer 1 and applies the smoothing formula again.
    • Layer 3: It takes the result of Layer 2 and applies the formula a final time.
    • The final output displayed on your chart is the result of this third calculation, ensuring that any sudden, erratic price spikes are significantly dampened.
  3. Configurable Sensitivity: The script provides six independent variables (x1, x2, z1, z2, w1, w2). These act as "tuning knobs." By adjusting these values, you can control how aggressively the indicator follows the price or how much it prioritizes smoothness. Because there are three layers of logic, you have the ability to fine-tune each stage of the calculation independently.

Key Features for the User

  • Reduced Lag: By using an "Ideal" smoothing logic rather than a simple arithmetic mean, the indicator aims to stay closer to the actual price action while remaining visually smooth.
  • Visual Clarity: The result is rendered as a solid yellow line on the main chart window, making it easy to identify the overarching trend at a glance.
  • Historical Consistency: The script includes internal logic to ensure that as new candles form, the historical data remains consistent and the calculations remain accurate from the oldest data point forward.
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
2pbIdeal3MA_nk
// 22.01.2009 Ðåäàêòèðîâàë Íèêîëàé Êîñèöèí
//+X================================================================X+
//|                                   Moving Average 2p-Ideal3MA.mq4 |
//|                     2p-IdealMA code: Copyright © 2009,   Neutron | 
//|      2p-Ideal3MA indicator: Copyright © 2009,   Nikolay Kositsin | 
//|                              Khabarovsk,   farria@mail.redcom.ru | 
//+X================================================================X+
#property copyright "Copyright © 2009, Nikolay Kositsin"
#property link      "farria@mail.redcom.ru"
//---- îòðèñîâêà èíäèêàòîðà â îñíîâíîì îêíå
#property indicator_chart_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ
#property  indicator_buffers 1
//---- öâåò ëèíèèè èíäèêàòîðîâ
#property indicator_color1 Yellow
//---- âõîäíûå ïàðàìåòðû ýêñïåðòà
extern double x1 =0.1;
extern double x2 = 0.1;
extern double z1 =0.1;
extern double z2 = 0.1;
extern double w1 =0.1;
extern double w2 = 0.1;
//---- èíäèêàòîðíûå áóôåðû
double MovingBuffer[];
//---- 
int StartBar;
//---- îáúÿâëåíèå ïåðåìåííûõ âðåìåíè
datetime time2;
//---- îáúÿâëåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé
double Moving0_0, Moving1_0, Moving2_0;
double Moving0_1, Moving1_1, Moving2_1;
double Moving0_1_, Moving1_1_, Moving2_1_;
//+X================================================================X+
//|  2p-IdealMA function                                             |
//+X================================================================X+
double GetIdealMASmooth(double W1_, double W2_,
                       double Series1, double Series0, double Resalt1)
 {
//---+
   double Resalt0, dSeries, dSeries2;
   dSeries = Series0 - Series1;
   dSeries2 = dSeries * dSeries - 1.0;
   
   Resalt0 = (W1_ * (Series0 - Resalt1) + 
                   Resalt1 + W2_ * Resalt1 * dSeries2) 
                                    / (1.0 + W2_ * dSeries2);	
   return(Resalt0);
//---+ 
 }
//+X================================================================X+
//|  initialization function                                         |
//+X================================================================X+
int init()
 {
//---+
   //---- ñòèëü èçîáðàæåíèÿ èíäèêàòîðà
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 3);
   //---- áóôåð äëÿ ïåðâîé ëèíèè èíäèêàòîðà
   SetIndexBuffer(0, MovingBuffer);
   //---- ìèíèìàëüíîå êîëè÷åñòâî áàðîâ äëÿ ðàñ÷¸òà
   StartBar = 2;
   //----
   return(0);
//---+
 }
//+X================================================================X+
//|  start function                                                  |
//+X================================================================X+
int start()
 {
//---+
   //---- ïðîâåðêà êîëè÷åñòâà áàðîâ 
                           //íà äîñòàòî÷íîñòü äëÿ äàëüíåéøåãî ðàñ÷¸òà
   if (Bars - 1 < StartBar)
                       return(-1);                     
   //---- 
   datetime Tnew;
   //---- Ââåäåíèå öåëûõ ïåðåìåííûõ è ïîëó÷åíèå óæå ïîñ÷èòàííûõ áàðîâ 
   int MaxBar, limit, bar, counted_bars = IndicatorCounted();  
   //---- ïðîâåðêà íà âîçìîæíûå îøèáêè
   if (counted_bars < 0)
                  return(-1);
   //---- ïîñëåäíèé ïîñ÷èòàííûé áàð äîëæåí áûòü ïåðåñ÷èòàí
   if (counted_bars > 0)
              counted_bars--;
   //---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà,
            //íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïåðåñ÷¸ò âñåõ áàðîâ
   MaxBar = Bars - 2;       
   //---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà,
            //íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïåðåñ÷¸ò íîâûõ áàðîâ
   limit = Bars - counted_bars - 1; 
   
   //---- èíèöèàëèçàöèÿ íóëÿ   
   if (limit >= MaxBar)
     {
      
	   Moving0_0 = Close[MaxBar];
      Moving1_0 = Close[MaxBar];
      Moving2_0 = Close[MaxBar];
      //---- 
      Moving0_1 = Close[MaxBar + 1];
      Moving1_1 = Close[MaxBar + 1];
      Moving2_1 = Close[MaxBar + 1];
      //----
      limit = MaxBar;
      MovingBuffer[MaxBar] = Close[MaxBar];
	   MovingBuffer[MaxBar + 1] = Close[MaxBar + 1]; 
     } 
     
   //+---+ âîññòàíîâëåíèå çíà÷åíèé ïåðåìåííûõ
   if (limit < MaxBar)
    {
     Tnew = Time[limit + 1];
     //---- 
     if (Tnew == time2)
      {
       Moving0_1 = Moving0_1_;
       Moving1_1 = Moving1_1_;
       Moving2_1 = Moving2_1_;
      }
     else
      {
       if (Tnew > time2)
            Print("Îøèáêà âîññòàíîâëåíèÿ ïåðåìåííûõ!!! Tnew > time2");
       else Print("Îøèáêà âîññòàíîâëåíèÿ ïåðåìåííûõ!!! Tnew < time2");
       Print("Áóäåò ïðîèçâåä¸í ïåðåñ÷¸ò èíäèêàòîðà íà âñåõ áàðàõ!");
       return(-1);  
      }
    }
   //+---+
   //---- ÂÛ×ÈÑËÅÍÈÅ ÈÍÄÈÊÀÒÎÐÀ  
	for( bar = limit; bar >= 0; bar--)
	 {
	   //+---+ Ñîõðàíåíèå çíà÷åíèé ïåðåìåííûõ
      if (bar == 1)
       if((limit == 1 && time2 == Time[2]) || limit > 1)
        {
         time2 = Time[2];
         Moving0_1_ = Moving0_1;
         Moving1_1_ = Moving1_1;
         Moving2_1_ = Moving2_1;
        }
      //+---+   
	  
      Moving0_0 = GetIdealMASmooth(x1, x2, 
                      Close[bar + 1], Close[bar], Moving0_1);
      //----                       
      Moving1_0 = GetIdealMASmooth(z1, z2, 
                            Moving0_1, Moving0_0, Moving1_1);
      //----                       
      Moving2_0 = GetIdealMASmooth(w1, w2, 
                            Moving1_1, Moving1_0, Moving2_1);
      //----                       
      Moving0_1 = Moving0_0;
      Moving1_1 = Moving1_0;
      Moving2_1 = Moving2_0;
      //---- 
      MovingBuffer[bar] = Moving2_0;
    }
   //----
   return(0);
//---+
 }
//+X----------------------------------------------------------------X+

Comments