My EA is not executing trades.

 

Hello great people. Kindly help me fix the error in my EA. It fails to pick trades.


//+------------------------------------------------------------------+

//|                                       BB_Ichimoku_AD_RSI_ATR.mq5 |
//|                        Stricter Confirmation Logic v2.6          |
//+------------------------------------------------------------------+
#property copyright "Generated by ChatGPT"
#property version   "2.6"
#property strict

#include <Trade\Trade.mqh>
CTrade trade;

// Input Parameters
input double RiskATRMultiplier     = 2.0;
input double TrailATRMultiplier    = 1.5;
input double TrailStepATR          = 1.0;
input int    ATRPeriod             = 14;
input double BreakEvenATR          = 1.0;
input bool   UseBreakEven          = true;
input bool   UseTrailingStop       = true;
input int    TrailingCheckInterval = 5;
input double LotSize               = 0.01;
input double RiskPercent           = 3.0;
input int    MaxOpenTrades         = 1;

input ENUM_TIMEFRAMES TimeFrame   = PERIOD_M15;
input string InpTradeSymbol       = "";  // Input Symbol
input int TradeStartHour          = 1;
input int TradeEndHour            = 22;

string TradeSymbol;
datetime lastTrailingCheck = 0;

// Indicator Handles & Buffers
int bbHandle, ichimokuHandle, adHandle, rsi9Handle, rsi20Handle, atrHandle;
double bbUpper[], bbMiddle[], bbLower[];
double tenkan[], kijun[];
double ad[];
double rsi9[], rsi20[], atr[];

//+------------------------------------------------------------------+
void OnInit() {
    TradeSymbol = (InpTradeSymbol == "") ? _Symbol : InpTradeSymbol;

    bbHandle        = iBands(TradeSymbol, TimeFrame, 20, 0, 2.0, PRICE_CLOSE);
    ichimokuHandle  = iIchimoku(TradeSymbol, TimeFrame, 9, 26, 52);
    adHandle        = iAD(TradeSymbol, TimeFrame, VOLUME_REAL);
    rsi9Handle      = iRSI(TradeSymbol, TimeFrame, 9, PRICE_CLOSE);
    rsi20Handle     = iRSI(TradeSymbol, TimeFrame, 20, PRICE_CLOSE);
    atrHandle       = iATR(TradeSymbol, TimeFrame, ATRPeriod);

    if (bbHandle == INVALID_HANDLE || ichimokuHandle == INVALID_HANDLE || adHandle == INVALID_HANDLE ||
        rsi9Handle == INVALID_HANDLE || rsi20Handle == INVALID_HANDLE || atrHandle == INVALID_HANDLE) {
        Print("Error creating one or more indicator handles.");
        ExpertRemove();
    }
}

//+------------------------------------------------------------------+
bool IsTradingAllowed() {
    datetime now = TimeCurrent();
    MqlDateTime dt;
    TimeToStruct(now, dt);
    int hour = dt.hour;
    return (hour >= TradeStartHour && hour < TradeEndHour);
}

//+------------------------------------------------------------------+
int OpenTradesOnSymbol(string symbol) {
    int count = 0;
    for (int i = PositionsTotal() - 1; i >= 0; i--) {
    ulong ticket = PositionGetTicket(i);  // <-- Get position ticket by index
        if (PositionSelectByTicket(ticket)) {
            if (PositionGetString(POSITION_SYMBOL) == symbol)
                count++;
        }
    }
    return count;
}

//+------------------------------------------------------------------+
void ManageTrailingStops() {
    if (!UseTrailingStop) return;
    if (TimeCurrent() - lastTrailingCheck < TrailingCheckInterval) return;

    lastTrailingCheck = TimeCurrent();

    for (int i = PositionsTotal() - 1; i >= 0; i--) {
        string symbol = PositionGetSymbol(i);
        if (!PositionSelect(symbol)) continue;
        if (symbol != TradeSymbol) continue;

        double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
        double currentPrice = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
            ? SymbolInfoDouble(TradeSymbol, SYMBOL_BID)
            : SymbolInfoDouble(TradeSymbol, SYMBOL_ASK);

        double atrVal[];
        if (CopyBuffer(atrHandle, 0, 0, 1, atrVal) < 0 || ArraySize(atrVal) < 1) continue;
        double atrValue = atrVal[0];

        int posType = (int)PositionGetInteger(POSITION_TYPE);

        double trailTrigger = openPrice + ((posType == POSITION_TYPE_BUY) ? 1 : -1) * TrailATRMultiplier * atrValue;
        double newStop = currentPrice - ((posType == POSITION_TYPE_BUY) ? 1 : -1) * TrailStepATR * atrValue;

        double currentSL = PositionGetDouble(POSITION_SL);
        bool shouldModify =
            (posType == POSITION_TYPE_BUY && currentPrice > trailTrigger && newStop > currentSL) ||
            (posType == POSITION_TYPE_SELL && currentPrice < trailTrigger && newStop < currentSL);

        if (shouldModify) {
            newStop = NormalizeDouble(newStop, _Digits);
            trade.PositionModify(TradeSymbol, newStop, PositionGetDouble(POSITION_TP));
            Print("Trailing Stop updated to: ", newStop);
        }
    }
}

//+------------------------------------------------------------------+
void OnTick() {
    if (!IsTradingAllowed()) return;
    ManageTrailingStops();

    if (Bars(TradeSymbol, TimeFrame) < 60) return;
    if (OpenTradesOnSymbol(TradeSymbol) >= MaxOpenTrades) return;

    int limit = 3;
    if (
        CopyBuffer(bbHandle, 0, 0, limit, bbUpper) < 0 ||
        CopyBuffer(bbHandle, 1, 0, limit, bbMiddle) < 0 ||
        CopyBuffer(bbHandle, 2, 0, limit, bbLower) < 0 ||
        CopyBuffer(ichimokuHandle, 0, 0, limit, tenkan) < 0 ||
        CopyBuffer(ichimokuHandle, 1, 0, limit, kijun) < 0 ||
        CopyBuffer(adHandle, 0, 0, limit, ad) < 0 ||
        CopyBuffer(rsi9Handle, 0, 0, limit, rsi9) < 0 ||
        CopyBuffer(rsi20Handle, 0, 0, limit, rsi20) < 0 ||
        CopyBuffer(atrHandle, 0, 0, limit, atr) < 0
    ) {
        Print("Indicator buffer copy failed.");
        return;
    }

    if (ArraySize(bbUpper) < 3 || ArraySize(tenkan) < 3 || ArraySize(ad) < 3 || ArraySize(rsi9) < 3 || ArraySize(atr) < 2) {
        Print("Not enough data in indicator buffers.");
        return;
    }

    double atr_now = atr[0];
    double atr_prev = atr[1];
    if (atr_now > atr_prev * 1.8) return;

    double spread = (SymbolInfoDouble(TradeSymbol, SYMBOL_ASK) - SymbolInfoDouble(TradeSymbol, SYMBOL_BID)) / _Point;
    if (spread > 50) return;

    double close = iClose(TradeSymbol, TimeFrame, 0);
    double open = iOpen(TradeSymbol, TimeFrame, 0);
    double body = MathAbs(close - open);
    double candleSize = iHigh(TradeSymbol, TimeFrame, 0) - iLow(TradeSymbol, TimeFrame, 0);
    bool bullishCandle = close > open && body > 0.5 * candleSize;
    bool bearishCandle = open > close && body > 0.5 * candleSize;

    bool adUp = ad[0] > ad[1] && ad[1] > ad[2];
    bool adDown = ad[0] < ad[1] && ad[1] < ad[2];

    bool buyCond =
        close > tenkan[0] &&
        close > kijun[0] &&
        rsi9[0] > rsi20[0] && rsi20[0] > 50 &&
        adUp &&
        close > bbMiddle[0] && iLow(TradeSymbol, TimeFrame, 1) < bbLower[1] &&
        bullishCandle;

    bool sellCond =
        close < tenkan[0] &&
        close < kijun[0] &&
        rsi9[0] < rsi20[0] && rsi20[0] < 50 &&
        adDown &&
        close < bbMiddle[0] && iHigh(TradeSymbol, TimeFrame, 1) > bbUpper[1] &&
        bearishCandle;

    if (buyCond || sellCond)
        Print("Signal Detected | Buy: ", buyCond, ", Sell: ", sellCond);

    double ask = SymbolInfoDouble(TradeSymbol, SYMBOL_ASK);
    double bid = SymbolInfoDouble(TradeSymbol, SYMBOL_BID);
    double stopLoss = atr_now * RiskATRMultiplier;
    double takeProfit = stopLoss * 2;

    double minStop = SymbolInfoInteger(TradeSymbol, SYMBOL_TRADE_STOPS_LEVEL) * _Point;

    if (buyCond) {
        double sl = NormalizeDouble(ask - stopLoss, _Digits);
        double tp = NormalizeDouble(ask + takeProfit, _Digits);
        if ((ask - sl) < minStop) {
            Print("Buy SL too close. SL=", sl, " MinStopLevel=", minStop);
            return;
        }

        if (!trade.Buy(LotSize, TradeSymbol, ask, sl, tp, NULL)) {
            Print("Buy failed: ", trade.ResultRetcode(), " ", trade.ResultRetcodeDescription());
        } else {
            Print("Buy order sent.");
        }
    }

    if (sellCond) {
        double sl = NormalizeDouble(bid + stopLoss, _Digits);
        double tp = NormalizeDouble(bid - takeProfit, _Digits);
        if ((sl - bid) < minStop) {
            Print("Sell SL too close. SL=", sl, " MinStopLevel=", minStop);
            return;
        }

        if (!trade.Sell(LotSize, TradeSymbol, bid, sl, tp, NULL)) {
            Print("Sell failed: ", trade.ResultRetcode(), " ", trade.ResultRetcodeDescription());
        } else {
            Print("Sell order sent.");
        }
    }
}

 
It fails because you (and ChatGPT) have no idea of what you are doing. Hire a professional developer in the freelance section. 

Coding, as well as all other jobs, cannot be improvised if you want to obtain a decent result. If a decent result is not your goal, you already obtained it!
 
  1. Why did you post your coding question in the MT5 General section (a miscellaneous catch-all category) instead of the MT5 EA section (non-indicator coding)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. I have moved this thread.

  2.     double atr_now = atr[0];
        double atr_prev = atr[1];
    

    Where did you set the indexing direction of all your arrays (before filling)?

    To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] and other arrays (copyXXXX functions), call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.
              Event Handling / OnCalculate - Reference on algorithmic/automated trading language for MetaTrader 5