mql4back-testingexpert-advisor

MQL4 - Expert Advisor EMA issues at closing order


I'm trying to build my own EA using EMA. When the program starts it will buy/sell according to the relative position of slow and fast EMA. Then the program should close the trade at the next EMA crossover. My first obstacle was to find a way to calculate EMA at the closing of candlestick, so I asked chatgpt to help me. After lots of attempts he brought me something using currenTime and prevTime values + the if condition. The calculations work even though i don't really understand why. I would be glad if someone can explain it to me. Now i'm struggling at closing my orders, when printing the EMA values I see the crossover but the program still won't close the trade. I'm sharing the code, i am backtesting it on GBP/JPY on M15 from 24 apr to 25 apr, there are 2 crossovers (picture), the program should run 3 trades on that day. Any help? here's my code below.

//+------------------------------------------------------------------+
//|                                                      EACrossover.mq4 |
//|                        Copyright 2024, Company Name               |
//|                                       http://www.company.net     |
//+------------------------------------------------------------------+
#property strict

// Global variables
input int fastEMA_Length = 9;     // Length of fast EMA
input int slowEMA_Length = 21;     // Length of slow EMA
input double lotSize = 0.1;  // Lot size
input int slippage = 3;     // slippage
input int magicNumber = 123456; // magic number
int buy_ticket, sell_ticket; // variables that store the OrderSend values
bool buying_order=false;
bool selling_order=false;
int i = 0; // bar counter
datetime prevTime = 0; // Variable to store the time of the previous tick (Created by ChatGPT)
// It is used for the calculation of EMA at the closure of candlestick

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   Print("EA Initialization");
   return(INIT_SUCCEEDED);
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   datetime currentTime = Time[0]; // Get the time of the current tick

// Check if the current tick is on a new bar by comparing its time with the previous tick
   if(currentTime != prevTime)
     {
      // Update the previous tick time with the current tick time
      prevTime = currentTime;
      i++; // Increment the bar counter

      // Calculate the EMAs for the current bar
      double currentFastEMA = iMA(NULL, 0, fastEMA_Length, 0, MODE_EMA, PRICE_CLOSE, 0);
      double currentSlowEMA = iMA(NULL, 0, slowEMA_Length, 0, MODE_EMA, PRICE_CLOSE, 0);

      if(OrdersTotal() == 0) // We open a trade when there is no trade actually opened
        {

         // Check EMA position to buy or sell
         if(currentFastEMA > currentSlowEMA)
           {
            buy_ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, slippage, 0, 0, "", magicNumber, 0, Green);
            buying_order=true;
            Print("I'm buying !");
           }
         else
           {
            sell_ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, slippage, 0, 0, "", magicNumber, 0, Green);
            selling_order=true;
            Print("I'm selling !");
           }
        }
      else
        {
         Print("FastEMA=",currentFastEMA," slowEMA=",currentSlowEMA," iteration n°",i);
         // If a trade is opened we decide to close it at the next EMA crossover
         if(buying_order==true)  //closing buying order
           {
            if(currentFastEMA < currentSlowEMA)
              {
               OrderClose(buy_ticket,lotSize, PRICE_CLOSE,slippage,clrNONE);
              }
            buying_order=false;
           }

         if(selling_order==true)  //closing selling order
           {
            if(currentFastEMA > currentSlowEMA)
              {
               OrderClose(sell_ticket,lotSize,PRICE_CLOSE,slippage,clrNONE);
              }
            selling_order=false;
           }
        }
     }
  }

Sometime I have the error 138 depending TF.


Solution

  • To close a buy order one must close it at bid price. To close a sell order one must close it at ask price.

    That means we rewrite the code :

                if(buying_order && currentFastEMA < currentSlowEMA) //closing buy order
                 {              
                   OrderClose(buy_ticket,lotSize, **Bid**,slippage,clrNONE);
                   buying_order=false;
                 }       
         
                if(selling_order && currentFastEMA > currentSlowEMA) //closing sell order
                 {
                   OrderClose(sell_ticket,lotSize,**Ask**,slippage,clrNONE);
                   selling_order=false;
                }