//+------------------------------------------------------------------+ //| RoNz Auto SL n TP.mq4| //| Copyright 2014-2018, Rony Nofrianto | //+------------------------------------------------------------------+ #property copyright "Copyright 2014-2018, Rony Nofrianto" #property link "https://www.mql5.com/en/users/ronz" #property version "2.06" #property strict //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ /* v1.0 + Auto SL and TP v1.22 + Correcting Min Stop Level v2.0 + Added modes for SL and TP (Hidden or Placed) + Added profit lock + Added stepping Trailing Stop v2.01 + Added option to enable/disable alert when closed by hidden sl/tp v2.03 + Fixed initial locking profit + Fixed trailing stop v2.04 + Fixed trailing stop step + Rearrange lock profit to a function v2.05 + Added Trailing Stop Method (Classic, Step Keep Distance, Step By Step) v2.06 + Added Option to Enable/Disable Profit Lock NOTE: + First of all, your orders SL and TP must be set to 0, then this EA will set appropriate SL and TP. + To disable SL, TP, Profit Lock, and Trailing Stop, set its value to 0. */ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum ENUM_CHARTSYMBOL { CurrentChartSymbol=0,//Current Chart Only AllOpenOrder=1,//All Opened Orders }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum ENUM_SLTP_MODE { Server=0,//Place SL n TP Client=1,//Hidden SL n TP }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum ENUM_LOCKPROFIT_ENABLE { LP_DISABLE=0,//Disable LP_ENABLE=1,//Enable }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum ENUM_TRAILINGSTOP_METHOD { TS_NONE=0,//No Trailing Stop TS_CLASSIC=1,//Classic TS_STEP_DISTANCE=2,//Step Keep Distance TS_STEP_BY_STEP=3, //Step By Step }; string STR_OPTYPE[]={"Buy","Sell","Buy Limit","Sell Limit","Buy Stop","Sell Stop"}; sinput string note1="";//-=[ SL & TP SETTINGS ]=- input int TakeProfit=500;//Take Profit input int StopLoss=250;//Stop Loss input ENUM_SLTP_MODE SLnTPMode=Client;//SL & TP Mode sinput string note2="";//-=[ PROFIT LOCK SETTINGS ]=- input ENUM_LOCKPROFIT_ENABLE LockProfitEnable=LP_ENABLE;//Enable/Disable Profit Lock input int LockProfitAfter=100;//Target Points to Lock Profit input int ProfitLock=60;//Profit To Lock sinput string note3="";//-=[ TRAILING STOP SETTINGS ]=- input ENUM_TRAILINGSTOP_METHOD TrailingStopMethod=TS_NONE;//Trailing Method input int TrailingStop=50;//Trailing Stop input int TrailingStep=10;//Trailing Stop Step sinput string note4="";//-=[ OTHER SETTINGS ]=- input ENUM_CHARTSYMBOL ChartSymbolSelection=AllOpenOrder;// input bool inpEnableAlert=false;//Enable Alert //+------------------------------------------------------------------+ //| Hitung Posisi Terbuka | //+------------------------------------------------------------------+ int CalculateCurrentOrders() { int buys=0,sells=0; //--- for(int i=0;i0) return(buys); else return(-sells); //--- } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool LockProfit(int TiketOrder,int TargetPoints,int LockedPoints) { if(LockProfitEnable==False || TargetPoints==0 || LockedPoints==0) return false; if(OrderSelect(TiketOrder,SELECT_BY_TICKET,MODE_TRADES)==false) return false; double CurrentSL=(OrderStopLoss()!=0)?OrderStopLoss():OrderOpenPrice(); double point=MarketInfo(OrderSymbol(),MODE_POINT); int digits=(int)MarketInfo(OrderSymbol(),MODE_DIGITS); double minstoplevel=MarketInfo(OrderSymbol(),MODE_STOPLEVEL); double ask=MarketInfo(OrderSymbol(),MODE_ASK); double bid=MarketInfo(OrderSymbol(),MODE_BID); double PSL=0; if((OrderType()==OP_BUY) && (bid-OrderOpenPrice()>=TargetPoints*point) && (CurrentSL<=OrderOpenPrice())) { PSL=NormalizeDouble(OrderOpenPrice()+(LockedPoints*point),digits); } else if((OrderType()==OP_SELL) && (OrderOpenPrice()-ask>=TargetPoints*point) && (CurrentSL>=OrderOpenPrice())) { PSL=NormalizeDouble(OrderOpenPrice()-(LockedPoints*point),digits); } else return false; Print(STR_OPTYPE[OrderType()]," #",OrderTicket()," ProfitLock: OP=",OrderOpenPrice()," CSL=",CurrentSL," PSL=",PSL," LP=",LockedPoints); if(OrderModify(OrderTicket(),OrderOpenPrice(),PSL,OrderTakeProfit(),0,clrRed)) return true; else return false; return false; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool RZ_TrailingStop(int TiketOrder,int JumlahPoin,int Step=1,ENUM_TRAILINGSTOP_METHOD Method=TS_STEP_DISTANCE) { if(JumlahPoin==0) return false; if(OrderSelect(TiketOrder,SELECT_BY_TICKET,MODE_TRADES)==false) return false; double CurrentSL=(OrderStopLoss()!=0)?OrderStopLoss():OrderOpenPrice(); double point=MarketInfo(OrderSymbol(),MODE_POINT); int digits=(int)MarketInfo(OrderSymbol(),MODE_DIGITS); double minstoplevel=MarketInfo(OrderSymbol(),MODE_STOPLEVEL); double ask=MarketInfo(OrderSymbol(),MODE_ASK); double bid=MarketInfo(OrderSymbol(),MODE_BID); double TSL=0; JumlahPoin=JumlahPoin+(int)minstoplevel; if((OrderType()==OP_BUY) && (bid-OrderOpenPrice()>JumlahPoin*point)) { if(CurrentSL=JumlahPoin*point) { switch(Method) { case TS_CLASSIC://Classic, no step TSL=NormalizeDouble(bid-(JumlahPoin*point),digits); break; case TS_STEP_DISTANCE://Step keeping distance TSL=NormalizeDouble(bid-((JumlahPoin-Step)*point),digits); break; case TS_STEP_BY_STEP://Step by step (slow) TSL=NormalizeDouble(CurrentSL+(Step*point),digits); break; default: TSL=0; } } } else if((OrderType()==OP_SELL) && (OrderOpenPrice()-ask>JumlahPoin*point)) { if(CurrentSL>OrderOpenPrice()) CurrentSL=OrderOpenPrice(); if((CurrentSL-ask)>=JumlahPoin*point) { switch(Method) { case TS_CLASSIC://Classic TSL=NormalizeDouble(ask+(JumlahPoin*point),digits); break; case TS_STEP_DISTANCE://Step keeping distance TSL=NormalizeDouble(ask+((JumlahPoin-Step)*point),digits); break; case TS_STEP_BY_STEP://Step by step (slow) TSL=NormalizeDouble(CurrentSL-(Step*point),digits); break; default: TSL=0; } } } if(TSL==0) return false; Print(STR_OPTYPE[OrderType()]," #",OrderTicket()," TrailingStop: OP=",OrderOpenPrice()," CSL=",CurrentSL," TSL=",TSL," TS=",JumlahPoin," Step=",Step); bool res=OrderModify(OrderTicket(),OrderOpenPrice(),TSL,OrderTakeProfit(),0,clrRed); if(res == true) return true; else return false; return false; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool SetSLnTP() { double SL,TP; SL=TP=0.00; for(int i=0;i