Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 407

 
BeerGod:

There is a function to close all orders, it closes starting from the last open, please advise how to re-do it so it starts closing from the first open and ends with the last one.


Change the cycle to reverse!

for(int i=total-1;i>=0;i--)
//на:
for(int i=0;i<total;i++)
 
BeerGod:

There is a function to close all orders, it closes from the last open, please advise how to re-do it so it starts closing from the first open and ends with the last one.


Do you really need it. Have you read my post here?
 
borilunad:

Reverse the cycle!


I also thought it was easy at first, but in this case it doesn't close even numbers, it only closes odd numbers. I've already racked my brains.


 
khorosh:
And do you need it. Have you read my post here?


Of course I read it, that's the point, it's not about closing loss-making or profitable orders first, it's about closing them in the same sequence as the orders were opened, then there will be less surges or dips in the balance when fixing equity.
 
BeerGod:

Of course I read it, that's the point, it's not about closing profitable or loss-making orders first, it's about closing them in the same sequence as the orders were opened, then there will be less surges or dips in balance when fixing equity.
Dips in balance do not matter at all, only equity dips are important. The balance can even be negative as long as equity is high.
 
khorosh:
Balance failures don't matter, only equity failures do. The balance can even be negative as long as equity is high.


This is all fine, it's just academic interest, why is the loop in reverse not working correctly?
 
khorosh:

I am sure that 1. You cannot reliably judge how much lot a position is opened, if only because the author did not provide the code for opening a position. And where you saw zero, that is the initial value of the variable. It is too early to teach anyone.
If I am not mistaken, it is not an initial value of the variable, but zeroing of this variable.
Although, you could have described it the way you did (the value of the variable at the time you start working with that variable, i.e. "0").
Thank you for "opening my eyes" (a phraseology).

You are right as always, dear Yuri. It's not me who needs to teach someone, but me who needs to teach someone. :)
 
BeerGod:

This is all fine, just an academic interest, why is the loop in reverse not working correctly ?

When you go through the orders from 0, then when you close them, their numbering changes (1st becomes zero, 2nd becomes first, etc.), but the variable with the number of the order being closed also increases by one. Thus, the next order is skipped when closing.
 
Contender:

When you go through the orders from 0, their numbering changes when they are closed (1st becomes null, 2nd becomes first, etc.), but the variable with the number of the order being closed is also incremented by one. Thus, the next order is skipped when closing it.

Can we work out the correct way to close the order starting from zero? If you can give us a line of code please.
 
khorosh:
To evaluate the correctness of the code, you need to know exactly what the author wanted to get. Your information is insufficient. What did you want to get is not quite clear. If you wanted to compensate for the loss after the grid closure by opening an opposite order and expecting that the price will pass in the direction of the last order some number of points, the compensation process depends both on the lot of this order and the distance the price will pass in the favourable direction. That means when you calculate the lot, you should also define the distance that the price will have to pass to compensate for the loss. But maybe you mean something else.


Yes, I should have written more precisely. It's just that I've already laid out in full twice what this function is supposed to do, but no one has answered. Once again, what this function should do. Suppose I have a grid of orders. Whether they are opened with the same step or not, it does not matter. One order was opened earlier and another one later, i.e. every position has passed a different amount of points with a different lot. The grid will be closed according to certain conditions and I need to calculate the LOT needed to cover the loss incurred from that grid for TP points. To avoid writing two mirror functions I have introduced an otype parameter in the function.

However, I have made a mistake somewhere. Please help correct it.

double FindRightLot (int otype) // функция поиска лота, необходимого для выхода из просадки после 
                               //закрытия сетки ордеров
{
  double Lot=0; double TotalLot=0;
  for (int i = OrdersTotal()-1; i>0; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
    {
       if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType() == otype)
       {
         if (otype == OP_BUY)
         {
           Lot = NormalizeDouble (((OrderOpenPrice()-Bid)*Point)*OrderLots()/TP,2); 
           if (Lot>0)
           {
              TotalLot= TotalLot+Lot;
           }
         }
           
       
         else if (otype == OP_SELL)
         {
           Lot = NormalizeDouble (((Ask-OrderOpenPrice())*Point)*OrderLots()/TP,2);
           if (Lot>0)
           {
            TotalLot= TotalLot+Lot;
           }
           
         }
       }
     }
   }
   return (TotalLot);
   
 }
Reason: