31191382:
int endIndex = MathMax(0, Bars - candlesToCheck - 2); // End index for the loop (last 32 closed candles)
This is not the last 32 closed candles.
//... int candlesToCheck = 32; // Number of 15-minute candles to check int startIndex = Bars - 2; // Start from the most recent closed candle int endIndex = MathMax(0, Bars - candlesToCheck - 2); // End index for the loop (last 32 closed candles) PrintFormat("for(int i = %i; i >= %i; i--)", Bars - 2, endIndex); PrintFormat("Current bar = %s, bar[endIndex] = %s", TimeToString(Time[0]), TimeToString(Time[endIndex])); for (int i = Bars - 2; i >= endIndex; i--) //...
script_test2 XAUUSD,M15: Current bar = 2024.06.25 02:30, bar[endIndex] = 2022.05.11 22:30 script_test2 XAUUSD,M15: for(int i = 50101; i >= 50069; i--)
Hi
Indexing is reversed in mt4/5 the current candle is 0 and last closed candle is 1. Soi f you want to check or exclude last 32 candles you simply count from 32 to 1. So in your example it might be something like this:
for (int i = candlesToCheck; i >= 0; i--) {
Of course you can add some extra checking if the numer of available bars is bigger than candle s to check i.e. candlesToCheck = MathMin(candlesToCheck, Bars-1);
Best Regards

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
hello guys, i have a code for detecting a break of key level, but when i run the code in strategy tester it never detects one and i cannot find what is wrong with the code.(it should check the last 8 hours for possible key levels and the code runs on the 15min timeframe, which is set from another part of the code). if anyone would help it would be appreciated.
here is the code: