//+------------------------------------------------------------------+ //| Breakout BSL EA.mq4 | //| Copyright © 2009, TradingSytemForex | //| http://www.tradingsystemforex.com | //+------------------------------------------------------------------+ /* Edited by sanpedro@windstream.net*/ /* Primary use is GBPUSD M15 */ #property copyright "Copyright © 2009, TradingSytemForex" #property link "http://www.tradingsystemforex.com" //|----------------------------------------------you can modify this expert //|----------------------------------------------you can change the name //|----------------------------------------------you can add "modified by you" //|----------------------------------------------but you are not allowed to erase the copyrights #define EAName "Breakout BSL EA" extern string S1="---------------- Entry Settings"; extern string StartHour="14:00";//|--------------start hour to set the range extern string EndHour="15:57";//|----------------end hour to set the range extern int CloseMode=2;//|-----------------------0=no close, 1=close hour, 2=expiration extern string CloseHour="17:00";//|--------------hour to close trades extern int Expiration=180;//|--------------------expiration in minutes extern int SetMinutes=-5;//|----------------------max number of minutes to set orders extern double MinRangePips=10;//|----------------minimum range in pips extern double MaxRangePips=80;//|---------------maximum range in pips extern int MarginRange=0;//|---------------------margin in pips added to the range extern string S2="---------------- Money Management"; extern double Lots=1.0;//|-----------------------lots size extern bool RiskMM=false;//|---------------------risk management extern double RiskPercent=25;//|------------------risk percentage extern bool Martingale=false;//|-----------------martingale extern double Multiplier=2.0;//|-----------------multiplier martingale extern double MinProfit=50;//--------------------minimum profit to apply the martingale extern bool BasketProfitLoss=false;//|-----------use basket loss/profit extern int BasketProfit=100000;//|---------------if equity reaches this level, close trades extern int BasketLoss=9999;//|-------------------if equity reaches this negative level, close trades extern string S3="---------------- Order Management"; extern bool HighLowSL=true;//|-------------------auto sl at opposite high low extern int StopLoss=30;//|------------------------stop loss extern int TakeProfit=15;//|----------------------take profit extern bool HideSL=True;//|---------------------hide stop loss extern bool HideTP=true;//|---------------------hide take profit extern int TrailingStop=0;//|--------------------trailing stop extern int TrailingStep=0;//|--------------------trailing step extern int BreakEven=0;//|-----------------------break even extern int MaxOrders=12;//|-----------------------maximum orders allowed extern int Slippage=4;//|------------------------slippage extern int Magic=2009;//|------------------------magic number extern string S4="---------------- MA Filter"; extern bool MAFilter=false;//|-------------------moving average filter extern int MAPeriod=20;//|-----------------------ma filter period extern int MAMethod=0;//|------------------------ma filter method extern int MAPrice=0;//|-------------------------ma filter price extern string S5="---------------- Time Filter"; extern bool TradeOnSunday=true;//|---------------time filter on sunday extern bool MondayToThursdayTimeFilter=false;//|-time filter the week extern int MondayToThursdayStartHour=0;//|-------start hour time filter the week extern int MondayToThursdayEndHour=24;//|--------end hour time filter the week extern bool FridayTimeFilter=false;//|-----------time filter on friday extern int FridayStartHour=0;//|-----------------start hour time filter on friday extern int FridayEndHour=21;//|------------------end hour time filter on friday extern string S6="---------------- Extras"; extern bool ReverseSystem=false;//|--------------buy instead of sell, sell instead of buy extern bool Hedge=false;//|----------------------enter an opposite trade extern int HedgeSL=30;//|-------------------------stop loss extern int HedgeTP=15;//|-------------------------take profit extern bool ReverseAtStop=false;//|--------------buy instead of sell, sell instead of buy extern bool Comments=false;//|--------------------allow comments on chart datetime PreviousBarTime1; datetime PreviousBarTime2; double maxEquity,minEquity,Balance=0.0; int cnt,Range=0,mStopLoss=0,mTakeProfit,TimeClose=0,BarsNumber=0,BarsBack=0,StartHour1=0,EndHour1=0,TimeStart=0,TimeEnd=0; double hi=0,lo=0,open_price=0,PriceOp=0,MyStopLoss=0,Profit=0; double pt,mt; int dg; //|---------initialization int init() { dg=Digits; if(dg==3 || dg==5){pt=Point*10;mt=10;}else{pt=Point;mt=1;} if(Comments)Comment("\nLoading..."); return(0); } //|---------deinitialization int deinit() { return(0); } int start() { //|---------trailing stop if(TrailingStop>0)MoveTrailingStop(); //|---------break even if(BreakEven>0)MoveBreakEven(); //|---------basket profit loss if(BasketProfitLoss) { double CurrentProfit=0,CurrentBasket=0; CurrentBasket=AccountEquity()-AccountBalance(); if(CurrentBasket>maxEquity)maxEquity=CurrentBasket; if(CurrentBasket=BasketProfit||CurrentBasket<=(BasketLoss*(-1))) { CloseBuyOrders(Magic); CloseSellOrders(Magic); return(0); } } //|---------time filter if((TradeOnSunday==false&&DayOfWeek()==0)||(MondayToThursdayTimeFilter&&DayOfWeek()>=1&&DayOfWeek()<=4&&!(Hour()>=MondayToThursdayStartHour&&Hour()<=MondayToThursdayEndHour))||(FridayTimeFilter&&DayOfWeek()==5&&!(Hour()>=FridayStartHour&&Hour()<=FridayEndHour))) { CloseBuyOrders(Magic); CloseSellOrders(Magic); return(0); } //|---------signal conditions int limit=1; for(int i=1;i<=limit;i++) { //|---------moving average filter double MAF=iMA(Symbol(),0,MAPeriod,0,MAMethod,MAPrice,i); string MABUY="false";string MASELL="false"; if((MAFilter==false)||(MAFilter&&Bid>MAF))MABUY="true"; if((MAFilter==false)||(MAFilter&&OrderClosePrice ()=0&&CurTime()-TimeStart<(TimeEnd-TimeClose)&&CloseMode==1) { for(cnt=0;cnt0&&CountOrders(OP_SELL,Magic)>0) { DeleteBuyStop(Symbol(),Magic); } if(CountOrders(OP_SELLSTOP,Magic)>0&&CountOrders(OP_BUY,Magic)>0) { DeleteSellStop(Symbol(),Magic); } if(CountOrders(OP_BUYLIMIT,Magic)>0&&CountOrders(OP_SELL,Magic)>0) { DeleteBuyLimit(Symbol(),Magic); } if(CountOrders(OP_SELLLIMIT,Magic)>0&&CountOrders(OP_BUY,Magic)>0) { DeleteSellLimit(Symbol(),Magic); } string BUY="false"; string SELL="false"; if(CurTime()-TimeEnd>=0&&CurTime()-TimeEnd<=SetMinutes*60&&Range>MinRangePips&&Range0&&CloseMode==2)Expire=TimeCurrent()+(Expiration*60)-5; if(OrdersTotal()0){SL=hi+MarginRange*Point-StopLoss*Point;OPP=Bid-StopLoss*Point;SLP=Bid;}else {SL=0;SLP=0;} if(HideTP==false&&TakeProfit>0){TP=hi+MarginRange*Point+TakeProfit*Point;TPP=Bid-(TakeProfit*2)*Point;}else {TP=0;TPP=0;} if(HideSL==false&&HedgeSL>0)SLH=Bid+HedgeSL*Point;else SLH=0; if(HideTP==false&&HedgeTP>0)TPH=Bid-HedgeTP*Point;else TPH=0; if(ReverseSystem){if(StopLoss>0)SLH=hi+(MarginRange+StopLoss)*Point;else SLH=0;if(TakeProfit>0)TPH=hi+(MarginRange-TakeProfit)*Point;else TPH=0;} if(HideSL==false&&HighLowSL){SL=NormalizeDouble(lo-MarginRange*Point,Digits);SLH=hi+(MarginRange+Range)*Point;} if(ReverseSystem)TicketH=OrderSend(Symbol(),OP_SELLLIMIT,Lots,hi+MarginRange*Point,Slippage,SLH,TPH,EAName,Magic,Expire,Red); else Ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,hi+MarginRange*Point,Slippage,SL,TP,EAName,Magic,Expire,Blue); if(ReverseAtStop&&StopLoss>0)TicketP=OrderSend(Symbol(),OP_SELLSTOP,Lots,OPP,Slippage,SLP,TPP,EAName,Magic,Expire,Red); } if (SignalSELL=="true"&&NewBarSell()) { if(HideSL==false&&StopLoss>0){SL=lo-MarginRange*Point+StopLoss*Point;OPP=OrderClosePrice ()+StopLoss*Point;SLP=OrderClosePrice ();}else {SL=0;SLP=0;} if(HideTP==false&&TakeProfit>0){TP=lo-MarginRange*Point-TakeProfit*Point;TPP=OrderClosePrice ()+(TakeProfit*2)*Point;}else {TP=0;TPP=0;} if(HideSL==false&&HedgeSL>0)SLH=OrderClosePrice ()-HedgeSL*Point;else SLH=0; if(HideTP==false&&HedgeTP>0)TPH=OrderClosePrice ()+HedgeTP*Point;else TPH=0; if(ReverseSystem){if(StopLoss>0)SLH=lo-(MarginRange-StopLoss)*Point;else SLH=0;if(TakeProfit>0)TPH=lo-(MarginRange-TakeProfit)*Point;else TPH=0;} if(HideSL==false&&HighLowSL){SL=NormalizeDouble(hi+MarginRange*Point,Digits);SLH=lo-(MarginRange+Range)*Point;} if(ReverseSystem)TicketH=OrderSend(Symbol(),OP_BUYLIMIT,Lots,lo-MarginRange*Point,Slippage,SLH,TPH,EAName,Magic,Expire,Blue); else Ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,lo-MarginRange*Point,Slippage,SL,TP,EAName,Magic,Expire,Red); if(ReverseAtStop&&StopLoss>0)TicketP=OrderSend(Symbol(),OP_BUYSTOP,Lots,OPP,Slippage,SLP,TPP,EAName,Magic,Expire,Red); } } //|---------close orders if(Hedge==false) { if((SELL=="true")||(ReverseSystem==false&&HideSL&&StopLoss>0&&OrderType()==OP_BUY&&(OrderOpenPrice()-OrderClosePrice ())/Point>=StopLoss)||(ReverseSystem==false&&HideTP&&TakeProfit>0&&OrderType()==OP_BUY&&(Bid-OrderOpenPrice())/Point>=TakeProfit)||(ReverseSystem&&HideSL&&StopLoss>0&&OrderType()==OP_SELL&&(Bid-OrderOpenPrice())/Point>=StopLoss)||(ReverseSystem&&HideTP&&TakeProfit>0&&OrderType()==OP_SELL&&(OrderOpenPrice()-OrderClosePrice ())/Point>=TakeProfit)) { if(ReverseSystem)CloseSellOrders(Magic);else CloseBuyOrders(Magic); } if((BUY=="true")||(ReverseSystem==false&&HideSL&&StopLoss>0&&OrderType()==OP_SELL&&(Bid-OrderOpenPrice())/Point>=StopLoss)||(ReverseSystem==false&&HideTP&&TakeProfit>0&&OrderType()==OP_SELL&&(OrderOpenPrice()-OrderClosePrice ())/Point>=TakeProfit)||(ReverseSystem&&HideSL&&StopLoss>0&&OrderType()==OP_BUY&&(OrderOpenPrice()-OrderClosePrice ())/Point>=StopLoss)||(ReverseSystem&&HideTP&&TakeProfit>0&&OrderType()==OP_BUY&&(Bid-OrderOpenPrice())/Point>=TakeProfit)) { if(ReverseSystem)CloseBuyOrders(Magic);else CloseSellOrders(Magic); } } else { if((HideSL&&StopLoss>0&&OrderType()==OP_BUY&&(OrderOpenPrice()-OrderClosePrice ())/Point>=StopLoss)||(HideTP&&TakeProfit>0&&OrderType()==OP_BUY&&(Bid-OrderOpenPrice())/Point>=TakeProfit)) { CloseBuyOrders(Magic); } if((HideSL&&StopLoss>0&&OrderType()==OP_SELL&&(Bid-OrderOpenPrice())/Point>=StopLoss)||(HideTP&&TakeProfit>0&&OrderType()==OP_SELL&&(OrderOpenPrice()-OrderClosePrice ())/Point>=TakeProfit)) { CloseSellOrders(Magic); } } //|---------not enough money warning int err=0; if(Ticket<0) { if(GetLastError()==134) { err=1; Print("Not enough money!"); } return (-1); } if(Comments) { Comment("\nCopyright © 2009, TradingSytemForex", "\n\nL o t s = " + DoubleToStr(Lots,2), "\nB a l a n c e = " + DoubleToStr(AccountBalance(),2), "\nE q u i t y = " + DoubleToStr(AccountEquity(),2)); } return(0); } //|---------close buy orders int CloseBuyOrders(int Magic) { int result,total=OrdersTotal(); for (int cnt=total-1;cnt>=0;cnt--) { OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) { OrderClose(OrderTicket(),OrderLots(),Bid,3); switch(OrderType()) { case OP_BUYLIMIT: case OP_BUYSTOP: result=OrderDelete(OrderTicket()); } } } } return(0); } //|---------close sell orders int CloseSellOrders(int Magic) { int result,total=OrdersTotal(); for(int cnt=total-1;cnt>=0;cnt--) { OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol()) { if(OrderType()==OP_SELL) { OrderClose(OrderTicket(),OrderLots(),OrderClosePrice (),3); switch(OrderType()) { case OP_SELLLIMIT: case OP_SELLSTOP: result=OrderDelete(OrderTicket()); } } } } return(0); } //|---------count orders int CountOrders(int Type,int Magic) { int _CountOrd; _CountOrd=0; for(int i=0;i0) { if((NormalizeDouble(OrderStopLoss(),Digits)0) { if((NormalizeDouble(OrderStopLoss(),Digits)>(NormalizeDouble(OrderClosePrice ()+Point*(TrailingStop+TrailingStep),Digits)))||(OrderStopLoss()==0)) { OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderClosePrice ()+Point*TrailingStop,Digits),OrderTakeProfit(),0,Red); return(0); } } } } } } //|---------break even void MoveBreakEven() { int cnt,total=OrdersTotal(); for(cnt=0;cnt0) { if(NormalizeDouble((Bid-OrderOpenPrice()),Digits)>BreakEven*Point) { if(NormalizeDouble((OrderStopLoss()-OrderOpenPrice()),Digits)<0) { OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+0*Point,Digits),OrderTakeProfit(),0,Blue); return(0); } } } } else { if(BreakEven>0) { if(NormalizeDouble((OrderOpenPrice()-OrderClosePrice ()),Digits)>BreakEven*Point) { if(NormalizeDouble((OrderOpenPrice()-OrderStopLoss()),Digits)<0) { OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-0*Point,Digits),OrderTakeProfit(),0,Red); return(0); } } } } } } } //|---------allow one action per bar bool NewBarBuy() { if(PreviousBarTime1MaxLots)Lots=MaxLots; return(0); } //|---------calculate martingale void CalculateMartingale() { double InitalLots=0.01; double MinLots=MarketInfo(Symbol(),MODE_MINLOT); double MaxLots=MarketInfo(Symbol(),MODE_MAXLOT); if(Balance!=0.0) { if(Balance>AccountBalance())Lots=Multiplier*Lots; else if((Balance+MinProfit)=AccountBalance())Lots=Lots; if(LotsMaxLots)Lots=MaxLots; } return(0); }