fract_sFractals_alert
Download scriptThis script is designed to identify specific price patterns known as "Fractals" on a financial trading chart. In technical analysis, fractals represent points where the market shifts direction—specifically, high peaks and low valleys that stand out relative to the candles surrounding them.
Core Logic and Functionality
1. Peak and Valley Identification The script constantly scans historical price data to locate two types of structural points:
- Upper Fractals (Peaks): These are identified when a specific candle's high price is higher than the highs of the candles immediately preceding it and following it.
- Lower Fractals (Valleys): These are identified when a specific candle's low price is lower than the lows of the candles immediately surrounding it.
2. Multi-Scale Pattern Recognition Unlike standard fractal indicators that only look at a small window of bars, this script features an expanded detection logic. It checks for several different "widths" of price structures:
- Standard Patterns: It looks for basic 5-bar formations where the center point is the highest or lowest.
- Extended Patterns: If a standard pattern isn't found, the script expands its search to recognize much wider structures spanning up to 9 bars in length. This allows it to identify significant "swings" in the market that might be overlooked by simpler tools.
3. Signal Processing and Alerts Once the script identifies these structural peaks and valleys, it uses a specific logic gate to provide actionable information:
- Signal Delay: The script is programmed to look at a specific number of bars back (set as a signal bar) to ensure the "fractal" has actually formed and been confirmed by the market before reacting.
- Directional Alerts:
- If it detects a Lower Fractal (a valley), it interprets this as a potential upward momentum shift and triggers a BUY alert.
- If it detects an Upper Fractal (a peak), it interprets this as a potential downward momentum shift and triggers a SELL alert.
- State Tracking: The script maintains an internal memory of the last signal sent. This prevents the system from spamming multiple alerts for the same price movement, ensuring it only notifies you when the market structure actually flips from one trend to another.
Summary for the User
Essentially, this tool acts as an automated "scout." It monitors the history of the chart to find significant turning points in price action, regardless of how wide those turns are. When it finds a confirmed valley, it alerts you to a potential buying opportunity; when it finds a confirmed peak, it alerts you to a potential selling opportunity.
//+------------------------------------------------------------------+
//| sFractals.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- buffers
double ExtUpFractalsBuffer[];
double ExtDownFractalsBuffer[];
// Íîìåð áàðà, ïî êîòîðîìó áóäåò èñêàòüñÿ ñèãíàë
#define SIGNAL_BAR 2
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator buffers mapping
SetIndexBuffer(0, ExtUpFractalsBuffer);
SetIndexBuffer(1, ExtDownFractalsBuffer);
//---- drawing settings
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 119);
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 119);
//----
SetIndexEmptyValue(0, 0.0);
SetIndexEmptyValue(1, 0.0);
//---- name for DataWindow
SetIndexLabel(0, "sFractal Up");
SetIndexLabel(1, "sFractal Down");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i, nCountedBars;
bool bFound;
double dCurrent;
nCountedBars = IndicatorCounted();
//---- last counted bar will be recounted
if(nCountedBars <= 2)
i = Bars - nCountedBars - 3;
if(nCountedBars > 2)
{
nCountedBars--;
i = Bars - nCountedBars - 1;
}
//----Up and Down Fractals
while(i >= 2)
{
//----Fractals up
bFound = false;
dCurrent = High[i];
if(dCurrent > High[i+1] && dCurrent > High[i+2] && dCurrent > High[i-1] &&
dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
//----6 bars Fractal
if(!bFound && (Bars - i - 1) >= 3)
{
if(dCurrent == High[i+1] && dCurrent > High[i+2] && dCurrent > High[i+3] &&
dCurrent > High[i-1] && dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
}
//----7 bars Fractal
if(!bFound && (Bars - i - 1) >= 4)
{
if(dCurrent >= High[i+1] && dCurrent == High[i+2] && dCurrent > High[i+3] &&
dCurrent > High[i+4] && dCurrent > High[i-1] && dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
}
//----8 bars Fractal
if(!bFound && (Bars - i - 1) >= 5)
{
if(dCurrent >= High[i+1] && dCurrent == High[i+2] && dCurrent == High[i+3] &&
dCurrent > High[i+4] && dCurrent > High[i+5] && dCurrent > High[i-1] &&
dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
}
//----9 bars Fractal
if(!bFound && (Bars - i - 1) >= 6)
{
if(dCurrent >= High[i+1] && dCurrent == High[i+2] && dCurrent >= High[i+3] &&
dCurrent == High[i+4] && dCurrent > High[i+5] && dCurrent > High[i+6] &&
dCurrent > High[i-1] && dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
}
//----Fractals down
bFound = false;
dCurrent = Low[i];
if(dCurrent < Low[i+1] && dCurrent < Low[i+2] && dCurrent < Low[i-1] &&
dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
//----6 bars Fractal
if(!bFound && (Bars - i - 1) >= 3)
{
if(dCurrent == Low[i+1] && dCurrent < Low[i+2] && dCurrent < Low[i+3] &&
dCurrent < Low[i-1] && dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
}
//----7 bars Fractal
if(!bFound && (Bars - i - 1) >= 4)
{
if(dCurrent <= Low[i+1] && dCurrent == Low[i+2] && dCurrent < Low[i+3] &&
dCurrent < Low[i+4] &&
dCurrent < Low[i-1] && dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
}
//----8 bars Fractal
if(!bFound && (Bars - i - 1) >= 5)
{
if(dCurrent <= Low[i+1] && dCurrent == Low[i+2] && dCurrent==Low[i+3] &&
dCurrent < Low[i+4] && dCurrent < Low[i+5] && dCurrent < Low[i-1] &&
dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
}
//----9 bars Fractal
if(!bFound && (Bars - i- 1) >= 6)
{
if(dCurrent <= Low[i+1] && dCurrent == Low[i+2] && dCurrent <= Low[i+3] &&
dCurrent == Low[i+4] && dCurrent < Low[i+5] && dCurrent < Low[i+6] &&
dCurrent < Low[i-1] && dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
}
i--;
}
// Ñòàòè÷åñêèå ïåðåìåííûå, â êîòîðûõ õðàíÿòñÿ âðåìÿ ïîñëåäíåãî áàðà è íàïðàâëåíèå
// ïîñëåäíåãî ñèãíàëà
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(ExtDownFractalsBuffer[SIGNAL_BAR] > 0)
{
PrevSignal = 1;
Alert("sFractals (", Symbol(), ", ", Period(), ") - BUY!!!");
}
}
if(PrevSignal >= 0)
{
if(ExtUpFractalsBuffer[SIGNAL_BAR] > 0)
{
PrevSignal = -1;
Alert("sFractals (", Symbol(), ", ", Period(), ") - SELL!!!");
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments