Help with script to find Support and Resistance in MQL5

 

Hello Everybody, I am still learning when it comes to mql5. I'm trying to code a script that will find support and resistance levels. I'm having problems populating arrays that will hold high values for resistance and low values for support as well as sorting them in ascending order to merge values which are close to each other.

//+------------------------------------------------------------------+
//|                                                          S&R.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

int   Candles = 250;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   MqlRates    rates[];
   double   support[];
   double   resistance[];

   ArraySetAsSeries(rates,true);
   CopyRates(_Symbol,_Period,0,Candles,rates);

   for(int i=5; i<Candles; i++)
     {
      Support(i,rates,support);
      Resistance(i,rates,resistance);
     } // for


  }
//+------------------------------------------------------------------+
//|                         Support                                  |
//+------------------------------------------------------------------+
void Support(int i, MqlRates& price[], double& st[])
  {
   int j=0;
   if(price[i+1].close<price[i+1].open && price[i+2].close<price[i+2].open &&
      price[i+1].low<price[i+2].low &&
      price[i].close>price[i].open && price[i-1].close>price[i-1].open &&
      price[i].low<price[i-1].low)
     {
      ArrayResize(st,j+1);
      st[j] = price[i].low;
      j++;
     }
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                         Resistance                               |
//+------------------------------------------------------------------+
void Resistance(int i, MqlRates& price[], double& rt[])
  {
   int j=0;
   if(price[i+1].close>price[i+1].open && price[i+2].close>price[i+2].open &&
      price[i+1].high>price[i+2].high &&
      price[i].close<price[i].open && price[i-1].close<price[i-1].open &&
      price[i].high>price[i-1].high)
     {
      ArrayResize(rt,j+1);
      rt[j] = price[i].high;
      j++;
     }
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                             Sort                                 |
//+------------------------------------------------------------------+
void  Sort(double& array[])
  {
   for(int i=0; i<ArraySize(array)-1; i++)
     {
      for(int j=i+1; j<ArraySize(array); j++)
        {
         if(array[j]<array[i])
           {
            double temp = array[j];
            array[j] = array[i];
            array[i] = temp;
           }
        }
     }

  }

//+------------------------------------------------------------------+

I tried to print the vales of the arrays, It works when It's inside the for loop but doesn't when the print function is outside the for loop, the sort function doesn't seem to be doing it's job but when I create a static array with random values, the sort function works on that array.

 
Really ? nobody ?
 
You have a problem with local and global variables: https://www.mql5.com/en/articles/2744
MQL5 Programming Basics: Global Variables of the MetaTrader 5 Terminal
MQL5 Programming Basics: Global Variables of the MetaTrader 5 Terminal
  • www.mql5.com
Global variables of the terminal provide an indispensable tool for developing sophisticated and reliable Expert Advisors. If you master the global variables, you will no more be able to imagine developing EAs on MQL5 without them.
Reason: