Error 4756 Invalid Request

 
I have written a bot that automatically places trades based on multiple indicators, after some trying and compiling I succeeded and went on with Metatrader 5 on Vantage, in the strategy tester it is giving me error code 4756 Invalid Request in the journal, I've already tried analysing the problem with chatgpt, but it doesn't seem to fix it.. 

//+------------------------------------------------------------------+
//| Multi-Indicator Strategy Trading Bot                            |
//+------------------------------------------------------------------+
#include <Trade/Trade.mqh>

// Handelsobject aanmaken
CTrade trade;

// --- Input Parameters ---
input int EMA_Period = 50;
input int SMA_Period = 200;
input int MACD_Fast = 12;
input int MACD_Slow = 26;
input int MACD_Signal = 9;
input int RSI_Period = 14;
input double RSI_Overbought = 70;
input double RSI_Oversold = 30;
input int Bollinger_Period = 20;
input double Bollinger_Deviation = 2.0;
input int KDJ_Period_K = 9;
input int KDJ_Period_D = 3;
input int KDJ_Period_J = 3;
input double Lot_Size = 0.1;
input int Slippage = 3;
input int MagicNumber = 123456;
input double StopLossATR_Multiplier = 1.5;
input double RiskRewardRatio = 3.0; // Risk-Reward Ratio Input

// --- Indicator Handles ---
int handleEMA, handleSMA, handleMACD, handleRSI, handleBands, handleStoch, handleATR;

// --- Global Variables ---
double EMA50[1], SMA200[1], MACD_Main[1], MACD_SignalLine[1], RSI_Value[1];
double BB_Upper[1], BB_Lower[1], BB_Mid[1], ATR_Value[1];
double K_Value[1], D_Value[1], J_Value[1];
double Close[1];

//+------------------------------------------------------------------+
//| Initialize Indicators                                           |
//+------------------------------------------------------------------+
int OnInit()
{
    // Initialize Indicator Handles
    handleEMA = iMA(Symbol(), PERIOD_M30, EMA_Period, 0, MODE_EMA, PRICE_CLOSE);
    handleSMA = iMA(Symbol(), PERIOD_M30, SMA_Period, 0, MODE_SMA, PRICE_CLOSE);
    handleMACD = iMACD(Symbol(), PERIOD_M30, MACD_Fast, MACD_Slow, MACD_Signal, PRICE_CLOSE);
    handleRSI = iRSI(Symbol(), PERIOD_M30, RSI_Period, PRICE_CLOSE);
    handleBands = iBands(Symbol(), PERIOD_M30, Bollinger_Period, Bollinger_Deviation, 0, PRICE_CLOSE);
    handleStoch = iStochastic(Symbol(), PERIOD_M30, KDJ_Period_K, KDJ_Period_D, KDJ_Period_J, MODE_SMA, STO_LOWHIGH);
    handleATR = iATR(Symbol(), PERIOD_M30, 14);

    // Validate Handles
    if (handleEMA == INVALID_HANDLE || handleSMA == INVALID_HANDLE || handleMACD == INVALID_HANDLE ||
        handleRSI == INVALID_HANDLE || handleBands == INVALID_HANDLE || handleStoch == INVALID_HANDLE ||
        handleATR == INVALID_HANDLE)
    {
        Print("Error initializing one or more indicators.");
        return INIT_FAILED;
    }

    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Calculate Indicators                                            |
//+------------------------------------------------------------------+
void CalculateIndicators()
{
    // Copy Indicator Buffers
    if (CopyBuffer(handleEMA, 0, 0, 1, EMA50) <= 0) Print("Error copying EMA: ", GetLastError());
    if (CopyBuffer(handleSMA, 0, 0, 1, SMA200) <= 0) Print("Error copying SMA: ", GetLastError());
    if (CopyBuffer(handleMACD, 0, 0, 1, MACD_Main) <= 0) Print("Error copying MACD Main: ", GetLastError());
    if (CopyBuffer(handleMACD, 1, 0, 1, MACD_SignalLine) <= 0) Print("Error copying MACD Signal: ", GetLastError());
    if (CopyBuffer(handleRSI, 0, 0, 1, RSI_Value) <= 0) Print("Error copying RSI: ", GetLastError());
    if (CopyBuffer(handleBands, 0, 0, 1, BB_Upper) <= 0) Print("Error copying Bollinger Upper: ", GetLastError());
    if (CopyBuffer(handleBands, 1, 0, 1, BB_Lower) <= 0) Print("Error copying Bollinger Lower: ", GetLastError());
    if (CopyBuffer(handleBands, 2, 0, 1, BB_Mid) <= 0) Print("Error copying Bollinger Mid: ", GetLastError());
    if (CopyBuffer(handleStoch, 0, 0, 1, K_Value) <= 0) Print("Error copying Stoch K: ", GetLastError());
    if (CopyBuffer(handleStoch, 1, 0, 1, D_Value) <= 0) Print("Error copying Stoch D: ", GetLastError());
    if (CopyBuffer(handleATR, 0, 0, 1, ATR_Value) <= 0) Print("Error copying ATR: ", GetLastError());

    // Calculate J value (KDJ indicator logic)
    J_Value[0] = (3 * K_Value[0]) - (2 * D_Value[0]);
}

//+------------------------------------------------------------------+
//| Entry Conditions                                                |
//+------------------------------------------------------------------+
bool BuySignal()
{
    return (Close[0] > EMA50[0] &&
            Close[0] > SMA200[0] &&
            MACD_Main[0] > MACD_SignalLine[0] &&
            RSI_Value[0] < RSI_Overbought &&
            Close[0] > BB_Mid[0] &&
            K_Value[0] > D_Value[0] && J_Value[0] > K_Value[0]);
}

bool SellSignal()
{
    return (Close[0] < EMA50[0] &&
            Close[0] < SMA200[0] &&
            MACD_Main[0] < MACD_SignalLine[0] &&
            RSI_Value[0] > RSI_Oversold &&
            Close[0] < BB_Mid[0] &&
            K_Value[0] < D_Value[0] && J_Value[0] < K_Value[0]);
}

//+------------------------------------------------------------------+
//| Execute Trades                                                  |
//+------------------------------------------------------------------+
void ExecuteTrade()
{
    CalculateIndicators();

    double atrDistance = ATR_Value[0] * StopLossATR_Multiplier;
    double stopLossDistance = atrDistance;
    double takeProfitDistance = stopLossDistance * RiskRewardRatio;

    MqlTradeRequest request = {};
    MqlTradeResult result = {};
    request.symbol = Symbol();
    request.volume = Lot_Size;
    request.deviation = Slippage;
    request.magic = MagicNumber;
    request.type_filling = ORDER_FILLING_FOK;

    if (BuySignal() && PositionsTotal() == 0)
    {
        double price = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK), _Digits);
        request.type = ORDER_TYPE_BUY;
        request.price = price;
        request.sl = NormalizeDouble(price - stopLossDistance, _Digits);
        request.tp = NormalizeDouble(price + takeProfitDistance, _Digits);

        if (!OrderSend(request, result))
            Print("Buy Order Failed. Error: ", GetLastError());
    }
    else if (SellSignal() && PositionsTotal() == 0)
    {
        double price = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID), _Digits);
        request.type = ORDER_TYPE_SELL;
        request.price = price;
        request.sl = NormalizeDouble(price + stopLossDistance, _Digits);
        request.tp = NormalizeDouble(price - takeProfitDistance, _Digits);

        if (!OrderSend(request, result))
            Print("Sell Order Failed. Error: ", GetLastError());
    }
}

//+------------------------------------------------------------------+
//| OnTick Function                                                 |
//+------------------------------------------------------------------+
void OnTick()
{
    CopyClose(Symbol(), PERIOD_M30, 0, 1, Close); // Ensure Close price is updated
    ExecuteTrade();
}

//+------------------------------------------------------------------+
//| OnDeinit Function                                               |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    IndicatorRelease(handleEMA);
    IndicatorRelease(handleSMA);
    IndicatorRelease(handleMACD);
    IndicatorRelease(handleRSI);
    IndicatorRelease(handleBands);
    IndicatorRelease(handleStoch);
    IndicatorRelease(handleATR);
}
Anyone any idea where it might go wrong?
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be...
 
Mike Nobel:
I have written a bot that automatically places trades based on multiple indicators, after some trying and compiling I succeeded and went on with Metatrader 5 on Vantage, in the strategy tester it is giving me error code 4756 Invalid Request in the journal, I've already tried analysing the problem with chatgpt, but it doesn't seem to fix it.. 

Anyone any idea where it might go wrong?

Hello 

You needed to add the request.action = TRADE_ACTION_DEAL;

and then call the GetFillingA function for the type filling.

void ExecuteTrade()
{
    CalculateIndicators();

    double atrDistance = ATR_Value[0] * StopLossATR_Multiplier;
    double stopLossDistance = atrDistance;
    double takeProfitDistance = stopLossDistance * RiskRewardRatio;

    MqlTradeRequest request = {};
    MqlTradeResult result = {};
    request.action = TRADE_ACTION_DEAL;
    request.symbol = Symbol();
    request.volume = Lot_Size;
    request.deviation = Slippage;
    request.magic = MagicNumber;
    request.type_filling = GetFillingA(_Symbol);


ENUM_ORDER_TYPE_FILLING GetFillingA( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
  const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
  const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);

  return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
         (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
           ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
          (ENUM_ORDER_TYPE_FILLING)Type);
}

 
also check the order of your variables of handlBands. deviation and shift are in wrong order.