//+------------------------------------------------------------------+ //| Bollinger Bands and MACD EA. //| ULITMATE HEDGE //| by: DAVID ADEDEJI. //+------------------------------------------------------------------+ #property copyright "King_David" #property version "4.00" #property strict extern int TakeProfit=500; extern int StopLoss=10; extern double LotSize = 0.01; extern bool UseMoveToBreakeven=true; extern int WhenToMoveToBE=100; extern int PipsToLockIn=5; extern bool UseTrailingStop = true; extern int WhenToTrail=30; extern int TrailAmount=40; extern int MagicNumber = 1234; double pips; extern int BollingerPeriod=20; extern int BollingerDeviation=2; extern int Fast_Macd_Ema=21; extern int Slow_Macd_Ema=89; extern double Macd_Threshold=50; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE); if (ticksize == 0.00001 || ticksize == 0.001) pips = ticksize*10; else pips =ticksize; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(OpenOrdersThisPair(Symbol())>=1) { if(UseMoveToBreakeven)MoveToBreakeven(); if(UseTrailingStop)AdjustTrail(); } if(IsNewCandle())CheckForBollingerBandTrade(); } //+------------------------------------------------------------------+ void CheckForBollingerBandTrade() { //----------------+ // MACD Settings //----------------+ double Macd_Value=iMACD(NULL,0,Fast_Macd_Ema,Slow_Macd_Ema,1,PRICE_CLOSE,MODE_MAIN,1); double threshold=Macd_Threshold*pips; //+-------------------------+ // |Bollinger Bands Settings //+-------------------------+ double MiddleBB=iBands(NULL,0,BollingerPeriod,BollingerDeviation,0,0,MODE_MAIN,1); double LowerBB=iBands(NULL,0,BollingerPeriod,BollingerDeviation,0,0,MODE_LOWER,1); double UpperBB=iBands(NULL,0,BollingerPeriod,BollingerDeviation,0,0,MODE_UPPER,1); double PrevMiddleBB=iBands(NULL,0,BollingerPeriod,BollingerDeviation,0,0,MODE_MAIN,2); double PrevLowerBB=iBands(NULL,0,BollingerPeriod,BollingerDeviation,0,0,MODE_LOWER,2); double PrevUpperBB=iBands(NULL,0,BollingerPeriod,BollingerDeviation,0,0,MODE_UPPER,2); //---------------------------------------+ //Bollinger Bands and MACD Conditions //---------------------------------------+ if(Macd_Value>0&&Macd_Value < threshold) if(Close[1]>LowerBB&&Close[2] -threshold) if(Close[1]PrevUpperBB)OrderEntry(1); } //+------------------------------------------------------------------+ //checks to see if any orders open on this currency pair. //+------------------------------------------------------------------+ int OpenOrdersThisPair(string pair) { int total=0; for(int i=OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()== pair) total++; } return (total); } //+------------------------------------------------------------------+ //Move to breakeven function //+------------------------------------------------------------------+ void MoveToBreakeven() { for(int b=OrdersTotal()-1; b >= 0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()== MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUYSTOP) if(Bid-OrderOpenPrice()>WhenToMoveToBE*pips) if(OrderOpenPrice()>OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE); } for (int s=OrdersTotal()-1; s >= 0; s--) { if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()== MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELLSTOP) if(OrderOpenPrice()-Ask>WhenToMoveToBE*pips) if(OrderOpenPrice()=0;b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUYSTOP) if(Bid-OrderOpenPrice()>WhenToTrail*pips) if(OrderStopLoss()=0;s--) { if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()== MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELLSTOP) if(OrderOpenPrice()-Ask>WhenToTrail*pips) if(OrderStopLoss()>Ask+TrailAmount*pips) OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmount*pips),OrderTakeProfit(),0,CLR_NONE); } } //+------------------------------------------------------------------+ //insuring its a new candle function //+------------------------------------------------------------------+ bool IsNewCandle() { static int BarsOnChart=0; if (Bars == BarsOnChart) return (false); BarsOnChart = Bars; return(true); } //+------------------------------------------------------------------+ //order entry function. //+------------------------------------------------------------------+ void OrderEntry(int direction) { if(direction==0) { BuySellStop(double price, double stop, double price1) } if(direction==1) { SellBuyStop(double price, double stop, double price1) } } //+------------------------------------------------------------------+ // Buy Stop Function //-------------------------------------------------------------------+ void BuySellStop(double price, double stop, double price1) { price = Ask; price1 = Bid; stop = StopLoss; if(OrdersTotal()==0) { ticket1 = OrderSend(Symbol(),OP_BUYSTOP, LotSize, price, 3, stop, price+(TakeProfit*pips), NULL, MagicNumber, Blue); ticket2 = OrderSend(Symbol(),OP_SELLSTOP, LotSize, price, 3, stop, price1-(TakeProfit*pips), NULL, MagicNumber, Red); } if(ticket1<0 && ticket2<0) { print("Order sent FAILED with error: # ", GetLastError); } else print("Order sent placed SUCCESSFULLY"); } //-------------------------------------------------------------------+ //Sell Stop function //-------------------------------------------------------------------+ void SellBuyStop(double price, double stop, double price1) { price = Ask; price1 = Bid; stop = StopLoss; if(OrdersTotal()==0) { ticket3 = OrderSend(Symbol(),OP_BUYSTOP, LotSize, price, 3, stop, price+(TakeProfit*pips), NULL, MagicNumber, Blue); ticket4 = OrderSend(Symbol(),OP_SELLSTOP, LotSize, price, 3, stop, price1-(TakeProfit*pips), NULL, MagicNumber, Red); } if(ticket3<0 && ticket4<0) { print("Order sent FAILED with error: # ", GetLastError); } else print("Order sent placed SUCCESSFULLY"); }