RFractals
Download scriptThis tool is designed to identify specific turning points in market price action by detecting "Fractals." In trading, these represent local peaks (highs) and valleys (lows) where the price momentarily reverses direction. The script simplifies the visual complexity of a chart by highlighting these key structural points automatically.
How the Logic Works
The indicator scans historical price data to find specific price candles that stand out relative to their neighbors. It looks for two distinct patterns:
1. Identifying Price Peaks (Fractals Up) To confirm a "high" point, the script checks a designated range of candles surrounding a central point. For a peak to be marked:
- The high price of the center candle must be higher than the candles immediately adjacent to it.
- The script then validates this by ensuring the center candle's high is also greater than the prices at the outer boundaries of the chosen range.
- If these conditions are met, a visual marker appears above that specific high point.
2. Identifying Price Valleys (Fractals Down) Similarly, to confirm a "low" point, the script evaluates the price floor:
- The low price of the center candle must be lower than the candles immediately adjacent to it.
- It then verifies that this central low is also lower than the prices at the edges of the defined range.
- If confirmed, a visual marker appears below that specific low point.
Key Functional Features
- Dynamic Range Adjustment: The logic includes a built-in safety check for the "Range" setting. It ensures that the search area is large enough to be statistically relevant (requiring at least 3 candles) and automatically adjusts the calculation to ensure the range remains consistent with the required symmetry.
- Symmetrical Validation: Rather than just looking at the candle immediately next to a peak, the script performs a "loop" check. It verifies every single candle within the user-defined range to ensure the identified point is truly the highest (or lowest) in that specific neighborhood.
- Visual Representation: The script translates these mathematical confirmations into clear visual cues on your chart—Blue markers for upward price peaks and Red markers for downward price valleys—allowing you to see market structure at a glance without having to manually measure price movements.
//+------------------------------------------------------------------+
//| RFractals.mq4 |
//| Lizhniyk E |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Lizhniyk E"
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_width1 1
#property indicator_width2 1
#property indicator_color1 Blue
#property indicator_color2 Red
//---- input parameters
//extern int rpoint=150;
extern int range_fractal=5; //ðàíã ôðàêòàëà (íå÷¸òíûå ÷èñëà íå ìåíåå 3)
//---- buffers
double Ext1[];
double Ext2[];
int center=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
if(range_fractal % 2 == 0) range_fractal++;
if(range_fractal<3) range_fractal=3;
center=range_fractal/2 + 1 ;
//scenter=range_fractal/2;
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,217);
SetIndexBuffer(0,Ext1);
SetIndexEmptyValue(0,0.0);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,218);
SetIndexBuffer(1,Ext2);
SetIndexEmptyValue(1,0.0);
SetIndexLabel(0,"Fractal Up");
SetIndexLabel(1,"Fractal Down");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
double cur=0;
bool found=false;
int start()
{
int counted_bars=IndicatorCounted();
//----
for(int i=Bars-counted_bars;i>=0;i--)
{
//*******create history******
//*******fractal up**********
found=false;
cur=High[i+center];
if(cur>High[i+1] && cur>High[i+range_fractal]) found=true;
else found=false;
if(found)
{
for(int j=1;j<center;j++)
{
if(cur>=High[i+center-j] && cur>=High[i+center+j]) found=true;
else {found=false; break;}
}
}
if(found) {Ext1[i+center]=cur; Ext1[i+center+1]=0;}
//*************fractal down*************
found=false;
cur=Low[i+center];
if(cur<Low[i+1] && cur<Low[i+range_fractal]) found=true;
else found=false;
if(found)
{
for(int k=1;k<center;k++)
{
if(cur<=Low[i+center-k] && cur<=Low[i+center+k]) found=true;
else {found=false; break;}
}
}
if(found) {Ext2[i+center]=cur; Ext2[i+center+1]=0;}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments