//+------------------------------------------------------------------+ //| PromedioDia.mq4 | //| Herber Carrero | //| https://www.mql5.com | //+------------------------------------------------------------------+ /* Value ID Description 0 OP_BUY Buy operation 1 OP_SELL Sell operation 2 OP_BUYLIMIT Buy limit pending order 3 OP_SELLLIMIT Sell limit pending order 4 OP_BUYSTOP Buy stop pending order 5 OP_SELLSTOP Sell stop pending order int ticket=OrderSend(Symbol(),OP_BUYLIMIT,dblLotaje,dblPrecioMin,3,dblStopLoss_BuyLimit,dblPrecioMedio,"Orden BuyLimit",123,0,clrGreen); int OrderSend( string symbol, // symbol int cmd, // operation double volume, // volume double price, // price int slippage, // slippage double stoploss, // stop loss double takeprofit, // take profit string comment=NULL, // comment int magic=0, // magic number datetime expiration=0, // pending order expiration color arrow_color=clrNONE // color ); */ //https://www.mql5.com/es/articles/1404 #property strict extern int intMagicNumber = 1123; extern int intCantidadSL = 1; extern int intCantidadTP = 1; extern double dblLotaje = 0.1; int BuyOrder=0; int SellOrder=0; int BuyLimitOrder=0; int SellLimitOrder=0; int ticketBuyLimit=0; int ticketSellLimit=0; string strOrder=""; int borrado=0; /* Versión Relación 1:1 (SL y TP a 0.5 del Max y Min) Lotaje: 0.01 Solo 1 orden a la vez (hasta que se cierre) */ //void OnTick() { int start() { //Solo 1 orden ejecutada a la vez //if( BuyOrder > 0 || SellOrder > 0 ) return(0); //if (OrdersTotal()>0) return(0); if(TimeDayOfWeek(Time[1]) != TimeDayOfWeek(Time[0]) ) { //Vela Actual es diferente en DÍA a la Vela Anterior //Las dos ordenes están pendiente por ejecutarse?, //Si: Borrarlas para crear las nuevas. //NO: No hacer nada, ya se ejecutaron. double dblPrecioMax=iHigh(Symbol(), PERIOD_D1,1); //Calcular el máximo del día anterior double dblPrecioMin=iLow(Symbol(),PERIOD_D1,1); //Calcular el mínimo del día anterior double dblCantPips = (dblPrecioMax - dblPrecioMin) / 2 ; //Obtener cantidad de PIPS double dblPrecioMedio = (dblPrecioMin + dblCantPips) ; //Calcular Precio Mitad double dblStopLoss_BuyLimit = dblPrecioMin - (dblCantPips * intCantidadSL); double dblStopLoss_SellLimit = dblPrecioMax + (dblCantPips * intCantidadSL); //Generar Orden de Buy Limit ticketBuyLimit=OrderSend(Symbol(),OP_BUYLIMIT,dblLotaje,dblPrecioMin,3,dblStopLoss_BuyLimit,dblPrecioMedio,"Orden BuyLimit",920190101,0,clrPurple); //Generar Orden de Sell Limit ticketSellLimit=OrderSend(Symbol(),OP_SELLLIMIT,dblLotaje,dblPrecioMax,3,dblStopLoss_SellLimit,dblPrecioMedio,"Orden SellLimit",820190101,0,clrPurple); borrado = 1; } else { //Vela Actual y Vela Anterior son el mismo DÍA //Se ejecutó alguna de las dos ordenes? //SI: buscar cual se ejecutó para borrar el otro. //NO: No hacer nada, esperar que se ejecute. // 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 el MagicNumber no es igual a _MagicNumber, saltamos esta posición // if(OrderMagicNumber()!=_MagicNumber) continue; // dependiendo del tipo de la posición, cambiamos el valor de la variable: if ( OrderSelect( z, SELECT_BY_POS, MODE_TRADES )) { switch(OrderType()) { case OP_BUY: BuyOrder = OrderTicket(); break; case OP_SELL: SellOrder = OrderTicket(); break; case OP_BUYLIMIT: BuyLimitOrder = OrderTicket(); break; case OP_SELLLIMIT: SellLimitOrder = OrderTicket(); break; } strOrder= StringConcatenate(strOrder, " - Z: ",z," Type: ", OrderType()); } } //for(int z=_OrdersTotal-1; z>=0; z --) Comment(strOrder); strOrder=""; if (BuyOrder > 0) { if(borrado==1){ borrado=2; if(!OrderDelete(SellLimitOrder)) { Alert("Error al borrar la posición #:",SellLimitOrder, " error:",GetLastError()); return(-1); } //if(!OrderDelete(SellLimitOrder) } //if(borrado=1) } // } //if(TimeDayOfWeek(Time[1]) != TimeDayOfWeek(Time[0]) ) return(0); }