//+------------------------------------------------------------------+ //| Triple Pantalla(Sergi Intradia).mq4 | //| Sergi Suades | //| | //+------------------------------------------------------------------+ #property copyright "Sergi Suades" #property link "" #property version "1.00" #property strict extern int _MagicNumber = 1123; extern double Lot = 1; //--- Otros parámetros int stochK; // identificador para nuestro indicador Estocastico int stochD; // identificador para nuestro indicador Estocastico int histo; // identificador para nuestro indicador MACD histo double stochDVal[], stochKVal[]; // Arrays dinámicos para guardar los valores de Estocastico para cada barra double histoVal[]; // Array dinámico para guardar los valores de MACD para cada barra double p_close[]; // Variable para almacenar el valor de la barra de cierre double p_max[]; // Variable para almacenar el valor de la barra de maximo double StopLossLevel; int ticket = 0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { for(int i = 0;i < 3;i++) { stochKVal[i] = iStochastic(NULL, PERIOD_M5, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, i); stochDVal[i] = iStochastic(NULL, PERIOD_M5, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, i); histoVal[i] = iOsMA(NULL,PERIOD_M30,12,26,9,PRICE_CLOSE,0); p_max[i] = iHigh(_Symbol,PERIOD_M5,0); p_close[i] = iClose(_Symbol,PERIOD_M5,0); } if(stochKVal[0]<0 || stochDVal[0]<0) { Alert("Error Creating Handles for Stochastic - error: ",GetLastError(),"!!"); return(-1); } return(INIT_SUCCEEDED); } void OnTick() { if(Bars(_Symbol,_Period)<60) // si el número de barras es inferior a 60 barras { Alert("Tenemos menos de 60 barras, EA va a salir ahora!!"); return; } // Usaremos la variable estática Old_Time para el tiempo de barra. // A cada ejecución de OnTick compararemos el tiempo de barra actual con el que está guardado. // Si el tiempo de barra no es igual al tiempo guardado, significa que tenemos un nuevo tick. static datetime Old_Time; datetime New_Time[1]; bool IsNewBar=false; // copiando el último tiempo de barra al elemento New_Time[0] int copied=CopyTime(_Symbol,_Period,0,1,New_Time); if(copied>0) // de acuerdo, se han copiado los datos con éxito { if(Old_Time!=New_Time[0]) // si el tiempo anterior no es igual al nuevo tiempo de barra { IsNewBar=true; // si no es la primera llamada, la nueva barra ha aparecido Old_Time=New_Time[0]; // guardando el tiempo de barra } } else { Alert("Ha ocurrido un error al copiar los datos del historial de los tiempos, error =",GetLastError()); ResetLastError(); return; } if(IsNewBar==false) { return; } // Variables donde se memorizan los tickets de las órdenes // de cada tipo int BuyStopOrder=0,SellStopOrder=0,BuyOrder=0,SellOrder=0; int _GetLastError=0,_OrdersTotal=OrdersTotal(); // buscamos en todas las posiciones abiertas y memorizamos, las posiciones // de qué tipo ya se han abierto: for(int z=_OrdersTotal-1; z>=0; z --) { // si ocurre algún error durante la búsqueda de la posición, vamos // a la siguiente if(!OrderSelect(z,SELECT_BY_POS)) { _GetLastError=GetLastError(); Print("OrderSelect(",z,", SELECT_BY_POS) - Error #",_GetLastError); continue; } // si la posición no se abrió para el símbolo actual, la saltamos if(OrderSymbol()!=Symbol()) continue; // si el MagicNumber no es igual a _MagicNumber, saltamos esta // posición if(OrderMagicNumber()!=_MagicNumber) continue; if(histoVal[0]>histoVal[1] && stochKVal[0]<30) { StopLossLevel = p_close[0] - (p_close[0]*0.01); // Stop loss ticket = OrderSend(Symbol(),OP_BUYSTOP,Lot,p_max[0],3,StopLossLevel,0,"Buy",_MagicNumber,0,clrGreen); if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return; } else Print("OrderSend placed successfully"); } if(histoVal[1]>histoVal[0] && stochKVal[0]>70) { OrderClose(Symbol(),Lot,Ask,0,Red); { Alert("Error al enviar la orden #",GetLastError()); return; } } } }The missing semicolon ;
Thank you Marco vd Heijden, now I don't have any error on the code.
Apart of this, is there any way to improve the code?
Yes put in more time, try everything, see what works and what does not, small modifications at a time, then discard the waste and combine the goodies.
Eventually you will end up with the masterplan.
//+------------------------------------------------------------------+ //| Triple Pantalla(Sergi Intradia).mq4 | //| Sergi Suades | //| | //+------------------------------------------------------------------+ #property copyright "Sergi Suades" #property link "" #property version "1.00" #property strict extern int _MagicNumber = 1123; extern double Lot = 1; //--- Otros parámetros double stochDVal[3], stochKVal[3]; // Arrays dinámicos para guardar los valores de Estocastico para cada barra double histoVal[3]; // Array dinámico para guardar los valores de MACD para cada barra double p_close[3]; // Variable para almacenar el valor de la barra de cierre double p_max[3]; // Variable para almacenar el valor de la barra de maximo double StopLossLevel; int ticket = 0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { for(int i = 0;i < 3;i++) { stochKVal[i] = iStochastic(NULL, PERIOD_M5, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, i); stochDVal[i] = iStochastic(NULL, PERIOD_M5, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, i); histoVal[i] = iOsMA(NULL,PERIOD_M30,12,26,9,PRICE_CLOSE,0); p_max[i] = iHigh(_Symbol,PERIOD_M5,0); p_close[i] = iClose(_Symbol,PERIOD_M5,0); } if(stochKVal[0]<0 || stochDVal[0]<0) { Alert("Error Creating Handles for Stochastic - error: ",GetLastError(),"!!"); return(-1); } return(INIT_SUCCEEDED); } void OnTick() { if(Bars(_Symbol,_Period)<60) // si el número de barras es inferior a 60 barras { Alert("Tenemos menos de 60 barras, EA va a salir ahora!!"); return; } // Usaremos la variable estática Old_Time para el tiempo de barra. // A cada ejecución de OnTick compararemos el tiempo de barra actual con el que está guardado. // Si el tiempo de barra no es igual al tiempo guardado, significa que tenemos un nuevo tick. static datetime Old_Time; datetime New_Time[1]; bool IsNewBar=false; // copiando el último tiempo de barra al elemento New_Time[0] int copied=CopyTime(_Symbol,_Period,0,1,New_Time); if(copied>0) // de acuerdo, se han copiado los datos con éxito { if(Old_Time!=New_Time[0]) // si el tiempo anterior no es igual al nuevo tiempo de barra { IsNewBar=true; // si no es la primera llamada, la nueva barra ha aparecido Old_Time=New_Time[0]; // guardando el tiempo de barra } } else { Alert("Ha ocurrido un error al copiar los datos del historial de los tiempos, error =",GetLastError()); ResetLastError(); return; } if(IsNewBar==false) { return; } // Variables donde se memorizan los tickets de las órdenes // de cada tipo int _GetLastError=0,_OrdersTotal=OrdersTotal(); // buscamos en todas las posiciones abiertas y memorizamos, las posiciones // de qué tipo ya se han abierto: for(int z=_OrdersTotal-1; z>=0; z --) { // si ocurre algún error durante la búsqueda de la posición, vamos // a la siguiente if(!OrderSelect(z,SELECT_BY_POS)) { _GetLastError=GetLastError(); Print("OrderSelect(",z,", SELECT_BY_POS) - Error #",_GetLastError); continue; } if(histoVal[0]>histoVal[1] && stochKVal[0]<30) { StopLossLevel = p_close[0] - (p_close[0]*0.01); // Stop loss ticket = OrderSend(Symbol(),OP_BUYSTOP,Lot,p_max[0],3,StopLossLevel,0,"Buy",_MagicNumber,0,clrGreen); if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return; } else Print("OrderSend placed successfully"); } if(histoVal[1]>histoVal[0] && stochKVal[0]>70) { OrderClose(Symbol(),Lot,Ask,0,Red); { Alert("Error al enviar la orden #",GetLastError()); return; } } }I have changed the code a little bit
In such a case place either Comment(__LINE__," ",...); or Print() everywhere and start a backtest in visual mode and find out, why the problem occurs.
- This way you don't have to wait hours for an answer of others
- You'll find that your indicator arrays will have strange values - as they have to be placed in OnTick() as they are not actualized in OnInit().
int OnInit(){ for(int i = 0;i < 3;i++){ stochKVal[i] = iStochastic(NULL, PERIOD_M5, 8, 3, 3, MODE_SMA, 0, MODE_MAIN, i); stochDVal[i] = iStochastic(NULL, PERIOD_M5, 8, 3, 3, MODE_SMA, 0, MODE_SIGNAL, i); histoVal[i] = iOsMA(NULL,PERIOD_M30,12,26,9,PRICE_CLOSE,0); p_max[i] = iHigh(_Symbol,PERIOD_M5,0); p_close[i] = iClose(_Symbol,PERIOD_M5,0);
These never change. Assign them in OnTick.- Why are the last three arrays filled with the same value?
- Why are you using fixed timeframes instead of the chart's?
for(int z=_OrdersTotal-1; z>=0; z --){ if(!OrderSelect(z,SELECT_BY_POS))
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum- Don't double post. (Moderator: Other topic has been removed)

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
How can I improve my EA code?
Now I have an error: 'ticket' - some operator expected 115 10