Deletion of pending orders based on lapsed time

 

I work with pending orders in my EA.

I want to delete any pending order that has not been activated after 30 minutes.

Can somebody please tell me how I can do this?

 
Set order expiration date?
 
forexCoder:
Set order expiration date?
Not all brokers accept "expiration date". Also not sure what the format should be. Like this: D'1980.07.19 12:30:27' ??
 

Um no. At order creation (OrderSend()) just get the TimeLocal() or timecurrent, whichever you prefer. Set orderExpiration to that time + 30*60 (minutes).

If your broker doesn't allow this, just have an array of currently open orders handy, where you put these times in and check on every tick.

You should know that if such EA goes through inti/deinit cycle, you'll lose that info unless you save it to disk promptly and read it at init cycle.

I think this can also be done through GlobalVariables, tbh I don't use them.

 
forexCoder:

Um no. At order creation (OrderSend()) just get the TimeLocal() or timecurrent, whichever you prefer. Set orderExpiration to that time + 30*60 (minutes).

If your broker doesn't allow this, just have an array of currently open orders handy, where you put these times in and check on every tick.

You should know that if such EA goes through inti/deinit cycle, you'll lose that info unless you save it to disk promptly and read it at init cycle.

I think this can also be done through GlobalVariables, tbh I don't use them.

  1. You MUST use TimeCurrent() as that is ONLY what the broker uses, your local time is irrelevant.
  2. If your broker doesn't allow expiration time just use:
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderType()         > OP_SELL                   // pending only
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair
    &&  TimeCurrent() = OrderOpenTime() > 30*60         // and too old.
    ){
        if (!OrderDelete(OrderTicket()) Alert(..., GetLastError());
    }
    
    No need for persistent storage, i.e. disk, no need for arrays.
  3. You can NOT use GlobalVarables for persistent storage. In the event of a power failure/OS or terminal crash, GVs will NOT be written to disk.
 
ernest02:
Also not sure what the format should be. Like this: D'1980.07.19 12:30:27' ??

Its a unix timestamp like all other dates also.
 

WHRoeder

You can NOT use GlobalVarables for persistent storage. In the event of a power failure/OS or terminal crash, GVs will NOT be written to disk.

This is not true. GlobalVariables will survive power failure and crash just fine. GlobalVariableSet() will write to disk immediately. You are confusing this with objects drawn on the chart that will only be saved on clean exit. GlobalVariables will be saved immediately.


Sometimes 90% of my MT4 exits are due to crash (because I am developing DLLs) and only exactly one clean exit per week on Friday evening. Therefore all my EAs are designed from ground up to survive any hard crash and automatically resume after restart without intervention. I am using only GlobalVariables to achieve this goal. I have never lost any single global variable so far.
 

1) It doesn't matter what you use, you set your orders at your local time, and the time that you set for expiration is (in this case) irrelevant. So here, the usage of either TimeLocal or TimeCurrent is acceptable. The broker will set your order expiration to what you tell him to set it to.

2) I have the knack for not seeing the simplest solution. :)

3) Hm. If that's the case, I really see no point in using global vars currently.... For what purpose do you guys use them mostly?

 
forexCoder: [GlobalVars] For what purpose do you guys use them mostly?
For saving state of the running EA between MT4 sessions and seamless resumption after crash or power outage. Works like a charm.
 
Ah I see you made another post before mine. Ok all cleared, thanks.
 

Thanks for all the advice guys.

I think I will use WHRoeder's piece of code since it will be accepted by all brokers.

(The discussion around Global Variables are fascinating! I must start to look further into this)

Your assistance in this matter is GREATLY appreciated!

Reason: