Question on best practice for Order Accounting

 

I'm new to coding EAs and have a question regarding the best (as in easiest and most robust) way to manage orders. My strategy will open up either 2 or 4 different orders with:

a) 1/2 set with a TP (set with the order and held on the broker's server)

b) the other 1/2 set for other exit criteria as controlled by the EA

All orders are also subject to certain global exit conditions including a fixed SL.

I'm unclear as to the best way to manage these trades, in particular to disintiguish between trades which hit TP and are closed by the server vs those my EA will close. In short, I'm not seeing a way to flag tickets with an ID I set other than including a note in the ticket comment which I've heard is not reliable as this data can be overwritten by the broker's server. So, I'm then thinking to save the ticket number in a client-side global variable along with the name/position of the trade. But, that seems like a lot of work and I was wondering if that was the best option.

Is the best practice for my case to have an OrderAccounting() function which loops through all open orders with each tick and compares them to ticket numbers stored in client global vars that are set when an order is made?

I'm envisioning logic and actions as follows:

Loop thru OrdersTotal():


1) No Trades Open. None Expected – if no open orders exist and no position global vars exist in client then break to next code block


2) No Trades Open. Some Expected – if no open orders exist but one or more position global vars exist in client then find closed orders by position ticket numbers and confirm open/close and print to journal and send to Report() function and delete appropriate client global var


3) Some Trades Open. Some Expected – Put ticket numbers into Tickets[] array.


4) Some Trades Open. None Expected – Error? Shouldn't happen where no position global vars exist in client but a trade is open. Could happen if trade opened manually or by other EA for the symbol used in this strategy.

Thanks in advance for insights and/or code examples.

Bill

 
I would use Magic# to distinguish between the 1/2 on broker side vs on my side.

As far as saving Ticket# and Arrays, I don't like either. Sure, I've seen allot of pro-programmers save Tickets and stuff to even external files. But I'm hoping I can get by without having that for the sake of ease.

I would much rather create a small function in my Library which returns the last Ticket# or Set of active ticket# before I play around with arrays. But that's just me.
 
ubzen:
I would use Magic# to distinguish between the 1/2 on broker side vs on my side.

As far as saving Ticket# and Arrays, I don't like either. Sure, I've seen allot of pro-programmers save Tickets and stuff to even external files. But I'm hoping I can get by without having that for the sake of ease.

I would much rather create a small function in my Library which returns the last Ticket# or Set of active ticket# before I play around with arrays. But that's just me.


Thanks ubzen. I forgot about magic numbers. However, the EA still needs to know which orders have been closed by the server and it seems to me the only sure way of knowing that is to store the ticket numbers on the client side in global vars (which persist in the event MT4 crashes and the EA needs to start up again) and compare that list with the list of open tickets on the server. Am I missing something here?

 

However, the EA still needs to know which orders have been closed by the server

As far as I know, for stuff like that you'll need some type of Event Driven code. Lets take your 2 orders for example.

- Say I know ahead of time which one is going to have TP. I'll give that one the magic# X and give the other magic# Y.
If I want to know which closed at TP, I do orderselect-history, if magic#=X

- Say I don't know which is going to have Tp but the program at some point is going to assign the Tp to one of em.
I'll write the program to Store the Ticket# during the sub-routine which gives the broker the Tp.

- Say I have an order with Tp which closed at "global exit conditions including a fixed SL"
I'll write the program to Store the Ticket# during the sub-routine which closed orders during fixed SL.

Keep in mind, you may want to expand the program to tell the difference between a third order which closed manually.
You can ask questions like :
Is Ticket# on Store Client Close list?
Is Ticket# on Store Server Close list?
Does order have Sl || Tp > 0?

As far as the best method for saving the Ticket#. It'll seem the writing them to a file via arrays[] is the sure winner.

 
ubzen:

However, the EA still needs to know which orders have been closed by the server

You loop through the open orders, if they're not there, they've been closed. Why does the EA care why.
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
 
WHRoeder:
You loop through the open orders, if they're not there, they've been closed. Why does the EA care why.


The EA cares so that it can write to a report file (as mentioned) and/or send an email.
 
    static datetime lastClose;  datetime lastClosePrev = lastClose;
    for(int pos=0; pos < HistoryTotal(); pos++) if (
        OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
    &&  OrderCloseTime()    > lastClosePrev             // not yet processed,
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         <= OP_SELL){    // Avoid cr/bal https://www.mql5.com/en/forum/126192
        lastClose = OrderCloseTime();
        // Do what you want with the latest closed order.
    }
 
WHRoeder:

Thanks for the code snippet. Makes sense. I'll give that a try. (For some reason I'm not receiving email alerts when subscribed to topics here so I only just now saw your follow up post.)
 
You also don't need two magic numbers. One order has a non-zero TP.
Reason: