Cualquier pregunta de los recién llegados sobre MQL4 y MQL5, ayuda y discusión sobre algoritmos y códigos - página 13

Está perdiendo oportunidades comerciales:
- Aplicaciones de trading gratuitas
- 8 000+ señales para copiar
- Noticias económicas para analizar los mercados financieros
Registro
Entrada
Usted acepta la política del sitio web y las condiciones de uso
Si no tiene cuenta de usuario, regístrese
Aquí es donde me he complicado un poco: he tomado este código (gracias a 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));
}
Y ahora pongo la cuenta:
Y aquí está el problema, porque inicialmente NumberOfBarCloseLastPos se establecerá en "-1". Y en consecuencia, el primer pedido nunca se abrirá.
¿Qué podemos hacer en esta situación? ¿O he entendido algo mal?
Aquí hay un pequeño matiz: tomé este código (gracias a 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));
}
En esta situación, podría intentarif(CONDITION && NumberOfBarCloseLastPos()>-2), o pensar
Aquí es donde me he complicado un poco: he tomado este código (gracias a 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));
}
Y ahora pongo la cuenta:
Y aquí está el problema, porque inicialmente NumberOfBarCloseLastPos se establecerá en "-1". Y en consecuencia, el primer pedido nunca se abrirá.
¿Qué se puede hacer en esta situación? ¿O he entendido algo mal?
Bueno, eso es lo que yo haría:
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);
}
//+------------------------------------------------------------------+
... y comprueba:
// Последняя позиция Buy была закрыта не на текущем баре
}
Bueno, eso es lo que yo haría:
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);
}
//+------------------------------------------------------------------+
... y comprueba:
// Последняя позиция Buy была закрыта не на текущем баре
}
¿Y si se cerrara sigilosamente en la barra actual?
Esto es exactamente lo que hay que echar de menos. No necesita abrir una posición si la anterior se cerró en esta barra - en cero.
Y si necesitamos comprobar la condición de que la posición se cerró en la barra actual, tenemos que comprobar el cero:
// Последняя позиция Buy была закрыта на текущем баре
}
Aquí es donde me he complicado un poco: he tomado este código (gracias a 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));
}
Y ahora pongo la cuenta:
Y aquí está el problema, porque inicialmente NumberOfBarCloseLastPos se establecerá en "-1". Y en consecuencia, el primer pedido nunca se abrirá.
¿Qué podemos hacer en esta situación? ¿O he entendido algo mal?