Array out or range

 

Hello,

I am new to programming and I am trying to learn. I get an array out of range error, can someone please tell me what I am doing wrong.

#property copyright "Copyright 2020, YarnnyArmy"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


int LowerFrac_1;

int LowerFrac1[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   ArrayInitialize(LowerFrac1,EMPTY_VALUE);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   for (int n=Bars-2; n>2; n--)
      {
     
         //if statement to determine a Fractal
         if (Low[n-2] > Low[n-1] && Low[n-1] > Low[n] && Low[n] < Low[n+1] && Low[n+1] < Low[n+2])
           { 
               LowerFrac_1 = n;
               LowerFrac1[n] = LowerFrac_1;
            
           }
   
      }
  }
//+------------------------------------------------------------------+
 
yarnnyarmy:can someone please tell me what I am doing wrong.
int LowerFrac1[];

Trying to store into an array that has no size.

 
William Roeder:

Trying to store into an array that has no size.

Thanks, can you give me a starting point? I am trying to make the array dynamic, but I am stuck.
 

You need to resize your dynamic array before you start doing something with it.

https://www.mql5.com/en/docs/array/arrayresize

Documentation on MQL5: Array Functions / ArrayResize
Documentation on MQL5: Array Functions / ArrayResize
  • www.mql5.com
If ArrayResize() is applied to a static array, a timeseries or an indicator buffer, the array size remains the same – these arrays will not be reallocated. In this case, if The function can be applied only to dynamic arrays. It should be noted that you cannot change the size of dynamic arrays assigned as indicator buffers by the...
 
yarnnyarmy:
Thanks, can you give me a starting point? I am trying to make the array dynamic, but I am stuck.

Welcome .

Here you go : save it in indicators . Try to do the same for the highs for training 

cheers

//+------------------------------------------------------------------+
//|                                                         Lowz.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//declare number of buffers that will be used
#property indicator_buffers 1
#property indicator_color1 clrBlue
#property indicator_type1 DRAW_ARROW
#property indicator_width1 3
input int BarsForFormation=3;//Bars for formation
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
/*
   in mql4 indicators you can assign one double array to a buffer and this way 
   it will automatically be associated with the data handling system of MT .In indicators only
*/
//declare that as you did
double LF[];
datetime barstamp;
int barsReduction=0;
int OnInit()
  {
//--- indicator buffers mapping
  //start of program ,tether your array to the system buffers 
    //literal assignment : buffer 1 is accesed at 0 
    SetIndexBuffer(0,LF);
    //empty value 
    SetIndexEmptyValue(0,0);
    SetIndexArrow(0,222);//arrowcode for marking your condition on the chart
    /*
    bars access 
    First and most important 
    bar[0] (ex : Low[0]) is the live bar , the most recent (the now)
    bar[1] (ex : Low[1]) is the bar immediately to the left (past) (before in time) of the live bar 
    etc
    bar[100] (ex : Low[100]) is 100 bars in the past 
    */
    /*
    barsReduction is for data access safety
    for example if the chart has 100 bars 
    and we want the lowest point among 3 bars to the left and 3 to the right 
    we can really start reading from bar 96 as there is no bar 100 (and there is no literal bar 100 either)
    if the chart has 100 bars and we start counting from 0 the 100th bar is bar[99] (ex : Low[99])
    in this indicator example you just assign a barsReduction of the Bars you want for the formation of the lows + 1
    in more complex indicators you will assing this value to the maximum period utilized + 1 
    */
    barsReduction=BarsForFormation+1;
    /*
    barstamp will be used to perform checks for a new bar 
    as each bar stores its open time in Time[x]
    So "barstamp" will be our "note" of what the latest open time we encountered was 
    This way when we meet a Time[0] that is different than the one we noted in the previous 
    tick we know the bar is new -since Time[0] only holds the bar open time
    */
    barstamp=0;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
    //rates total stands for how many bars were fed into the calculation function
    //prev calculated stands for previously calculated bars ,and it will be 0 at the first pass
    //so on the first pass we will calculate all the bars
    int total=rates_total-prev_calculated;
    int i_from=total-barsReduction;//but we dont want all the bars because we will request a bar that does not exist so : 
    int i_to=0+barsReduction;//the same for the right end of the check ,we dont need to check bars that cant have the required amount of bars to their right side
    /*
    remember data goes Left to Right 
    Left is old 
    Right is new 
    High number is old 0 is the newest (in bars ,example : Low[1] is older than Low[0])
    */
    //if not first run and new bar 
    /*
    if this is not our first run we just need to calculate one bar which is 
    "BarsForFormation" away from the current bar
    */
    if(i_from<=i_to){i_from=BarsForFormation;i_to=BarsForFormation;}
    
    //Loop
    for(int i=i_from;i>=i_to;i--)
    {
    int local_from=i+BarsForFormation;
    int local_to=i-BarsForFormation;
    //sub loop
      double comparison_price=Low[i];
      bool Valid=true;
      for(int s=local_from;s>=local_to;s--)
      {
      //you are looking for the lowest point so if anything within the sub loop range 
      //is lower than your comparison value we will invalidate the condition and exit 
      //the subloop . Also , we will not compare the bar with itself
        //if the subloop bar is not the main loop bar 
        if(s!=i)
        {
        if(Low[s]<=comparison_price)
          {
          Valid=false;//invalidate first 
          break;//exit the sub loop to :ES
          }
        }
        //if the subloop bar is not the main loop bar ends here
      }
    //sub loop ends here 
    //:ES ,sub loop exits here -es has nothing to do with code its just for show
    //so now you probably want to mark the bar if the condition was valid 
      if(Valid==true)
      {
      //LowFractal of bar i (main loop) ,we give it a price and it becomes a placement for your sign
      LF[i]=comparison_price;
      }
    /*
    and one more check 
    there is a senario where a previously marked bar should no 
    longer be marked as the live (now/current/recent/0) bar goes below its lows
    to detect such a case we will ask : 
    a.is the i_from equal to the i_to > remember this is what we do when the calculation is not the first pass (but live)
    b.is the value not empty 
    c.Is our result not Valid  
    */
      if(Valid==false&&i_from==i_to&&LF[i]!=0)
      {
      LF[i]=0;
      }
    } 
    //Loop ends here 
    //we update the barstamp 
    barstamp=Time[0];

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
 
Lorentzos Roussos:

Welcome .

Here you go : save it in indicators . Try to do the same for the highs for training 

cheers

Thank you, sooooo much!!!!
Reason: