simple question

 

Greetings to all of you. I have created a code (modifying other codes) for closing market orders.



int l_pos_44 = 0;

if (CheckFirstOP(OP_BUY) > 1) {

for (l_pos_44 = 0; l_pos_44 < OrdersTotal(); l_pos_44++) {

OrderSelect(l_pos_44, SELECT_BY_POS, MODE_TRADES);

if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber || OrderType() != OP_BUY) continue;

double Price = OrderOpenPrice();

if (NormalizeDouble(MathAbs(Bid - Price), Digits) < NormalizeDouble(Dist, Digits)) {

Dist = MathAbs(Bid - Price);

int Ticket = OrderTicket();

double Lot = OrderLots();

OrderClose(Ticket, Lot, Bid, 3);

}

}


(the same code for closing sell positions (obviously with OP_SELL variable instead of OP_BUY))


int CheckFirstOP(int a_cmd_0) {

int l_count_4 = 0;

for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {

if (OrderSelect(l_pos_8, SELECT_BY_POS)) {

if (OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {

if (OrderType() == a_cmd_0) {

l_count_4++;

break;

}

}

}

}

return (l_count_4);

}


I need a code to close positions long or short when there are more than 1 positions opened for type...so to keep 2 positions in total (1 buy and 1 sell)...I need also to keep the most recent opened position i.e. in this case the one with the higher open price for buy position and the one with the lower open price for sell position. The problem is that the code does not close the positions...it does not close any of them.


when: "if (CheckFirstOP(OP_BUY) == 1)" then the code closes the first positions imidiately after they are opened...i.e. after a buy and a sell order are opened they are closed with the next tick. And when: "if (CheckFirstOP(OP_BUY) > 1)" none of the positions is closed. I don't know what i'm doing wrong.

Please can anyone help me?

 

I forgot:


double Dist = 1000000.0;

 
Aljohin:

Please can anyone help me?

1. This is only part of your code, show us the function CheckFirstOP() for example.

2. The for loop is incrementing. That won't work properly if closing orders in the loop, please see -> https://www.mql5.com/en/forum/119840.

3. Your variable naming convention is... not so good.

4. Please use the SRC button to post your code so it will retain formatting. It's very hard to read your code like this.

 
gordon:

1. This is only part of your code, show us the function CheckFirstOP() for example.

2. The for loop is incrementing. That won't work properly if closing orders in the loop, please see -> https://www.mql5.com/en/forum/119840.

3. Your variable naming convention is... not so good.

4. Please use the SRC button to post your code so it will retain formatting. It's very hard to read your code like this.


double Dist=1000000.0;
if (CheckFirstOP(OP_BUY) > 1) {
      for (l_pos_44 = 0; l_pos_44 < OrdersTotal(); l_pos_44++) {
          OrderSelect(l_pos_44, SELECT_BY_POS, MODE_TRADES);
          if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber || OrderType() != OP_BUY) continue;     
          double Price = OrderOpenPrice();
          if (NormalizeDouble(MathAbs(Bid - Price), Digits) > NormalizeDouble(Dist, Digits)) {              
             Dist = MathAbs(Bid - Price);                 
             int Ticket = OrderTicket();           
             double Lot = OrderLots();                          
             OrderClose(Ticket, Lot, OrderClosePrice(), 3); 
         }    
      }   
   }

int CheckFirstOP(int a_cmd_0) {
   int l_count_4 = 0;
   for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
      if (OrderSelect(l_pos_8, SELECT_BY_POS)) {
         if (OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {
            if (OrderType() == a_cmd_0) {
               l_count_4++;
               break;
            }
         }
      }
   }
   return (l_count_4);
}
my fault...i'm very new to this stuf...
 
gordon:

3. Your variable naming convention is... not so good.

looks like a decompiler name convention ...

"he is new to this stuff"

 
meikel:

looks like a decompiler name convention ...

"he is new to this stuff"

probably yes...I want to modify an EA that I bought...they also sent me the code (they said by mistake)...now I understand the reason of this so odd nomenclature...as I said I'm new (have just read the book of MQL4 in this site) and didn't want to modify a lot of things at least at the begining so I let the names as I found them.

 
Ais:

Hi

Selected order can be completely closed by expression: "OrderClose ( OrderTicket (), OrderLots (), OrderClosePrice (), 0 ) ;"

Best regards

doesn't solve my problem...

 
gordon:

2. The for loop is incrementing. That won't work properly if closing orders in the loop, please see -> https://www.mql5.com/en/forum/119840.

why incrementing doesn't work? in the book of MQL4 it isn't mentioned (I think)...anyway I tried:


if (CheckFirstOP(OP_BUY) > 1) {
      for(int i = OrdersTotal() - 1; i >= 0; i--) {
          OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
          if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber || OrderType() != OP_BUY) continue;     
          double Price = OrderOpenPrice();
          if (NormalizeDouble(MathAbs(Bid - Price), Digits) > NormalizeDouble(Dist, Digits)) {              
             Dist = MathAbs(Bid - Price);                 
             int Ticket = OrderTicket();           
             double Lot = OrderLots();                          
             OrderClose(Ticket, Lot, OrderClosePrice(), 3); 
         }    
      }   
   }
it doesn't work, don't know why
 
Ais:

simply be sure in this line now

Yes ofcourse I tried and it changes nothing...strange things with this programing :) it wouldn't be difficult to write a simple closing order (I thought)...

 
it doesn't work, don't know why
When you close order index 5, order index 6 becomes index 5, 7 becomes 6, etc. Counting up you are missing every other open order.
 
WHRoeder:
When you close order index 5, order index 6 becomes index 5, 7 becomes 6, etc. Counting up you are missing every other open order.

ah ok, understood...anyway the code is ment to keep only one open order and to start working imidiately when there are 2 orders opened and closes one of them after selecting and analysing all of them (i.e. 2 orders)...so I don't think it is a big mistake incrementing in order selecting...anyway I tried the decrementing method without any results...maybe I'm trying in a wrong way...don't know...can any one try my code and see if it really closes the trades...or can anyone write a simple code for selecting and closing the previous opened order (i.e. the one with higher openprice for buy and with lower openprice for sell)?

thankyou

Reason: