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

 
Vitaly Muzichenko:

The function goes through and selects the newest one by time, i.e. the last one

Next, we have a selected one, and we substitute it in the functioniBarShift(sym, tf, oot ,true);

The function returns the bar number by time. That's all.

Unfortunately I can't check it, but when I used it, it worked perfectly, as well as all functions published here by I. Kim

and if i have a ticket assigned when i open an order and the function selects a sequential number is that ok?
 
Вадим Мотеюнас:
Writes error indicatingif( iOpen[0] != time_open )'iOpen' - undeclared identifie

I wrote something a little wrong, a little in the other direction of thought. Change iOpen[0] to Open[0]. Corrected. To put it simply though. When I open a bar, I remember the time of bar opening and no orders are already taken on this bar.

 

It is also written in the function description that

The selection of positions to be taken into account is set by external parameters:

  • sy- Name of market instrument. If you set this parameter, the function will only consider positions of the specified instrument. The default value -NULL means the current market instrument.
  • tf- Timeframe. The default value0 means the current timeframe.
  • op- Trade operation, position type. Valid values:OP_BUY,OP_SELL or-1. The default value-1 means any position.
  • mn- Position identifier, MagicNumber. Default value-1 means any identifier.
These values should be declared before the first special function, i.e. the fact that they are included in the description of theNumberOfBarOpenLastPos() function itselfisn't enough?

 
Вадим Мотеюнас:

It is also written in the function description that

The selection of positions to be taken into account is set by external parameters:

  • sy- Name of market instrument. If you set this parameter, the function will only consider positions of the specified instrument. The default value -NULL means the current market instrument.
  • tf- Timeframe. The default value0 means the current timeframe.
  • op- Trade operation, position type. Valid values:OP_BUY,OP_SELL or-1. The default value-1 means any position.
  • mn- Position identifier, MagicNumber. Default value-1 means any identifier.
By external parameters? Is it necessary to declare them before the first special function? i.e. it'snot enough that they are in the description of theNumberOfBarOpenLastPos() function itself?

Enough. In the external parameters you can enter the values you need: MagicNumber, which positions you are looking for, if you want BUY(0) and SELL(1) - then write "-1".

 
Konstantin Nikitin:

I wrote something a little wrong, a little in the other direction of thought. Change iOpen[0] to Open[0]. Corrected. To put it simply though. When I open a bar, it will remember the time of bar opening and no orders will be opened on this bar.

a lot of orders are opened and it looks like the condition if(NumberOfBarOpenLastPos(Symbol(),0,-1,-1)>0) before the opening of a non-order is fulfilled since NumberOfBarOpenLastPos returns -1

 
Вадим Мотеюнас:

a lot of orders are opened, and it seems that the condition if(NumberOfBarOpenLastPos(Symbol(),0,-1,-1)>0) is met before a non-order is opened, because NumberOfBarOpenLastPos returns -1

You are looking for orders, then you need to use the function in its full version, namely, with the ability to search by orders, not only by positions:

int NumberOfBarOpenLastPos(string sym="",int tf=0,int op=-1,int mn=-1)
  {
   datetime oot=0; // Инициализируем нулём
   int      i,k=OrdersTotal();

   if(sym=="") sym=Symbol();
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==sym)
           {
            if(OrderType()<6) // Ищем и по ордерам
              {
               if(op<0 || OrderType()==op)
                 {
                  if(mn<0 || OrderMagicNumber()==mn)
                    {
                     if(oot<OrderOpenTime()) oot=OrderOpenTime(); // Ищем последнее
                    }
                 }
              }
           }
        }
     }
   return(iBarShift(sym, tf, oot, true));
  }
 
Vitaly Muzichenko:

This is sufficient. In the external parameters you can enter the required values: Majik, what positions are looked for, maybe only BUY, if BUY(0) and SELL(1) are needed - then we write "-1".

when I start in the tester, there are no open orders, maybe that's why -1 is returned, which is why the condition if(NumberOfBarOpenLastPos(Symbol(),0,-1,-1)>0) does not hold?

 
Вадим Мотеюнас:

When starting in the tester, there are no open orders, maybe that's why -1 is returned and therefore the condition if(NumberOfBarOpenLastPos(Symbol(),0,-1,-1)>0) is not met?

I will check it now.

 
Vitaly Muzichenko:

You are looking for orders, then you need to use the function in its full version, namely with the ability to search by orders, not just by positions:

still returns -1

 
Вадим Мотеюнас:

still returns -1

It works perfectly!

From two positions it has chosen the last one, which is on bar 11, as you can see in the screenshot:


Check code:

 void OnTick() 
  {
   ...
   Comment( NumberOfBarOpenLastPos("",0,-1,-1) );
   ...
  }

int NumberOfBarOpenLastPos(string sym="",int tf=0,int op=-1,int mn=-1)
  {
   datetime oot=0; // Инициализируем нулём
   int      i,k=OrdersTotal();

   if(sym=="") sym=Symbol();
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==sym)
           {
            if(OrderType()<6) // Ищем и по ордерам
              {
               if(op<0 || OrderType()==op)
                 {
                  if(mn<0 || OrderMagicNumber()==mn)
                    {
                     if(oot<OrderOpenTime()) oot=OrderOpenTime(); // Ищем последнее
                    }
                 }
              }
           }
        }
     }
   return(iBarShift(sym, tf, oot, true));
  }
Reason: