Questions from Beginners MQL4 MT4 MetaTrader 4 - page 250

 

If it's simple, to check quickly, it's something like this:

   double profit = 0;
   int cnt=LockTicket, i , ototal = OrdersHistoryTotal();

   for(i = ototal-1; i >=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
         if(OrderSymbol() == Symbol() && OrderCloseTime() > 0)
           {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
              {
               if(OrderMagicNumber() == Magic )
                 {
                  profit += OrderProfit()+OrderCommission()+OrderSwap();
                  if(profit>max)
                    { 
                    cnt=OrdersTicket();
                    break;
                    }                  
                 }
              }
           }
        }
     }

Orders usually follow one another in time, but this is not guaranteed. Therefore, the chronology should be checked. But for experiments this will do.

 
Aleksei Stepanenko #:

If it's simple, to check quickly, it's something like this:

Orders usually follow one another in time, but this is not guaranteed. Therefore, the chronology should be checked. But this will do for experiments.

I agree, the timing is not guaranteed. That is why I want to go through all of the closed orders by the ticket. I know the order ticket and am trying to count from it, but something has gone wrong) What would be the right way?

 
Aleksei Stepanenko #:

If it's simple, to check quickly, it's something like this:

Orders usually follow one another in time, but this is not guaranteed. Therefore, the chronology should be checked. But this will do for experiments.

If we look through the history, it would be better to look through the orders from smaller to larger ones that would close at the moment of recalculation as market orders are considered from larger to smaller ones so that we could correctly consider those opened and closed on the same tick. I think so)

Aleksei Stepanenko #:

If it is simple to quickly check, then it is something like this:

As a rule of thumb, orders follow each other in time, but this is not guaranteed. Therefore, in a good way, the chronology should be checked. But it will do for experiments.

Doesn't Alexey's code get it right too?

 
Valeriy Yastremskiy #:

In the history it is better to go from less to more orders to take into account those that will close at the time of recalculation, it is the market orders we count from more to less to correctly account for open and closed on the same tick. I think so)

Does Alexey's code also calculate incorrectly?

I have not tried it. I will try it now. I will let you know.

 
Aleksei Stepanenko #:

If it's simple, to check quickly, it's something like this:

Orders usually follow one another in time, but this is not guaranteed. Therefore, the chronology should be checked. But it will do for experiments.

I checked it and got a lot of errors during compilation, I fixed them all, except for :

if(profit>max)

This line, I don't quite understand it. Can you explain?

 
makssub #:
The idea is to change the ticket after you reach a certain profit

The max is the limit of the profit after which the ticket changes. The value is set by you.

 
Aleksei Stepanenko #:

The max is the limit of the profit after which the ticket changes. Its value is set by you.

The problem is different, if I understand it correctly. There are 1, 2 and 3 orders. I do not know how the open prices are formed. Order 3 is closed, the total profit is equal to the profit of order 3. I.e. we need to calculate the ticket for the order opened before order 3 . Then order 2 is closed. We should find the ticket of the order opened before order 2. The total profit is equal to the profit of orders 2 and 3.

I do not understand why, but it seems to be the logic.)

 
Valery, I don't fully understand the logic on those two sentences.
 
Aleksei Stepanenko #:
Valery, I didn't fully understand the logic on those two sentences.

Forum on Trading, Automated Trading Systems and Strategy Tests

FAQ from Beginners MQL4 MT4 MetaTrader 4

makssub, 2021.09.01 16:38

int FindTicket()
   {
   int oldticket;
   int tick=0;
   ticket=0;
   
   
   for(int cnt = OrdersTotal ()-1; cnt>=0; cnt--)
      {
      if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
         {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
            {
            oldticket = OrderTicket();
            if (oldticket > ticket)
               {
               ticket = oldticket;
               tick = OrderTicket();
               }
            }
         }
      }
   return(tick); 
   }              
int TickF = FindTicket();
int CalculateProfitHistory() 
{
  double _point;
  int    i, _ototal = OrdersHistoryTotal(), _profit=0;
  for   (i = 0; i < OrdersHistoryTotal(); i++) 
  {
    if (OrderSelect(TickF, SELECT_BY_TICKET, MODE_HISTORY)) 
    {
      if (OrderSymbol() == Symbol())
      {
        if (OrderMagicNumber() == Magic) 
        {
           _point = MarketInfo(OrderSymbol(), MODE_POINT);
           if (_point == 0) 
           {
              if (StringFind(OrderSymbol(), "") < 0) 
                 _point = 0.0001; 
              else _point = 0.01;
           }   
           if (OrderType() == OP_BUY) 
           {
              _profit += int((MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice())/_point);
           }
           if (OrderType()==OP_SELL) 
           {
              _profit += int((OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK))/_point);
           }
         }
      }
    }
  }
  return(_profit);
}

I should use the first function to find the required order ticket and the second function should calculate the profit of all closed orders following that ticket. I am not interested in profit of the ones before that. But the second one does not calculate it correctly. When an order is opened these 2 functions are called and therefore it should be equal to 0, but it is not.
PS took your advice, gave up arrays)
12th box above)


 

I have thought of everything and made it my own based on your tips)

The task was to cover a losing order with other orders at profit.

We have implemented it. We memorized the ticket of the order opened after the losing one; this was our report point. After this point, we start calculating the profit of the loss-making order and the profit of the closed orders after it. Let me remind you that the first order is open and we close it when it reaches a profit.

if ( (CalculateProfitHistory() + FirstProfit() >= Profit)

{

CloseFirst();

Print ("Close the first losing order");

}


double CalculateProfitHistory() 
{
   double profit = 0;
   int cnt=Ticket, i , ototal = OrdersHistoryTotal();

   for(i = ototal-1; i >=0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      {
         if(OrderSymbol() == Symbol() && OrderCloseTime() > 0)
         {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            {
               if(OrderMagicNumber() == Magic )
               {
                  if (Ticket !=0)
                  {
                     if (OrderTicket() >= Ticket)
                     {
                        profit += OrderProfit()+OrderCommission()+OrderSwap();
                     }                  
                  }
               }
            }
         }
      }
   }
   return(profit);
}
Reason: