MTF iFractals Support Resistance Indicator help - page 2

 

I could not get your version to work...so I decided to continue with the original... Basically I get the data in the inputed TF, copy it to the buffer.. and then collect only over the most recent TF candle.

I followed the tutorial on MTF but I could not get this to work... I think I have an error on the last loop... can an expert given me a hand? I am stuck with this... thanks...

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   2
//--- the FractalUp plot
#property indicator_label1  "FractalUp"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
//--- the FractalDown plot
#property indicator_label2  "FractalDown"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed


input ENUM_TIMEFRAMES      period=PERIOD_M30;   

int    handle;
int    bars_calculated=0;

double         FractalUpBuffer[];
double         FractalDownBuffer[];
int            Multiplier;

int OnInit() {
    Multiplier= PeriodSeconds(period)/PeriodSeconds(Period());  

   SetIndexBuffer(0,FractalUpBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,FractalDownBuffer,INDICATOR_DATA);
   PlotIndexSetInteger(0,PLOT_ARROW,217); // arrow up
   PlotIndexSetInteger(1,PLOT_ARROW,218); // arrow down
   
   
ArraySetAsSeries(FractalUpBuffer,true);
ArraySetAsSeries(FractalDownBuffer,true);

handle=iFractals(_Symbol,period);

   return(INIT_SUCCEEDED);
}

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 values_to_copy;
//--- determine the number of values calculated in the indicator
   int calculated=BarsCalculated(handle);
   if(calculated<=0) {GetLastError();return(0); }

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) {
        if(calculated>rates_total) values_to_copy=rates_total;
      else                       values_to_copy=calculated;
   } else {
       values_to_copy=(rates_total-prev_calculated)+1;
   }

values_to_copy=values_to_copy*Multiplier ;
if(values_to_copy<=0) {GetLastError();return(0); }
   
double UpBuffer[]; 
double DownBuffer[]; 
ArraySetAsSeries(UpBuffer,true);
ArraySetAsSeries(DownBuffer,true);
 
   if(!CopyBuffer(handle,         0,0   ,  values_to_copy,      UpBuffer)) return(0); 
   if(!CopyBuffer(handle,         1,0   ,  values_to_copy,     DownBuffer)) return(0); 
      
      for(int i=0; i<values_to_copy && !IsStopped(); i++)
     {

      if(i-values_to_copy-1 <0 ) continue; //by pass first part of array...aovoid error due to out of range array.
       
      double Up=UpBuffer[i];
      double Down=DownBuffer[i];
     if( Up == EMPTY_VALUE)
            {
            FractalUpBuffer[i]=FractalUpBuffer[i-1];
            } else{
            FractalUpBuffer[i]=Up;
            }
     if( Down == EMPTY_VALUE)
            {
            FractalDownBuffer[i]=FractalDownBuffer[i-1];
            } else{
            FractalDownBuffer[i]=Down;
            }
      } 
      
         
       
         

   return(rates_total);
}

void OnDeinit(const int reason) {
   if(handle!=INVALID_HANDLE)
      IndicatorRelease(handle);
//--- clear the chart after deleting the indicator
   Comment("");
}
 

I decided to hire a freelancer to make this code MTF...here it is for those interested... credit to the developer..

//+------------------------------------------------------------------+
//|                                           GGP Fractal SR MTF.mq5 |
//|           Copyright 2024, SaeEd Abbasi Programming-Trading Group |
//|                       https://www.mql5.com/en/users/abctrading43 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, SaeEd Abbasi Programming-Trading Group"
#property link      "https://www.mql5.com/en/users/abctrading43"
#property description "https://www.mql5.com/en/users/abctrading43"
#property description "For ordering any custom Expert, Script or Indicator send a direct message to our Instagram page."
#property description "         @ProTrading43"
#property description "or send us an Email at:"
#property description "         ProTrading43@gmail.com"
#property version   "1.20"
#property strict

// Indicator Buffer Settings
#property indicator_chart_window
#property indicator_buffers   4                 // How many data buffers are we using
#property indicator_plots     2                 // How many indicators are being drawn on screen

#property indicator_label1    "Support"
#property indicator_type1     DRAW_ARROW
#property indicator_style1    STYLE_SOLID
#property indicator_color1    clrMagenta
#property indicator_width1    1

#property indicator_label2    "Resistance"
#property indicator_type2     DRAW_ARROW
#property indicator_style2    STYLE_SOLID
#property indicator_color2    clrLime
#property indicator_width2    1

// Indicator Input Parameters
input ENUM_TIMEFRAMES      TimeFrame                  =  PERIOD_CURRENT;                     // Indicator Timeframe

// Indicator Variables
string   IndName              =  "GGP S/R MTF";
int      MaxPeriod            =  10;

// Indicator Buffers
double   Support[];
double   Resistance[];

// Sub indicator buffers and handles 
double   FractalUpBuffer[];            // Dynamic arrays to hold the Fractal up values for each bars
double   FractalDownBuffer[];          // Dynamic arrays to hold the Fractal down values for each bars
int      FractalHandle;                // Handle for Fractal Indicator

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Set indicator name
   IndicatorSetString(INDICATOR_SHORTNAME, IndName);

   // Set indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS, (int) SymbolInfoInteger(Symbol(), SYMBOL_DIGITS));

   // Indicator buffers mapping
   int   bufferIndex    =  0;
   SetIndexBuffer(bufferIndex, Support, INDICATOR_DATA);
   PlotIndexSetInteger(bufferIndex, PLOT_DRAW_BEGIN, MaxPeriod);
   PlotIndexSetInteger(bufferIndex, PLOT_ARROW, 159);

   bufferIndex++;
   SetIndexBuffer(bufferIndex, Resistance, INDICATOR_DATA);
   PlotIndexSetInteger(bufferIndex, PLOT_DRAW_BEGIN, MaxPeriod);
   PlotIndexSetInteger(bufferIndex, PLOT_ARROW, 159);

   // Preparing Buffers
   ArraySetAsSeries(Support, true);
   ArraySetAsSeries(Resistance, true);

   ArrayInitialize(Support, EMPTY_VALUE);
   ArrayInitialize(Resistance, EMPTY_VALUE);

   // Get handle for Fractal indicator
   FractalHandle     =  iFractals(_Symbol, TimeFrame);
   // What if handle returns Invalid Handle
   if(FractalHandle < 0)
      {
       Alert("Error Creating Handles for Fractals indicator - error: ", GetLastError(), "!!");
       return(-1);
      }

   // Additional Buffers for Calculations
   bufferIndex++;
   SetIndexBuffer(bufferIndex, FractalUpBuffer, INDICATOR_DATA);

   bufferIndex++;
   SetIndexBuffer(bufferIndex, FractalDownBuffer, INDICATOR_DATA);

   // Preparing sub indicator buffers
   ArraySetAsSeries(FractalUpBuffer, true);
   ArraySetAsSeries(FractalDownBuffer, true);

   ArrayInitialize(FractalUpBuffer, EMPTY_VALUE);
   ArrayInitialize(FractalDownBuffer, EMPTY_VALUE);

//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(FractalHandle);
   ChartRedraw(0);   
  }

//+------------------------------------------------------------------+
//| 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[])
  {
   // Do calculations on new bar only
   if(rates_total == prev_calculated)
      return(rates_total);

   // Checks the forced shutdown of the MetaTrader program
   if(IsStopped())
      return(0);

   // Check that if enough bars available to calculate values
   if(rates_total < MaxPeriod)
      return(0);

   // Check all the indicators bars have been calculated
   if(BarsCalculated(FractalHandle) == -1)
      return(0);

   // Finding how many bars needed to be calculated each time in order to increase the indicator speed
   int Limit = 0;
   if(prev_calculated > rates_total || prev_calculated <= 0)
     {
      Limit = rates_total- MaxPeriod- 1;

      ArrayResize(Support, rates_total);
      ArrayResize(Resistance, rates_total);
      ArrayResize(FractalUpBuffer, rates_total);
      ArrayResize(FractalDownBuffer, rates_total);
      
      ArrayInitialize(Support, EMPTY_VALUE);
      ArrayInitialize(Resistance, EMPTY_VALUE);
      ArrayInitialize(FractalUpBuffer, EMPTY_VALUE);
      ArrayInitialize(FractalDownBuffer, EMPTY_VALUE);
     }

   else
     {
      Limit = rates_total- prev_calculated- MaxPeriod- 1;
      
      // Making the indicator to calculate for the last bar
      if(Limit <= 1)
         Limit = 10;
     }

   // Indicator Calculations
   for(int i=Limit; i>=0; i--)
     {
      datetime    barTime  =  iTime(_Symbol, _Period, i);
      int         barMTF   =  iBarShift(_Symbol, TimeFrame, barTime, false);

      if(ShiftUpFractal(barMTF) > 0 && ShiftUpFractal(barMTF) != EMPTY_VALUE)
         Resistance[i]     =  ShiftUpFractal(barMTF);
      else
         Resistance[i]     =  Resistance[i+1];
      
      if(ShiftDownFractal(barMTF) > 0 && ShiftDownFractal(barMTF) != EMPTY_VALUE)
         Support[i]        =  ShiftDownFractal(barMTF);
      else
         Support[i]        =  Support[i+1];
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }

//+------------------------------------------------------------------+
//| Up Fractal Values                                                |
//+------------------------------------------------------------------+
double ShiftUpFractal(int bar)
   {
    // Get Fractal Data from Handle
    int upCount      =  CopyBuffer(FractalHandle, 0, bar, 1, FractalUpBuffer);
    if(upCount < 0)
      {
       int upErr     =  GetLastError();
       Print("Reading data from up Fractal indicator handle failed!! Error: ", upErr);
       return(-1);
      }
   
    return(FractalUpBuffer[bar]);    
   }

//+------------------------------------------------------------------+
//| Down Fractal Values                                              |
//+------------------------------------------------------------------+
double ShiftDownFractal(int bar)
   {
    // Get Fractal Data from Handle
    int downCount    =  CopyBuffer(FractalHandle, 1, bar, 1, FractalDownBuffer);
    if(downCount < 0)
      {
       int downErr   =  GetLastError();
       Print("Reading data from low Fractal indicator handle failed!! Error: ", downErr);
       return(-1);
      }
   
    return(FractalDownBuffer[bar]);    
   }
 

WE encountered a problem with the code above regarding some 4806 error... so in the meantime I figure out an alternative version that seems to work... want it to share it...in case needed in the future...

#property copyright "Copyright 2000-2023, 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_LINE
#property indicator_type2   DRAW_LINE
#property indicator_color1  Blue
#property indicator_color2  Red
#property indicator_label1  "Fractal Up"
#property indicator_label2  "Fractal Down"
//--- input parameters
input  ENUM_TIMEFRAMES TimeFrame = PERIOD_M5;
//--- indicator buffers
double ExtUpperBuffer[];
double ExtLowerBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0, ExtUpperBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, ExtLowerBuffer, INDICATOR_DATA);
   ArraySetAsSeries(ExtUpperBuffer, true);
   ArraySetAsSeries(ExtLowerBuffer, true);
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);


}
//+------------------------------------------------------------------+
//|  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);

   ArraySetAsSeries(time, true);

   int start;
//--- clean up arrays
   if(prev_calculated < 7)
   {
      start = rates_total - 3;
      ArrayInitialize(ExtUpperBuffer, EMPTY_VALUE);
      ArrayInitialize(ExtLowerBuffer, EMPTY_VALUE);
   }
   else
      start = 5;
//--- main cycle of calculations
   for(int i = start; i >= 3 && !IsStopped(); i--)
   {
      int y = iBarShift(_Symbol, TimeFrame, time[i]);

      //--- Upper Fractal
      if(iHigh(_Symbol, TimeFrame, y) > iHigh(_Symbol, TimeFrame, y + 1) && iHigh(_Symbol, TimeFrame, y) > iHigh(_Symbol, TimeFrame, y + 2) && iHigh(_Symbol, TimeFrame, y) >= iHigh(_Symbol, TimeFrame, y - 1) && iHigh(_Symbol, TimeFrame, y) >= iHigh(_Symbol, TimeFrame, y - 2))
         ExtUpperBuffer[i] = iHigh(_Symbol, TimeFrame, y);
      else
         ExtUpperBuffer[i] = ExtUpperBuffer[i+1];
             ExtUpperBuffer[2] = ExtUpperBuffer[i+1];
             ExtUpperBuffer[1] = ExtUpperBuffer[i+1];
             ExtUpperBuffer[0] = ExtUpperBuffer[i+1];

      //--- Lower Fractal
      if(iLow(_Symbol, TimeFrame, y) < iLow(_Symbol, TimeFrame, y + 1) && iLow(_Symbol, TimeFrame, y) < iLow(_Symbol, TimeFrame, y + 2) && iLow(_Symbol, TimeFrame, y) <= iLow(_Symbol, TimeFrame, y - 1) && iLow(_Symbol, TimeFrame, y) <= iLow(_Symbol, TimeFrame, y - 2))
         ExtLowerBuffer[i] = iLow(_Symbol, TimeFrame, y);
      else
         ExtLowerBuffer[i] = ExtLowerBuffer[i+1];
             ExtLowerBuffer[2] = ExtLowerBuffer[i+1];
             ExtLowerBuffer[1] = ExtLowerBuffer[i+1];
             ExtLowerBuffer[0] = ExtLowerBuffer[i+1];
   }
   

    

      
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
}
 

