#property copyright "Cacus MACCImo" #property link "Cacus MACCImo" #include #include extern int Trailingstop = 5; extern double StopLoss = 100.0; extern double TakeProfit = 45.0; extern bool RecoveryMode = false; extern int RecoveryModeMulti = 2; double Lots = 0.01; extern double Risk = 0.5; extern bool DayFilter = False; extern int Day1 = 5; extern int Day2 = 0; extern int StopDayMonth = 31; extern bool HourFilter = False; extern int GMTOffset = 3; extern int ExceptHs1 = 0; extern int SlipPage = 3; extern int Magic = 4985; extern string cualquier = "AAAAAAAAAAAAAAAAAAA"; extern int MaxOrders = 5; extern int PipDifMin = 10; extern double PerCCI1 = 14.0; extern double PerIMA1 = 80.0; extern double PerIMA2 = 295.0; extern double PerIMA3 = 588.0; extern double IMATrendRate1 = 0.57; extern double IMATrendRate2 = 0.13; extern double CCIRange = 136.0; extern int PipsFromIMA2 = 1; extern int PipsFromIMA = 12; int bandTiempo = 0; double maxdd,maxpercentdd; double banderaBalance=0; double myPoint; int deinit() { Print("MaxDD: ",maxdd); Print("MaxPercDD: ",maxpercentdd); return(0); } int init() { myPoint = SetPoint(Symbol()); if (Digits == 5) { StopLoss=StopLoss*10; TakeProfit=TakeProfit*10; SlipPage=SlipPage*10; PipDifMin=PipDifMin*10; IMATrendRate1=IMATrendRate1*10; IMATrendRate2=IMATrendRate2*10; PipsFromIMA=PipsFromIMA*10; PipsFromIMA2=PipsFromIMA2*10;} return (0); } int start() { //&&&&& TRAILING &&&&&&&&&&&&&&&&&&&&&&&&& if(Trailingstop>0){Trailing();} //&&&&& TRAILING &&&&&&&&&&&&&&&&&&&&&&&&& int ticket1; if (Time[0] == bandTiempo) return (0); bandTiempo = Time[0]; double puntos = Point; bool Trading=true; if (TimeDay(TimeCurrent())==Day1 && DayFilter) Trading=false; if (TimeDay(TimeCurrent())==Day2 && DayFilter) Trading=false; //############################################################################################## //##########CONTROLO LA PERDIDA MAXIMA Y EL PORCENTAJE DE PERDIDA############################### double TickValue = MarketInfo(Symbol(), MODE_TICKVALUE); double OpenProfit; int OrderTicket1; bool tipo1; double OrderClosePrice1; double OrderLots1; OpenProfit = 0; OrderTicket1 = 0; tipo1 = FALSE; OrderClosePrice1 = 0; OrderLots1 = 0; for (int i = 0; i < OrdersTotal(); i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { OrderTicket1 = OrderTicket(); if (OrderType() == OP_BUY) tipo1 = FALSE; if (OrderType() == OP_SELL) tipo1 = TRUE; OrderClosePrice1 = OrderClosePrice(); OrderLots1 = OrderLots(); if (tipo1 == FALSE) { if (OrderClosePrice() < OrderOpenPrice()) {OpenProfit -= (OrderOpenPrice() - OrderClosePrice()) * OrderLots() / Point;} if (OrderClosePrice() > OrderOpenPrice()) {OpenProfit += (OrderClosePrice() - OrderOpenPrice()) * OrderLots() / Point;} } if (tipo1 == TRUE) { if (OrderClosePrice() > OrderOpenPrice()) {OpenProfit -= (OrderClosePrice() - OrderOpenPrice()) * OrderLots() / Point;} if (OrderClosePrice() < OrderOpenPrice()) {OpenProfit += (OrderOpenPrice() - OrderClosePrice()) * OrderLots() / Point;} } } } OpenProfit *= TickValue; if (OpenProfit CCIRange && CCI1 < CCI2 && Bid - IMA11 > PipsFromIMA * myPoint) { OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid,Digits), SlipPage, Violet); return (0); } if (OrderType() == OP_SELL && CCI2 < (-CCIRange) && CCI1 > CCI2 && Ask - IMA11 < (-PipsFromIMA) * myPoint) { OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask,Digits), SlipPage, Violet); return (0); } } } } } if (OpenOrders < MaxOrders && TimeDay(TimeCurrent()) IMATrendRate1 * myPoint && IMA31 - IMA32 > IMATrendRate2 * myPoint && IMA11 > IMA21 && CCI2 < (-CCIRange) && CCI1 > CCI2 && Ask - IMA11 < (-PipsFromIMA2) * myPoint && OrderOpenPrice1 == 0.0 || Ask - OrderOpenPrice1 < (-PipDifMin) * myPoint && Trading) { ticket1 = OpenTrade(OP_BUY, Lots, StopLoss, TakeProfit, Symbol() + "-" + "cacus: comprando", Magic); if (ticket1 > 0) Alert(Symbol() + "-" + "cacus: comprando"); } if (IMA21 - IMA22 < (-IMATrendRate1) * myPoint && IMA31 - IMA32 < (-IMATrendRate2) * myPoint && IMA11 < IMA21 && CCI2 > CCIRange && CCI1 < CCI2 && Bid - IMA11 > PipsFromIMA2 * myPoint && OrderOpenPrice1 == 0.0 || Bid - OrderOpenPrice1 > PipDifMin * myPoint && Trading) { ticket1 = OpenTrade(OP_SELL, Lots, StopLoss , TakeProfit , Symbol() + "-" + "cacus: vendiendo", Magic); if (ticket1 > 0) Alert(Symbol() + "-" + "cacus: vendiendo"); } } return (0); } 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() == Magic) { if (GetAsk() < OrderOpenPrice() - Trailingstop * Point && Ask1 < OrderStopLoss()) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask1, OrderTakeProfit(), 0, Green); return (0); } } if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { 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)); } double NormalizeLots(double PreLot) { double LotStep = MarketInfo(Symbol(), MODE_LOTSTEP); double MinLot = MarketInfo(Symbol(), MODE_MINLOT); double MaxLot = MarketInfo(Symbol(), MODE_MAXLOT); double valor = MathCeil(PreLot / LotStep) * LotStep; if (valor < MinLot) { valor = MinLot; } if (valor > MaxLot) { valor = MaxLot; } return (valor); } double MMLots(double risk, double margin) { return(NormalizeLots(risk * margin / MarketInfo(Symbol(), MODE_MARGINREQUIRED) / (AccountLeverage() / 20.0))); //The Original function didnt return any value } double RecoveryLot(double PreLot) { double bal = AccountBalance(); if (banderaBalance < bal) banderaBalance = bal; if (banderaBalance > bal) return (NormalizeLots(RecoveryModeMulti * PreLot)); return (PreLot); } double SetPoint(string mySymbol) { double mPoint, myDigits; myDigits = MarketInfo (mySymbol, MODE_DIGITS); if (myDigits < 4) mPoint = 0.01; if (myDigits == 4) mPoint = 0.0001; if (myDigits == 5) mPoint = 0.00001; return(mPoint); } int OpenTrade(int signal, double mLots, double StopLoss, double TakeProfit, string msg, int MagicNumber) { int ticket, err; double TPprice,STprice; RefreshRates(); if (signal==OP_BUY) { double Ask1=NormalizeDouble(Ask,Digits); ticket=OrderSend(Symbol(),OP_BUY,mLots,Ask1,SlipPage,0,0,msg,MagicNumber,0,Green); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) { TPprice = Ask + TakeProfit * myPoint; TPprice = ValidTakeProfit(OP_BUY,Ask, TPprice); } STprice = 0; if (StopLoss > 0) { STprice = Ask - StopLoss * myPoint; STprice = ValidStopLoss(OP_BUY,Bid, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen); } } } } else if (signal==OP_SELL) { double Bid1=NormalizeDouble(Bid,Digits); ticket=OrderSend(Symbol(),OP_SELL,mLots,Bid1,SlipPage,0,0,msg,MagicNumber,0,Red); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) { TPprice=Bid - TakeProfit * myPoint; TPprice = ValidTakeProfit(OP_SELL,Bid, TPprice); } STprice = 0; if (StopLoss > 0) { STprice = Bid + StopLoss * myPoint; STprice = ValidStopLoss(OP_SELL,Ask, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen); } } } } if(ticket<0) { err = GetLastError(); Print("Error opening SELL order : ",err, " ", ErrorDescription(err)); } return(ticket); } double ValidStopLoss(int type, double price, double SL) { double newSL, temp; if (SL < 0.1) return(SL); temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; newSL = SL; if (type == OP_BUY) { if((price - SL) < temp) newSL = price - temp; } if (type == OP_SELL) { if((SL-price) < temp) newSL = price + temp; } newSL = NormalizeDouble(newSL,Digits); return(newSL); } double ValidTakeProfit(int type, double price, double TP) { double newTP, temp; temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; newTP = TP; if (type == OP_BUY) { if((TP - price) < temp) newTP = price + temp; } if (type == OP_SELL) { if((price - TP) < temp) newTP = price - temp; } newTP = NormalizeDouble(newTP,Digits); return(newTP); } int ConvertHS(int Hs,int GMTOff) { if (Hs>=0){ Hs = Hs+GMTOff; if (Hs<0) Hs=Hs+24; if (Hs>24) Hs=Hs-24; } return(Hs); }