Trader’s Tech – Writing Your Own EA Part 63 – Detecting Events

Forex Robot

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 62 we began talking about how to detect events, specifically trading events, in MQL4. We’ll continue that discussion in this installment.

For the purposes of this article, I’ve written a sample DetectTradeEvent() function. As with any programming exercise, there are only a thousand other ways to do this. I just wanted to provide an example. Most of the samples I provide in this series are actual code segments that I use. This one is not. It is completely untested code. So take that for what it’s worth. 😉 You can download this function in the form of a .mqh (MQL4 include file) from my GitHub.


int DetectTradeEvent(string PairName, int MagicNumber,
                     datetime EntryTime, int Type)
   int Status = 0; //0 = No Event, -1 = New Short Trade, -2 = New Short Pending, 
                   //1 = New Long Trade, 2 = New Long Pending, 10 = Trade Closed
   int i,TotalOrders = OrdersTotal();
   bool OrderFound = false;
   int OType;

   for(i = 0; i < TotalOrders; i++)
      if(OrderSymbol() == PairName && 
         (MagicNumber == -1 || MagicNumber == OrderMagicNumber()))
         OrderFound = true;

         if(OrderOpenTime() != EntryTime || OrderType() != Type)
            OType = OrderType();
            if(OType == OP_SELLLIMIT || OType == OP_SELLSTOP)
               Status = -2;
            if(OType == OP_BUYLIMIT || OType == OP_BUYSTOP)
               Status = 2;
            if(OType == OP_SELL)
               Status = -1;
            if(OType == OP_BUY)
               Status = 1;
            } //if(OrderOpenTime() != EntryTime || OrderType() != Type)

         } //if(OrderSymbol() == PairName && (MagicNumber == -1 || MagicNumber == OrderMagicNumber()))
      } //for(i = 0;i < TotalOrders;i++)

   if(Type != -1 && !OrderFound) //Was expecting order and none found
   Status = 10;

   } //int DetectTradeEvent(string PairName, int MagicNumber, datetime EntryTime, int Type)


We’ve declared this function to return the status to the calling function. We pass the following parameters to the function:

PairName – string – This is the pairname for which we are looking. Our code doesn’t make any special allowance for symbol suffixes

MagicNumber – int – The magic number is necessary if you are looking for a trade entered by an EA. Otherwise, just send -1.

EntryTime – datetime – If you are looking for an existing order send the order entry time (the MQL4 function OrderOpenTime() will return that value when the order is selected)

Type – int – This must correspond to the value returned by MQL4’s OrderType() function. As you see later in the code, we will compare this to the OrderType() of the orders through which we search.

Upon completion our function will return the Status based upon the provided parameters. Status will return the following values:

0 = No Event Detected

-1 = New Short Trade Entered

-2 = New Short Pending Entered

1 = New Long Trade Entered

2 = New Long Pending Entered

10 = Order/Trade has been closed and no new order is found matching the pair/magic number combination.

The status can return much more information, but for the purposes of this example I’ve limited it to the above six possible situations. A look at the code will make the returned status more clear.

We’ll start by declaring our integer variable Status and initializing it to 0. That way we only have to worry about assigning a new Status if we actually detect a trade event. Remember, we’re not just looking for an open trade, we have to return a Status of 10 if there was an order (indicated by the passed parameters EntryTime and Type) and now there is none.

Next we’ll declare our counter integer i and an integer, TotalOrders, to store the total number of orders. You can actually use the OrdersTotal() function in the for() statement, but I’ve made it a habit to assign that to a variable.

Now we declare the boolean OrderFound and initialize it to false. That will let us know if there was an order found in our loop.

And lastly we declare the integer variable OType to store the OrderType() of our active order as we search.

Next time we’ll go through the code and see what’s going on. Thanks for your attention and please follow me on Twitter and LinkedIn.




The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news