Why is this simple EA not opening trades in mql5 strategy tester

 

Hello,


I wrote a very simple EA that opens a trade when there is a difference between 2 EMA's. All seems good to me except it does not open any trades on mql5.


Here is the code:


#property strict

input int EMA_50_Period = 50;  // EMA period for 50
input int EMA_200_Period = 200; // EMA period for 200
input double PipValue = 0.0001; // Pip value for 5-digit pricing
input double SL_Pips = 15;      // Stop loss in pips
input double TP_Pips = 15;      // Take profit in pips

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
    // Initialization function
  }

void OnTick()
  {
    // Calculate EMA values
    double ema_50 = iMA(_Symbol, 0, EMA_50_Period, 0, MODE_EMA, PRICE_CLOSE);
    double ema_200 = iMA(_Symbol, 0, EMA_200_Period, 0, MODE_EMA, PRICE_CLOSE);

    // Calculate the difference in pips
    double pips_difference = (ema_50 - ema_200) / PipValue;

    // Check if the condition is met for opening a long trade
    if (pips_difference > 50)
      {
        // Declare variables for Bid and Ask prices
        double bid, ask;
        // Get the Bid and Ask prices
        SymbolInfoDouble(_Symbol, SYMBOL_BID, bid);
        SymbolInfoDouble(_Symbol, SYMBOL_ASK, ask);
        
        // Calculate stop loss and take profit levels
        double stop_loss = NormalizeDouble(ask - SL_Pips * _Point, _Digits);
        double take_profit = NormalizeDouble(ask + TP_Pips * _Point, _Digits);

        // Prepare a trading request
        MqlTradeRequest request={};;
        request.action = TRADE_ACTION_DEAL;   // Action: dealing trade
        request.symbol = _Symbol;             // Symbol
        request.volume = 0.01;                // Volume
        request.price = ask;                  // Price
        request.type = ORDER_TYPE_BUY;        // Order type: buy
        request.sl = stop_loss;               // Stop loss
        request.tp = take_profit;             // Take profit

        // Send the order request
        MqlTradeResult result = {0};
        if(!OrderSend(request, result))
        {
            Print("OrderSend failed with error code ", GetLastError());
        }
      }
  }
//+------------------------------------------------------------------+

Any ideas on why it is not working?


Thanks

 
Because it's not correctly coded. Chat GPT? Searching into codebase will give you better examples that trying using AI...
 
Fabio Cavalloni #:
Because it's not correctly coded. Chat GPT? Searching into codebase will give you better examples that trying using AI...

I wrote it in mql4 and did use chatgpt to make it to mql5. It did give some errors, but I fixed them and thought it was all good. You can say I am still a beginner at coding lol


Can you tell me which parts are incorrectly coded, I will look into them.

Thanks

 
Fabio Cavalloni #:
Because it's not correctly coded. Chat GPT? Searching into codebase will give you better examples that trying using AI...

Nvm, got it. I have to use copybuffer.

 
Adel Basher #:

Nvm, got it. I have to use copybuffer.

Yes, the indicator usage is wrong, as always from codes coming from AI that mixes mql4 and mql5 despite their differences.

Initialize handles with iMA commands into OnInit.

Use CopyBuffer into OnTick to retrieve values of your indicators.
 

In MQL5 you also need to define the request.type_filling, it won't work on the metaquotes demo using this oldschool approach to executing trades, this will only work on a broker demo account (after you fix the iMA with copybuffer)

There are youtube tutorials which discuss how to use the Trade.mqh library which is a better way to execute trades with the EA

 
Fabio Cavalloni #:
Yes, the indicator usage is wrong, as always from codes coming from AI that mixes mql4 and mql5 despite their differences.

Initialize handles with iMA commands into OnInit.

Use CopyBuffer into OnTick to retrieve values of your indicators.

Yes, I trusted it lol.


Thanks, looked into the codebase and it was an easy fix

 
Conor Mcnamara #:

In MQL5 you also need to define the request.type_filling, it won't work on the metaquotes demo using this oldschool approach to executing trades, this will only work on a broker demo account (after you fix the iMA with copybuffer)

There are youtube tutorials which discuss how to use the Trade.mqh library which is a better way to execute trades with the EA

Thank you, got it to work now :D