The OnInit() function run more than once when using breakpoints to test code with real data?

 
While running my code in debug mode and testing with real data using breakpoints, I encountered a peculiar problem. I was testing a custom function in the OnTimer() that enables users to delay the start of an EA by placing a pending order. Once the pending order is filled, the rest of the EA is allowed to run. However, after this function completes, the OnInit() function runs again, but only when using breakpoints.

bool delayed_start_routine_option(
    const double delayed_price,
    const string symbol,
    const double volume,
    const double grid_offset,
    const int magic_number) {

    //--- First check of the price is 0.0
    if (delayed_price == 0.0) {
        logger.info("No delayed start price set...continuing with EA immediately", __FUNCTION__, __LINE__);
        return true;
    }

    //--- If the delayed price is greater than 0.0
    if (delayed_price > 0.0) {

        logger.info("Delaying EA start until" + (string)delayed_price + " is reached", __FUNCTION__, __LINE__);

        //--- Get the current Ask price
        double live_ask = SymbolInfoDouble(symbol, SYMBOL_ASK);

        //--- if the ⁡⁣⁣⁢delayed_price⁡ is less than the ⁡⁣⁣⁢live_ask⁡, we will place a ⁡⁣⁣⁢Buy Limit Order⁡.
        if (delayed_price < live_ask) {

            double limit_price = delayed_price;
            double limit_tp = delayed_price + grid_offset;

            //--- place limit order
            place_buy_order(
                symbol,                                      //--- symbol
                ORDER_TYPE_BUY_LIMIT,                        //--- order type
                volume,                                      //--- volume
                limit_price,                                 //--- price
                NULL,                                        //--- stop loss
                limit_tp,                                    //--- take profit
                commentGridMagic(grid_offset, magic_number), //--- custom comment
                magic_number                                 //--- magic number
            );

            //--- Declare the array of orders
            CArrayOfOrders array_of_orders;

            //--- Fetch all orders with the specified symbol, magic number, market side and order type
            array_of_orders.FetchAllOrdersDynamic(symbol, magic_number, MARKET_SIDE_BUY, ORDER_TYPE_BUY_LIMIT_EX);

            //--- Get the first order in the array
            CActiveOrderInfo order = array_of_orders.m_array_of_orders[0];

            //--- Get ticket
            ulong ticket = order.m_ticket_o;

            datetime histStart = 0;
            datetime histEnd = TimeCurrent();
            HistorySelect(histStart, histEnd);

            ENUM_ORDER_STATE order_state = ENUM_ORDER_STATE(HistoryOrderGetInteger(ticket, ORDER_STATE));

            bool while_loop = true;

            //--- Loop until order is filled, this will prevent the EA from continuing until the order is filled.
            while (order_state != ORDER_STATE_FILLED && while_loop) {

                histStart = 0;
                histEnd = TimeCurrent();
                HistorySelect(histStart, histEnd);

                //--- Get updated order state
                // order.updateOrderState();
                order_state = ENUM_ORDER_STATE(HistoryOrderGetInteger(ticket, ORDER_STATE));

                //--- Check if order is filled
                if (order_state == ORDER_STATE_FILLED) {
                    logger.info("Order filled - EA will now initiate Grid Monitoring.", __FUNCTION__, __LINE__);

                    //--- if order is filled, break out of the loop and allow rest of the EA to run.
                    while_loop = false;
                    return true;
                }

                //--- Wait 1 second before checking again
                Sleep(1000);
            }

        } else if (delayed_price > live_ask) { //--- if ⁡⁣⁣⁢delayed_price⁡ is greater than the ⁡⁣⁣⁢live_ask⁡, we will place a ⁡⁣⁣⁢Buy Stop Order⁡.

            double stop_price = delayed_price;
            double stop_tp = delayed_price + grid_offset;

            //--- place stop order
            place_buy_order(
                symbol,                                      //--- symbol
                ORDER_TYPE_BUY_STOP,                         //--- order type
                volume,                                      //--- volume
                stop_price,                                  //--- price
                NULL,                                        //--- stop loss
                stop_tp,                                     //--- take profit
                commentGridMagic(grid_offset, magic_number), //--- custom comment
                magic_number                                 //--- magic number
            );

            //--- Declare the array of orders
            CArrayOfOrders array_of_orders;

            //--- Fetch all orders with the specified symbol, magic number, market side and order type
            array_of_orders.FetchAllOrdersDynamic(symbol, magic_number, MARKET_SIDE_BUY, ORDER_TYPE_BUY_STOP_EX);

            //--- Get the first order in the array
            CActiveOrderInfo order = array_of_orders.m_array_of_orders[0];

            //--- Get ticket
            ulong ticket = order.m_ticket_o;

            datetime histStart = 0;
            datetime histEnd = TimeCurrent();
            HistorySelect(histStart, histEnd);

            ENUM_ORDER_STATE order_state = ENUM_ORDER_STATE(HistoryOrderGetInteger(ticket, ORDER_STATE));

            bool while_loop = true;

            //--- Loop until order is filled, this will prevent the EA from continuing until the order is filled.
            while (order_state != ORDER_STATE_FILLED && while_loop) {

                histStart = 0;
                histEnd = TimeCurrent();
                HistorySelect(histStart, histEnd);

                //--- Get updated order state
                order_state = ENUM_ORDER_STATE(HistoryOrderGetInteger(ticket, ORDER_STATE));

                //--- Check if order is filled
                if (order_state == ORDER_STATE_FILLED) {
                    logger.info("Order filled - EA will now initiate Grid Monitoring.", __FUNCTION__, __LINE__);

                    //--- if order is filled, break out of the loop and allow rest of the EA to run.
                    while_loop = false;
                    return true;
                }

                //--- Wait 1 second before checking again
                Sleep(1000);
            }
        }
    } else {
        logger.info("No delayed start price set...continuing with EA immediately", __FUNCTION__, __LINE__);
        return true;
    }
    return true;
}
Reason: