//+------------------------------------------------------------------+ //| StopLossPercent.mq4 | //| Yuriy Tokman (YTG) | //| http://ytg.com.ua/ | //+------------------------------------------------------------------+ #property copyright "Yuriy Tokman (YTG)" #property link "http://ytg.com.ua/" #property version "1.00" #property strict input double StopLossPercent = 13; input int Slippage = 3; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- Comment(""); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- double ab = AccountBalance(); double ae = AccountEquity(); double pr = (ab - ae)/(ab/100); Comment( "StopLossPercent="+DoubleToStr(-StopLossPercent,1)+"%"+ "\nActual StopLossPercent="+DoubleToStr(-pr,1)+"%" ); if(pr>StopLossPercent) { Alert(WindowExpertName()+" Close Stop Loss Percent = "+DoubleToStr(StopLossPercent,1)+"%"); ClosePositions(); DeleteOrders(); } //--- } //+------------------------------------------------------------------+ void DeleteOrders(string sy="", int op=-1, int mn=-1) { bool fd; int err, i, it, k=OrdersTotal(), ot; if (sy=="0") sy=Symbol(); for (i=k-1; i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { ot=OrderType(); if (ot>1 && ot<6) { if ((OrderSymbol()==sy || sy=="") && (op<0 || ot==op)) { if (mn<0 || OrderMagicNumber()==mn) { for (it=1; it<=5; it++) { if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) break; while (!IsTradeAllowed()) Sleep(5000); fd=OrderDelete(OrderTicket(), White); if (fd) { PlaySound("timeout"); break; } else { err=GetLastError(); Print("Error(",err,") delete order ",ot, ": ",err,", try ",it); Sleep(1000*5); } } } } } } } } //---- //---- void ClosePosBySelect( int t) { bool fc; color clClose; double ll, pa, pb, pp; int err, it; if (OrderSelect(t, SELECT_BY_TICKET, MODE_TRADES)) { for (it=1; it<=5; it++) { if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) break; while (!IsTradeAllowed()) Sleep(5000); RefreshRates(); pa=NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK),Digits); pb=NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID),Digits); if (OrderType()==OP_BUY) { pp=pb; clClose=Aqua; } else { pp=pa; clClose=Gold; } ll=OrderLots(); fc=OrderClose(OrderTicket(), ll, pp, Slippage, clClose); if (fc) { PlaySound("tick"); break; } else { err=GetLastError(); if (err==146) while (IsTradeContextBusy()) Sleep(1000*11); Print("Error(",err,") Close ",OrderType()," ",", try ",it); Print(OrderTicket()," Ask=",pa," Bid=",pb," pp=",pp); Print("sy=",OrderSymbol()," ll=",ll," sl=",OrderStopLoss(), " tp=",OrderTakeProfit()," mn=",OrderMagicNumber()); Sleep(1000*5); } } } else Print("Incorrect trade operation. Close ",OrderType()); } //--- void ClosePositions(string sy="", int op=-1, int mn=-1) { int i, k=OrdersTotal(); if (sy=="0") sy=Symbol(); for (i=k-1; i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if ((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) { if (OrderType()==OP_BUY || OrderType()==OP_SELL) { if (mn<0 || OrderMagicNumber()==mn) ClosePosBySelect(OrderTicket()); } } } } } //----