Questions from Beginners MQL5 MT5 MetaTrader 5 - page 642

 
paylesss:

Thanks! Rebooting the computer helped! It's a shame, I've been sitting here all morning))

Now the more relevant is the build of the terminal and it would be desirable to remember what you have done to make the global variables fail. If it is possible to repeat the situation, that would be great.

 
Kirill Andreev:

wrote an owl, but for some reason the trailing stop is not working... What could be the reason?

to make it work you need to put it in OnTick
 
Vladislav Andruschenko:
It has to be put in OnTick for it to work.
Thanks, I'll give it a try!!!
 

Hello.

Please help, I want to modify all positions except the last one, the "youngest" one. Where do I add or subtract one?


 int i; total = OrdersTotal();
          for(i = 0; i < total; i++)
             {
              if(OrderSelect(i, SELECT_BY_POS)   && OrderLots() == _Lots)
              {
              if (OrderType()==OP_BUY) 
              {
              if (OrderOpenPrice()!=OrderStopLoss()) 
              {
              ModifyOrder(-1,OrderOpenPrice() , -1);
              }
             } 
            }  
           }
 
mila.com:

Hello.

Please help, I want to modify all positions except the last one, the "youngest" one. Where do I add or subtract one?



 int i; total = OrdersTotal();
          for(i = 0; i < total-1; i++)
             {
              if(OrderSelect(i, SELECT_BY_POS)   && OrderLots() == _Lots)
              {
              if (OrderType()==OP_BUY) 
              {
              if (OrderOpenPrice()!=OrderStopLoss()) 
              {
              ModifyOrder(-1,OrderOpenPrice() , -1);
              }
             } 
            }  
           }


 
-Aleks-:
int i; total = OrdersTotal();
          for(i = 0; i < total-1; i++)
             {
              if(OrderSelect(i, SELECT_BY_POS)   && OrderLots() == _Lots)
              {
              if (OrderType()==OP_BUY) 
              {
              if (OrderOpenPrice()!=OrderStopLoss()) 
              {
              ModifyOrder(-1,OrderOpenPrice() , -1);
              }
             } 
            }  
           }

Are you sure you have shown a reliable way of missing the last position in the list?

If you trade realistically, aren't you worried that sorting may suddenly become dependent?

Still, IMHO, we need two cycles here - in the first we search for the most recent position by opening time, in the second we modify all except the one whose ticket was found in the first cycle.

 
mila.com:

Hello.

Please help, I want to modify all positions except the last one, the "youngest" one. Where do I add or subtract one?


I would do so.

/********************Script program start function********************/
void OnStart()
{
 int i, t1, t2, clTicket, total = OrdersTotal();
  datetime d1, d2;
   double v1, v2, clVolume;
    for(i = total-1; i > 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS))
       {
        t1 = OrderTicket();
         d1 = OrderOpenTime();
        v1 = OrderLots();
       }
      else continue;
        if(OrderSelect(i-1, SELECT_BY_POS))
         {
          t2 = OrderTicket();
           d2 = OrderOpenTime();
          v2 = OrderLots();
         }
        else continue;
        clTicket = d1 > d2 ? t1 : t2;
       clVolume = d1 > d2 ? v1 : v2;
      if(!OrderClose(clTicket, clVolume, OrderClosePrice(), 100)) // закрывает независимо от типа ордера OP_BUY или OP_SELL
       Print("фигня кака-та");
    }
}/********************************************************************/

I do not have it checked and I cannot decide if I will have a "gap" in my order list when I close the next order.

If anyone is able to explain the possibility of such a "gap" appearing, I would be grateful...

As an insurance, we have inserted else continue; if there is a gap, one order will be just skipped. It seems to me so.

 
Alexey Viktorov:

I would do so.

Unfortunately, this has not been tested and I cannot figure out if there is a "hole" in the list of orders after the next order is closed.

If anyone is able to explain the possibility of such a "gap" appearing, I would be grateful...

As an insurance, another continue is inserted; if there is a gap, it will simply skip one order. It seems to me this way.

Originally, the question was about skipping the last order. And that is where you start.

You have to do it this way:

for(int i = total-2; i >= 0; i--)

Then there will be no gaps after deletion and the most recent order will be skipped.

 
Alexey Kozitsyn:

The original question was to skip the last order. This is where you start.

You have to do it this way:

Then there will be no skips after deletion and the most recent order will be skipped.

Not the last one on the list, but the "youngest" one by time.

Although I'm just as inconsiderate. The question is about modification and I'm writing close... And if you simply modify it, there will be no holes in the list of orders... You just might need to add variables and assign them values, and MUST check parameters to avoid error #1. Well, Mila can handle it herself.

 
Alexey Kozitsyn:

The original question was to skip the last order. This is where you start.

You have to do it this way:

for(int i = total-2; i >= 0; i--)

Then there will be no skipping after deletion and the most recent order will be skipped.

How can you be sure that there will not be a sorting dependency, and that you will miss the wrong order?

Once again, to reliably find exactly the last order, it must be identified by its opening time, not by its position in the list.

Reason: