Close Market Order when current close price is smaller than the lowest close from the previous 10 bars

[Deleted]  

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, several orders remain open, but I´m having a bit of trouble on closing each order separately when certain criteria is met; I need to close an order if the current close price is less than or equal to the lowest close from a series of 10 past bars from which an order was opened. This is what I came up with so far: 

 

double LowestClose = ( Low[iLowest( NULL, 0, MODE_CLOSE, 10, 0 )] );

if ( Close[0] <= LowestClose )
  {
    total = OrdersTotal();

    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(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, Green );
              break;

            case OP_SELL: result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 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, or how would it be a better way to do it? Help will be much appreciated. 

 

Best regards and thank you,

codeMolecules 

 

First thing I see is that the variable total is not assigned a value in the code.

You are testing the Integer select as a bool

There is no need for the switch, just don't use Bid or Ask as the closing price. Use OrderClosePrice() which can be applied to both buys and sells 

[Deleted]  

My bad, I assigned OrdersTotal() to the total variable and changed result to integer and assigned 0 to it. I will give it a try.

 

Best regards and thank you,

codeMolecules 

 
double LowestClose = ( Low[iLowest( NULL, 0, MODE_CLOSE, 10, 0 )] );
if ( Close[0] <= LowestClose 
if the if is true it's because iLowest == 0 and Close[0] == Low[0]. If you want "10 past bars" don't look at the current bar.
 
.