How do I avoid selecting a closed ticket?

 

I tested order select for false condition of a closed ticket which returns true and not false.

Print((bool)OrderSelect(61057926,SELECT_BY_TICKET,MODE_TRADES));

How to know if a ticket was closed, by not being an open order to select?

Documentation on MQL5: Trade Functions / OrderSelect
Documentation on MQL5: Trade Functions / OrderSelect
  • www.mql5.com
OrderSelect - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Brian Lillard:

I tested order select for false condition of a closed ticket which returns true and not false.

How to know if a ticket was closed, by not being an open order to select?

Always check the documentation

OrderSelect

The pool parameter is ignored if the order is selected by the ticket number. The ticket number is a unique order identifier.


Check the order close time to see if it has been closed.
 
Keith Watford #:

Always check the documentation

Check the order close time to see if it has been closed.

Sure it's convienient to read historical orders

 
Brian Lillard #:

Sure it's convienient to read historical orders

I don't know what that means.

 
Brian Lillard #:

Sure it's convienient to read historical orders

Hello?

I am completely puzzled by your response.

I would appreciate it if you could explain what you mean.

 
Brian Lillard:

I tested order select for false condition of a closed ticket which returns true and not false.

How to know if a ticket was closed, by not being an open order to select?

If you select by ticket it returns "true" if the ticket exists, regardless of whether the position is closed, open, pending, etc. True = exists, false = doesn't exist. Selecting by position has a mode which lets you filter if you wanna look at the pool of trades (open + pending) or the history pool (closed). That mode parameter is ignored when selecting by ticket.

I understand, tho, that if you have the ticket number it's more convenient, faster and clean to check the ticket directly. In that case, if you wanna check if the order is closed or not just add...

if(SelectOrder(myTicket, SELECT_BY_TICKET, MODE_TRADES) && !OrderCloseTime()) // This is true if the order exists and is OPEN

...or, of course, the opposite...

if(SelectOrder(myTicket, SELECT_BY_TICKET, MODE_TRADES) && OrderCloseTime()) // This is true if the order exists and is CLOSED

Hope that helps.

 
Carlos Moreno Gonzalez #:

Please note that OrderCloseTime() returns a datetime, not a bool.

Although

!OrderCloseTime()

may do what you want, the correct way is

OrderCloseTime()==0
 
Brian Lillard: How to know if a ticket was closed, by not being an open order to select?
  1. The question has been answered: bool isClosed=OCT!=0;

  2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

 
Keith Watford #:

Please note that OrderCloseTime() returns a datetime, not a bool.

Although

may do what you want, the correct way is

Damn, I knew someone was gonna reply with that LOL. Yeah, I know it returns a DATETIME but any numeric value equals to zero is considered FALSE logically, and any numeric value different from zero is considered logically TRUE. Therefore the code does what I want because zero is FALSE and any other date is considered TRUE.

See the second example of my post here: https://www.mql5.com/en/forum/410367/page36#comment_42323466. Cheers.

EDIT: Since it's an interesting topic that a lot of people don't know about, I'll leave some more reading:

Boolean Operations

Logical Negation NOT (!)

Operand of the logical negation (!) must be of arithmetic type. The result is TRUE (1), if the operand value is FALSE (0); and it is equal to FALSE (0), if the operand differs from FALSE (0).

Source: https://docs.mql4.com/basis/operations/bool
Features of the mql4 language, subtleties and techniques
Features of the mql4 language, subtleties and techniques
  • 2022.09.27
  • www.mql5.com
This topic will discuss undocumented ways of working with the mql4 language , examples of solving certain problems...
 
Keith Watford #:

Hello?

I am completely puzzled by your response.

I would appreciate it if you could explain what you mean.

No more ever a need to use MODE_HISTORY ?

OrderCloseTime()==0 for closed trades

OrderCloseTime()!=0 for open trades

Thanks for your help

 
Brian Lillard #: No more ever a need to use MODE_HISTORY ?

OrderCloseTime()==0 for closed trades

OrderCloseTime()!=0 for open trades

  1. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. We don't know what you want to read.

  2. You have those backwards. You have to check if you select by ticket.

  3. If you select by position, you only have open and pending orders.

  4. If you select by position in MODE_HISTORY, you will never have open orders.

    MT4:

    1. Do not assume history has only closed orders.
                OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

    2. Do not assume history is ordered by date, it's not.
                Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
                Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

    3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
                "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

      Broker History
      FXCM
      Commission - <TICKET>
      Rollover - <TICKET>

      >R/O - 1,000 EUR/USD @0.52

      #<ticket>  N/A
      OANDA
      Balance update
      Financing (Swap: One entry for all open orders.)

Reason: