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

 
Viachaslau Baiko:

Here's where I got a little tricky: I took this code (thanks to Alekseu Fedotov):

//+----------------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает номер бара закрытия последней позиции или -1.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" или NULL - текущий символ)          |
//|    tf - таймфрейм                  (    0       - текущий таймфрейм)       |
//|    op - операция                   (   -1       - любая позиция)           |
//|    mn - MagicNumber                (   -1       - любой магик)             |
//+----------------------------------------------------------------------------+
int NumberOfBarCloseLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) {
  datetime t;
  int      i, k=OrdersHistoryTotal();

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderCloseTime()) t=OrderCloseTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sy, tf, t, True));
}

And now I put the check:

if(УСЛОВИЕ && NumberOfBarCloseLastPos()>0)

And here is the hitch, because initially NumberOfBarCloseLastPos will be set to "-1". And consequently, the first order will never open.

What can we do in this situation? Or did I misunderstand something?

In this situation, we could tryif(CONTROL && NumberOfBarCloseLastPos()>-2), or think
 
Viachaslau Baiko:

Here's a little nuance: I took this code (thanks to Alekseu Fedotov):

//+----------------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает номер бара закрытия последней позиции или -1.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" или NULL - текущий символ)          |
//|    tf - таймфрейм                  (    0       - текущий таймфрейм)       |
//|    op - операция                   (   -1       - любая позиция)           |
//|    mn - MagicNumber                (   -1       - любой магик)             |
//+----------------------------------------------------------------------------+
int NumberOfBarCloseLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) {
...
  return(iBarShift(sy, tf, t, True));
}
if( УСЛОВИЕ && (NumberOfBarCloseLastPos()>0 || NumberOfBarCloseLastPos()==-1) )
 
Vitaly Muzichenko:
Well, there you go. And I suggested thinking about it.
 
Алексей Тарабанов:
In this situation, you could tryif(CONDITION && NumberOfBarCloseLastPos()>-2), or think
But with this option there will also be a value of "0".
 
Vitaly Muzichenko:
if( УСЛОВИЕ && (NumberOfBarCloseLastPos()>0 || NumberOfBarCloseLastPos()==-1) )
It's all working! Thank you!
 
Viachaslau Baiko:

Here's where I got a little tricky: I took this code (thanks to Alekseu Fedotov):

//+----------------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает номер бара закрытия последней позиции или -1.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" или NULL - текущий символ)          |
//|    tf - таймфрейм                  (    0       - текущий таймфрейм)       |
//|    op - операция                   (   -1       - любая позиция)           |
//|    mn - MagicNumber                (   -1       - любой магик)             |
//+----------------------------------------------------------------------------+
int NumberOfBarCloseLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) {
  datetime t;
  int      i, k=OrdersHistoryTotal();

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderCloseTime()) t=OrderCloseTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sy, tf, t, True));
}

And now I put the check:

if(УСЛОВИЕ && NumberOfBarCloseLastPos()>0)

And here is the hitch, because initially NumberOfBarCloseLastPos will be set to "-1". And consequently, the first order will never open.

What can be done in this situation? Or have I misunderstood something?

Well, that's what I would do:

//+------------------------------------------------------------------+
int BarCloseLastPos(string symbol_name, ENUM_TIMEFRAMES timeframe, int type, int magic_number) {
   datetime time=0;
   int      j=-1;
   for(int i=OrdersHistoryTotal()-1; i>=0; i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) {
         if(OrderMagicNumber()!=magic_number)   continue;
         if(OrderSymbol()!=symbol_name)         continue;
         if(OrderType()!=type)                  continue;
         if(OrderCloseTime()>time) {
            time=OrderCloseTime();
            j=i;
            }
         }
      }
   if(OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) return(iBarShift(symbol_name,timeframe,time));
   return(EMPTY);
}
//+------------------------------------------------------------------+

... and check:

if(BarCloseLastPos(Symbol(),PERIOD_CURRENT,OP_BUY,Magic)>0) {
   // Последняя позиция Buy была закрыта не на текущем баре
   }
 
Artyom Trishkin:

Well, that's what I would do:

//+------------------------------------------------------------------+
int BarCloseLastPos(string symbol_name, ENUM_TIMEFRAMES timeframe, int type, int magic_number) {
   datetime time=0;
   int      j=-1;
   for(int i=OrdersHistoryTotal()-1; i>=0; i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) {
         if(OrderMagicNumber()!=magic_number)   continue;
         if(OrderSymbol()!=symbol_name)         continue;
         if(OrderType()!=type)                  continue;
         if(OrderCloseTime()>time) {
            time=OrderCloseTime();
            j=i;
            }
         }
      }
   if(OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) return(iBarShift(symbol_name,timeframe,time));
   return(EMPTY);
}
//+------------------------------------------------------------------+

... and check:

if(BarCloseLastPos(Symbol(),PERIOD_CURRENT,OP_BUY,Magic)>0) {
   // Последняя позиция Buy была закрыта не на текущем баре
   }
Well, what if it, sneakily, closed on the current bar?
 
Алексей Тарабанов:
Well, what if it sneaky closed on the current bar?

This is exactly what needs to be missed. It does not need to open a position if the previous one closed on this bar - on zero.

And if we need to check the condition that the position closed on the current bar, we need to check zero:

if(BarCloseLastPos(Symbol(),PERIOD_CURRENT,OP_BUY,Magic)==0) {
   // Последняя позиция Buy была закрыта на текущем баре
   }
 
Viachaslau Baiko:

Here's where I got a little tricky: I took this code (thanks to Alekseu Fedotov):

//+----------------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает номер бара закрытия последней позиции или -1.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" или NULL - текущий символ)          |
//|    tf - таймфрейм                  (    0       - текущий таймфрейм)       |
//|    op - операция                   (   -1       - любая позиция)           |
//|    mn - MagicNumber                (   -1       - любой магик)             |
//+----------------------------------------------------------------------------+
int NumberOfBarCloseLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) {
  datetime t;
  int      i, k=OrdersHistoryTotal();

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderCloseTime()) t=OrderCloseTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sy, tf, t, True));
}

And now I put the check:

if(УСЛОВИЕ && NumberOfBarCloseLastPos()>0)

And here is the hitch, because initially NumberOfBarCloseLastPos will be set to "-1". And consequently, the first order will never open.

What can we do in this situation? Or did I misunderstand something?

if(УСЛОВИЕ && NumberOfBarCloseLastPos()!=0)
 
Good morning MT4 forum users! I faced a problem and I think either I do not know how to calculate and a lot of calculators or MT4 compiler has its own way. I am writing the code print((1.1145-1.1123-0.0020)/0.0001) should get 2.I tried to print((OrdArr[i].buy-Ord[1,3]-PipStep)/Point) pinstep is 0.0020, but I keep getting only one tenth of what it should be. Maybe someone knows what the problem is! SPS.