MQL4 Reversal Indicator
Here's a simple MQL4 indicator that can help spot potential price reversals by combining several technical analysis techniques:
//+------------------------------------------------------------------+
//| Reversal.mq4 |
//| Copyright 2024, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 2
//--- Plot Bullish Reversal
#property indicator_label1 "Bullish Reversal"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrLime
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- Plot Bearish Reversal
#property indicator_label2 "Bearish Reversal"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//--- Input parameters
input int RSI_Period = 14; // RSI Period
input int RSI_Overbought = 70; // RSI Overbought Level
input int RSI_Oversold = 30; // RSI Oversold Level
input int MA_Period = 50; // Moving Average Period
input ENUM_MA_METHOD MA_Method = MODE_SMA; // MA Method
input double Price_Deviation = 1.5; // Price Deviation % from MA for reversal
input bool Use_Candlestick_Patterns = true; // Use candlestick patterns
//--- Indicator buffers
double BullishReversalBuffer[];
double BearishReversalBuffer[];
double RSIBuffer[];
double MABuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Indicator buffers mapping
SetIndexBuffer(0, BullishReversalBuffer);
SetIndexBuffer(1, BearishReversalBuffer);
SetIndexBuffer(2, RSIBuffer);
SetIndexBuffer(3, MABuffer);
//--- Set arrow codes for plots
SetIndexArrow(0, 233); // Up arrow
SetIndexArrow(1, 234); // Down arrow
//--- Set empty value
SetIndexEmptyValue(0, EMPTY_VALUE);
SetIndexEmptyValue(1, EMPTY_VALUE);
//--- Set indicator short name
IndicatorShortName("Reversal Spotter");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
int limit = rates_total - prev_calculated;
if(prev_calculated > 0) limit++;
//--- Calculate RSI
for(int i=limit-1; i>=0; i--)
{
RSIBuffer[i] = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, i);
}
//--- Calculate Moving Average
for(int i=limit-1; i>=0; i--)
{
MABuffer[i] = iMA(NULL, 0, MA_Period, 0, MA_Method, PRICE_CLOSE, i);
}
//--- Main calculation loop
for(int i=limit-1; i>=1; i--)
{
// Reset buffers
BullishReversalBuffer[i] = EMPTY_VALUE;
BearishReversalBuffer[i] = EMPTY_VALUE;
// Check for bullish reversal conditions
if(RSIBuffer[i] < RSI_Oversold &&
close[i] < MABuffer[i] &&
(close[i] - MABuffer[i])/MABuffer[i]*100 <= -Price_Deviation)
{
// Additional candlestick pattern check
if(Use_Candlestick_Patterns)
{
if(IsBullishReversalCandle(i, open, high, low, close))
{
BullishReversalBuffer[i] = low[i] - 10*Point;
}
}
else
{
BullishReversalBuffer[i] = low[i] - 10*Point;
}
}
// Check for bearish reversal conditions
if(RSIBuffer[i] > RSI_Overbought &&
close[i] > MABuffer[i] &&
(close[i] - MABuffer[i])/MABuffer[i]*100 >= Price_Deviation)
{
// Additional candlestick pattern check
if(Use_Candlestick_Patterns)
{
if(IsBearishReversalCandle(i, open, high, low, close))
{
BearishReversalBuffer[i] = high[i] + 10*Point;
}
}
else
{
BearishReversalBuffer[i] = high[i] + 10*Point;
}
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Check for bullish reversal candlestick patterns |
//+------------------------------------------------------------------+
bool IsBullishReversalCandle(int index, const double &open[], const double &high[], const double &low[], const double &close[])
{
// Hammer pattern
if((close[index] > open[index] &&
(close[index] - open[index]) <= (high[index] - close[index])/3 &&
(open[index] - low[index]) >= 2*(close[index] - open[index])) ||
(open[index] > close[index] &&
(open[index] - close[index]) <= (high[index] - open[index])/3 &&
(close[index] - low[index]) >= 2*(open[index] - close[index])))
{
return true;
}
// Engulfing pattern
if(open[index] < close[index] &&
open[index-1] > close[index-1] &&
close[index] > open[index-1] &&
open[index] < close[index-1])
{
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Check for bearish reversal candlestick patterns |
//+------------------------------------------------------------------+
bool IsBearishReversalCandle(int index, const double &open[], const double &high[], const double &low[], const double &close[])
{
// Shooting star pattern
if((close[index] > open[index] &&
(close[index] - open[index]) <= (close[index] - low[index])/3 &&
(high[index] - close[index]) >= 2*(close[index] - open[index])) ||
(open[index] > close[index] &&
(open[index] - close[index]) <= (close[index] - low[index])/3 &&
(high[index] - open[index]) >= 2*(open[index] - close[index])))
{
return true;
}
// Engulfing pattern
if(open[index] > close[index] &&
open[index-1] < close[index-1] &&
close[index] < open[index-1] &&
open[index] > close[index-1])
{
return true;
}
return false;
}
How This Indicator Works
This reversal indicator combines several technical analysis methods to identify potential trend reversals:
-
RSI (Relative Strength Index):
- Identifies overbought (above 70) and oversold (below 30) conditions
- Potential reversals often occur when RSI reaches these extremes
-
Moving Average Deviation:
- Price must deviate significantly from the moving average (configurable percentage)
- Reversals often occur when price stretches too far from the mean
-
Candlestick Patterns (optional):
- Hammer and bullish engulfing patterns for bullish reversals
- Shooting star and bearish engulfing patterns for bearish reversals
Customization Options
You can adjust the following parameters in the indicator settings:
- RSI period and threshold levels
- Moving average period and method
- Price deviation percentage from MA
- Toggle candlestick pattern detection on/off
How to Use
- Bullish reversal signals appear as green up arrows below price
- Bearish reversal signals appear as red down arrows above price
- These signals should be confirmed with other technical analysis tools
- Works best on higher timeframes (H1, H4, D1)
Remember that no indicator is perfect, and you should always use proper risk management when trading based on reversal signals.