For Loop not Deleting Both Orders (Yes, the loop is Decrementing)

 

Hi, I'm trying to open a buy and a sell order then delete them if they don't open in 40 seconds. The loop is decrementing, but only the sell stop order deletes. I can't figure out what I'm doing wrong. Here's my code:

 double sell_ticket;
   double buy_ticket;
   
   int TotalNumberOrders = OrdersTotal();

    
   if (TimeCurrent() >= start_time && TimeCurrent() <= end_time && open_orders) 
   {
    double sell_ticket = OrderSend(NULL,OP_SELLSTOP,.1,Bid-Point*from_price,300,Bid+Point*SL,Bid-Point*TP,NULL,101,clrNONE);
    double buy_ticket = OrderSend(NULL,OP_BUYSTOP,1,Ask+Point*from_price,300,Ask-Point*SL,Ask+Point*TP,NULL,101,clrNONE);
    
   Sleep(40000);
   int cnt;
   int TotalNumberOrders = OrdersTotal();
   for(cnt=TotalNumberOrders-1;cnt>=0;cnt-- )
   if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false)
   continue;
   
   if((OrderType() == OP_SELL || OrderType() == OP_BUY) && OrderSymbol() == Symbol() && OrderMagicNumber() == magic_number)
   OrderClose(OrderTicket(),OrderLots(),Bid,300,clrNONE);
   
  
   if((OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP) && OrderSymbol() == Symbol() && OrderMagicNumber() == magic_number)
   OrderDelete(OrderTicket(),clrNONE);

  

I would very much appreciate any help anyone can provide.

Thank you

Basic Principles - Trading Operations - MetaTrader 5
Basic Principles - Trading Operations - MetaTrader 5
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 

Please use the </> button to insert your above code.


 
Put in some { } where they are required.
 
  1. Keith Watford: Put in some { } where they are required.
    Correct

  2. if((OrderType() == OP_SELL || OrderType() == OP_BUY) && OrderSymbol() == Symbol() && OrderMagicNumber() == magic_number)
       OrderClose(OrderTicket(),OrderLots(),Bid,300,clrNONE);
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  3. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  4. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.

 

You should use the RefreshRates() function

Sleep(40000);
RefreshRates();


You should use the OrderClosePrice() function to set the closing price.

OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),300,clrNONE);


Put in some { } where they are required.

   for(cnt=TotalNumberOrders-1;cnt>=0;cnt-- )
   {
      if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) 
         continue;
      .
      .
      .
   }
 
You should not sleep your algo at all. That's asking for trouble. Instead use a timer. 
 

Hi All,

Thank you all so much for you input, it's really very much appreciated. The issue was no curly brackets on the for loop. Everyone's comments are very helpful in general though.

Thank you guys so much again.

 
Drew Clayman:

Hi All,

Thank you all so much for you input, it's really very much appreciated. The issue was no curly brackets on the for loop. Everyone's comments are very helpful in general though.

Thank you guys so much again.

You likely wouldn't have missed that if you properly indented your code... 

 
nicholi shen:

You likely wouldn't have missed that if you properly indented your code... 

True.

Or puritanically use { } for conditionals, always.

https://softwareengineering.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no

Reason: