Beginner needs help

 

Hi @all!


I have a (simple?) problem with my EA (code below).


What it should do is to positionate the stoploss depending on the current price. The orders are entered manually before. The code checkes permanently if there are any active trades. If yes, then the price of all acitve trades should be checked.


If only one trade is active, than everything is fine - the stoploss is set to the right points at the right time. But if another trade becomes active, he only checks the one which was opened at last. On this trade the positioning of the stoploss also acts normal. If the trade closes, one of the other active trades is permanently checked by the EA.


So the only problem I have is, that the EA doesn't checks more than one active trade at the same time till this trade is closed - no mather what the others are doing. The EA has the right count of active trades.


   int countOrders = OrdersTotal();

      for (i = 0; i < countOrders; i++) {         
         OrderSelect(i, SELECT_BY_POS);
         
         if (OrderType() == OP_BUY || OrderType() == OP_SELL) {;
            
            double value = 0.0;
            
            double digs = 1;
            int _Digits = 0;

            if (StringFind(OrderSymbol(), "JPY", 0) != -1) {
               _Digits = 2;
            } else {
               _Digits = 4;
            }
            
            for (i = 0; i < _Digits; i++) {
               digs /= 10;
            }

            double stoploss = NormalizeDouble(OrderStopLoss(), _Digits);
            double tmpStoploss = stoploss;
            
            double orderOpen = NormalizeDouble(OrderOpenPrice(), _Digits);
            
            double t1 = NormalizeDouble(StrToDouble(OrderComment()), _Digits);
            double t2 = NormalizeDouble(OrderTakeProfit(), _Digits);
               
            if (t1 == NULL) {
               if (OrderType() == OP_BUY) {
                  ...
               } else if (OrderType() == OP_SELL) {
                  ...
               }
            }
               
            bool modifyOrder = false;
            
            if (OrderType() == OP_BUY) {
               value = NormalizeDouble(OrderClosePrice(), _Digits);
            } else if (OrderType() == OP_SELL) {
               value = NormalizeDouble(OrderClosePrice(), _Digits);
            }

            if ((OrderType() == OP_BUY && value - orderOpen >= digs * 20) || 
               (OrderType() == OP_SELL && value - orderOpen <= -digs * 20)) {

               if ((OrderType() == OP_BUY && stoploss < orderOpen) || 
                  (OrderType() == OP_SELL && stoploss > orderOpen)) {
                  ...
                  modifyOrder = true;
               }
            }
               
            if ((OrderType() == OP_BUY && value >= t1 && value < t1 + (digs * 10)) || 
               (OrderType() == OP_SELL && value <= t1 && value > t1 - (digs * 10))) {

               if ((OrderType() == OP_BUY && stoploss < t1 - (digs * 10)) || 
                  (OrderType() == OP_SELL && stoploss > t1 + (digs * 10))) {

                  if (OrderType() == OP_BUY) {
                     ...
                  } else if (OrderType() == OP_SELL) {
                     ...
                  }
                  
                  modifyOrder = true;
               }
            }
            
            if ((OrderType() == OP_BUY && value >= t1 + (digs * 10) && value < t2) ||
               (OrderType() == OP_SELL && value <= t1 - (digs * 10) && value > t2)) {

               if ((OrderType() == OP_BUY && stoploss < t1) ||
                  (OrderType() == OP_SELL && stoploss > t1)) {

                  if (OrderType() == OP_BUY) {
                     ...
                  } else if (OrderType() == OP_SELL) {
                     ...
                  }
                  
                  modifyOrder = true;
               }                           
            }
            
         OrderModify(OrderTicket(), value, stoploss, OrderTakeProfit(), 0, Blue));           

         }
      }


Has anyone an idea? I can't find an logical bug in my code...


Regards, MisterM

 
the code looks fine and may have logical error and that is difficult to find
 

Make it to your habit to count down openOrders!

int countOrders = OrdersTotal();
while (countOrders-->0) {..

For example if you close order 0 in the list of open orders order 1 became order 0 and countOrders  would be one less and if you try to do something with the last order you'll fail.

Additionally mt4 does not guaranty any sort order like time-open, time-close, profit, type, ticket,... so you have to loop though all of them anyway!

Reason: