Ask for help .

 

i want to close all my limit orders at the end of the day. Can anyonee help me with the code please? Also i have tried to code. Please mark if any thing is wrong.

int openOrderID;
int magicNB = 55555;

if (Hour() == 0 && Minute() < 2 && Seconds() == 0 && OrdersTotal() > 0)
            
            {
            
            OrderDelete(openOrderID,clrRed);
         
            }
            
            if (Hour() == 0 && Minute() == 0 && Seconds() == 0 && OrdersTotal() > 0)
            
            { 
                      
            OrderDelete(openOrderID,0); 
           
            }
 
aviyadav321:

i want to close all my limit orders at the end of the day. Can anyonee help me with the code please? Also i have tried to code. Please mark if any thing is wrong.

Iterate through ordersSelect and if its type is a limit order, then get its ticket  to delete it. Also you check all functions return in case of failure.


Did you search in the CodeBase?

 
aviyadav321: Please mark if any thing is wrong
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.


  2. if (Hour() == 0 && Minute() < 2 && Seconds() == 0 && OrdersTotal() > 0)
    ⋮            
    if (Hour() == 0 && Minute() == 0 && Seconds() == 0 && OrdersTotal() > 0)

    There can be minutes between ticks during the Asian session. Code fails the first second of the day/second minute of the day if there is no tick that specific second.

    Check for a new day instead.
              Please help with one trade per day code - Day Trading - Expert Advisors and Automated Trading - MQL5 programming forum #3.2 (2020)

  3. Why are you deleting twice?


  4.             OrderDelete(openOrderID,clrRed);

    You don't check if the order has opened. Delete would fail.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

  5. 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.

    On a network disconnection you might get ERR_NO_RESULT or ERR_TRADE_TIMEOUT. There is nothing to be done, but log the error, return and wait for a reconnection and a new tick. Then reevaluate. Was an order opened or is the condition still valid.

  6. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.
Reason: