MagicOscillator2.00

Download script
Author: zfs
MagicOscillator2.00

The Magic Oscillator 2.00 is a specialized technical analysis tool designed to identify market momentum and potential trend reversals by analyzing price action dynamics and moving average differentials. Instead of relying on a single data point, it evaluates the relationship between recent price "breaks" and broader moving averages to provide a clearer picture of market sentiment.

Core Logic Overview

The script operates by processing three distinct layers of information:

1. Momentum Comparison (Moving Averages) The tool begins by calculating the difference between two moving averages (a fast period and a slow period). By measuring how these averages move relative to one another, the oscillator determines the underlying "pulse" of the market. If the fast average is pulling away from the slow average, it indicates increasing momentum in a specific direction.

2. Price Action Dynamics (The "Delta" Analysis) Unlike standard oscillators that only look at current price, this script examines the behavior of the last three completed candles. It calculates "deltas"—essentially measuring how far the current closing price has pushed past the high or low of previous candles.

  • It checks for specific conditions where a price move is strong enough to overcome historical resistance or support levels within a very short timeframe.
  • The logic prioritizes these "breaks" sequentially: it first looks at the most recent candle, then moves back two candles, and finally three.

3. Conflict Resolution & Signal Filtering The script uses a sophisticated filtering system to decide which signal to display on your chart. It evaluates the price breaks and the moving average momentum simultaneously:

  • If a significant "Buy" break is detected and aligns with positive momentum, it generates a bullish signal.
  • If a "Sell" break occurs alongside negative momentum, it produces a bearish signal.
  • The Priority Hierarchy: If multiple signals occur at once (e.g., a small break on the recent candle vs. a larger break three candles back), the script follows a specific hierarchy to determine which one is most significant. If the most recent data is inconclusive, it automatically "looks back" to find the next strongest valid signal.

Visual Representation and Alerts

  • The Histogram: The indicator displays as a histogram in a separate window. Positive values (appearing on one side of the zero line) represent bullish pressure, while negative values represent bearish pressure.
  • Dynamic Scaling: The height of the bars is calculated based on price volatility, ensuring that the oscillator remains readable regardless of whether you are trading a highly volatile pair or a stable one.
  • Smart Alerts: The script includes a notification system that filters out "noise." It only triggers an alert when the signal actually changes (e.g., moving from a Buy state to a Sell state), preventing the system from sending multiple redundant notifications on the same trend.

Summary for the User

In simple terms, this tool acts as a "momentum gatekeeper." It doesn't just tell you if the price is going up or down; it verifies if the current move has enough strength to break through recent price boundaries while being supported by the larger moving average trend. It filters out minor fluctuations and focuses on significant structural shifts in the market.

Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
15 Views
0 Downloads
0 Favorites
MagicOscillator2.00
//+------------------------------------------------------------------+
//|                                          MagicOscillator2.00.mq4 |
//|                                                              zfs |
//|                                                                @ |
//+------------------------------------------------------------------+
#property copyright "zfs"
#property link      "vasbsm@mail.ru"

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color2  Red
#property  indicator_color3  Blue
#property  indicator_width2 5
#property  indicator_width3 5
#property indicator_minimum 0
#property indicator_maximum 100
//----
#define SIGNAL_BAR 0
//----
extern bool Alerts=true;
extern int Period1=5;//îñíîâíîé ïåðèîä
extern int Period2=34;//ñãëàæèâàþùèé ïåðèîä
extern int TypeMA=MODE_SMA;//0-3
extern int TypePrice=PRICE_MEDIAN;//0-6
extern int delta1buy=0;//îòêëþ÷åíèå -1
extern int delta1sell=0;//îòêëþ÷åíèå -1
extern int delta2buy=0;//îòêëþ÷åíèå -1
extern int delta2sell=0;//îòêëþ÷åíèå -1
extern int delta3buy=-1;//îòêëþ÷åíèå -1
extern int delta3sell=-1;//îòêëþ÷åíèå -1
//---- indicator buffers
double     TempBuffer[];
double     OscBuyBuffer[];
double     OscSellBuffer[];
int init()
  {
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);

   IndicatorDigits(Digits);
   SetIndexDrawBegin(0,Period2);
   SetIndexDrawBegin(1,Period2);
   SetIndexDrawBegin(2,Period2);

   
   SetIndexBuffer(0,TempBuffer);
   SetIndexBuffer(1,OscBuyBuffer);
   SetIndexBuffer(2,OscSellBuffer);
    
   IndicatorShortName("MAGIC OSCILLATOR 2.00");
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);

   return(0);
  }
int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   double prev,current;

   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
     
   for(int i=0; i<limit; i++)
   {
    double high3=iHigh(NULL,0,i+3);
    double low3=iLow(NULL,0,i+3);
    double close3=iClose(NULL,0,i+3);
        
    double high2=iHigh(NULL,0,i+2);
    double low2=iLow(NULL,0,i+2);
    double close2=iClose(NULL,0,i+2);
       
    double high1=iHigh(NULL,0,i+1);
    double low1=iLow(NULL,0,i+1);
    double close1=iClose(NULL,0,i+1);
    
    double ao1=iMA(NULL,0,Period1,0,TypeMA,TypePrice,i+1)-iMA(NULL,0,Period2,0,TypeMA,TypePrice,i+1);
    double ao0=iMA(NULL,0,Period1,0,TypeMA,TypePrice,i)-iMA(NULL,0,Period2,0,TypeMA,TypePrice,i);
    
    double close0=iClose(NULL,0,i);//òåêóùàÿ öåíà
    double high0=iHigh(NULL,0,i);
    double low0=iLow(NULL,0,i);
    
    double l10,h10,hmax10,lmin10,Repaint;
       
    if (high1>high0)
    {
     h10=high0;
     hmax10=high1;
    }
    else
    { 
     h10=high1;
     hmax10=high0;
    }
    if (low1<low0)
    {
     l10=low0;
     lmin10=low1;
    }
    else
    {
     l10=low1;
     lmin10=low0;
    } 
    if (h10-l10<0)Repaint=0;else Repaint=100-((h10-l10)/(hmax10-lmin10)*100); 
    
    double deltab1=0;double deltas1=0;
    double deltab2=0;double deltas2=0;
    double deltab3=0;double deltas3=0;
        
    if (close1>(high1+low1)/2.0)deltab1=close0-high1;     
    if (close1<(high1+low1)/2.0)deltas1=low1-close0;
    
    if (close2>(high2+low2)/2.0)deltab2=close1-high2;     
    if (close2<(high2+low2)/2.0)deltas2=low2-close1;
    
    if (close3>(high3+low3)/2.0)deltab3=close2-high3;     
    if (close3<(high3+low3)/2.0)deltas3=low3-close2;
    
    if (delta1buy==-1)delta1buy=9999;
    if (delta2buy==-1)delta2buy=9999;
    if (delta3buy==-1)delta3buy=9999;
    if (delta1sell==-1)delta1sell=9999;
    if (delta2sell==-1)delta2sell=9999;
    if (delta3sell==-1)delta3sell=9999;
 
     int Break1=0;
     int Break2=0;
     int Break3=0;
     if (deltas3>delta3sell*Point&&deltas3>=deltab3&&ao0>ao1)Break3=-1;   
     if (deltab3>delta3buy*Point&&deltab3>=deltas3&&ao0<ao1)Break3=1; 
           
     if (deltas2>delta2sell*Point&&deltas2>=deltab2&&ao0>ao1)Break2=-1;
     if (deltab2>delta2buy*Point&&deltab2>=deltas2&&ao0<ao1)Break2=1;
          
     if (deltas1>delta1sell*Point&&deltas1>=deltab1&&ao0>ao1)Break1=-1;
     if (deltab1>delta1buy*Point&&deltab1>=deltas1&&ao0<ao1)Break1=1; 
     
     if (Break1==1)TempBuffer[i]=Repaint;
     if (Break1==-1)TempBuffer[i]=-Repaint;
     if (Break1==0)
      {
       if (Break2==1)TempBuffer[i]=Repaint;
       if (Break2==-1)TempBuffer[i]=-Repaint;
       if (Break2==0)
        {
         if (Break3==1)TempBuffer[i]=Repaint;
         if (Break3==-1)TempBuffer[i]=-Repaint;
         if (Break3==0)
          {
          if (ao0>ao1)TempBuffer[i]=Repaint;
           else TempBuffer[i]=-Repaint;
          }
        }
      }
   }
    
   for(i=0; i<limit; i++)
   {    
    if (TempBuffer[i]>0)
    {
    OscBuyBuffer[i]=TempBuffer[i];
    OscSellBuffer[i]=0;
    }
    if (TempBuffer[i]<0)
    {
    OscSellBuffer[i]=-TempBuffer[i];
    OscBuyBuffer[i]=0;
    }
   }
  if (Alerts)
   {
   //---- Ñòàòè÷åñêèå ïåðåìåííûå, â êîòîðûõ õðàíÿòñÿ
   //---- âðåìÿ ïîñëåäíåãî áàðà è íàïðàâëåíèå ïîñëåäíåãî ñèãíàëà
	  static int PrevSignal = 0, PrevTime = 0;
   //---- Åñëè áàðîì äëÿ àíàëèçà âûáðàí íå 0-é, íàì íåò ñìûñëà ïðîâåðÿòü ñèãíàë
   //---- íåñêîëüêî ðàç. Åñëè íå íà÷àëñÿ íîâûé áàð, âûõîäèì.
	  if(SIGNAL_BAR > 0 && Time[0] <= PrevTime ) 
	      return(0);
   //---- Îòìå÷àåì, ÷òî ýòîò áàð ïðîâåðåí
	  PrevTime = Time[0];
   //---- Åñëè ïðåäûäóùèé ñèãíàë áûë ÑÅËË èëè ýòî ïåðâûé çàïóñê (PrevSignal=0)
	  if(PrevSignal <= 0)
	    {
		     if(OscBuyBuffer[SIGNAL_BAR]> 0 && OscSellBuffer[SIGNAL_BAR+1]> 0)
		       {
			        PrevSignal = 1;
			        Alert("MAGIC (", Symbol(), ", ", Period(), ")  -  BUY!!!");
		       }
	    }
	  if(PrevSignal >= 0)
	    {
		     if(OscSellBuffer[SIGNAL_BAR]> 0 && OscBuyBuffer[SIGNAL_BAR+1]> 0)
		       {
			        PrevSignal = -1;
			        Alert("MAGIC (", Symbol(), ", ", Period(), ")  -  SELL!!!");
		       }
	    }
    }
   //--
   return(0);
  }

Comments