First, don't resize the buffers. That is done by the system. You should not be messing with it. Remove the following ...
// Resize buffers to the number of bars available ArrayResize(LineHighBuffer, rates_total); ArrayResize(LineLowBuffer, rates_total);
Second, you should be using the "rates_total" provided by the OnCalculate() handler, not using "Bars".
You should also be testing if "rates_total" is greater than 0, to prevent working with an empty array.
So fix the following accordingly ...
// Ensure there are enough bars to calculate high/low int rates_total = Bars; // Total number of bars on the chart if (rates_total < Nbars) return; // Avoid calculation if not enough bars
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
First, don't resize the buffers. That is done by the system. You should not be messing with it. Remove the following ...
Second, you should be using the "rates_total" provided by the OnCalculate() handler, not using "Bars".
You should also be testing if "rates_total" is greater than 0, to prevent working with an empty array.
So fix the following accordingly ...
// Ensure there are enough bars to calculate high/low int rates_total; // Total number of bars on the chart if (rates_total <= 0) return; // Avoid calculation if not enough bars
i did this and array out of range is gone but now my indicator is not plotting arrows and horizontal lines !
When you make changes to your code, think of the consequences. Don't just blindly do things without taking the time to understand the reason.
Pass the "rates_total" variable as a parameter to the function ...
// Check if a new bar has opened (compare current bar time with lastBarTime) if (time[0] != lastBarTime) { lastBarTime = time[0]; // Update the last bar time to the current bar DrawHighLow( rates_total ); // Update high/low lines and arrows on the new bar }
I've used the same variable name in both cases to reduce the number of changes, but you should actually use different names to prevent confusion in your code.void DrawHighLow( double rates_total ) { ... // Ensure there are enough bars to calculate high/low if (rates_total <= 0) return; // Avoid calculation if not enough bars
Also as I good practice, you should not be using iHighest() or iLowest(), or iHigh() or iLow() ...
double highestHigh = iHigh(symbol, timeframe, highestIndex); double lowestLow = iLow(symbol, timeframe, lowestIndex);
You should in fact be using the the ArrayMaximum() and ArrayMinimum() on the high[] and low[] arrays passed the OnCalculate() handler to guarantee that you are using the proper data in a synchronised manner.
You do not need to normalise the existing quote data. They are already normalised. Remove the following. It is unnecessary.
// Normalize prices for a 5-digit broker highestHigh = NormalizeDouble(highestHigh, MarketInfo(symbol, MODE_DIGITS)); lowestLow = NormalizeDouble(lowestLow, MarketInfo(symbol, MODE_DIGITS));
Use the passed parameters in your calculations to guarantee that you are using the proper data in a synchronised manner.
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[])
Don'tuse iHigh(), iLow() or iTime(), etc.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Why i get : Array out of range on the highlighted code ( LineHighBuffer [0] ?