#property copyright "Copyright © 2009, Juan Cruz Baudino" #property link "http://www.cacus.com.ar" // Este expert advisor con settings default está optimizado para EUR/USD M15 // para un depósito de 500 USD los lotes recomendados son 0.3 // modifique los lotes si quiere ajustar mejor la posible ganancia. extern string _1 = "Parámetros de las EMAs"; extern int LargaEMA = 10; extern int CortaEMA = 2; extern string _2 = "Parámetros del Estocástico"; extern int periodos = 2; extern int nivelsup = 78; extern int nivelinf = 25; //extern string _3 = "Lotes"; //extern double Size = 0.3; extern string _4 = "Take Profit"; extern double TP = 80; extern string _5 = "Stop Loss"; extern double SL = 100; extern string _6 = "Trailing stop"; extern double Trailingstop = 55; extern int BalanceInicial = 500; extern double Lots = 0.01; extern double loteminimo = 0.01; extern double Exponente = 1; extern int magicEA = 14523; extern int HoraIngreso = 1; extern int HoraSalida = 11; extern int PerADX = 14; //-------- no sirve --------- int TrendEMA = 100; int back = 10; double UpTrend = 0; double DownTrend = 0; //-------- no sirve --------- //veo el depósito inicial int start() { double realTP, realSL; //double deposito = AccountBalance(); static datetime MarcaTiempo = 0; static int ticket; int señal=-1; datetime NuevaTiempo = Time[0]; double Size; //&&&&& TRAILING &&&&&&&&&&&&&&&&&&&&&&&&& if(Trailingstop>0){Trailing();} //&&&&& TRAILING &&&&&&&&&&&&&&&&&&&&&&&&& // A cada nueva barra if((TimeHour(TimeCurrent())<=HoraSalida && TimeHour(TimeCurrent())>=HoraIngreso )&& (HoraIngreso25) && (AccountBalance()<=35)) {Size = 0.02;} if ((AccountBalance()>35) && (AccountBalance()<=45)) {Size = 0.03;} if ((AccountBalance()>45) && (AccountBalance()<=55)) {Size = 0.04;} if ((AccountBalance()>55) && (AccountBalance()<=65)) {Size = 0.05;} if ((AccountBalance()>65) && (AccountBalance()<=80)) {Size = 0.06;} if ((AccountBalance()>75) && (AccountBalance()<=100)) {Size = 0.07;} if ((AccountBalance()>100) && (AccountBalance()<=150)) {Size = 0.08;} if ((AccountBalance()>150) && (AccountBalance()<=250)) {Size = 0.1;} if ((AccountBalance()>250) && (AccountBalance()<=500)) {Size = 0.2;} if ((AccountBalance()>500) && (AccountBalance()<=1000)) {Size = 0.5;} if ((AccountBalance()>1000) && (AccountBalance()<=1500)) {Size = 0.7;} if ((AccountBalance()>1500) && (AccountBalance()<=2000)) {Size = 1;} if ((AccountBalance()>2000) && (AccountBalance()<=3000)) {Size = 1.5;} if ((AccountBalance()>3000) && (AccountBalance()<=5000)) {Size = 2;} if ((AccountBalance()>5000) && (AccountBalance()<=7000)) {Size = 3;} if ((AccountBalance()>7000) && (AccountBalance()<=10000)) {Size = 4;} if ((AccountBalance()>10000) && (AccountBalance()<=15000)) {Size = 5;} if ((AccountBalance()>15000)) {Size = 10;}*/ // Tomamos datos de las dos EMAs (Corta y Larga). double emaLargaAntes = iMA(NULL,0,LargaEMA,0, MODE_EMA, PRICE_CLOSE,2); double emaLargaAhora = iMA(NULL,0,LargaEMA,0, MODE_EMA, PRICE_CLOSE,1); double emaLargaDespues = iMA(NULL,0,LargaEMA,0, MODE_EMA, PRICE_CLOSE,0); double emaCortaAntes = iMA(NULL,0,CortaEMA,0, MODE_EMA, PRICE_CLOSE,2); double emaCortaAhora = iMA(NULL,0,CortaEMA,0, MODE_EMA, PRICE_CLOSE,1); double emaCortaDespues = iMA(NULL,0,CortaEMA,0, MODE_EMA, PRICE_CLOSE,0); double ADXPlus = iADX(NULL,0,PerADX,PRICE_CLOSE,MODE_PLUSDI,0); double ADXMinus = iADX(NULL,0,PerADX,PRICE_CLOSE,MODE_MINUSDI,0); double ADXtend1 = iADX(NULL,0,PerADX,PRICE_CLOSE,MODE_MAIN,1); double ADXtend2 = iADX(NULL,0,PerADX,PRICE_CLOSE,MODE_MAIN,0); double stoch=iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,periodos); //calculo la tendencia general // double tendencia=emaTrendAhora/emaTrendAntes; // Calculamos si se han cruzado if((emaCortaAhora < emaLargaAhora) && (emaCortaAntes > emaLargaAntes) && (emaCortaDespues < emaLargaDespues) && (stoch>=nivelsup) /*&& (ADXMinus>ADXPlus) && (ADXtend1>ADXtend2)*/) { señal = 1;//VENTA } else if((emaCortaAhora > emaLargaAhora) && (emaCortaAntes < emaLargaAntes) && (emaCortaDespues > emaLargaDespues) && (stoch<=nivelinf) /*&& (ADXMinusADXtend2)*/) { señal = 0;//COMPRA } else { señal = -1; } // Si no hay órden abierta y hay señal podemos operar. if(OpenOrders() == 0 && señal != -1) { if(señal == 0) { if(SL > 0){ realSL = Ask - SL * Point;} else { realSL = 0;} //--------------------------- if(TP > 0){ realTP = Ask + TP * Point;} else{ realTP = 0;} ticket = OrderSend(Symbol(),OP_BUY,CalculoLote(),Ask,3,realSL,realTP,NULL,magicEA,0,Green); } if(señal == 1) { if(SL > 0){ realSL = Bid + SL * Point;} else { realSL = 0;} //--------------------------- if(TP > 0){ realTP = Bid - TP * Point;} else{ realTP = 0;} ticket = OrderSend(Symbol(),OP_SELL,CalculoLote(),Bid,3,realSL,realTP,NULL,magicEA,0,Red); } } //------------------- //ESCRIBO EN PANTALLA Comment("\nSeñal:",señal, "\nStoch:",stoch, "\nBalance:",AccountBalance(), "\nLot:",Size, "\n---------\n"); //ESCRIBO EN PANTALLA //------------------- // Si hay una orden abierta y hay señal en contra cerramos y abrimos la operación contraria if(OrdersTotal() != 0 && señal != -1) { // Seleccionamos la orden. OrderSelect(ticket,SELECT_BY_TICKET); //Comprobamos si la señal es contraria if(OrderType() == 0 && señal == 1){ OrderClose(ticket,OrderLots(),Bid,3,Yellow);//cierro //abro la contraria if(SL > 0){ realSL = Bid + SL * Point;} else { realSL = 0;} //--------------------------- if(TP > 0){ realTP = Bid - TP * Point;} else{ realTP = 0;} ticket = OrderSend(Symbol(),OP_SELL,Size,Bid,3,realSL,realTP,"cacus vende",magicEA,0,Red); } if(OrderType() == 1 && señal == 0){ OrderClose(ticket,OrderLots(),Ask,3,Yellow); //cierro //abro la contraria if(SL > 0){ realSL = Ask - SL * Point;} else { realSL = 0;} //--------------------------- if(TP > 0){ realTP = Ask + TP * Point;} else{ realTP = 0;} ticket = OrderSend(Symbol(),OP_BUY,CalculoLote(),Bid,3,realSL,realTP,"cacus compra",magicEA,0,Green); } } MarcaTiempo = NuevaTiempo; } } return(0); } //+------------------------------------------------------------------+ double CalculoLote() { double cal=(AccountBalance()/BalanceInicial); double cal1=MathRound(cal*10); double Multiplicador=cal1/10; double cal3=Lots*Multiplicador*Exponente; double cal4=MathRound(cal3*100); double Lote=cal4/100; return(Lote); } void Trailing() { double Ask1 = GetAsk() + Trailingstop * Point; double Bid1 = GetBid() - Trailingstop * Point; if (OrdersTotal() > 0) { for (int i = 0; i < OrdersTotal(); i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == magicEA) { if (GetAsk() < OrderOpenPrice() - Trailingstop * Point && Ask1 < OrderStopLoss()) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask1, OrderTakeProfit(), 0, Green); return (0); } } if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == magicEA) { if (GetBid() > OrderOpenPrice() + Trailingstop * Point && Bid1 > OrderStopLoss()) { OrderModify(OrderTicket(), OrderOpenPrice(), Bid1, OrderTakeProfit(), 0, Green); return (0); } } } }} double GetBid() { return (NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits)); } double GetAsk() { return (NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits)); } int OpenOrders() { int num; for (int i=0;i