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

Back to topics list To post a new topic, please log in or register
avatar
15
codeMolecules 2015.03.20 00:27
 

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 

avatar
Moderator
3404
GumRai 2015.03.20 01:07 #
 

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 

 

avatar
15
codeMolecules 2015.03.20 01:31 #
 

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 

avatar
12984
WHRoeder 2015.03.20 19:50 #
 
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.
avatar
21
CristianoKonrad 2015.05.12 21:24 #
 
.
Back to topics list  

To add comments, please log in or register