help with metatrader5 code

 
Hello everyone, I converted this code from MT4 to MT5 but in the MT5 backtest the EA is unable to open any positions even if no errors are detected during compilation. I can't understand why. Is there any error in the logic of the code? Thanks to anyone who is willing to help me.
input double TP = 300.0;          // Take profit in points
input double SL = 250.0;          // Stop loss in points
input int MaxPositions = 1;       // Maximum number of concurrent positions
input double LotSize = 0.10;      // Trade size
input int MagicNumber = 1211103;  // Magic number for orders

bool rsi30Flag = false;
bool rsi70Flag = false;

void OnTick()
{
    int totalBuyOrders = 0;
    int totalSellOrders = 0;

    for (int i = 0; i < OrdersTotal(); i++)
    {
        ulong ticket = OrderGetTicket(i);
        if (ticket != 0)
        {
            if (OrderGetString(ORDER_SYMBOL) == Symbol() && OrderGetInteger(ORDER_MAGIC) == MagicNumber)
            {
                if (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY)
                    totalBuyOrders++;
                else if (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL)
                    totalSellOrders++;
            }
        }
    }

    double stochastic = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, STO_LOWHIGH);  // Use correct parameters
    double rsi = iRSI(Symbol(), 0, 14, PRICE_CLOSE);

    if (stochastic <= 20 && totalBuyOrders < MaxPositions)
    {
        if (rsi <= 30)
        {
            rsi30Flag = true;
        }
        else if (rsi >= 35 && rsi30Flag)
        {
            double stopLossLong = SymbolInfoDouble(Symbol(), SYMBOL_BID) - SL * _Point;
            MqlTradeRequest request = {};
            request.action = TRADE_ACTION_DEAL;  // Correct trade action
            request.symbol = Symbol();
            request.volume = LotSize;
            request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
            request.sl = stopLossLong;
            request.tp = request.price + TP * _Point;
            request.magic = MagicNumber;
            request.type = ORDER_TYPE_BUY;
            MqlTradeResult result;
            if (OrderSend(request, result))
            {
                Print("Long order opened successfully.");
                rsi30Flag = false;
            }
            else
            {
                Print("Error opening long order: ", GetLastError());
            }
        }
    }

    if (stochastic >= 80 && totalSellOrders < MaxPositions)
    {
        if (rsi >= 70)
        {
            rsi70Flag = true;
        }
        else if (rsi <= 65 && rsi70Flag)
        {
            double stopLossShort = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + SL * _Point;
            MqlTradeRequest request = {};
            request.action = TRADE_ACTION_DEAL;  // Correct trade action
            request.symbol = Symbol();
            request.volume = LotSize;
            request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
            request.sl = stopLossShort;
            request.tp = request.price - TP * _Point;
            request.magic = MagicNumber;
            request.type = ORDER_TYPE_SELL;
            MqlTradeResult result;
            if (OrderSend(request, result))
            {
                Print("Short order opened successfully.");
                rsi70Flag = false;
            }
            else
            {
                Print("Error opening short order: ", GetLastError());
            }
        }
    }
}
 

Sorry for the mistake.

I'm very inexperienced with MT5, and I'm not sure how to rectifying the code. I'm interested in identifying the errors and learning how to solve them to make it functional.

 
Dhavderiva #:

Sorry for the mistake.

I'm very inexperienced with MT5, and I'm not sure how to rectifying the code. I'm interested in identifying the errors and learning how to solve them to make it functional.

iStoch and iRSI give you a handle, create it once in OnInit(), use CopyBuffer to access the data.

Read the manual about iRSI and CopyBuffer, look at the examples given.
 
Hello. If it can help you I made two codes with stochastic and RSI. https://www.mql5.com/en/code/46331 and https://www.mql5.com/en/code/46158