Mastering Divergences: Your Go-To Template Indicator for MetaTrader 4

Mike 2024.03.10 23:41 23 0 0
Attachments

DivEurusd

Understanding the Indicator Template

The Divergences Template Indicator is designed to help you plot divergences using your favorite oscillator. Whether you lean towards the CCI, RSI, or a custom-built indicator, this template gives you the flexibility you need.

To harness the power of your chosen oscillator, you’ll want to tweak the code a bit. Here’s a quick look at how to do that:

   /////////////////////////////////////////////
   //Load indicator data into buffer
   //Easily swap RSI with any indicator
   int BARS=MathMax(rates_total-IndicatorCounted()-pivots_period,1);
   for(int i=BARS;i>=0;i--)
   {
      indicatorBuffer[i]=iRSI(_Symbol, PERIOD_CURRENT, 14, PRICE_CLOSE, i);
   }
   //End of load indicator section
   /////////////////////////////////////////////

Signal Buffers Explained

This indicator comes equipped with four distinct buffers to track the signals generated. Whenever one of these buffers holds a non-empty value, you’ll have a signal to act on:

   SetIndexBuffer(3,bull_reg_divBuffer);
   SetIndexBuffer(4,bear_reg_divBuffer);
   SetIndexBuffer(5,bull_hid_divBuffer);
   SetIndexBuffer(6,bear_hid_divBuffer);

Inputs Section

input int pivots_period=5; //Period for finding indicator pivots
input int alert_confirm_candles=1; //#candles for signal confirmation (0=disable alert)

Finding pivot highs and pivot lows in the indicatorBuffer is influenced by the pivots_period. A larger value means the indicator will search for bigger swings, giving you more potential divergence signals.

The alert_confirm_candles input specifies how many bars to wait for confirming a signal. Since divergence indicators can lag and produce false signals, increasing this value can help reduce those false alarms. It’s all about finding the right balance between timely signals and reliable confirmations.

Remember, it’s generally not advisable to set pivots_period to less than 2.

The Repaint Issue

Indicators that rely on pivot calculations can repaint signals as they depend on the pivots_period to confirm recent highs and lows. Here’s how that looks in code:

   BARS=MathMax(rates_total-IndicatorCounted()-pivots_period,pivots_period);
   for(int i=BARS;i>=0;i--)
   {
      PHBuffer[i]=pivothigh(indicatorBuffer, pivots_period, pivots_period, i);
      PLBuffer[i]=pivotlow(indicatorBuffer, pivots_period, pivots_period, i);
      bull_reg_divBuffer[i]=BullRegDiv(i);
      bear_reg_divBuffer[i]=BearRegDiv(i);
      bull_hid_divBuffer[i]=BullHidDiv(i);
      bear_hid_divBuffer[i]=BearHidDiv(i);
   }
List
Comments 0