Check if another Order were closed

 

Hi Guys,

in my current EA I need to find out if a new order were closed.

It doesn't matter if it's closed by hitting TP or SL...

At the moment I use therefor:

bool NEW_CLOSE()
{
   bool new_close = false;
   static datetime close_time = 0;  
   
   for(int i=0; i<=OrdersHistoryTotal()-1; i++) 
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) 
         if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber) 
            if(OrderType()<2) 
               if(OrderCloseTime()> close_time)
               {
                  close_time = OrderCloseTime(); new_close = true;
               }    
return(new_close);
}

My Problem is, that he loop's trough ALL History Orders, meaning the longer the EA runs, the slower it will be (especially during backtesting --> very bad runtime).

Do you know if there is another way how I can find out if a new orderclose happend or not, w/o loop'ing trough ALL history orders?

 
  1. There is no need to call your function unless OrdersTotal() changes.

  2. Optimize your loop. One call for count (count down). Integer tests first. Most likely first.

       for(int i=OrdersHistoryTotal()-1; i >=0 ; --i) if(
          OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
       && OrderCloseTime()   >  close_time
       && OrderType()        <  2
       && OrderMagicNumber() == MagicNumber
       && OrderSymbol()      == Symbol()
       ){
  3. V <= X-1 is the same as V < X
 
Markus Wilhelm:

Hi Guys,

in my current EA I need to find out if a new order were closed.

It doesn't matter if it's closed by hitting TP or SL...

At the moment I use therefor:

My Problem is, that he loop's trough ALL History Orders, meaning the longer the EA runs, the slower it will be (especially during backtesting --> very bad runtime).

Do you know if there is another way how I can find out if a new orderclose happend or not, w/o loop'ing trough ALL history orders?

//+------------------------------------------------------------------+
//|                                               WindowsMessage.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

// https://www.mql5.com/ru/forum/110207/page3
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
// 33058 ALL HISTORY

// 33057 - Last 3 Months

// 33063 - Last Month
#include <WinUser32.mqh>
#import "user32.dll"
  int    GetAncestor(int, int);
#import
void OnStart()
  {
//---
   #define GA_ROOT 2
   // https://forum.mql4.com/ru/14463/page5#401551
   #define MT4_WMCMD_ALL_HISTORY 33063
   int      main = GetAncestor(WindowHandle(Symbol(), Period()), GA_ROOT);
   PostMessageA(main, WM_COMMAND, MT4_WMCMD_ALL_HISTORY, 0);
   
}