break of key level detection code mulfunction

 

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: 


bool CheckForBreakOfKeyLevelsLong()

{

    Print("CheckForBreakOfKeyLevelsLong called");



    if (Period() != PERIOD_M15)

    {

        Print("CheckForBreakOfKeyLevelsLong: Not on M15 timeframe.");

        return false;

    }



    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)



    for (int i = Bars - 2; i >= endIndex; i--)

    {

        double prevOpen = Open[i + 1];

        double prevClose = Close[i + 1];

        double prevHigh = High[i + 1];

        double prevLow = Low[i + 1];



        double currOpen = Open[i];

        double currClose = Close[i];

        double currHigh = High[i];

        double currLow = Low[i];



        // Check if the previous candle is bullish and the current candle is bearish

        if (prevClose > prevOpen && currClose < currOpen)

        {

            // Calculate body size and maximum wick size for the previous candle

            double prevBodySize = MathAbs(prevClose - prevOpen);

            double prevMaxWickSize = prevBodySize * 0.25; // Allow a wick of up to 25% of the candle's body size



            // Ensure the previous candle meets the body and wick size requirements

            if ((prevHigh - prevClose) <= prevMaxWickSize)

            {

                currentKeyLevelLong.time = Time[i + 1]; // Use time of the previous candle

                currentKeyLevelLong.price = prevClose; // Key level is set at the Close of the bullish candle

                currentKeyLevelLong.broken = false;



                PrintFormat("Key level set at bar %d: Time=%d, Price=%.5f", i + 1, currentKeyLevelLong.time, currentKeyLevelLong.price);



                double lastClosedCandleOpen = Open[1];

                double lastClosedCandleClose = Close[1];

                double lastClosedCandleHigh = High[1];

                double lastClosedCandleLow = Low[1];



                PrintFormat("Last closed candle: Open=%.5f, Close=%.5f, High=%.5f", lastClosedCandleOpen, lastClosedCandleClose, lastClosedCandleHigh);



                // Ensure the last closed candle breaks the key level

                if (lastClosedCandleOpen < currentKeyLevelLong.price && 

                    lastClosedCandleClose > currentKeyLevelLong.price &&

                    (lastClosedCandleHigh - lastClosedCandleClose) <= (MathAbs(lastClosedCandleOpen - lastClosedCandleLow) * 0.1))

                {

                    currentKeyLevelLong.broken = true;

                    Print("Key level broken by last closed candle.");

                    return true; // Immediately return true when a key level break is found

                }

            }

        }

    }



    Print("No broken key level found.");

    return false; // Return false only if no key level breaks are found

}
 
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