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

 
EVGENII SHELIPOV #:

Good day gentlemen programmers!!!!

I will try once again to ask my question, but I have rephrased it a bit.

In a grid EA, we need to find the ticket of the penultimate order. I think, the solution -1 from the ticket of the maximum order does not work in the strategy tester, but it may be so on the demo and real accounts, it is difficult to see why.

I have written the code of the function that determines the next to last order ticket. This logic is as follows: if we look through all open orders in the grid, we will find the maximal one following it and it will be the necessary order ticket.

However, my programming level is not high enough and there is some error in the code. The result of this function is the minimum ticket. These two functions max ticket and penultimate ticket. The function defining max ticket works without problems.

Please advise what the error is. Thank you.

This second picture shows that you do not need a maximum ticket or maximum open time, but you need to choose a minimum open price for Buy orders and a maximum open price for Sell orders.
 
Alexey Viktorov #:
This is the second picture telling you that you do not need a maximum ticket or maximum opening time, but you need to select the minimum opening price for Buy orders and the maximum opening price for Sell orders.

Alexei I have the functions that determine these prices could you explain how to reach the ticket you are looking for?

 
Alexey Viktorov #:
This second picture shows that you do not need a maximum ticket or maximum open time, but you need to choose a minimum open price for Buy orders and a maximum open price for Sell orders.
//+----------------------------------------------------------------------------+
//| Определение цены открытия макс лота                                        |
//+----------------------------------------------------------------------------+
double PriceMaxOrder()
  {
   double max_price = 0 ;
   max_ticket = 0;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() > max_ticket)
                    {
                     max_ticket = OrderTicket();
                     max_price = OrderOpenPrice();
                    }
                 }
              }
           }
        }
     }
   return(max_price);
  }
 
EVGENII SHELIPOV #:

Alexei I have the functions that determine these prices could you explain how to reach the ticket you are looking for?

Like this.

//+------------------------------------------------------------------+
//| Расчет тикета нижнего ордера BUY в сетке                         |
//+------------------------------------------------------------------+
int GetTicketMaxOrder(int Magic)// int Magic вставлен только для компиляции без ошибок.
 {
  int total = OrdersTotal(),
      my_ticket = 0;
  double minPrice = DBL_MAX;
  for(int cnt = total; cnt-- > 0;)
   {
    if(OrderSelect(cnt, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
     {
      if(OrderType() == OP_BUY)
       {
        if(OrderOpenPrice() < minPrice)
          my_ticket = OrderTicket();
       }
     }
   }
  return(my_ticket);
 }
/*****************************End program****************************/
 
Alexey Viktorov #:

Like this

Alexey thanks!!! But before getting into the gist of the function I wanted to clarify: is this function for selecting the ticket of the penultimate order?

 
EVGENII SHELIPOV #:

Alexey thank you!!! But before getting into the gist of the function I wanted to clarify: is this function to select the ticket of the penultimate order?

No, it finds the BUY order ticket with the lowest price. From here on it's up to me or Makar to let me practice.

 
Alexey Viktorov #:

No, it finds the BUY order ticket with the lowest price. From here on it's up to you or Makar to practice.

For the second day I feel inadequate in your company. I've got functions to calculate min/max ticket, they work without problems as Kalashnikov's machine, I haven't asked them yet, but the second day they turned me into a rut!

//+----------------------------------------------------------------------------+
//| Расчет тикета максимального ордера в сетке                                 |
//+----------------------------------------------------------------------------+
int GetTicketMaxOrder()
  {
   max_ticket = 0;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() > max_ticket)
                     max_ticket = OrderTicket();
                 }
              }
           }
        }
     }
   return(max_ticket);
  }
//+----------------------------------------------------------------------------+
//| Расчет тикета минимального ордера в сетке                                  |
//+----------------------------------------------------------------------------+
int GetTicketMinOrder()
  {
   min_ticket=INT_MAX;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() < min_ticket)
                     min_ticket = OrderTicket();

                 }
              }
           }
        }
     }
   return(min_ticket);
  }

Well, anyway, thanks for the heads up

 
EVGENII SHELIPOV #:

Yes Alexey I feel the second day among you not quite adequate person. Functions to calculate the min/max ticket is working smoothly as Kalashnikov's machine I did not ask anyone about them, but the second day I get turned on a snag!!!

Well, anyway, thanks for the tip.

Isn't this picture

Forum on trading, automated trading systems and testing trading strategies

Any questions from newbies on MQL4 and MQL5, help and discussion of algorithms and codes

EVGENII SHELIPOV, 2021.11.07 06:43

Good day gentlemen programmers!!!!

I will try once again to ask my question, but slightly rephrased.

In a grid EA, I need to find the ticket of the penultimate order. A solution of -1 from the ticket of the maximum order does not work in the strategy tester, maybe, but it will not work on a demo or real account, so I attach a picture, it will be clear why.

I have written the code of the function that determines the next to last order ticket. This logic is as follows: if we look through all open orders in the grid, we will find the maximal one following it and it will be the necessary order ticket.

However, my programming level is not high enough and there is some error in the code. The result of this function is the minimum ticket. These two functions max ticket and penultimate ticket. The function defining max ticket works without problems.

//+----------------------------------------------------------------------------+
//| Расчет тикета предпоследнего ордера в сетке                                |
//+----------------------------------------------------------------------------+
int GetTicketPenultimateOrder()
  {
   penultimate_ticket = 0;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() > penultimate_ticket && penultimate_ticket != GetTicketMaxOrder())
                     penultimate_ticket = OrderTicket();
                 }
              }
           }
        }
     }
   return(penultimate_ticket);
  }
//+----------------------------------------------------------------------------+
//| Расчет тикета максимального ордера в сетке                                 |
//+----------------------------------------------------------------------------+
int GetTicketMaxOrder()
  {
   max_ticket = 0;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() > max_ticket)
                     max_ticket = OrderTicket();
                 }
              }
           }
        }
     }
   return(max_ticket);
  }

Please advise what the error is. Thank you.


are the arrows showing the maximum ticket? I see arrows here pointing to the second lowest order price. What is wrong?
 
Alexey Viktorov #:

Is this picture


the arrows show the maximum ticket? I see arrows here pointing to the second lowest order price. What is wrong?

Alexei, can you enlarge the picture, please?

 
EVGENII SHELIPOV #:

Alexei, can you enlarge the picture, please?

This isthe PREDICTED order