Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1718

 
// Функция модификации ордеров Sell при локировании
void Modif_Count_Lok_Sell()
{
double   Price;

      for(int pos=0; pos < OrdersTotal(); pos++)
      {
          if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES))
          {
             if(OrderSymbol()== Symbol())
             {
                if(OrderType() == OP_SELL)
                {
                   if(OrderTakeProfit() > 0)
                   {
                       if(CountBuy1() > 0) // функция для пересчёта ордеров Buy
                       {
                             Price = OrderOpenPrice();
                        double     TP3 = NormalizeDouble( 0*Point, Digits);
                          if(OrderTakeProfit() > TP3)
                          {
                         
                             if(!OrderModify( OrderTicket(), OrderOpenPrice(), 0, TP3, 0, 0))
                                        Print("Ошибка модификации  ордера на продажу");
                           }             
                        }
                    }
                }
               } 
             }   
           }     
 }      

Help to understand. The point of this function is that if there are open sell orders and open buy order, modify the TP of the sell order. But modification happens only one first order, and then it gives error OrderModify ERROR 1

 
Snajper007 sell orders and open buy order, modify the TP of the sell order. But modification occurs only one the first order, and then gives out error OrderModify ERROR 1

In this case TakeProfit is simply deleted

ERROR 1 - ERR_NO_RESULT No error, but result unknown

 
MakarFX #:

In this case the TakeProfit is simply deleted

ERROR 1 - ERR_NO_RESULT No error, but result unknown

Why, of the two orders, is it removed on one and not on the other?
 
Snajper007 #:
Why of the two orders, it is deleted on one and not on the other?

Deleted only if OrderTakeProfit() > 0

i.e. the trade is on the upside

 
MakarFX #:

Deleted only if OrderTakeProfit() > 0

i.e. the trade is on the upside

Reworked the code.

// Функция модификации ордеров Sell при локировании
void Modif_Count_Lok_Sell()
{
double   Price;

      for(int pos=0; pos < OrdersTotal(); pos++)
      {
          if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES))
          {
             if(OrderSymbol()== Symbol())
             {
                if(OrderType() == OP_SELL)
                {
                   
                   
                       if(CountBuy1() > 0) // функция для пересчёта ордеров Buy
                       {
                             Price = OrderOpenPrice();
                        double     TP3 = NormalizeDouble( 0*Point, Digits);
                          
                         
                             if(!OrderModify( OrderTicket(), OrderOpenPrice(), 0, TP3, 0, 0))
                                        Print("Ошибка модификации  ордера на продажу");
                                       
                        }
                    
                }
               } 
             }   
           }     
 }      
Same thing. There are 2 sell orders with an average TP. When we open a buy order in both of these orders, the TP should be deleted. But it is deleted only at the first order, and the second order remains with the averaged TP.
 
MakarFX #:

Deleted only if OrderTakeProfit() > 0

i.e. the trade is on the upside

if the OrderTakeProfit()>0, it doesn't mean that the trade is in the plus position...It just means that the position has a TakeProfit...

 
Snajper007 #:

Reworked the code.

Same thing. There are 2 sell orders with an average TP. When we open a buy order in both of these orders, the TP should be deleted. But it is deleted only at the first order, and the second order remains with the averaged TP.

You've just got it all mixed up...

First you highlight the order

if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES))

But then you call a function

CountBuy1()

Which probably changes the selection... And that's messed up. Separate the flies from the cutlets and everything will work...

 
Nikolay Ivanov #:

You've just got it all mixed up...

First you highlight an order

But then you call the function

Which probably changes the selection... And then you get a mess... Separate the flies from the cutlets and everything will work...

Thank you! Removed the function and it's working fine.
 
Nikolay Ivanov #:

If OrderTakeProfit()>0 it doesn't mean that the trade is in the black... It just means that the position has a TakeProfit...

You're right, I got confused with OrderProfit()
 

Another question has arisen. Here is the average price calculation function:

//Средний тейкпрофит для ордеров Sell
double SR_CENA_SELL()
{
double     sr_cena_sell = 0;
if(CountSell() > 1)
      {
   double     lot_price_Sell = 0;
   double     sum_lot_Sell_1  = 0;
         for (int pos=0; pos < OrdersTotal(); pos++)
           if( OrderSelect(pos, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderMagicNumber() == MN || OrderMagicNumber() == 0)
              {
                if(OrderSymbol() == Symbol() && OrderType() == OP_SELL)
                  {
                    lot_price_Sell += NormalizeDouble(OrderOpenPrice()* OrderLots(), Digits);
                    sum_lot_Sell_1   += OrderLots();
                    sr_cena_sell = NormalizeDouble(lot_price_Sell/sum_lot_Sell_1, Digits);
                    
                  }
              }
           }
           
      }
   return(sr_cena_sell);
 }
For some reason, it recalculates every time. I.e. the 2nd sell order is opened, the function calculates the average price. However, when the third order is opened, this function displays the previous and the new value (I checked this in the print journal) but as a result, the previous value remains. What have I done wrong?
Reason: