Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 13
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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:
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?
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));
}
In this situation, you could tryif(CONDITION && NumberOfBarCloseLastPos()>-2), or think
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:
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:
// Последняя позиция Buy была закрыта не на текущем баре
}
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:
// Последняя позиция Buy была закрыта не на текущем баре
}
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:
// Последняя позиция Buy была закрыта на текущем баре
}
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:
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?