• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 3:26pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 3:26pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

need help to "array out of range" message 5 replies

Please help: Getting "Array Out of Range" in EA 7 replies

Help with " array out of range in" - Error 6 replies

Two Dimensional Array vs One Dimensional Array 13 replies

Calculate Stochastic Formula on a array (non indicator buffer array) 2 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: Array out of range? Help?
Exit Attachments

Array out of range? Help?

  • Post #1
  • Quote
  • First Post: Edited at 7:35pm Feb 18, 2018 3:55pm | Edited at 7:35pm
  •  AntiVi
  • | Joined Dec 2015 | Status: Member | 277 Posts
EDIT: Here is the fix thanks to Johnvanwijk! -> https://www.forexfactory.com/showthr...9#post10791049


Hey guys,
So I've been trying to draw some stuff however it's giving me the error "Array out of range".
It draws the green arrows perfectly however the red arrows just won't work.

If someone could tell me what I'm doing wrong it would be great.

It's not because I'm trying to access candles that aren't there yet or at all as far as I know.
I get that some people might say that it's going past available bars but I have tested even with a limit of 1000 which I'm sure my chart has and it still gives me out of range.

This is the code:

Inserted Code
//+------------------------------------------------------------------+
//|                                             FractalsExercise.mq4 |
//|                                                           AntiVi |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "AntiVi"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
double Highs[];
double Lows[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- drawing style  
  SetIndexStyle(0, DRAW_ARROW, EMPTY, 1, clrGreen);
  SetIndexArrow(0, 234);
 
  SetIndexStyle(1, DRAW_ARROW, EMPTY, 1, clrRed);
  SetIndexArrow(1, 233);
 
//--- indicator buffers
   SetIndexBuffer(0, Highs);
   SetIndexBuffer(1, Lows);
  
//---
   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[])
  {
//---
  
int i = 1;
int ii = 1;
int Limit = Bars - 1;
   while(i < Limit)
      {  
         if(High[i] > High[i + 1])
            {
               if(High[i] > High[i - 1])
                  {
                     Highs[i] = High[i];
                  }
            }
         i++;
      }    
    while(ii < Limit)
      {  
         if(Low[ii] < Low[ii + 1])
            {
               if(Low[ii] < Low[ii - 1])
                  {
                     Lows[ii] = Low[ii];
                  }
            }
         ii++;  
      }
  
  
  
  
  
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Thanks!
  • Post #2
  • Quote
  • Feb 18, 2018 6:21pm Feb 18, 2018 6:21pm
  •  AntiVi
  • | Joined Dec 2015 | Status: Member | 277 Posts
If I split the while loops into 2 indicators it works perfectly with not a single error but not when they're in there together.
It's not accessing anything that's not there either.

So I'm not sure why it says it's out of range.
 
 
  • Post #3
  • Quote
  • Feb 18, 2018 6:41pm Feb 18, 2018 6:41pm
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,403 Posts
Quoting AntiVi
Disliked
Hey guys, So I've been trying to draw some stuff however it's giving me the error "Array out of range". It draws the green arrows perfectly however the red arrows just won't work. If someone could tell me what I'm doing wrong it would be great. It's not because I'm trying to access candles that aren't there yet or at all as far as I know. I get that some people might say that it's going past available bars but I have tested even with a limit of 1000 which I'm sure my chart has and it still gives me out of range. This is the code: //+------------------------------------------------------------------+...
Ignored
In each loop, try to break it with something like the example below to avoid to go past the Bars available:

Inserted Code
if(i >= Bars-1) break;
Inserted Code
if(ii >= Bars-1) break;
MT4/MT5 EAs/Indicators/Alerts coder
 
1
  • Post #4
  • Quote
  • Feb 18, 2018 6:54pm Feb 18, 2018 6:54pm
  •  AntiVi
  • | Joined Dec 2015 | Status: Member | 277 Posts
Quoting FerruFx
Disliked
{quote} In each loop, try to break it with something like the example below to avoid to go past the Bars available: if(i >= Bars-1) break; if(ii >= Bars-1) break;
Ignored
From what I can see that is what my "Limit" does already however I still inserted it into the code just to test and even tried it in several positions however the error persists.

I'm honestly baffled as to why it's giving me the error.

This is a picture of the result so far. It should do the same for the Lows with red arrows pointing up.
You can also see the error just to give people an idea.
Attached Image (click to enlarge)
Click to Enlarge

Name: Untitled.png
Size: 96 KB
 
 
  • Post #5
  • Quote
  • Feb 18, 2018 7:03pm Feb 18, 2018 7:03pm
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,403 Posts
Quoting AntiVi
Disliked
{quote} From what I can see that is what my "Limit" does already however I still inserted it into the code just to test and even tried it in several positions however the error persists. I'm honestly baffled as to why it's giving me the error. This is a picture of the result so far. It should do the same for the Lows with red arrows pointing up. You can also see the error just to give people an idea. {image}
Ignored
Which line is 71 ?
MT4/MT5 EAs/Indicators/Alerts coder
 
 
  • Post #6
  • Quote
  • Feb 18, 2018 7:05pm Feb 18, 2018 7:05pm
  •  AntiVi
  • | Joined Dec 2015 | Status: Member | 277 Posts
Quoting FerruFx
Disliked
{quote} Which line is 71 ?
Ignored
That would be "Lows[ii] = Low[ii];" in the second while loop.
 
 
  • Post #7
  • Quote
  • Feb 18, 2018 7:17pm Feb 18, 2018 7:17pm
  •  johnvanwijk
  • | Joined Dec 2010 | Status: Member | 7 Posts
Hi AntiVi,

you should indicate how many buffers are required....

the following change at the start in your code (after #property indicator_chart_window) makes it work nicely.....

#property indicator_buffers 2

Cheers & Enjoy!

John


Inserted Code
//+------------------------------------------------------------------+
//|                                             FractalsExercise.mq4 |
//|                                                           AntiVi |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "AntiVi"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
 
// START ADDED JvW
#property indicator_buffers 2
// END ADDED JvW
 
double Highs[];
double Lows[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- drawing style  
  
  SetIndexStyle(0, DRAW_ARROW, EMPTY, 1, clrGreen);
  SetIndexArrow(0, 234);
 
  SetIndexStyle(1, DRAW_ARROW, EMPTY, 1, clrRed);
  SetIndexArrow(1, 233);
 
//--- indicator buffers
   SetIndexBuffer(0, Highs);
   SetIndexBuffer(1, Lows);
  
//---
   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[])
  {
//---
  
int i = 1;
int ii = 1;
int Limit = Bars - 1;
   while(i < Limit)
      {  
         if(High[i] > High[i + 1])
            {
               if(High[i] > High[i - 1])
                  {
                     Highs[i] = High[i];
                  }
            }
         i++;
      }    
    while(ii < Limit)
      {  
         if(Low[ii] < Low[ii + 1])
            {
               if(Low[ii] < Low[ii - 1])
                  {
                     Lows[ii] = Low[ii];
                  }
            }
         ii++;  
      }
  
  
  
  
  
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
2
  • Post #8
  • Quote
  • Feb 18, 2018 7:34pm Feb 18, 2018 7:34pm
  •  AntiVi
  • | Joined Dec 2015 | Status: Member | 277 Posts
Quoting johnvanwijk
Disliked
Hi AntiVi, you should indicate how many buffers are required.... the following change at the start in your code (after #property indicator_chart_window) makes it work nicely..... #property indicator_buffers 2 Cheers & Enjoy! John //+------------------------------------------------------------------+ //| FractalsExercise.mq4 | //| AntiVi | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "AntiVi" #property link "https://www.mql5.com" #property version "1.00" #property strict #property...
Ignored

You sir are a genius.
I can't believe the fix was so simple.

I'm going to blame this on my inexperience and do it yourself learning.
I spent a good 5 hours trying to figure it out and all I found were workarounds that didn't fix anything.

I'll edit the first post to include the fix so people can get the easy answer if they bother looking for it!

Thanks a lot!
 
 
  • Post #9
  • Quote
  • Feb 19, 2018 12:10am Feb 19, 2018 12:10am
  •  Nicholishen
  • Joined Jul 2005 | Status: zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz | 1,289 Posts
Quoting AntiVi
Disliked
{quote} You sir are a genius. I can't believe the fix was so simple. I'm going to blame this on my inexperience and do it yourself learning. I spent a good 5 hours trying to figure it out and all I found were workarounds that didn't fix anything. I'll edit the first post to include the fix so people can get the easy answer if they bother looking for it! Thanks a lot!
Ignored
Hi AntiVi,
In addition to what john has shared, you also want to get in the habit of using the data passed as args into the OnCalculate function instead of relying on data from the built in vars. Also, you want to make sure you are doing your look-backs correctly. You don't need to process the entire loop (all chart data) on every tick. You only need to process the portion of the indicator data that needs updating. Here is an example of how to make better use of the OnCalculate function.
Inserted Code
#property strict
#property indicator_chart_window
// START ADDED JvW
#property indicator_buffers 2
// END ADDED JvW
double Highs[];
double Lows[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- drawing style  
   SetIndexStyle(0,DRAW_ARROW,EMPTY,1,clrGreen);
   SetIndexArrow(0,234);
   SetIndexStyle(1,DRAW_ARROW,EMPTY,1,clrRed);
   SetIndexArrow(1,233);
//--- indicator buffers
   SetIndexBuffer(0,Highs);
   SetIndexBuffer(1,Lows);
   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[])
  {
//---
   int limit = rates_total - prev_calculated - 2;
   limit = limit < 1 ? 1 : limit;
   for(int i = limit; i > 0; i--)
   {
      if(high[i] > high[i+1] && high[i] > high[i-1])
         Highs[i] = high[i];
      if(low[i] < low[i+1] && low[i] < low[i-1])
         Lows[i] = low[i];  
   }
//--- return value of prev_calculated for next call
   return(rates_total);
}
 
1
  • Post #10
  • Quote
  • Feb 19, 2018 6:13am Feb 19, 2018 6:13am
  •  johnvanwijk
  • | Joined Dec 2010 | Status: Member | 7 Posts
Genius ??? Thanks, but I would not think so.

More a bit of a stumbler with experience :-)

I bet for every mistake, omission or code typo that you could show here: I have made the same in the past...and probably TWICE!

Cheerio
 
1
  • Post #11
  • Quote
  • Edited at 10:12am Feb 19, 2018 9:41am | Edited at 10:12am
  •  AntiVi
  • | Joined Dec 2015 | Status: Member | 277 Posts
Quoting Nicholishen
Disliked
{quote} Hi AntiVi, In addition to what john has shared, you also want to get in the habit of using the data passed as args into the OnCalculate function instead of relying on data from the built in vars. Also, you want to make sure you are doing your look-backs correctly. You don't need to process the entire loop (all chart data) on every tick. You only need to process the portion of the indicator data that needs updating. Here is an example of how to make better use of the OnCalculate function. #property strict #property indicator_chart_window...
Ignored
Alright so it took me a little while to figure out what exactly everything meant however when I put in the code and put a print it within the for loop it would continue to print every tick no matter what.

It seems to be fixed if I change this piece of code:
"limit = limit < 1 ? 1 : limit;"

To this:
"limit = limit < 1 ? 0 : limit;"

Because otherwise it seems that Limit would be 1 no matter what.
Let me know if I'm wrong here.

It also seems like it's not drawing new arrows but maybe that's because of the change i made so i will test that.

EDIT:
So it seems that me changing that changed the ability for it to draw new ones.

EDIT 2:
So I changed it back to what it was and it is drawing new arrow however it's printing them wrong because it's thinking the currently forming candle is already complete.

EDIT 3:
I change this piece of code: "limit = limit < 1 ? 1 : limit;"
To this: "limit = limit < 1 ? 2 : limit;"

Which shifts it to only use completed candles which means that it won't be printing any arrows using a candle that's currently forming and it's now printing correctly.
Correct me if I'm wrong.
 
 
  • Post #12
  • Quote
  • Last Post: Feb 19, 2018 10:28am Feb 19, 2018 10:28am
  •  AntiVi
  • | Joined Dec 2015 | Status: Member | 277 Posts
Alright here is a little update!

So in order to fix it constantly running which I checked by printing stuff I put in an if statement which is this one:
"if(rates_total > prev_calculated){}"

This prevents anything from running if there isn't a new candle.
I'm not sure if that makes the rest obsolete but there should be some stuff I can do to clean up this code a bit and make it simpler.

It also prints properly because of the previous mentioned change in my third Edit on the previous post.

Inserted Code
//+------------------------------------------------------------------+
//|                                             FractalsExercise.mq4 |
//|                                                           AntiVi |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "AntiVi"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
double Highs[];
double Lows[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- drawing style  
  SetIndexStyle(0, DRAW_ARROW, EMPTY, 1, clrGreen);
  SetIndexArrow(0, 226);
 
  SetIndexStyle(1, DRAW_ARROW, EMPTY, 1, clrRed);
  SetIndexArrow(1, 225);
 
//--- indicator buffers
   SetIndexBuffer(0, Highs);
   SetIndexBuffer(1, Lows);
   
//---
   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[])
  {
//---
int limit = rates_total - prev_calculated - 2;
limit = limit < 1 ? 2 : limit;

for(int i = limit; i > 1; i--)
   {  
   if(rates_total > prev_calculated)
      {
         Print("=== NEW CANDLE ===");
      
         if(High[i] > High[i + 1] && High[i] > High[i - 1])
            {
               Highs[i] = High[i] + 0.0001;
               Print("NEW HIGH DETECTED ", i);
            }
         if(Low[i] < Low[i + 1] && Low[i] < Low[i - 1])
            {
               Lows[i] = Low[i] - 0.0001;
               Print("NEW LOW DETECTED ", i);
            }
      }
}
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
 
  • Platform Tech
  • /
  • Array out of range? Help?
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022