My MT5 Bot is Not Executing Trades (Even in Backtest) – Need Help

 

Hello everyone,


I’ve developed an automated trading bot for MetaTrader 5, but I’m encountering a major issue: the bot is not executing any trades, even during backtesting.


Issue Summary:

The bot is running, but no trades are being placed.

There are no errors in the Journal or Experts tab.

The strategy should be triggering trades, but nothing happens.

Auto-trading is enabled.


What I’ve Tried So Far:

Checking for logic errors in my code.

Ensuring the bot is properly attached to the chart.

Verifying that the symbol and timeframe match the strategy conditions.

Testing different input parameters to see if they affect execution.

Reviewing the Strategy Tester settings to ensure proper backtesting conditions.


Full Code Available


I’m willing to share my full code so that others can review it and help me identify the issue.


Has anyone encountered a similar problem? Any suggestions on how to debug this?


I’d really appreciate any help.

Thanks in advance!


//+------------------------------------------------------------------+
//|  קובץ MQ5 עם אינדיקטורים וכניסות מבוססות תנאי שוק            |
//+------------------------------------------------------------------+
#property strict

// הגדרת משתנים
double atrValue, rsiValue, macdMain, macdSignal, bollTop, bollMiddle, bollBottom;
double maFast, maSlow;
double closePrice;

// הגדרת ידיות לאינדיקטורים
int atrHandle, rsiHandle, macdHandle, bollHandle, maFastHandle, maSlowHandle;

//+------------------------------------------------------------------+
//| פונקציה לאתחול האינדיקטורים                                    |
//+------------------------------------------------------------------+
void InitIndicators()
{
    // ATR
    atrHandle = iATR(_Symbol, PERIOD_M15, 14);
    
    // RSI
    rsiHandle = iRSI(_Symbol, PERIOD_M15, 14, PRICE_CLOSE);
    
    // MACD
    macdHandle = iMACD(_Symbol, PERIOD_M15, 12, 26, 9, PRICE_CLOSE);
    
    // Bollinger Bands
    bollHandle = iBands(_Symbol, PERIOD_M15, 20, 2, 0, PRICE_CLOSE);
    
    // ממוצעים נעים (MA)
    maFastHandle = iMA(_Symbol, PERIOD_M15, 9, 0, MODE_SMA, PRICE_CLOSE);
    maSlowHandle = iMA(_Symbol, PERIOD_M15, 21, 0, MODE_SMA, PRICE_CLOSE);
}

//+------------------------------------------------------------------+
//| פונקציה למשיכת נתוני האינדיקטורים                              |
//+------------------------------------------------------------------+
void UpdateIndicators()
{
    double atrBuffer[1], rsiBuffer[1], maFastBuffer[1], maSlowBuffer[1];
    double macdBuffer[2], bollBufferTop[1], bollBufferMiddle[1], bollBufferBottom[1];

    // משיכת נתוני ATR
    if (atrHandle != INVALID_HANDLE && CopyBuffer(atrHandle, 0, 0, 1, atrBuffer) > 0)
        atrValue = atrBuffer[0];

    // משיכת נתוני RSI
    if (rsiHandle != INVALID_HANDLE && CopyBuffer(rsiHandle, 0, 0, 1, rsiBuffer) > 0)
        rsiValue = rsiBuffer[0];

    // משיכת נתוני MACD
    if (macdHandle != INVALID_HANDLE && CopyBuffer(macdHandle, 0, 0, 1, macdBuffer) > 0)
    {
        macdMain = macdBuffer[0];
        macdSignal = macdBuffer[1];
    }

    // משיכת נתוני Bollinger Bands
    if (bollHandle != INVALID_HANDLE)
    {
        if (CopyBuffer(bollHandle, 0, 0, 1, bollBufferTop) > 0)
            bollTop = bollBufferTop[0];

        if (CopyBuffer(bollHandle, 1, 0, 1, bollBufferMiddle) > 0)
            bollMiddle = bollBufferMiddle[0];

        if (CopyBuffer(bollHandle, 2, 0, 1, bollBufferBottom) > 0)
            bollBottom = bollBufferBottom[0];
    }

    // משיכת נתוני ממוצעים נעים
    if (maFastHandle != INVALID_HANDLE && CopyBuffer(maFastHandle, 0, 0, 1, maFastBuffer) > 0)
        maFast = maFastBuffer[0];

    if (maSlowHandle != INVALID_HANDLE && CopyBuffer(maSlowHandle, 0, 0, 1, maSlowBuffer) > 0)
        maSlow = maSlowBuffer[0];

    // משיכת מחיר סגירה נוכחי
    closePrice = iClose(_Symbol, PERIOD_M15, 0);
}

//+------------------------------------------------------------------+
//| פונקציה ראשית                                                   |
//+------------------------------------------------------------------+
void OnTick()
{
    // בדיקה אם אינדיקטורים מאותחלים
    if (atrHandle == INVALID_HANDLE || rsiHandle == INVALID_HANDLE || macdHandle == INVALID_HANDLE || bollHandle == INVALID_HANDLE || maFastHandle == INVALID_HANDLE || maSlowHandle == INVALID_HANDLE)
    {
        Print("Error: One or more indicators failed to initialize.");
        return;
    }
    
    // עדכון נתונים
    UpdateIndicators();

    // הדפסת נתונים (לבדיקה)
    Print("ATR: ", atrValue, " | RSI: ", rsiValue, " | MACD: ", macdMain, "/", macdSignal);
    Print("Bollinger Bands: Top=", bollTop, " Middle=", bollMiddle, " Bottom=", bollBottom);
    Print("MA Fast: ", maFast, " | MA Slow: ", maSlow);
    
    // דוגמת לוגיקת כניסה לעסקה
    if (closePrice > maFast && closePrice > maSlow && macdMain > macdSignal && rsiValue > 50)
    {
        Print("Signal: BUY");
        // פקודת קנייה
    }
    else if (closePrice < maFast && closePrice < maSlow && macdMain < macdSignal && rsiValue < 50)
    {
        Print("Signal: SELL");
        // פקודת מכירה
    }
}

 
If the code can be compiled without any error but it doesn't do what you expect use the debugger to find out why - it is exactly made for this.
Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272
Code debugging - Developing programs - MetaEditor Help
Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)


  2. Where do you call initIndicators?