No DEAL_ENTRY_OUT? How do I determine a deal closes a Position? - page 2

 
Fernando Carreiro #:

Please show the sample of your test trade history as shown by MetaTrader, that shows "ins" and "outs". Here is an example of only the deals ...


Then show debugging log output from your program for the same deals shown in your trade history.

Sample Logs: 

Deal Entry Logs


Full History: 


Log Deals


To see the loop that generated this log, check previous comments. Thanks for looking into this for me. If no one is having this issue, then I will chalk it down to being a broker platform issue. 

 

Use the following to test on your own setup and see if it produces equivalent output as mine.

If it does, then use it as a reference to analyse your own code. But if not, report your results as I have done below ...

Sample MQL5 Script code ...

#property script_show_inputs

input datetime i_dtStart = D'2023.11.29 00:00:00';
   
void OnStart( void ) {
   // Get current time
      datetime dtCurrent = TimeCurrent();
   // Get trade history
      ResetLastError();
      if( HistorySelect( i_dtStart, dtCurrent ) ) {
         // Get total number of deals
            int nDealsTotal = HistoryDealsTotal();
            Print( "Total Deals Count: ", nDealsTotal );
         // Scan the deals
            for( int i = 0; i < nDealsTotal; i++ ) {
               // Select and get ticket for deal by index
                  ResetLastError();
                  ulong nDealTicket = HistoryDealGetTicket( i );
                  if( nDealTicket > 0 ) {
                     // Get deal properties
                        #define _MDealGetInteger( _var, _property, _type ) \
                           _type _var = (_type) HistoryDealGetInteger( nDealTicket, _property )
                        _MDealGetInteger( nDealPositionID, DEAL_POSITION_ID, long             );
                        _MDealGetInteger( dtDealTime,      DEAL_TIME,        datetime         );
                        _MDealGetInteger( eDealType,       DEAL_TYPE,        ENUM_DEAL_TYPE   );
                        _MDealGetInteger( eDealEntry,      DEAL_ENTRY,       ENUM_DEAL_ENTRY  );
                        _MDealGetInteger( eDealReason,     DEAL_REASON,      ENUM_DEAL_REASON );
                        #undef _MDealGetInteger
                     // Print deal properties
                        Print( "Deal (", i, ") — Ticket: ",  nDealTicket,
                           ", Position ID: ", nDealPositionID,
                           ", Time: ",        dtDealTime,
                           ", Type: ",        EnumToString( eDealType   ),
                           ", Entry: ",       EnumToString( eDealEntry  ),
                           ", Reason: ",      EnumToString( eDealReason ),
                           "" );
                  } else {
                     PrintFormat( "Error %d: Unable to select deal by index!", _LastError );
                  };
            };
      } else {
         PrintFormat( "Error %d: Unable to aquire the trade history!", _LastError );
      };
};

Sample log output ...

(EURUSD,H1) Total Deals Count: 8
(EURUSD,H1) Deal (0) — Ticket: 306153369, Position ID: 360127657, Time: 2023.11.29 02:56:59, Type: DEAL_TYPE_BUY, Entry: DEAL_ENTRY_IN, Reason: DEAL_REASON_CLIENT
(EURUSD,H1) Deal (1) — Ticket: 306153397, Position ID: 360127657, Time: 2023.11.29 02:57:26, Type: DEAL_TYPE_SELL, Entry: DEAL_ENTRY_OUT, Reason: DEAL_REASON_CLIENT
(EURUSD,H1) Deal (2) — Ticket: 306153398, Position ID: 360127692, Time: 2023.11.29 02:57:29, Type: DEAL_TYPE_SELL, Entry: DEAL_ENTRY_IN, Reason: DEAL_REASON_CLIENT
(EURUSD,H1) Deal (3) — Ticket: 306153402, Position ID: 360127692, Time: 2023.11.29 02:57:32, Type: DEAL_TYPE_BUY, Entry: DEAL_ENTRY_OUT, Reason: DEAL_REASON_CLIENT
(EURUSD,H1) Deal (4) — Ticket: 317096942, Position ID: 372858088, Time: 2024.02.09 14:26:25, Type: DEAL_TYPE_SELL, Entry: DEAL_ENTRY_IN, Reason: DEAL_REASON_CLIENT
(EURUSD,H1) Deal (5) — Ticket: 317096966, Position ID: 372858088, Time: 2024.02.09 14:26:50, Type: DEAL_TYPE_BUY, Entry: DEAL_ENTRY_OUT, Reason: DEAL_REASON_SL
(EURUSD,H1) Deal (6) — Ticket: 317157540, Position ID: 372922337, Time: 2024.02.09 17:43:49, Type: DEAL_TYPE_SELL, Entry: DEAL_ENTRY_IN, Reason: DEAL_REASON_CLIENT
(EURUSD,H1) Deal (7) — Ticket: 317158861, Position ID: 372922337, Time: 2024.02.09 17:48:02, Type: DEAL_TYPE_BUY, Entry: DEAL_ENTRY_OUT, Reason: DEAL_REASON_CLIENT

Sample trade history ...


 
Fernando Carreiro #:

Use the following to test on your own setup and see if it produces equivalent output as mine.

If it does, then use it as a reference to analyse your own code. But if not, report your results as I have done below ...

Sample MQL5 Script code ...

Thank you soo much, this actually gives DEAL_ENTRY_OUT. Once I figure out the flaw in my code I will make a follow up. Thanks again


Edit/Follow-up: In the complete version of my code, I actually look into the data from the start of position part way through collecting data on the current ticket, which dequeues the current ticket (Reason being to collect the commission so I get an accurate final profit sum). Though embarrassing, I don't want to cause any confusion due to my oversight. Thanks for the help again

 
TraderTogami #: Edit/Follow-up: In the complete version of my code, I actually look into the data from the start of position part way through collecting data on the current ticket, which dequeues the current ticket (Reason being to collect the commission so I get an accurate final profit sum). Though embarrassing, I don't want to cause any confusion due to my oversight. Thanks for the help again

It is an understandable oversight, due to the "strange" ways that MetaQuotes has implemented the trade history. It is best to cache your results in memory, to help prevent the issue.

 
I would like to correct my statement because not true:
dcstoyanov #:

Closing a position by a SL or TP doesn't trigger DEAL_ENTRY_OUT.

A deal caused by a SL/TP has an entry ENTRY_OUT.


I suppose your issue was caused by the fact that your positions were closed manually:

TraderTogami #:

I made the trade close manually and the DEAL_ENTRY is still always DEAL_ENTRY_IN. Do you see a case of DEAL_ENTRY_OUT on your platform? 


If this is the case, you got a MAGIC_NUMBER = 0 and your loop failed here:

   for(int i = total_deals - 1; i >= 0 && !found_position; i--){
     ...
      found_position = ticket_accessible && curr_id == id && deal_entry == DEAL_ENTRY_OUT;
   }


The following image and the relative Print() output is an example where you can see the value of MAGIC_NUMBER, DEAL_ENTRY and DEAL_REASON.

2025.01.17 13:15:00.604 eaTEST (US500,M1)        | MAGIC: 101 | TICKET: 14609847 | ID: 26813628 | ENTRY: DEAL_ENTRY_IN | REASON: DEAL_REASON_EXPERT
2025.01.17 13:17:42.452 eaTEST (US500,M1)        | MAGIC: 101 | TICKET: 14609939 | ID: 26813628 | ENTRY: DEAL_ENTRY_OUT | REASON: DEAL_REASON_TP

2025.01.17 13:18:00.678 eaTEST (US500,M1)        | MAGIC: 101 | TICKET: 14609971 | ID: 26813774 | ENTRY: DEAL_ENTRY_IN | REASON: DEAL_REASON_EXPERT
2025.01.17 13:18:22.693 eaTEST (US500,M1)        | MAGIC: 0 | TICKET: 14609985 | ID: 26813774 | ENTRY: DEAL_ENTRY_OUT | REASON: DEAL_REASON_CLIENT

2025.01.17 13:21:00.620 eaTEST (US500,M1)        | MAGIC: 101 | TICKET: 14610043 | ID: 26813868 | ENTRY: DEAL_ENTRY_IN | REASON: DEAL_REASON_EXPERT
2025.01.17 13:24:00.574 eaTEST (US500,M1)        | MAGIC: 101 | TICKET: 14610063 | ID: 26813868 | ENTRY: DEAL_ENTRY_OUT | REASON: DEAL_REASON_EXPERT

2025.01.17 13:27:00.328 eaTEST (US500,M1)        | MAGIC: 101 | TICKET: 14610127 | ID: 26813997 | ENTRY: DEAL_ENTRY_IN | REASON: DEAL_REASON_EXPERT
2025.01.17 13:27:15.892 eaTEST (US500,M1)        | MAGIC: 101 | TICKET: 14610129 | ID: 26813997 | ENTRY: DEAL_ENTRY_OUT | REASON: DEAL_REASON_SL
No DEAL_ENTRY_OUT? How do I determine a deal closes a Position? - I am running into an issue with TraderTogami, where every time I request the DEAL IN is returned no matter if I
No DEAL_ENTRY_OUT? How do I determine a deal closes a Position? - I am running into an issue with TraderTogami, where every time I request the DEAL IN is returned no matter if I
  • 2025.01.06
  • TraderTogami
  • www.mql5.com
I am running into an issue, where every time i request the deal in is returned. Related code found below parameter causing an issue because i have confirmed that all available tickets are being found no problem by printing out necessary information