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

 
MakarFX #:

I'll try to attach it, thank you. Just a question how to deal with SELL orders

 
EVGENII SHELIPOV #:

I'll try to attach it, thank you. Just a question how to deal with SELL orders

Semyon Semyonych...
   double GetBuyOrderSwap()
     {
      double order_swap = 0;
      for(int i = OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if (OrderType() == OP_BUY)
                 {
                  order_swap += OrderSwap();
                 }
              }
           }
        }
      return(order_swap);
     }
   double GetSellOrderSwap()
     {
      double order_swap = 0;
      for(int i = OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if (OrderType() == OP_SELL)
                 {
                  order_swap += OrderSwap();
                 }
              }
           }
        }
      return(order_swap);
     }
 
MakarFX #:
Semyon Semyonych...

Makar, why is it so complicated to cram it into one function? Or is there something I don't understand?

 
int GetMagic(Order &order)
{
  int magic = 0;
  
  if(order.cmd == OP_SELLSTOP || order.cmd == OP_BUYSTOP)
    if(OrdersTotal() >= 1)
      for(;;){
        magic++;
        for(int i = OrdersTotal(); i > 0 ; i --)
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) != false)
            if(magic != OrderMagicNumber())
              return magic;
            
      }
    else if (OrdersTotal() == 0)
      return (magic + 1);
  return magic;
}
Replacing it with == doesn't help. In both cases, no thread goes there (I checked with print)
 
Nerd Trader #:
It says in the commentary that it sends the majic to the write array, don't let that be misleading, as long as the majic goes back to the retorn.

I don't quite understand the general logic, I try to separate the functions

  if(order.cmd == OP_SELLSTOP || order.cmd == OP_BUYSTOP)
  int order_magic = GetMagic(order);

  if(order.is_init == true)
  {
    int order_send = OrderSend(Symbol(), order.cmd, 0.01, order.open_price, 10, 
    order.sl_price, order.tp_price, "", order_magic, 0, order.arrow_color);

    if(order_send == -1){
      Print(order.error_text," | ",GetLastError()," db_last.third ",db_last.third,
        " | db_last.size_open_to_low ",db_last.size_open_to_low," | order.sl_price: ",
        order.sl_price," | order.cmd ",order.cmd);
      ResetLastError();
      return;
    }
  }
   //+---
   int GetMagic(Order &order)
   {
     int magic = 0;
     for(int i = OrdersTotal(); i > 0 ; i --)
      {
       if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
          if(magic == OrderMagicNumber()) magic+=1;
          else magic=0;
         }
      }
     return (magic);
   }
 
EVGENII SHELIPOV #:

Makar, why is it so complicated to cram it into one function? Or is there something I don't understand?

   double GetOrderSwap(int ot=-1)// 0-Buy; 1-Sell
     {
      double order_swap = 0;
      for(int i = OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType()==ot||ot<0)
                 {
                  order_swap += OrderSwap();
                 }
              }
           }
        }
      return(order_swap);
     }
 
MakarFX #:

Makar, you're being a baby with me.

Thanks, Makar. I changed the function a little, and it's working.

 
MakarFX #:

I don't really understand the general logic, I try to separate the functions

Now GetMagic should generate a unique magic at the moment of sending a pending order and return it by retorn. In your version, there is only one loop, so when the order finishes, else will be executed anyway, and magic == OrderMagicNumber() will always be true. I want magic != OrderMagicNumber() when it is not true, it means that magic is unique and it is incremented by itself in the first for(;;).
 
Nerd Trader #:

Check your personal message.

 
Hello, I am facing the following problem: EA opened a pending order, but the terminal does not track it and it is not present in the trade history. Please advise if someone has faced such a problem.