//+------------------------------------------------------------------+ //| BB_Stop_EA.mq4 | //| Copyright © 2010, Robert Hill | //| | //+------------------------------------------------------------------+ #property copyright "Copyright 2010, Robert Hill" #include #include // Add defines for signals #define LONG 1 #define SHORT -1 #define FLAT 0 // Add defines for position status #define NONE 0 #define OPENED 1 #define CLOSED 2 // MoneyManagement Account Usage #define EQUITY 2 #define MARGIN 3 extern int BuyBuf = 2; extern int SellBuf = 3; extern int MagicBase = 100000; extern string UserComment = "BB Stop"; extern string m0 = "BB Stop Settings"; extern int Length = 5; extern int Slip = 3; extern int Risk = 1; //+---------------------------------------------------+ //|Money Management | //+---------------------------------------------------+ extern string mm = "---Money Management---"; extern double Lots=0.1; extern bool UseMoneyManagement = false; extern string mm0 = "Select One of the following"; extern string mm1 = " 1. Use Account Balance"; extern string mm2 = " 2. Use Account Equity"; extern string mm3 = " 3. Use Account Free Margin"; extern int mmAccount = 1; extern bool BrokerIsIBFX = false; extern string mm4="Set mini and micro to false for standard account"; extern bool AccountIsMini = true; extern bool AccountIsMicro = false; extern double TradeSizePercent = 1; // Change to whatever percent of equity you wish to risk. extern bool BrokerPermitsFractionalLots = true; extern int TakeProfit = 130; extern int StopLoss=60;// 0= no stoploss extern int Slippage = 3; extern string ts0 = "---TrailingStopLoss---"; extern string ts1 = " 1. None"; extern string ts2 = " 2. Standard at input"; extern string ts3 = " 3. Trail immediately"; extern int TrailingStopMethod = 2; extern string ts12 = "Settings for Type 2"; extern double TrailingStop = 20; int SignalCandle = 1; int MagicNumber=0; double myPoint; double mLots; double TPprice, STprice; string gTPpriceName, gSTpriceName; int init() { MagicNumber = MagicBase + func_Symbol2Val(Symbol())*100 + func_TimeFrame_Const2Val(Period()); myPoint = SetPoint(); GetGlobalVars(); return(0); } int deinit() { return(0); } //+------------------------------------------------------------------+ //| GetSignal | //| | //+------------------------------------------------------------------+ int GetSignal() { double b, s; //---- go trading only for first tiks of new bar if(Volume[0]>1) return(FLAT); //---- get BB Stop //---- sell conditions s = iCustom(NULL, 0, "BBands_Stop_v2", Length, Slip, Risk, SellBuf, 1); if(s > 0.1) return(SHORT); //---- buy conditions b = iCustom(NULL, 0, "BBands_Stop_v2", Length, Slip, Risk, BuyBuf, 1); if(b > 0.1) return(LONG); return (FLAT); } //+------------------------------------------------------------------+ //| Start function | //+------------------------------------------------------------------+ void start() { int NumOrders = 0, Position, signal; RefreshRates(); if (IsTradeAllowed()==false ) return(0); //+------------------------------------------------------------------+ //| Check for Open Position | //+------------------------------------------------------------------+ // Only allow 1 trade per Symbol Position = HandleOpenPositions(); if (Position == OPENED) return(0); signal=GetSignal(); // wait until first tick after bar close to take any action; if (signal != FLAT) { mLots = GetLots(); if(signal == LONG) { OpenTrade(OP_BUY, mLots); return(0); } if(signal == SHORT) { OpenTrade(OP_SELL, mLots); } } return(0); //---- } int OpenTrade(int signal, double mLots) { int err, ticket; RefreshRates(); if (signal==OP_BUY) { ticket=OrderSend(Symbol(),OP_BUY,mLots,Ask,Slippage,0,0,UserComment,MagicNumber,0,Green); if (ticket > 0) { OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) { TPprice=TakeLong(OrderOpenPrice(), TakeProfit); TPprice = ValidTakeProfit(OP_BUY,Ask, TPprice); } STprice = 0; if (StopLoss > 0) { STprice=StopLong(OrderOpenPrice(), StopLoss); STprice = ValidStopLoss(OP_BUY,Bid, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } SaveTPprice (TPprice); SaveSTprice (STprice); OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen); } } } else if (signal==OP_SELL) { ticket=OrderSend(Symbol(),OP_SELL,mLots,Bid,Slippage,0,0,UserComment,MagicNumber,0,Red); if (ticket > 0) { OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) { TPprice=TakeShort(OrderOpenPrice(),TakeProfit); TPprice = ValidTakeProfit(OP_SELL,Bid, TPprice); } STprice = 0; if (StopLoss > 0) { STprice=StopShort(OrderOpenPrice() ,StopLoss); STprice = ValidStopLoss(OP_SELL,Ask, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } SaveTPprice (TPprice); SaveSTprice (STprice); OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen); } } } if(ticket<0) { err = GetLastError(); Print("OrderSend failed with error(" + err + ") " + ErrorDescription(err) ); } return(ticket); } //+------------------------------------------------------------------+ //| Handle Open Positions | //| Check if any open positions need to be closed or modified | //+------------------------------------------------------------------+ int HandleOpenPositions() { bool Done; int ticket, OrderTy; double OrderLTS, OrderOP; double OrderTP, OrderSL; int cnt; int Closed; Closed = NONE; for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES); if ( OrderSymbol() != Symbol()) continue; if ( OrderMagicNumber() != MagicNumber) continue; ticket = OrderTicket(); OrderTy = OrderType(); OrderLTS = OrderLots(); OrderOP = OrderOpenPrice(); OrderTP = OrderTakeProfit(); OrderSL = OrderStopLoss(); Closed = OPENED; // Check for TakeProfit Exits Done = false; Done = CheckTPClose(OrderTy, ticket, OrderLTS, TPprice); if (Done == true) return(CLOSED); // Check for internal StopLoss Done = false; Done = CheckSLClose(OrderTy, ticket, OrderLTS, STprice); if (Done == true) return(CLOSED); // Check for exit on signal Done = false; Done = CheckExitCondition(OrderTy, ticket, OrderLTS); if (Done == true) return(CLOSED); // If we get to here the trade is still Open // check for trailing stop if (TrailingStopMethod > 0) { HandleTrailingStop(OrderTy,ticket,OrderOP,OrderSL,OrderTP); } } return(Closed); } //+------------------------------------------------------------------+ //| Check for close signal | //+------------------------------------------------------------------+ bool CheckExitCondition(int cmd, int ticket, double ol) { double b, s; //---- go trading only for first tiks of new bar if(Volume[0]>1) return(false); if (cmd == OP_BUY) { s = iCustom(NULL, 0, "BBands_Stop_v2", Length, Slip, Risk, SellBuf, 1); if(s > 0.1) { OrderClose(ticket, ol, Bid, Slippage, Green); return(true); } } if (cmd == OP_SELL) { b = iCustom(NULL, 0, "BBands_Stop_v2", Length, Slip, Risk, BuyBuf, 1); if(b > 0.1) { OrderClose(ticket, ol ,Ask, Slippage, Red); return(true); } } return(false); //---- } bool CheckSLClose(int cmd, int ticket, double ol, double IntSTprice) { if (cmd == OP_BUY) { if(Bid <= IntSTprice) { OrderClose(ticket,ol,Bid, Slippage, Green); return (true); } return(false); } if (cmd == OP_SELL) { if (IntSTprice > 0.1) { if(Ask >= IntSTprice) { OrderClose(ticket,ol,Ask, Slippage, Red); return(true); } } } return(false); } bool CheckTPClose(int cmd, int ticket, double ol, double IntTPprice ) { if (cmd == OP_BUY) { if(Bid >= IntTPprice && IntTPprice > 0.1) { OrderClose(ticket,ol,Bid, Slippage, Green); return(true); } return(false); } if (cmd == OP_SELL) { if(Ask <= IntTPprice) { OrderClose(ticket,ol,Bid, Slippage, Red); return(true); } } return(false); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } double StopLong(double price,int stop) { if(stop==0) return(0); else return(price-(stop*myPoint)); } double StopShort(double price,int stop) { if(stop==0) return(0); else return(price+(stop*myPoint)); } double TakeLong(double price,int take) { if(take==0) return(0); else return(price+(take*myPoint)); } double TakeShort(double price,int take) { if(take==0) return(0); else return(price-(take*myPoint)); } double ValidStopLoss(int type, double price, double SL) { double minstop; double newSL; if (SL < 0.1) return(SL); minstop = MarketInfo(Symbol(),MODE_STOPLEVEL); if (Digits == 3 || Digits == 5) minstop = minstop / 10; newSL = SL; if (type == OP_BUY) { if((price - SL) < minstop*myPoint) newSL = price - minstop*myPoint; } if (type == OP_SELL) { if((SL-price) < minstop*myPoint) newSL = price + minstop*myPoint; } newSL = NormalizeDouble(newSL,Digits); return(newSL); } double ValidTakeProfit(int type, double price, double TP) { double newTP, temp; temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; newTP = TP; if (type == OP_BUY) { if((TP - price) < temp) newTP = price + temp; } if (type == OP_SELL) { if((price - TP) < temp) newTP = price - temp; } newTP = NormalizeDouble(newTP,Digits); return(newTP); } //+------------------------------------------------------------------+ //| HandleTrailingStop | //| Type 1 is do not trail | //| Type 2 waits for price to move the amount of the trailStop | //| before moving stop loss then moves like type 4 | //| Type 3 moves the stoploss without delay. | //+------------------------------------------------------------------+ void HandleTrailingStop(int cmd, int ticket, double op, double os, double tp) { switch (TrailingStopMethod) { case 2 : Delayed_TrailingStop (cmd, ticket, op, os, tp); break; case 3 : Immediate_TrailingStop (cmd, ticket, op, os, tp); break; } } //+------------------------------------------------------------------+ //| Immediate_TrailingStop.mq4 | //| Copyright © 2006, Forex-TSD.com | //| Written by MrPip,robydoby314@yahoo.com | //| | //| Moves the stoploss without delay. | //+------------------------------------------------------------------+ void Immediate_TrailingStop(int type, int ticket, double op, double os, double tp) { int digits; double pt, pBid, pAsk, BuyStop, SellStop; digits = MarketInfo(Symbol( ), MODE_DIGITS); if (type==OP_BUY) { pBid = MarketInfo(Symbol(), MODE_BID); pt = StopLoss * myPoint; if(pBid-os > pt) { BuyStop = pBid - pt; if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits); BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop); if (os < BuyStop) { SaveSTprice(BuyStop); OrderModify(ticket,op,BuyStop,tp,0,LightGreen); } return; } } if (type==OP_SELL) { pAsk = MarketInfo(Symbol(), MODE_ASK); pt = StopLoss * myPoint; if(os - pAsk > pt) { SellStop = pAsk + pt; if (digits > 0) SellStop = NormalizeDouble( SellStop, digits); SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop); if (os > SellStop) { SaveSTprice(SellStop); OrderModify(ticket,op,SellStop,tp,0,DarkOrange); } return; } } } //+------------------------------------------------------------------+ //| Delayed_TrailingStop.mq4 | //| Copyright © 2006, Forex-TSD.com | //| Written by MrPip,robydoby314@yahoo.com | //| | //| Waits for price to move the amount of the TrailingStop | //| Moves the stoploss pip for pip after delay. | //+------------------------------------------------------------------+ void Delayed_TrailingStop(int type, int ticket, double op, double os, double tp) { int digits; double pt, pBid, pAsk, BuyStop, SellStop; pt = TrailingStop * myPoint; digits = MarketInfo(Symbol(), MODE_DIGITS); if (type==OP_BUY) { pBid = MarketInfo(Symbol(), MODE_BID); BuyStop = pBid - pt; if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits); BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop); if (pBid-op > pt && os < BuyStop) { SaveSTprice(BuyStop); OrderModify(ticket,op,BuyStop,tp,0,LightGreen); } return; } if (type==OP_SELL) { pAsk = MarketInfo(Symbol(), MODE_ASK); pt = TrailingStop * myPoint; SellStop = pAsk + pt; if (digits > 0) SellStop = NormalizeDouble( SellStop, digits); SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop); if (op - pAsk > pt && os > SellStop) { SaveSTprice (SellStop); OrderModify(ticket,op,SellStop,tp,0,DarkOrange); } return; } } //+------------------------------------------------------------------+ //| Get number of lots for this trade | //+------------------------------------------------------------------+ double GetLots() { double lot, AccountValue; double myMaxLot = MarketInfo(Symbol(), MODE_MAXLOT); if (UseMoneyManagement == false) return(Lots); if (BrokerIsIBFX == true) { lot = Calc_IBFX_Money_Management(); return(lot); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ if (mmAccount == EQUITY) AccountValue = AccountEquity(); else if (mmAccount == MARGIN) AccountValue = AccountFreeMargin(); else AccountValue = AccountBalance(); lot=NormalizeDouble(AccountValue*TradeSizePercent/1000.0,1); // lot = MathRound( lot/myStep ) * myStep; // Use at least 1 micro lot if (AccountIsMicro == true) { lot = MathFloor(lot*100)/100; if (lot < 0.01) lot = 0.01; if (lot > myMaxLot) lot = myMaxLot; return(lot); } // Use at least 1 mini lot if(AccountIsMini == true) { lot = MathFloor(lot*10)/10; if (lot < 0.1) lot = 0.1; if (lot > myMaxLot) lot = myMaxLot; return(lot); } // Standard account if( BrokerPermitsFractionalLots == false) { if (lot >= 1.0) lot = MathFloor(lot); else lot = 1.0; } if (lot < 1.0) lot = 1.0; if (lot > myMaxLot) lot = myMaxLot; return(lot); } //+------------------------------------------------------------------+ //| Calculate optimal lot size for IBFX | //+------------------------------------------------------------------+ double Calc_IBFX_Money_Management() { // variables used for money management double lot, AccountValue; double myMaxLot = MarketInfo(Symbol(), MODE_MAXLOT); if (mmAccount == EQUITY) AccountValue = AccountEquity(); else if (mmAccount == MARGIN) AccountValue = AccountFreeMargin(); else AccountValue = AccountBalance(); lot=NormalizeDouble(AccountValue*TradeSizePercent/1000.0,2); // Use at least 1 micro lot if (AccountIsMicro == true) { lot = lot * 10; lot = MathFloor(lot*100)/100; if (lot < 0.1) lot = 0.1; if (lot > myMaxLot) lot = myMaxLot; return(lot); } // Use at least 1 mini lot if(AccountIsMini == true) { lot = lot * 10; lot = MathFloor(lot*10)/10; if (lot < 1) lot = 1; if (lot > myMaxLot) lot = myMaxLot; return(lot); } // Standard Account if(BrokerPermitsFractionalLots == true) lot = StrToDouble(DoubleToStr(lot, 2)); else lot = MathFloor(lot); if (lot > myMaxLot) lot = myMaxLot; return(lot); } int func_Symbol2Val(string symbol) { string mySymbol = StringSubstr(symbol,0,6); if(mySymbol=="AUDCAD") return(1); if(mySymbol=="AUDJPY") return(2); if(mySymbol=="AUDNZD") return(3); if(mySymbol=="AUDUSD") return(4); if(mySymbol=="CHFJPY") return(5); if(mySymbol=="EURAUD") return(6); if(mySymbol=="EURCAD") return(7); if(mySymbol=="EURCHF") return(8); if(mySymbol=="EURGBP") return(9); if(mySymbol=="EURJPY") return(10); if(mySymbol=="EURUSD") return(11); if(mySymbol=="GBPCHF") return(12); if(mySymbol=="GBPJPY") return(13); if(mySymbol=="GBPUSD") return(14); if(mySymbol=="NZDJPY") return(15); if(mySymbol=="NZDUSD") return(16); if(mySymbol=="USDCAD") return(17); if(mySymbol=="USDCHF") return(18); if(mySymbol=="USDJPY") return(19); if(mySymbol=="CADJPY") return(20); Comment("unexpected Symbol"); return(999); } //+------------------------------------------------------------------+ //| Time frame interval appropriation function | //+------------------------------------------------------------------+ int func_TimeFrame_Const2Val(int Constant ) { switch(Constant) { case 1: // M1 return(1); case 5: // M5 return(2); case 15: return(3); case 30: return(4); case 60: return(5); case 240: return(6); case 1440: return(7); case 10080: return(8); case 43200: return(9); } } string tf2txt(int tf) { switch(tf) { case PERIOD_M1: return("M1"); case PERIOD_M5: return("M5"); case PERIOD_M15: return("M15"); case PERIOD_M30: return("M30"); case PERIOD_H1: return("H1"); case PERIOD_H4: return("H4"); case PERIOD_D1: return("D1"); case PERIOD_W1: return("W1"); case PERIOD_MN1: return("MN"); } return("??"); } void GetGlobalVars() { NameGlobalVars(); InitGlobalVars(); GetGlobalVarValues(); } void GetGlobalVarValues() { double temp; // Booleans stored as double so convert to boolean // > 0 true, < 0 false // temp = GlobalVariableGet(gFirst_TP_LevelName); // if (temp > 0) First_TP_Level = true; else First_TP_Level = false; TPprice = GlobalVariableGet (gTPpriceName); STprice = GlobalVariableGet (gSTpriceName); } void InitGlobalVars() { // check variable before use if(!GlobalVariableCheck(gTPpriceName)) GlobalVariableSet(gTPpriceName,0); if(!GlobalVariableCheck(gSTpriceName)) GlobalVariableSet(gSTpriceName,0); } void NameGlobalVars() { string prefix; prefix = "BBS_" + Symbol() + tf2txt(Period()); gTPpriceName = prefix + "_TPprice"; gSTpriceName = prefix + "_STprice"; } void SaveSTprice (double myVal) { GlobalVariableSet(gSTpriceName,myVal); } void SaveTPprice (double myVal) { GlobalVariableSet(gTPpriceName,myVal); } //+------------------------------------------------------------------+