ok, this is the last version...seems to work well...credit to the freelancer...

//+------------------------------------------------------------------+
//|                                       GGP Fractal SR MTF New.mq5 |
//|           Copyright 2023, SaeEd Abbasi Programming-Trading Group |
//|                       https://www.mql5.com/en/users/abctrading43 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, SaeEd Abbasi Programming-Trading Group"
#property link      "https://www.mql5.com/en/users/abctrading43"
#property description "https://www.mql5.com/en/users/abctrading43"
#property description "For ordering any custom Expert, Script or Indicator send a direct message to our Instagram page."
#property description "         @ProTrading43"
#property description "or send us an Email at:"
#property description "         ProTrading43@gmail.com"
#property version   "3.10"
#property strict

// Indicator Buffer Settings
#property indicator_chart_window
#property indicator_buffers   4                 // How many data buffers are we using
#property indicator_plots     2                 // How many indicators are being drawn on screen

#property indicator_label1    "Support"
#property indicator_type1     DRAW_ARROW
#property indicator_style1    STYLE_SOLID
#property indicator_color1    clrMagenta
#property indicator_width1    1

#property indicator_label2    "Resistance"
#property indicator_type2     DRAW_ARROW
#property indicator_style2    STYLE_SOLID
#property indicator_color2    clrLime
#property indicator_width2    1

// Indicator Input Parameters
input ENUM_TIMEFRAMES      TimeFrame                  =  PERIOD_CURRENT;                     // Indicator Timeframe

// Indicator Variables
string   IndName              =  "GGP S/R MTF";
int      ShiftPeriod;

// Indicator Buffers
double   Support[];
double   Resistance[];

// Sub indicator buffers and handles 
double   FractalUpBuffer[];            // Dynamic arrays to hold the Fractal up values for each bars
double   FractalDownBuffer[];          // Dynamic arrays to hold the Fractal down values for each bars
int      FractalHandle;                // Handle for Fractal Indicator

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Check the TimeFrame and period
   if(PeriodSeconds(TimeFrame) < PeriodSeconds(Period()))
     {
      PrintFormat("You must select a timeframe higher than the current chart.");
      return(INIT_PARAMETERS_INCORRECT);
     }
   
   ShiftPeriod          =  (int)(PeriodSeconds(TimeFrame)/ PeriodSeconds(Period()));

   // Set indicator name
   IndicatorSetString(INDICATOR_SHORTNAME, IndName);

   // Set indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS, (int) SymbolInfoInteger(Symbol(), SYMBOL_DIGITS));

   // Indicator buffers mapping
   int   bufferIndex    =  0;
   SetIndexBuffer(bufferIndex, Support, INDICATOR_DATA);
   PlotIndexSetInteger(bufferIndex, PLOT_DRAW_BEGIN, ShiftPeriod);
   PlotIndexSetInteger(bufferIndex, PLOT_ARROW, 159);

   bufferIndex++;
   SetIndexBuffer(bufferIndex, Resistance, INDICATOR_DATA);
   PlotIndexSetInteger(bufferIndex, PLOT_DRAW_BEGIN, ShiftPeriod);
   PlotIndexSetInteger(bufferIndex, PLOT_ARROW, 159);

   // Preparing Buffers
   ArraySetAsSeries(Support, true);
   ArraySetAsSeries(Resistance, true);

   ArrayInitialize(Support, EMPTY_VALUE);
   ArrayInitialize(Resistance, EMPTY_VALUE);

   // Get handle for Fractal indicator
   FractalHandle     =  iFractals(_Symbol, TimeFrame);
   // What if handle returns Invalid Handle
   if(FractalHandle < 0)
      {
       Alert("Error Creating Handles for Fractals indicator - error: ", GetLastError(), "!!");
       return(-1);
      }

   // Additional Buffers for Calculations
   bufferIndex++;
   SetIndexBuffer(bufferIndex, FractalUpBuffer, INDICATOR_DATA);

   bufferIndex++;
   SetIndexBuffer(bufferIndex, FractalDownBuffer, INDICATOR_DATA);

   // Preparing sub indicator buffers
   ArraySetAsSeries(FractalUpBuffer, true);
   ArraySetAsSeries(FractalDownBuffer, true);

   ArrayInitialize(FractalUpBuffer, EMPTY_VALUE);
   ArrayInitialize(FractalDownBuffer, EMPTY_VALUE);
      
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(FractalHandle);
   ChartRedraw(0);   
  }

//+------------------------------------------------------------------+
//| 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[])
  {
   // Do calculations on new bar only
   if(rates_total == prev_calculated)
      return(rates_total);

   // Check that if enough bars available to calculate values
   if(rates_total < ShiftPeriod)
      return(0);

   // Checks the forced shutdown of the MetaTrader program
   if(IsStopped())
      return(0);

   // Check all the indicators bars have been calculated
   if(BarsCalculated(FractalHandle) == -1)
      return(0);

   // Finding how many bars needed to be copied to buffer in order to increase the indicator speed
   int         CopyBars    =  0;
   static int  waitCount   =  10;

   if(prev_calculated > rates_total || prev_calculated <= 0)
     {
      // Force the terminal to read the data
      datetime    t           =  iTime(_Symbol, TimeFrame, 0);
      int         error       =  GetLastError();
      if(t == 0)
        {
         waitCount--;
         PrintFormat("Waiting for data. %i", error);
         return(prev_calculated);
        }

      CopyBars       =  rates_total- ShiftPeriod;
            
      ArrayInitialize(Support, EMPTY_VALUE);
      ArrayInitialize(Resistance, EMPTY_VALUE);
      ArrayInitialize(FractalUpBuffer, EMPTY_VALUE);
      ArrayInitialize(FractalDownBuffer, EMPTY_VALUE);
     }

   else
      CopyBars       =  ShiftPeriod;

   // Checks the forced shutdown of the MetaTrader program
   if(IsStopped())
      return(0);

   // Get up Fractal Data from Handle
   int upCount       =  CopyBuffer(FractalHandle, 0, 0, CopyBars, FractalUpBuffer);
   if(upCount < 0)
     {
      int upErr      =  GetLastError();
      Print("Reading data from up Fractal indicator handle failed!! Error: ", upErr);
      return(0);
     }

    // Get low Fractal Data from Handle
    int downCount    =  CopyBuffer(FractalHandle, 1, 0, CopyBars, FractalDownBuffer);
    if(downCount < 0)
      {
       int downErr   =  GetLastError();
       Print("Reading data from low Fractal indicator handle failed!! Error: ", downErr);
       return(0);
      }

   // Making the indicator to calculate for the last cycle
   int   Limit          =  CopyBars- ShiftPeriod;
   
   if(Period() == TimeFrame)
     {
      Limit =  1000;
     }
   if(Limit <= 0)
      return(0);

   // Indicator Calculations
   for(int i=Limit-1; i>=0; i--)
     {
      datetime    barTime  =  iTime(_Symbol, _Period, i);
      int         barMTF   =  iBarShift(_Symbol, TimeFrame, barTime, false);
      
      if(barMTF+1 >= Limit-1)
         continue;

      Resistance[i]  =  Resistance[i+1];
      if(FractalUpBuffer[barMTF+1] > 0 && FractalUpBuffer[barMTF+1] != EMPTY_VALUE)
        {
         Resistance[i]  =  FractalUpBuffer[barMTF+1];
         int inCounter  =  1;
         while(inCounter <= ShiftPeriod)
           {
            if(Resistance[i+inCounter] != FractalUpBuffer[barMTF+1])
               Resistance[i+inCounter]    =  FractalUpBuffer[barMTF+1];
            inCounter++;
           }
        }

      Support[i]  =  Support[i+1];
      if(FractalDownBuffer[barMTF+1] > 0 && FractalDownBuffer[barMTF+1] != EMPTY_VALUE)
        {
         Support[i]  =  FractalDownBuffer[barMTF+1];
         int inCounter  =  1;
         while(inCounter <= ShiftPeriod)
           {
            if(Support[i+inCounter] != FractalDownBuffer[barMTF+1])
               Support[i+inCounter]    =  FractalDownBuffer[barMTF+1];
            inCounter++;
           }
        }
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }