sMACD

This script is a custom technical analysis tool designed for the MetaTrader platform to help traders identify potential entry points in the market based on momentum and trend direction. It focuses on the relationship between two different types of moving averages to generate visual data and automated alerts.

Core Functionality

The logic operates by calculating three specific components:

  1. Fast and Slow Averages: The tool tracks two different price averages. The "Fast" average reacts quickly to recent price changes, while the "Slow" average follows the broader trend more gradually.
  2. The MACD Value (Histogram): By subtracting the slow average from the fast average, the script creates a "difference" value. This is displayed as a histogram. When this difference is growing, it suggests increasing momentum in one direction; when it shrinks, it suggests slowing momentum.
  3. The Signal Line: The script then takes that difference and applies a third moving average to it. This smooths out the data even further, creating a "Signal Line."

How the Signals Work

The primary goal of this script is to detect "crossovers"—moments when the Histogram and the Signal Line cross paths. These crossovers are interpreted as shifts in market sentiment:

  • Buy Signal: The logic looks for a specific moment where the Histogram moves from below the Signal Line to above it. When this upward crossover occurs on a completed bar, the script triggers an alert.
  • Sell Signal: Conversely, the script monitors for moments where the Histogram moves from above the Signal Line to below it. This downward crossover suggests a shift toward a bearish trend, triggering a corresponding alert.

Smart Filtering Logic

To ensure the tool is practical for real-world trading, the script includes specific "intelligence" to prevent unnecessary noise:

  • New Bar Detection: The script is programmed to only evaluate signals at the start of a new candle (bar). This prevents it from triggering multiple alerts on the same price movement as the market fluctuates rapidly within a single minute or hour.
  • State Memory: The script remembers its previous state. It tracks whether the last alert was a "Buy" or a "Sell." By keeping track of these historical states, it helps ensure that alerts are meaningful and distinct, rather than repeating the same signal multiple times in a row.

Visual Presentation

On your trading chart, the tool provides two visual elements:

  • A Histogram representing the immediate momentum difference.
  • A Line representing the smoothed signal trend.

When these two elements interact by crossing over one another, the script identifies the transition in market energy and notifies the user immediately via an alert popup.

Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
16 Views
0 Downloads
0 Favorites
sMACD
//+------------------------------------------------------------------+
//|                                                        sMACD.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2004, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Silver
#property  indicator_color2  Red
#property  indicator_width1  2
//---- indicator parameters
extern int FastEMA = 12;
extern int SlowEMA = 26;
extern int SignalSMA = 9;
//---- indicator buffers
double MacdBuffer[];
double SignalBuffer[];
// Íîìåð áàðà, ïî êîòîðîìó áóäåò èñêàòüñÿ ñèãíàë
#define SIGNAL_BAR 1
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexDrawBegin(1, SignalSMA);
   IndicatorDigits(Digits + 1);
//---- indicator buffers mapping
   SetIndexBuffer(0, MacdBuffer);
   SetIndexBuffer(1, SignalBuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("sMACD(" + FastEMA + "," + SlowEMA + "," + SignalSMA + ")");
   SetIndexLabel(0, "sMACD"); 
   SetIndexLabel(1, "sSignal");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars = IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars > 0) 
       counted_bars--;
   limit = Bars - counted_bars;
//---- macd counted in the 1-st buffer
   for(int i = 0; i < limit; i++)
       MacdBuffer[i] = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i) - 
                       iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
//---- signal line counted in the 2-nd buffer
   for(i = 0; i < limit; i++)
       SignalBuffer[i] = iMAOnArray(MacdBuffer, Bars, SignalSMA, 0, MODE_SMA, i);
//---- done
//---- Ñòàòè÷åñêèå ïåðåìåííûå, â êîòîðûõ õðàíÿòñÿ
//---- âðåìÿ ïîñëåäíåãî áàðà è íàïðàâëåíèå ïîñëåäíåãî ñèãíàëà
	  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(MacdBuffer[SIGNAL_BAR] - SignalBuffer[SIGNAL_BAR] > 0 && 
				      SignalBuffer[SIGNAL_BAR+1] - MacdBuffer[SIGNAL_BAR+1] >= 0)
		       {
			        //---- Åñëè ïåðåñåêëèñü, îòìå÷àåì ÷òî ïîñëåäíèé ñèãíàë - áàé
			        PrevSignal = 1;
			        //---- è âûâîäèì èíôîðìàöèþ:
			        Alert( "sMACD (", Symbol(), ", ", Period(), ")  -  BUY!!!" );
           //			Print("sMACD (", Symbol(), ", ", Period(), ")  -  BUY!!!");
           //			Comment("sMACD (", Symbol(), ", ", Period(), ")  -  BUY!!!");
           //			PlaySound("Alert.wav");
		       }
	    }
//---- Ïîëíîñòüþ àíàëîãè÷íî äëÿ ñèãíàëà ÑÅËË
	  if(PrevSignal >= 0)
	    {
		     if(SignalBuffer[SIGNAL_BAR] - MacdBuffer[SIGNAL_BAR] > 0 && 
				      MacdBuffer[SIGNAL_BAR+1] - SignalBuffer[SIGNAL_BAR+1] >= 0)
		       {
			        PrevSignal = -1;
			        Alert("sMACD (", Symbol(), ", ", Period(), ")  -  SELL!!!");
           //			Print("sMACD (", Symbol(), ", ", Period(), ")  -  SELL!!!");
           //			Comment("sMACD (", Symbol(), ", ", Period(), ")  -  SELL!!!");
           //			PlaySound("Alert.wav");
		       }
	    }
//----	
   return(0);
  }
//+------------------------------------------------------------------+

Comments