mql-for-begginers/Indicators/Examples/Fractals.mq5
2025-07-22 18:30:17 +03:00

88 lines
3.3 KiB
MQL5

//+------------------------------------------------------------------+
//| Fractals.mq5 |
//| Copyright 2000-2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_ARROW
#property indicator_type2 DRAW_ARROW
#property indicator_color1 clrGray
#property indicator_color2 clrGray
#property indicator_label1 "Fractal Up"
#property indicator_label2 "Fractal Down"
//--- indicator buffers
double ExtUpperBuffer[];
double ExtLowerBuffer[];
//--- 10 pixels upper from high price
int ExtArrowShift=-10;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,ExtUpperBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ExtLowerBuffer,INDICATOR_DATA);
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
PlotIndexSetInteger(0,PLOT_ARROW,217);
PlotIndexSetInteger(1,PLOT_ARROW,218);
//--- arrow shifts when drawing
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,ExtArrowShift);
PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-ExtArrowShift);
//--- sets drawing line empty value--
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
}
//+------------------------------------------------------------------+
//| Fractals on 5 bars |
//+------------------------------------------------------------------+
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[])
{
if(rates_total<5)
return(0);
int start;
//--- clean up arrays
if(prev_calculated<7)
{
start=2;
ArrayInitialize(ExtUpperBuffer,EMPTY_VALUE);
ArrayInitialize(ExtLowerBuffer,EMPTY_VALUE);
}
else
start=rates_total-5;
//--- main cycle of calculations
for(int i=start; i<rates_total-3 && !IsStopped(); i++)
{
//--- Upper Fractal
if(high[i]>high[i+1] && high[i]>high[i+2] && high[i]>=high[i-1] && high[i]>=high[i-2])
ExtUpperBuffer[i]=high[i];
else
ExtUpperBuffer[i]=EMPTY_VALUE;
//--- Lower Fractal
if(low[i]<low[i+1] && low[i]<low[i+2] && low[i]<=low[i-1] && low[i]<=low[i-2])
ExtLowerBuffer[i]=low[i];
else
ExtLowerBuffer[i]=EMPTY_VALUE;
}
//--- OnCalculate done. Return new prev_calculated.
return(rates_total);
}
//+------------------------------------------------------------------+