Hola,
Tengo crea un EA y cuando lo pongo en varios activos me cierra posiciones que no debería. Cuando trabaja solo en 1 me funciona bien.
He probado crear el mismo EA pero con distintos nombres pero también me hace lo mismo. ¿Qué puede pasar?
Necesito ayuda, ya no sé como continuar.
Gracias,
Quizas uno de los inputs o parametros que puedas configurar antes de iniciar el EA es un numero Magico. Si lo puedes modificar, pon un numero distinto para cada Symbolo o Par
Saludos!!!
Hola Miguel Antonio,
También lo he probado y tambien funciona mal...
Gracias por la ayuda
Hola,
Tengo crea un EA y cuando lo pongo en varios activos me cierra posiciones que no debería. Cuando trabaja solo en 1 me funciona bien.
He probado crear el mismo EA pero con distintos nombres pero también me hace lo mismo. ¿Qué puede pasar?
Necesito ayuda, ya no sé como continuar.
Gracias,
Hola,
Puedes compartir las líneas de código donde cierras las posiciones? para ver si te puedo ayudar.
Saludos...
Hola,
Puedes compartir las líneas de código donde cierras las posiciones? para ver si te puedo ayudar.
Saludos...
Hola Antonio,
Te adjunto todo el código por si me puedes echar una mano. Es un sistema que utiliza el RSI para la entrada y la línea média del donchian para hacer trailing stop. El tamaño de la posición se calcula con el 1% del importe de la cuenta.
Gracias,
input double Conv_Div = 1; // €/Div. Contrapartida
input int Per_RSI = 14;
input int Per_Donchian = 21;
input double RSI_L = 62; //RSI apertura largos
input double RSI_L_Cie = 57; //RSI cierre largos
input double RSI_L_Ent_Max = 100; //RSI maximo entrada
input double RSI_C = 38; // RSI apertura cortos
input double RSI_C_Cie = 43; // RSI cierre cortos
input double RSI_C_Ent_Min = 00; // RSI mínimo entrada
input double Franja_no_ent = 0; // Franja no entrar hasta retroceso
input int n_entradas = 1; //Nª Entradas por tramo
input double riesgo =1; // Riesgo máximo
input double pips_min = 0; // pips entre open y stop
input double Ini_Trailing_Stop = 80; // % de benefico inico stop
input double Incre_Donchian = 0; //Diferencia Donchian para operar
input int velas_retraso = 2; // Velas retraso para comparar Donchian
input int magic = 12;
int Mov_stop;
int tipo_orden = 0;
int entrada = 1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
bool compra(){
bool indicador = 1; //iRSI(NULL,0,Per_RSI,0,1);
if (indicador = 1){return true;} else{return false;}
}
bool venta(){
bool indicador = 1; //iRSI(NULL,0,Per_RSI,0,1);
if (indicador = 1){return true;} else{return false;}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double stop = NormalizeDouble(((High[iHighest(NULL,0,MODE_HIGH,Per_Donchian,0)]+Low[iLowest(NULL,0,MODE_LOW,Per_Donchian,0)])/2),5);
double stop_ant = NormalizeDouble(((High[iHighest(NULL,0,MODE_HIGH,Per_Donchian,velas_retraso)]+Low[iLowest(NULL,0,MODE_LOW,Per_Donchian,velas_retraso)])/2),5);
double l_sup = NormalizeDouble(High[iHighest(NULL,0,MODE_HIGH,Per_Donchian,0)],5);
double l_sup_ant = NormalizeDouble(High[iHighest(NULL,0,MODE_HIGH,Per_Donchian,velas_retraso)],5);
double l_inf = NormalizeDouble(Low[iLowest(NULL,0,MODE_LOW,Per_Donchian,0)],5);
double l_inf_ant = NormalizeDouble(Low[iLowest(NULL,0,MODE_LOW,Per_Donchian,velas_retraso)],5);
double Ini_Trailing = (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100);
double Beneficio = OrderProfit();
bool trade = true;
if (iRSI(NULL,0,Per_RSI,0,1)<(RSI_L-Franja_no_ent) && iRSI(NULL,0,Per_RSI,0,1)>(RSI_C+Franja_no_ent) && OrdersTotal()==0){entrada=1;}
if( ((stop-Bid)!=0) && ((Ask-stop))!=0) {
double lote_c = MathRound((AccountBalance()*riesgo*Conv_Div/100)/(Ask-stop));
if(compra()==true && iRSI(NULL,0,Per_RSI,0,1)>RSI_L && iRSI(NULL,0,Per_RSI,0,1)<RSI_L_Ent_Max &&
(MathAbs((Ask-stop)*10000)>=pips_min) && ((stop-Bid)!=0) && (Ask-stop)!=0
&& ((l_sup-l_sup_ant)*10000>=Incre_Donchian) && entrada < 2){
ObjectDelete("lote_c");
double compra = OrderSend(NULL,OP_BUY,lote_c,Ask,3,stop,NULL,
"LARGOS",magic,0,clrGreen);
Mov_stop = 1;
tipo_orden = 1;
entrada++;
Alert (GetLastError());}
if(venta()==true && iRSI(NULL,0,Per_RSI,0,1)<RSI_C && iRSI(NULL,0,Per_RSI,0,1)>RSI_C_Ent_Min &&
(MathAbs((stop-Bid)*10000)>=pips_min) && ((stop-Bid)!=0) && ((Ask-stop)!=0)
&& ((l_inf_ant-l_inf)*10000>=Incre_Donchian) && entrada < 2){
ObjectDelete("lote_v");
double lote_v = MathRound((AccountBalance()*riesgo*Conv_Div/100)/(stop-Bid));
double venta = OrderSend(NULL,OP_SELL,lote_v,Bid,3,stop,NULL,
"CORTOS",magic,0,clrGreen);
Mov_stop = 1;
tipo_orden = 2;
entrada++;
Alert (GetLastError());
}
}
for (int i=0;i<OrdersTotal();i++){
int orden = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if( Beneficio >= Ini_Trailing && Mov_stop==1){
int modificar_orden = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),NULL,0,clrGreen);
Alert (GetLastError());
Mov_stop++;}
if(tipo_orden == 1 && OrderProfit() > (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100) && (OrderProfit() >= (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100))
&& (Mov_stop> 1) && stop > OrderOpenPrice()){
int modificar_orden = OrderModify(OrderTicket(),OrderOpenPrice(),stop,NULL,0,clrBlue);
Alert (GetLastError());}
if(tipo_orden == 2 && OrderProfit() > (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100) && (OrderProfit() >= (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100))
&& (Mov_stop> 1) && stop < OrderOpenPrice() ){
int modificar_orden = OrderModify(OrderTicket(),OrderOpenPrice(),stop,NULL,0,clrBlue);
Alert (GetLastError());}
}
for(int i=0;i<OrdersTotal();i++){
int orden = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(Beneficio < 0 && iRSI(NULL,0,Per_RSI,0,1)<= RSI_L_Cie){
if(OrderType()==OP_BUY){
int cierre_largos_RSI = OrderClose(OrderTicket(),OrderLots(),Bid,10,clrRed);
break;}
if(Beneficio < 0 && iRSI(NULL,0,Per_RSI,0,1)>= RSI_C_Cie){
if(OrderType()==OP_SELL){
int cierre_cortos_RSI = OrderClose(OrderTicket(),OrderLots(),Ask,10,clrRed);
break;}
}
}
}
}
//+------------------------------------------------------------------+
- 2021.05.05
- www.mql5.com
Hola Antonio,
Te adjunto todo el código por si me puedes echar una mano. Es un sistema que utiliza el RSI para la entrada y la línea média del donchian para hacer trailing stop. El tamaño de la posición se calcula con el 1% del importe de la cuenta.
Gracias,
input double Conv_Div = 1; // €/Div. Contrapartida
input int Per_RSI = 14;
input int Per_Donchian = 21;
input double RSI_L = 62; //RSI apertura largos
input double RSI_L_Cie = 57; //RSI cierre largos
input double RSI_L_Ent_Max = 100; //RSI maximo entrada
input double RSI_C = 38; // RSI apertura cortos
input double RSI_C_Cie = 43; // RSI cierre cortos
input double RSI_C_Ent_Min = 00; // RSI mínimo entrada
input double Franja_no_ent = 0; // Franja no entrar hasta retroceso
input int n_entradas = 1; //Nª Entradas por tramo
input double riesgo =1; // Riesgo máximo
input double pips_min = 0; // pips entre open y stop
input double Ini_Trailing_Stop = 80; // % de benefico inico stop
input double Incre_Donchian = 0; //Diferencia Donchian para operar
input int velas_retraso = 2; // Velas retraso para comparar Donchian
input int magic = 12;
int Mov_stop;
int tipo_orden = 0;
int entrada = 1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
bool compra(){
bool indicador = 1; //iRSI(NULL,0,Per_RSI,0,1);
if (indicador = 1){return true;} else{return false;}
}
bool venta(){
bool indicador = 1; //iRSI(NULL,0,Per_RSI,0,1);
if (indicador = 1){return true;} else{return false;}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double stop = NormalizeDouble(((High[iHighest(NULL,0,MODE_HIGH,Per_Donchian,0)]+Low[iLowest(NULL,0,MODE_LOW,Per_Donchian,0)])/2),5);
double stop_ant = NormalizeDouble(((High[iHighest(NULL,0,MODE_HIGH,Per_Donchian,velas_retraso)]+Low[iLowest(NULL,0,MODE_LOW,Per_Donchian,velas_retraso)])/2),5);
double l_sup = NormalizeDouble(High[iHighest(NULL,0,MODE_HIGH,Per_Donchian,0)],5);
double l_sup_ant = NormalizeDouble(High[iHighest(NULL,0,MODE_HIGH,Per_Donchian,velas_retraso)],5);
double l_inf = NormalizeDouble(Low[iLowest(NULL,0,MODE_LOW,Per_Donchian,0)],5);
double l_inf_ant = NormalizeDouble(Low[iLowest(NULL,0,MODE_LOW,Per_Donchian,velas_retraso)],5);
double Ini_Trailing = (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100);
double Beneficio = OrderProfit();
bool trade = true;
if (iRSI(NULL,0,Per_RSI,0,1)<(RSI_L-Franja_no_ent) && iRSI(NULL,0,Per_RSI,0,1)>(RSI_C+Franja_no_ent) && OrdersTotal()==0){entrada=1;}
if( ((stop-Bid)!=0) && ((Ask-stop))!=0) {
double lote_c = MathRound((AccountBalance()*riesgo*Conv_Div/100)/(Ask-stop));
if(compra()==true && iRSI(NULL,0,Per_RSI,0,1)>RSI_L && iRSI(NULL,0,Per_RSI,0,1)<RSI_L_Ent_Max &&
(MathAbs((Ask-stop)*10000)>=pips_min) && ((stop-Bid)!=0) && (Ask-stop)!=0
&& ((l_sup-l_sup_ant)*10000>=Incre_Donchian) && entrada < 2){
ObjectDelete("lote_c");
double compra = OrderSend(NULL,OP_BUY,lote_c,Ask,3,stop,NULL,
"LARGOS",magic,0,clrGreen);
Mov_stop = 1;
tipo_orden = 1;
entrada++;
Alert (GetLastError());}
if(venta()==true && iRSI(NULL,0,Per_RSI,0,1)<RSI_C && iRSI(NULL,0,Per_RSI,0,1)>RSI_C_Ent_Min &&
(MathAbs((stop-Bid)*10000)>=pips_min) && ((stop-Bid)!=0) && ((Ask-stop)!=0)
&& ((l_inf_ant-l_inf)*10000>=Incre_Donchian) && entrada < 2){
ObjectDelete("lote_v");
double lote_v = MathRound((AccountBalance()*riesgo*Conv_Div/100)/(stop-Bid));
double venta = OrderSend(NULL,OP_SELL,lote_v,Bid,3,stop,NULL,
"CORTOS",magic,0,clrGreen);
Mov_stop = 1;
tipo_orden = 2;
entrada++;
Alert (GetLastError());
}
}
for (int i=0;i<OrdersTotal();i++){
int orden = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if( Beneficio >= Ini_Trailing && Mov_stop==1){
int modificar_orden = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),NULL,0,clrGreen);
Alert (GetLastError());
Mov_stop++;}
if(tipo_orden == 1 && OrderProfit() > (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100) && (OrderProfit() >= (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100))
&& (Mov_stop> 1) && stop > OrderOpenPrice()){
int modificar_orden = OrderModify(OrderTicket(),OrderOpenPrice(),stop,NULL,0,clrBlue);
Alert (GetLastError());}
if(tipo_orden == 2 && OrderProfit() > (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100) && (OrderProfit() >= (AccountBalance()*riesgo/100)*(Ini_Trailing_Stop/100))
&& (Mov_stop> 1) && stop < OrderOpenPrice() ){
int modificar_orden = OrderModify(OrderTicket(),OrderOpenPrice(),stop,NULL,0,clrBlue);
Alert (GetLastError());}
}
for(int i=0;i<OrdersTotal();i++){
int orden = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(Beneficio < 0 && iRSI(NULL,0,Per_RSI,0,1)<= RSI_L_Cie){
if(OrderType()==OP_BUY){
int cierre_largos_RSI = OrderClose(OrderTicket(),OrderLots(),Bid,10,clrRed);
break;}
if(Beneficio < 0 && iRSI(NULL,0,Per_RSI,0,1)>= RSI_C_Cie){
if(OrderType()==OP_SELL){
int cierre_cortos_RSI = OrderClose(OrderTicket(),OrderLots(),Ask,10,clrRed);
break;}
}
}
}
}
//+------------------------------------------------------------------+
Hola Otorrente,
Normalmente trabajo solo con MT5 y no con MT4 pero igual te sugiero dos cosas:
- Prueba filtrar según el Magic, utilizando la función OrderMagicNumber(). Ejemplo: if(OrderMagicNumber()==Magic)... De esa manera podrás saber que la orden que vas a cerrar sea realmente de ese EA.
- Prueba recorrer el bucle for de manera inversa. En vez de for(int i = 0; i < OrdersTotal(); i++), hazlo así: for(int = OrdersTotal() - 1; i > 0; i--). La razón es porque al cerrar órdenes de 0 a n, el índice cambia de posición. Me explico, si tengo 5 órdenes (la orden 0, la orden 1, la orden 2,..., la orden 4) y cierro la orden 1, por ejemplo, la orden 2 ya no va a ser la orden 2 sino que pasaría a ocupar la posición 1. Ese cambio de índice de las ordenes puede que te esté generando error. Recorriendo el bucle de forma decreciente evitas ese error.
Espero te pueda servir de algo las ideas que te comento.
Saludos...
- Aplicaciones de trading gratuitas
- 8 000+ señales para copiar
- Noticias económicas para analizar los mercados financieros
Usted acepta la política del sitio web y las condiciones de uso
Hola,
Tengo crea un EA y cuando lo pongo en varios activos me cierra posiciones que no debería. Cuando trabaja solo en 1 me funciona bien.
He probado crear el mismo EA pero con distintos nombres pero también me hace lo mismo. ¿Qué puede pasar?
Necesito ayuda, ya no sé como continuar.
Gracias,