Close Buy Order when current close price is smaller than the lowest close of the previous 10 bars (MQL4)

 

Hello forum, good day.

I´m working on a Market Orders EA that may open only one order at a time, but there may be several open orders during the day. Up to what I have, all the orders remain open and I´m having trouble on closing each order separately when certain criteria is met.

The criteria is as follows: close a buy order if the current close price is less than or equal to the lowest close price of the 10 previous bars from which the order was opened. Here is what I've got so far:

 

double LowestClose = ( Low[iLowest( NULL, 0, MODE_CLOSE, 10, 1 )] );
int    total       = OrdersTotal();

if ( Close[0] <= LowestClose )
  {
    for ( int i = total - 1; i >= 0; i-- )
      {
        int select = OrderSelect( i, SELECT_BY_POS ),
        type       = OrderType();

        int result = 0;

        if ( !select ) continue;
        if ( OrderSymbol() != Symbol() ) continue;
        if ( OrderMagicNumber() != MagicNumber ) continue;

        switch( type )
          {
            case OP_BUY:  result = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Green );
              break;

            case OP_SELL: result = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Red );
              break;
          }

        if ( result == false )
          {
            Alert("Order #", OrderTicket(), " failed to close. Error: ", GetLastError(), ".");
            Sleep(1000);
          }
      }
  }

 


Is this the correct way to close a market order, if not, how could I improve this?

My guess for the needed criteria is that I must associate the order's ticket to a variable holding the lowest close price of the previous 10 bars. If so, how could it be done? Your help will be much appreciated. 

 

Best regards and thank you,

codeMolecules 

 

Function check criteria for each market order and call 'order close' function, if criteria meet. Return the number of closed orders

int Close_Orders(int i_Magic = -1) {
        int
                i_Order = OrdersTotal(),
                i_Closed_Orders_Count = 0
        ;
        while(i_Order-- > 0) {
                if(!OrderSelect(i_Order, SELECT_BY_POS)) continue;
                if(OrderType() > 1) continue;
                if(OrderSymbol() != _Symbol) continue;
                if(i_Magic > 0 && OrderMagicNumber() != i_Magic) continue;
                
                if(OrderType() == OP_BUY) {
                        if(Bid > Low[iLowest(
                                _Symbol, PERIOD_CURRENT, MODE_CLOSE, 10,
                                iBarShift(_Symbol, PERIOD_CURRENT, OrderOpenPrice())
                        )]) continue;
                } else {
                        if(Ask < High[iHighest(
                                _Symbol, PERIOD_CURRENT, MODE_CLOSE, 10,
                                iBarShift(_Symbol, PERIOD_CURRENT, OrderOpenPrice())
                        )]) continue;
                }
                // criteria meet if we are here
                // call your 'order close' function:
                if(Close_Order(OrderTicket())) i_Closed_Orders_Count++;
        }
        
        return(i_Closed_Orders_Count);
}
 
f2011:

Function check criteria for each market order and call 'order close' function, if criteria meet. Return the number of closed orders

Hello f2011, good day.

Thank you very much for your help!! I will give it a try to check it out.

Best regards, 
codeMolecules

Reason: