ReversalFractals
Download scriptThis script is designed to identify potential price turning points on a trading chart by detecting specific geometric patterns known as "fractals." Instead of simply looking at every minor price fluctuation, it filters the market data to find significant peaks and valleys that suggest a change in trend—specifically moving from an upward move to a downward one (bearish) or vice versa (bullish).
Core Logic and Detection Methods
The script scans the price history to identify two distinct types of "fractal" patterns based on their surrounding price structure:
- 5-Bar Fractals: These are more sensitive signals. The logic looks for a central high or low point that is higher (for peaks) or lower (for valleys) than the four bars immediately surrounding it. This identifies shorter-term structural changes.
- 7-Bar Fractals: These are broader, more conservative signals. The logic identifies a peak or valley that stands out against a wider range of seven surrounding bars. Because it requires more data points to confirm the pattern, these signals represent larger, more established price pivots.
Signal Filtering and Smart Execution
To ensure the chart remains clear and to prevent "false" repetitive signals, the script employs three specific logic gates:
- Alternating Action: The script is programmed to recognize the transition of momentum. It will only highlight a "Bullish Reversal" (buy signal) if the previous confirmed move was bearish, and vice versa. This prevents the tool from painting multiple buy signals in a row during a single upward trend.
- Unique Value Check: The logic ensures that it does not repeat the exact same price level as a signal twice in a row, filtering out redundant data.
- Visual Mapping: Once a valid reversal pattern is confirmed based on these rules, the script places a visual marker (an arrow) on the chart at the precise historical point where the peak or valley occurred.
User Customization
The script includes an optional setting that allows users to decide whether signals should only appear once a price candle has fully closed. By default, it can monitor live movements, but this toggle provides an extra layer of confirmation for traders who prefer waiting for settled prices before identifying a reversal.
//+------------------------------------------------------------------+
//| ReversalFractals.mq4
//| Copyright © Pointzero-indicator.com
//+------------------------------------------------------------------+
#property copyright "Copyright © Pointzero-indicator.com"
#property link "http://www.pointzero-indicator.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
#define IName "ReversalFractals"
//-------------------------------
// Input parameters
//-------------------------------
extern bool CalculateOnBarClose = false;
//-------------------------------
// Buffers
//-------------------------------
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//-------------------------------
// Internal variables
//-------------------------------
double last_signal = 0;
double last_action = OP_BUY;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
// ZigZag signals
SetIndexStyle(0, DRAW_ARROW, STYLE_DOT, 1);
SetIndexArrow(0, 233);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(1, DRAW_ARROW, STYLE_DOT, 1);
SetIndexArrow(1, 234);
SetIndexBuffer(1, ExtMapBuffer2);
// Data window
IndicatorShortName("Reversal Fractals");
SetIndexLabel(0, "Bullish reversal");
SetIndexLabel(1, "Bearish reversal");
// Copyright
Comment("Copyright © http://www.pointzero-indicator.com");
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
// Start, limit, etc..
int start = 0;
int limit;
int counted_bars = IndicatorCounted();
// nothing else to do?
if(counted_bars < 0)
return(-1);
// do not check repeated bars
limit = Bars - 1 - counted_bars;
// Check if ignore bar 0
if(CalculateOnBarClose == true) start = 1;
// Check the signal foreach bar from past to present
for(int i = limit; i >= start; i--)
{
// Grab all fractals
double upper_fractal_5b = upper_fractal_5b(i, true);
double upper_fractal_7b = upper_fractal_7b(i, true);
double lower_fractal_5b = lower_fractal_5b(i, true);
double lower_fractal_7b = lower_fractal_7b(i, true);
// Long 5bar reversal
if(lower_fractal_5b != 0 && lower_fractal_5b != last_signal && last_action == OP_SELL)
{
ExtMapBuffer1[i+2] = lower_fractal_5b;
last_signal = lower_fractal_5b;
last_action = OP_BUY;
} else
// Long 7bar reversal
if(lower_fractal_7b != 0&& lower_fractal_7b != last_signal && last_action == OP_SELL)
{
ExtMapBuffer1[i+3] = lower_fractal_7b;
last_signal = lower_fractal_7b;
last_action = OP_BUY;
} else
// Short 5bar reversal
if(upper_fractal_5b != 0 && upper_fractal_5b != last_signal && last_action == OP_BUY)
{
ExtMapBuffer2[i+2] = upper_fractal_5b;
last_signal = upper_fractal_5b;
last_action = OP_SELL;
} else
// Short 7bar reversal
if(upper_fractal_7b != 0&& upper_fractal_7b != last_signal && last_action == OP_BUY)
{
ExtMapBuffer2[i+3] = upper_fractal_7b;
last_signal = upper_fractal_7b;
last_action = OP_SELL;
}
// Is in only a slow zigzag signal?
// ExtMapBuffer2[i+2] = fr_t;
}
return(0);
}
//+------------------------------------------------------------------+
//| Custom code ahead
//+------------------------------------------------------------------+
/**
* Returns fractal resistance
* @param int shift
*/
double upper_fractal_7b(int shift = 1, bool bk = false)
{
double middle = iHigh(Symbol(), 0, shift + 3);
double v1 = iHigh(Symbol(), 0, shift);
double v2 = iHigh(Symbol(), 0, shift+1);
double v3 = iHigh(Symbol(), 0, shift+2);
double v5 = iHigh(Symbol(), 0, shift + 4);
double v6 = iHigh(Symbol(), 0, shift + 5);
double v7 = iHigh(Symbol(), 0, shift + 6);
double v1_c = iLow(Symbol(), 0, shift);
double v7_c = iLow(Symbol(), 0, shift + 5);
if((middle > v1 &&
middle > v2 &&
middle > v3 &&
middle > v5 &&
middle > v6 &&
middle > v7) && (bk == false || v1_c < v7_c))
{
return(middle);
} else {
return(0);
}
}
/**
* Returns fractal support and stores wether it has changed or not
* @param int shift
*/
double lower_fractal_7b(int shift = 1, bool bk = false)
{
double middle = iLow(Symbol(), 0, shift + 3);
double v1 = iLow(Symbol(), 0, shift);
double v2 = iLow(Symbol(), 0, shift+1);
double v3 = iLow(Symbol(), 0, shift+2);
double v5 = iLow(Symbol(), 0, shift + 4);
double v6 = iLow(Symbol(), 0, shift + 5);
double v7 = iLow(Symbol(), 0, shift + 6);
double v1_c = iHigh(Symbol(), 0, shift);
double v7_c = iHigh(Symbol(), 0, shift + 5);
if((middle < v1 &&
middle < v2 &&
middle < v3 &&
middle < v5 &&
middle < v6 &&
middle < v7) && (bk == false || v1_c > v7_c))
{
return(middle);
} else {
return(0);
}
}
/**
* Returns fractal resistance
* @param int shift
*/
double upper_fractal_5b(int shift = 1, bool bk = false)
{
double middle = iHigh(Symbol(), 0, shift + 2);
double v1 = iHigh(Symbol(), 0, shift);
double v2 = iHigh(Symbol(), 0, shift+1);
double v3 = iHigh(Symbol(), 0, shift + 3);
double v4 = iHigh(Symbol(), 0, shift + 4);
double v1_c = iLow(Symbol(), 0, shift);
double v4_c = iLow(Symbol(), 0, shift + 3);
if((middle > v1 && middle > v2 && middle > v3 && middle > v4) && (bk == false || v1_c < v4_c))
{
return(middle);
} else {
return(0);
}
}
/**
* Returns fractal support and stores wether it has changed or not
* @param int shift
*/
double lower_fractal_5b(int shift = 1, bool bk = false)
{
double middle = iLow(Symbol(), 0, shift + 2);
double v1 = iLow(Symbol(), 0, shift);
double v2 = iLow(Symbol(), 0, shift+1);
double v3 = iLow(Symbol(), 0, shift + 3);
double v4 = iLow(Symbol(), 0, shift + 4);
double v1_c = iHigh(Symbol(), 0, shift);
double v4_c = iHigh(Symbol(), 0, shift + 3);
if((middle < v1 && middle < v2 && middle < v3 && middle < v4) && (bk == false || v1_c > v4_c))
{
return(middle);
} else {
return(0);
}
}
//+------------------------------------------------------------------+
Comments