Void for close a grid of orders

 

I'm trying to close a grid of orders at specified distance from breakeven point from a void function, without success. This is the code:

void CloseGridLong(){
   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderType()==OP_BUY){       // Select BUY orders
            if(OrderProfit()==0){       // Get BREAKEVEN point
               static double Breakeven=Bid;
               if(Bid>=Breakeven+(TakeProfit*10)*Point){ // TP distance from BREAKEVEN
                  if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrLimeGreen)){
                     Print("OrderClose error ",GetLastError());
                     return;
                     }
                  else if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrDimGray)){
                     Print("OrderClose error ",GetLastError());
                     return;
                     }
                  } 
               }
            }
         }
      }
   }

Hope someone can help, thank you in advance.

 
  1. if(OrderProfit()==0){       // Get BREAKEVEN point
    The breakeven price is the OrderOpenPrice.

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. static double Breakeven=Bid;
    Static and global variables are initialized once on program load. They don't update unless you assign to them.

    Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrLimeGreen)){
       Print("OrderClose error ",GetLastError());
       return;
       }
    else if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrDimGray)){
       Print("OrderClose error ",GetLastError());
       return;
       }
    
    Why are you trying to close the order a second time when it already did? If the first fails, you print and return.

  5. You can use OrderClosePrice() instead of Bid/Ask.
              www.mql5.com/en/forum/158890/page3#comment_3812636

  6.    for(int i=0;i<OrdersTotal();i++){
    
    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, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

  7. if(Bid>=Breakeven+(TakeProfit*10)*Point){ // TP distance from BREAKEVEN
    
    Is there a reason that you are doing this instead of just setting the TP on open?
 
William Roeder:
  1. The breakeven price is the OrderOpenPrice.

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. Static and global variables are initialized once on program load. They don't update unless you assign to them.

    Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Why are you trying to close the order a second time when it already did? If the first fails, you print and return.

  5. You can use OrderClosePrice() instead of Bid/Ask.
              www.mql5.com/en/forum/158890/page3#comment_3812636

  6. 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, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

  7. Is there a reason that you are doing this instead of just setting the TP on open?

1 & 7: This is a GRID of orders, so BE is not OP and TP is a distance from BE price. The difficult resides on get this BE value, that I tried to get by OrdersProfit==0.

4: There is a mistake, its LimeGreen close for OrderProfit>0.

5: Don't understand what you mean.

 

Hello friend,

Silly  code,but try try,

   Profit= OrderProfit(); 
  if( Profit > var[breakEven]
 
GrumpyDuckMan:

Hello friend,

Silly  code,but try try,

So how do I get var[breakeven]?
Reason: