It does not automatically open orders

 
Hello. Kind regards to anyone who might read this. I would like your help.

My EA compiles without errors, but when I backtest it, it does not open trades automatically. Can you please help me fix this? So that I can open the trade when the conditions are met?

Thank you in advance.
#include <Trade\Trade.mqh>

input int EMA_Period_1 = 20;           // Periodo de la EMA de 20
input int EMA_Period_2 = 50;           // Periodo de la EMA de 50
input int RSI_Period = 14;              // Periodo del RSI
input double TakeProfitPips = 30;       // Take Profit en pips
input double StopLossPips = 10;         // Stop Loss en pips
input double LotSize = 0.01;             // Tamaño de lote para las operaciones

CTrade trade;  // Declarar objeto de la clase CTrade

void OnTick()
{
    // Obtener el símbolo actual
    string symbol = Symbol();

    // Calcular EMAs y RSI en el periodo de 1 minuto
    double ema_20 = iMA(symbol, PERIOD_M1, EMA_Period_1, 0, MODE_EMA, PRICE_CLOSE);
    double ema_50 = iMA(symbol, PERIOD_M1, EMA_Period_2, 0, MODE_EMA, PRICE_CLOSE);
    double rsi = iRSI(symbol, PERIOD_M1, RSI_Period, PRICE_CLOSE);

    // Imprimir los valores para depuración
    Print("EMA 20: ", ema_20, " EMA 50: ", ema_50, " RSI: ", rsi);

    // Dibuja líneas en el gráfico para EMA 20 y EMA 50
    if (ObjectFind(0, "EMA20") != -1) 
        ObjectDelete(0, "EMA20");  // Corregido: se pasa el gráfico como primer argumento

    ObjectCreate(0, "EMA20", OBJ_TREND, 0, TimeCurrent(), ema_20, TimeCurrent() + PeriodSeconds(), ema_20);
    ObjectSetInteger(0, "EMA20", OBJPROP_COLOR, clrBlue);
    
    if (ObjectFind(0, "EMA50") != -1) 
        ObjectDelete(0, "EMA50");  // Corregido: se pasa el gráfico como primer argumento

    ObjectCreate(0, "EMA50", OBJ_TREND, 0, TimeCurrent(), ema_50, TimeCurrent() + PeriodSeconds(), ema_50);
    ObjectSetInteger(0, "EMA50", OBJPROP_COLOR, clrRed);

    // Verificar si no hay posiciones abiertas
    if (PositionsTotal() == 0) 
    {
        // Verificar condición para compra
        if (ema_20 > ema_50 && rsi < 70)
        {
            Print("Se cumple la condición de compra.");
            double price = SymbolInfoDouble(symbol, SYMBOL_ASK);
            double sl = price - StopLossPips * SymbolInfoDouble(symbol, SYMBOL_POINT);   // Calcular Stop Loss
            double tp = price + TakeProfitPips * SymbolInfoDouble(symbol, SYMBOL_POINT); // Calcular Take Profit
            
            if (!trade.Buy(LotSize, NULL, price, sl, tp, "Compra por cruce EMA")) {
                Print("Error al abrir compra: ", GetLastError());
            }
        }

        // Verificar condición para venta
        if (ema_20 < ema_50 && rsi > 30)
        {
            Print("Se cumple la condición de venta.");
            double price = SymbolInfoDouble(symbol, SYMBOL_BID);
            double sl = price + StopLossPips * SymbolInfoDouble(symbol, SYMBOL_POINT);   // Calcular Stop Loss
            double tp = price - TakeProfitPips * SymbolInfoDouble(symbol, SYMBOL_POINT); // Calcular Take Profit
            
            if (!trade.Sell(LotSize, NULL, price, sl, tp, "Venta por cruce EMA")) {
                Print("Error al abrir venta: ", GetLastError());
            }
        }
    }
};
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 
RiLu Manzon: Hello. Kind regards to anyone who might read this. I would like your help. My EA compiles without errors, but when I backtest it, it does not open trades automatically. Can you please help me fix this? So that I can open the trade when the conditions are met? Thank you in advance.

You are mixing MQL4 with MQL5. The indicator functions like iMA(), do not return buffer values. It returns a handle which you use with CopyBuffer() function.

Please reference the documentation on the correct way to use the iMA() function in MQL5.

And, in case this is ChatGPT code, then please stop using.

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
The function returns the handle of the Moving Average indicator. It has only one buffer. Parameters symbol [in] The symbol name of the security...
 

Hi

As was mentioned above – this is code in mq5 and the MA and RSI return handles not buffer values. You need to add those functions in the OnInit and here use CopyBuffer functions to get the values like I showed you below – example is for getting values from last closed bar.  Also you don’t need those ObjectFind functions.

int bar =1;

double rsivalues[1];
int copied1 = CopyBuffer(rsi,0,bar,1,rsivalues);

double ema20values[1];
int copied2 = CopyBuffer(ema_20,0,bar,1,ema20values);
double ema50values[1];
int copied3 = CopyBuffer(ema_50,0,bar,1,ema50values);
...
if(ema20values

Have a nice day👍📊

 
@Marzena Maria Szmit y @Fernando Carreiro Thank you very much for taking the time to read my code and respond to my post. You are already running the EA correctly. I am sorry if using chatgpt is bad practice, but I don't know the programming language and where I can get it.
Marzena Maria Szmit
Marzena Maria Szmit
  • 2024.09.17
  • www.mql5.com
Trader's profile
 
RiLu Manzon #: I am sorry if using chatgpt is bad practice, but I don't know the programming language and where I can get it.
You "get it", by learning it, by reading the documentation or the book and practising to code. This website is for helping those willing to learn, not for fixing the output from ChatGPT. If that is what you need, then consider using the Freelance section instead.
 

Fernando Carreiro #:
You "get it", by learning it, by reading the documentation or the book and practising to code. This website is for helping those willing to learn, not for fixing the output from ChatGPT. If that is what you need, then consider using the Freelance section instead.


Can you provide me with a link where I can read the MT5 language?

 
RiLu Manzon #:Can you provide me with a link where I can read the MT5 language?

I did! Two links! Did you not see them?


They are also at the top menu of the website ...


 
I didn't see them, sorry. I use a translator, thanks bro.