[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 346

 
hoz Vadim, why recreate the object when you can use ObjectSet() ?

Why ask for advice and then challenge it? Is this the mainstream?
 
FAQ:

I think there is no point in arguing any further. I have explained everything in detail step by step. keep doing what you have been doing.

What kind of discussion can there be? We should close the order with the maximum deviation in price. Which means we're interested in the price... I.e. we should find an extreme pending order (with the maximum or minimum open price ). If you don't agree, you could argue. Otherwise I am right after all! I know exactly what I'm saying.
 

The code deletes ST and TP. I add a condition for selective deletion

What's not written correctly? Keeps deleting everything :(

 #property show_inputs
extern bool   StopLoss     = True;    // Удалить только StopLoss
extern bool   TakeProfit   = True;    // Удалить только TakeProfit
extern bool   ALL          = True;    // Удалить все
void start()
   {
   bool   fm;
   int     i;
   double SL=OrderStopLoss();
   double TP=OrderTakeProfit();

      if (StopLoss)   SL=0;
      if (TakeProfit) TP=0;
      if (ALL)       {SL=0; TP=0;}
     
      for (i=0; i<OrdersTotal(); i++)
         {
           if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
               {
                  if (OrderType()==OP_BUY)
                     {
                        fm=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,CLR_NONE);
                     }
                  if (OrderType()==OP_SELL)
                     {
                        fm=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,CLR_NONE);
                     }
               }
         }
   }

Or is there no other solution and have to write the delete code for each condition?

#property show_inputs
#include <WinUser32.mqh>
extern bool   StopLoss     = False;    // Удалить только StopLoss
extern bool   TakeProfit   = False;    // Удалить только TakeProfit
extern bool   ALL          = False;    // Удалить все
void start()
   {
   bool   fm;
   int     i;

    if (StopLoss)
         {
            for (i=0; i<OrdersTotal(); i++)
               {
                 if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                     {
                        if (OrderType()==OP_BUY)
                           {
                              fm=OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderTakeProfit(),CLR_NONE);
                           }
                        if (OrderType()==OP_SELL)
                           {
                              fm=OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderTakeProfit(),CLR_NONE);
                           }
                     }
               }
           return(0);
         }
      if (TakeProfit)
         {
            for (i=0; i<OrdersTotal(); i++)
               {
                 if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                     {
                        if (OrderType()==OP_BUY)
                           {
                              fm=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),0,CLR_NONE);
                           }
                        if (OrderType()==OP_SELL)
                           {
                              fm=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),0,CLR_NONE);
                           }
                     }
               }
           return(0);
         }
      if (ALL)
         {
            for (i=0; i<OrdersTotal(); i++)
               {
                 if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                     {
                        if (OrderType()==OP_BUY)
                           {
                              fm=OrderModify(OrderTicket(),OrderOpenPrice(),0,0,CLR_NONE);
                           }
                        if (OrderType()==OP_SELL)
                           {
                              fm=OrderModify(OrderTicket(),OrderOpenPrice(),0,0,CLR_NONE);
                           }
                     }
               }
           return(0);
         }
   }             

 
hoz:


Vadim, why create the object again when you can use ObjectSet() ?

In that case, there was a function where the graphical object was completely created and configured. In it, the creation function is optimised. If the object already exists, it is not created. Check it yourself. Measure the time to create objects, then to change properties. The second call will be much shorter. I.e. nothing is recreated.

As a result, we have a universal function that not only creates an object, but can also customize it by changing object properties without much time.

 
Can you suggest a script to an EA to determine the market trend?
 

I wrote a function that will deletethe sell orders with the lowest prices.The number of short orders to close is equal tothe number of buy orders that triggered, i.e. went into market orders.

So if one buy order triggered, then we close one sell order, if 2 buy orders triggered, then we close 2 sell orders.

Here is the code:

//+-------------------------------------------------------------------------------------+
//| Удаление несработанных отложеннык шортов                                            |
//+-------------------------------------------------------------------------------------+
void DeletePendingSells(int& amountOfCurrPending)
{
   int total = OrdersTotal() - 1,
       ordersToDelete = level - amountOfCurrPending,
       s_ticket;
   amountOfCurrPendingBuys = 0;
   amountOfCurrPendingSells = 0;
   Print("DeletePendingSells: level = ", level);
   Print("ordersToDelete = level - amountOfCurrPending ; ", level, " - ", amountOfCurrPending);
   int n = 0;
   double OOP = 2.0;
   Print("функция сноса отложек DeletePendingSells");
   for (int i=total; i>=0; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != i_magic) continue;
      if(OrderType() != OP_SELLSTOP) continue;
      
      while (n < ordersToDelete)
      {
         if (OOP > OrderOpenPrice())
         {
             Print("OOP > OrderOpenPrice() ; ", OOP, " > ", OrderOpenPrice());
             OOP = OrderOpenPrice();
             s_ticket = OrderTicket();
             ordersToDelete--;
         }
      }
   }
}

amountOfCurrPending - amount of pending buy orders.

Level- number of pending orders initially sent to each side.

OOP- open price of the pending order, I have taken any unreachable value.

Our condition is as follows: if the counter is lower than theordersToDelete, we delete the pending order with the lowest price. But all short positions are deleted. What is wrong with me?

 
hoz:

I wrote a function that will deletethe sell orders with the lowest prices.The number of short orders to close is equal tothe number of buy orders that triggered, i.e. went into market orders.

So if one buy order triggered, then we close one sell order, if 2 buy orders triggered, then we close 2 sell orders.

Here is the code:

amountOfCurrPending - amount of pending buy orders.

Level- number of pending orders initially sent to each side.

OOP- opening price of the pending order, I have taken any unreachable value.

Our condition is as follows: if the counter is lower than theordersToDelete, we delete the pending order with the lowest price. But all short positions are deleted. What is wrong with me?

I do not understand your logic. Why not: the order was transformed into a market position - the furthest opposite pending order was deleted.
That is all. Or do you have a bunch of orders on one price?
 
artmedia70:

That is all. Or do you have a pack of orders at one price?


No, orders in increments...
artmedia70:
I don't understand your logic... Why not: The order was converted into a market position - the furthest opposite pending order was removed.

That's how I delete the furthest opposite position, i.e. since it's a short position, it means the position with the lowest opening price. Here I have rewritten the function, everything should be clear here. I removed the prints and removed an extra variable.

The variableamountOfCurrPending transfers the number of remaining trades in buy. I.e., if the initial amount is level andamountOfCurrPending is the current amount of orders, then subtracting the initial amount of orders from the current one, we will get the difference and this is the amount needed to delete the shorts. Do you see?

//+-------------------------------------------------------------------------------------+
//| Удаление несработанных отложеннык шортов                                            |
//+-------------------------------------------------------------------------------------+
void DeletePendingSells(int& amountOfCurrPending)
{
   int total = OrdersTotal() - 1,
       ordersToDelete = level - amountOfCurrPending,  // Количество отложек, которые требуется удалить
       s_ticket;
   amountOfCurrPendingBuys = 0;                       // Количество текущих отложек на покупку
   amountOfCurrPendingSells = 0;                      // Количество текущих отложек на продажу
   Print("DeletePendingSells: level = ", level);
   Print("ordersToDelete = level - amountOfCurrPending ; ", level, " - ", amountOfCurrPending);

   double OOP = 2.0;
   Print("функция сноса отложек DeletePendingSells");
   for (int i=total; i>=0; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != i_magic) continue;
      if(OrderType() != OP_SELLSTOP) continue;
      
      while (ordersToDelete > 0)                      // Если есть ордера, которые требуется удалить..
      {
         if (OOP > OrderOpenPrice())
         {
             Print("OOP > OrderOpenPrice() ; ", OOP, " > ", OrderOpenPrice());
             OOP = OrderOpenPrice();                  // Ищется ордер, с минимальной ценой открытия
             s_ticket = OrderTicket();                // Получаем тикет ордера с минимальной ценой открытия

             Print("DeletePendingSells: s_ticket = ", s_ticket);
             Print("DeletePendingSells: OOP = ", OOP);
             OrderDelete(s_ticket,Black); // Тут нужно закрыть найденный ордер, и видимо проверить, что он закрыт. И счётчик уменьшить!
             ordersToDelete--;                        // Уменьшаем количество требуемых ордеров для удаления
         }
      }
   }
   Print("DeletePendingSells: ordersToDelete = ", ordersToDelete);
}
Is there a mistake where I marked in red? How to solve it?
 
hoz:

No, orders in increments.

This is how I remove the furthest position, i.e. since it is a short position, it means the position with the lowest opening price. Here I have rewritten the function, it should be clear. I removed the prints and removed an extra variable.

The variableamountOfCurrPending transfers the number of remaining trades in buy. I.e., if the initial amount is level andamountOfCurrPending is the current amount of orders, then subtracting the initial amount of orders from the current one, we will get the difference and this is the amount needed to delete the shorts. Do you see?

Is there a mistake where I marked in red? How to solve it better?
 Ищется ордер, с минимальной ценой открытия

I would specify in the comment of a pending order its order number in the grid and write the same number in the corresponding opposite pending order. Then when a market position is found, after reading the number in its comment, the corresponding opposite position can be found with an identical number in its comment. Since magiks are used for something else.

ZS. I don't have time to read/parsing your code yet

 
artmedia70:

I would write in the comment of the pending position its sequence number in the grid and write the same number in the corresponding opposite pending position. Then when a market position is found, after reading the number in its comment, the corresponding opposite pending position can be found without any problems by an identical number in its comment. Since you use magicians for something else.

ZS. I don't have time to read/parsing your code yet



Hmm. Yes, no problem finding the corresponding pending order that way, if the number of pending orders in each direction is the same. And if different, then this will not work as I understand it.