#property version "1.00" #property strict extern double StartLots=0.1; extern double MaxLot=10; input int Depo=0; input int RsiPeriod=14; input ENUM_APPLIED_PRICE RsiPrice=6; input int RsiChannel=10; input int CciPeriod=14; input ENUM_APPLIED_PRICE CciPrice=6; input int CciChannel=40; input double ScalingCoef=4; extern double Martin=1.5; extern int StopLoss=400; extern int TakeProfit=1500; extern int TrailStart=700; extern int TrailDistance=300; extern int TrailStep=10; input bool NewCycle=false; // input double LossPercent=0; input bool CloseFromInverseSignal=false; input int Magic=9; extern int slippage=50; input int TimeOut=120; string ExpertName; int DigitsMinLot; int DigitsStepLot; int Totals[2]; double Volumes[2]; double LastPrice[2]; double Profit[2]; double VolumesAll[2]; int CountAll; double NoLossBid; double VolumeResult; bool Start=true; double Lots; double StartBalance=0; double ProfitAll; int CountThisMagic; int OnInit() { ExpertName=WindowExpertName(); DigitsMinLot=(int)MathRound(MathAbs(MathLog(MarketInfo(Symbol(),MODE_MINLOT))/MathLog(10))); DigitsStepLot=(int)MathRound(MathAbs(MathLog(MarketInfo(Symbol(),MODE_LOTSTEP))/MathLog(10))); if(MaxLot=0 ? Green : Red); if(ObjectFind("ProfitSell")==-1) { ObjectCreate("ProfitSell",OBJ_LABEL,0,0,0); ObjectSet("ProfitSell",OBJPROP_CORNER,CORNER_RIGHT_UPPER); ObjectSet("ProfitSell",OBJPROP_XDISTANCE,100); ObjectSet("ProfitSell",OBJPROP_YDISTANCE,80); ObjectSetInteger(0,"ProfitSell",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER); ObjectSetText("ProfitSell","Profit sell:",10,"",Red); ObjectSetInteger(0,"ProfitSell",OBJPROP_SELECTABLE,false); } if(ObjectFind("ProfitSellValue")==-1) { ObjectCreate("ProfitSellValue",OBJ_LABEL,0,0,0); ObjectSet("ProfitSellValue",OBJPROP_CORNER,CORNER_RIGHT_UPPER); ObjectSet("ProfitSellValue",OBJPROP_XDISTANCE,40); ObjectSet("ProfitSellValue",OBJPROP_YDISTANCE,80); ObjectSetInteger(0,"ProfitSellValue",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER); ObjectSetInteger(0,"ProfitSellValue",OBJPROP_SELECTABLE,false); } ObjectSetText("ProfitSellValue",DoubleToStr(Profit[OP_SELL],2),10); ObjectSet("ProfitSellValue",OBJPROP_COLOR,Profit[OP_SELL]>=0 ? Green : Red); if(ObjectFind("Profit")==-1) { ObjectCreate("Profit",OBJ_LABEL,0,0,0); ObjectSet("Profit",OBJPROP_CORNER,CORNER_RIGHT_UPPER); ObjectSet("Profit",OBJPROP_XDISTANCE,100); ObjectSet("Profit",OBJPROP_YDISTANCE,100); ObjectSetInteger(0,"Profit",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER); ObjectSetText("Profit","Profit:",10); ObjectSetInteger(0,"Profit",OBJPROP_SELECTABLE,false); } ObjectSet("Profit",OBJPROP_COLOR,ProfitAll>=0 ? Green : Red); if(ObjectFind("ProfitValue")==-1) { ObjectCreate("ProfitValue",OBJ_LABEL,0,0,0); ObjectSet("ProfitValue",OBJPROP_CORNER,CORNER_RIGHT_UPPER); ObjectSet("ProfitValue",OBJPROP_XDISTANCE,40); ObjectSet("ProfitValue",OBJPROP_YDISTANCE,100); ObjectSetInteger(0,"ProfitValue",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER); ObjectSetInteger(0,"ProfitValue",OBJPROP_SELECTABLE,false); } ObjectSetText("ProfitValue",DoubleToStr(ProfitAll,2),10,"",Red); ObjectSet("ProfitValue",OBJPROP_COLOR,ProfitAll>=0 ? Green : Red); if(ObjectFind("Free")==-1) { ObjectCreate("Free",OBJ_LABEL,0,0,0); ObjectSet("Free",OBJPROP_CORNER,CORNER_RIGHT_UPPER); ObjectSet("Free",OBJPROP_XDISTANCE,100); ObjectSet("Free",OBJPROP_YDISTANCE,120); ObjectSetInteger(0,"Free",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER); ObjectSetText("Free","Free:",10); ObjectSetInteger(0,"Free",OBJPROP_SELECTABLE,false); } ObjectSet("Free",OBJPROP_COLOR,AccountFreeMargin()>=0 ? Green : Red); if(ObjectFind("FreeValue")==-1) { ObjectCreate("FreeValue",OBJ_LABEL,0,0,0); ObjectSet("FreeValue",OBJPROP_CORNER,CORNER_RIGHT_UPPER); ObjectSet("FreeValue",OBJPROP_XDISTANCE,40); ObjectSet("FreeValue",OBJPROP_YDISTANCE,120); ObjectSetInteger(0,"FreeValue",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER); ObjectSetInteger(0,"FreeValue",OBJPROP_SELECTABLE,false); } ObjectSetText("FreeValue",DoubleToStr(AccountFreeMargin(),2),10,"",Red); ObjectSet("FreeValue",OBJPROP_COLOR,AccountFreeMargin()>=0 ? Green : Red); TrailingAll(TrailStart,TrailDistance,TrailStep,Magic); if(StartBalance!=0 && LossPercent!=0 && -ProfitAll/StartBalance*100>LossPercent) CloseThisSymbolAll(-1,Magic); double Rsi1=iRSI(_Symbol,_Period,RsiPeriod,RsiPrice,1); double Rsi2=iRSI(_Symbol,_Period,RsiPeriod,RsiPrice,2); double Cci1=iCCI(_Symbol,_Period,CciPeriod,CciPrice,1); double Cci2=iCCI(_Symbol,_Period,CciPeriod,CciPrice,2); int Signal=-1; if((Rsi1+Rsi2)/2>50-RsiChannel && (Rsi1+Rsi2)/2<50+RsiChannel && (Cci1+Cci2)/2>-CciChannel && (Cci1+Cci2)/2ScalingCoef*(Rsi1-50)) Signal=OP_BUY; else if(Cci2>ScalingCoef*(Rsi2-50) && Cci10) CloseThisSymbolAll(OP_SELL,Magic); if(Signal==OP_SELL && Totals[OP_BUY]>0) CloseThisSymbolAll(OP_BUY,Magic); } GetInfo(); if(CountThisMagic==0 && Signal>=OP_BUY) { // --- Открытие ордера ------------------- Lots = Depo==0 ? StartLots : StartLots*AccountBalance()/Depo; if(!NewCycle) for(int i=OrdersHistoryTotal()-1; i>=0; i--) if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==_Symbol && OrderMagicNumber()==Magic && StringFind(OrderComment(),WindowExpertName())>=0) { if(OrderProfit()<0) Lots=NormalizeDouble(OrderLots()*Martin,DigitsStepLot); break; } Lots=NormalizeDouble(MathMin(Lots,MaxLot),DigitsStepLot); StartBalance=AccountBalance(); if(Signal==OP_BUY) { int Ticket=OpenOrder(OP_BUY,Lots,0,0,WindowExpertName(),Magic); if(Ticket>0 && OrderSelect(Ticket,SELECT_BY_TICKET) && (StopLoss!=0 || TakeProfit!=0)) { double StopLevel=MarketInfo(_Symbol,MODE_STOPLEVEL)*Point; double SL= StopLoss==0 ? 0 : NormalizeDouble(MathMin(OrderOpenPrice()-StopLoss*Point,Bid-StopLevel),Digits); double TP= TakeProfit==0 ? 0 : NormalizeDouble(MathMax(OrderOpenPrice()+TakeProfit*Point,Ask+StopLevel),Digits); OrderModify(Ticket,OrderOpenPrice(),SL,TP,OrderExpiration(),Blue); } } else { if(Signal==OP_SELL) { int Ticket=OpenOrder(OP_SELL,Lots,0,0,WindowExpertName(),Magic); if(Ticket>0 && OrderSelect(Ticket,SELECT_BY_TICKET) && (StopLoss!=0 || TakeProfit!=0)) { double StopLevel=MarketInfo(_Symbol,MODE_STOPLEVEL)*Point; double SL=StopLoss==0 ? 0 : NormalizeDouble(MathMax(OrderOpenPrice()+StopLoss*Point,Ask+StopLevel),Digits); double TP=TakeProfit==0 ? 0 : NormalizeDouble(MathMin(OrderOpenPrice()-TakeProfit*Point,Bid-StopLevel),Digits); OrderModify(Ticket,OrderOpenPrice(),SL,TP,OrderExpiration(),Red); } } } } // ------------------------------------------------------------------------------- } //+------------------------------------------------------------------+ void GetInfo() { CountAll=0; NoLossBid=0; ProfitAll=0; ArrayInitialize(Totals,0); ArrayInitialize(Volumes,0); ArrayInitialize(LastPrice,0); ArrayInitialize(VolumesAll,0); ArrayInitialize(Profit,0); CountThisMagic=0; for(int i=0; iOP_SELL) continue; CountAll++; ProfitAll+=OrderProfit(); Profit[OT]+=OrderProfit(); VolumesAll[OT]+=OrderLots(); NoLossBid += OT==OP_BUY ? OrderOpenPrice()*OrderLots() : -OrderOpenPrice()*OrderLots(); if(StringFind(OrderComment(),"Lock")<0) { Totals[OT]++; LastPrice[OT]=OrderOpenPrice(); Volumes[OT]+=OrderLots(); } } } VolumeResult=VolumesAll[OP_BUY]-VolumesAll[OP_SELL]; NoLossBid = VolumeResult==0 ? 0 : (NoLossBid+MarketInfo(Symbol(),MODE_SPREAD)*VolumesAll[OP_SELL]*Point)/VolumeResult; } // -------------------------- End GetInfo --------------------------------------------- void TralingLock(int TrailStart,int TrailDistance,int TrailStep) { bool Result; double SL; if(TrailDistance=0) { double OldSL = OrderStopLoss(); if (OrderType() == OP_BUY && Bid-OrderOpenPrice()>=TrailStart*Point) { SL=NormalizeDouble(Bid-TrailDistance*Point,Digits); if(SL>=OldSL+TrailStep*Point || OldSL == 0) Result=OrderModify(OrderTicket(), OrderOpenPrice(),SL,OrderTakeProfit(),0,Blue); } else { if (OrderType() == OP_SELL && OrderOpenPrice()-Ask>TrailStart*Point) { SL=NormalizeDouble(Ask+TrailDistance*Point,Digits); if(SL<=OldSL-TrailStep*Point || OldSL == 0) Result=OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Red); } } } } // ------------------------------ End TrailingLock ----------------------------------------------- int OpenOrder(int Cmd, double vol, double SL, double TP, string OComment, int Magic) { if(volMarketInfo(Symbol(),MODE_MAXLOT)) { vol=MarketInfo(Symbol(),MODE_MAXLOT); Print("Lot more than the maximum. Order opens maximum lot"); } vol=NormalizeDouble(vol,DigitsStepLot); if(AccountFreeMarginCheck(Symbol(),Cmd,vol)<0) { Print("Not Enoth money to open pozition"); return(-134); } int Ticket; int Error=0; datetime dt; dt=TimeLocal(); int CountTry=1; while (TimeLocal()-dt < TimeOut) { RefreshRates(); if (Cmd == OP_BUY) { Print("Opening orders Buy Try ", CountTry); Ticket=OrderSend(Symbol(), Cmd, vol, Ask, slippage, SL, TP, OComment, Magic, 0, Blue); } else { Print("Opening orders Sell Try ",CountTry); Ticket=OrderSend(Symbol(), Cmd, vol, Bid, slippage, SL, TP, OComment, Magic, 0, Red); } if (Ticket > 0) { Print("Order #", Ticket, "open "); return(Ticket); } Error=GetLastError(); Print("Opening orders Try ", CountTry, " Error ", Error); switch(Error) { case 2: Alert("Total mistake"); return(-Error); case 3: Alert("Wrong trading parameters"); return(-Error); case 4: Sleep(60000); break; /* Сервер занят */ case 5: Alert("The client terminal is out of date"); return(-Error); case 6: Sleep( 5000); break; /* Нет связи */ case 8: Alert("Too frequent queries"); return(-Error); case 64: Alert("Account is blocked"); return(-Error); case 65: Alert("Wrong account number"); return(-Error); case 128: Sleep(60000); break; /* Истек срок ожидания */ case 129: Sleep(5000); break; /* Неправильная цена */ case 130: Sleep(1000); break; /* Ошибка стопов */ case 131: Alert("Wrong Lots"); return(-Error); case 132: Alert("Market Close"); return(-Error); case 133: Alert("trade is disabled"); return(-Error); case 134: Alert("Not enough money"); return(-Error); case 135: break; /* Изменилась цена */ case 136: Sleep(1000); break; /* Нет цен */ case 138: break; /* Цена устарела */ case 139: return(-Error); /* Ордер заблокирован */ case 140: Alert("Allowed only buying"); return(-Error); case 141: Alert("Too many requests"); return(-Error); case 142: Sleep(60000); break; /* Ордер поставлен в очередь */ case 143: Sleep(60000); break; /* Ордер принят к исполнению */ case 144: Alert("Order canceled customer"); return(-Error); case 146: while(IsTradeContextBusy()); break; /* Подсистема торговли занята */ case 148: Alert("Too many orders"); return(-Error); case 149: Alert("hedging is disabled"); return(-Error); } CountTry+=1; } Print("Failed open an order ", " completed by TimeOut Error ", Error); return(-Error); } //* ----------------------------------------------------- End OpenOrder ------------------- void CloseThisSymbolAll(int Op, int MagicNumber) { //* ------ Закрытие всех ордеров ------ for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) if (OrderSelect(l_pos_0, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && (OrderType() == Op || Op==-1)) if(OrderType()<=OP_SELL) CloseOrder(OrderTicket(),0); else OrderDelete(OrderTicket()); } //* ------------------------------------ End CloseThisSymbolAll --------------------- int CloseOrder(int Ticket, double Vol) { datetime dt=TimeLocal(); int CountTry=1; double PriceCurrent; int ArrowColor; int Error=0; if (!OrderSelect(Ticket, SELECT_BY_TICKET)) { Alert("order #", Ticket, " not found"); return(-1); } if(Vol == 0 || Vol>OrderLots()) Vol=OrderLots(); while (TimeLocal()-dt < TimeOut) { Print("closing orders #", Ticket, "try",CountTry); RefreshRates(); if (OrderType() == OP_BUY) { PriceCurrent=Bid; ArrowColor=Blue; } else if (OrderType() == OP_SELL) { PriceCurrent=Ask; ArrowColor=Red; } else return(-1); if (OrderClose(Ticket, Vol, PriceCurrent, slippage, ArrowColor)) { Print("order #", Ticket, " Close"); return(Ticket); } Error=GetLastError(); Print("order #", Ticket, "Try ", CountTry, " Error ", Error); CountTry += 1; } Print("Failed close the order #", Ticket, " completed by TimeOut Error ", Error); return(-Error); } //* ------------------------------------------ End CloseOrder ---------------------------- int TrailingAll(int TrailStart, int TrailDistance, int TrailStep, int Magic) { int AmountOrdersBuy = 0, AmountOrdersSell = 0; double AveragePriceBuy = 0, AveragePriceSell = 0; double SumLotBuy = 0, SumLotSell = 0; double SLBuy, SLSell; double OldSL; int ProfitBuy, ProfitSell; bool Result; TrailStep = TrailStep >0 ? TrailStep : 1; if(TrailDistance == 0 || TrailDistance < MarketInfo(Symbol(),MODE_STOPLEVEL)) return(0); for (int Count = OrdersTotal() - 1; Count >= 0; Count--) if (OrderSelect(Count,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { if (OrderType() == OP_BUY) { AmountOrdersBuy ++; AveragePriceBuy += OrderOpenPrice() * OrderLots(); SumLotBuy += OrderLots(); } else if (OrderType() == OP_SELL) { AmountOrdersSell ++; AveragePriceSell += OrderOpenPrice() * OrderLots(); SumLotSell += OrderLots(); } } if (SumLotBuy != 0) { AveragePriceBuy = AveragePriceBuy / SumLotBuy; ProfitBuy = NormalizeDouble((Bid - AveragePriceBuy) / Point, 0); if (ProfitBuy >= TrailStart) SLBuy = Bid - TrailDistance * Point; else SLBuy = 0; } if (SumLotSell != 0) { AveragePriceSell = AveragePriceSell / SumLotSell; ProfitSell = NormalizeDouble((AveragePriceSell - Ask) / Point, 0); if (ProfitSell >= TrailStart) SLSell = Ask + TrailDistance * Point; else SLSell = 0; } for (int Count = OrdersTotal() - 1; Count >= 0; Count--) { if (OrderSelect(Count,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { OldSL = OrderStopLoss(); if (OrderType() == OP_BUY && SLBuy != 0 && (SLBuy >= OldSL+TrailStep*Point || OldSL == 0)) Result=OrderModify(OrderTicket(), OrderOpenPrice(), SLBuy, OrderTakeProfit(), 0, Blue); else if (OrderType() == OP_SELL && SLSell != 0 && (SLSell <= OldSL-TrailStep*Point || OldSL == 0)) Result=OrderModify(OrderTicket(), OrderOpenPrice(), SLSell, OrderTakeProfit(), 0, Red); } } return(0); } //* -------------------------------------- End TrailingAll ---------------------