//+------------------------------------------------------------------+ //| Moving Average.mq4 | //| Copyright © 2005, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | /* Simple Rules as follows:- GBPUSD, TF H1 Buy Set up JMA 200 sloping upwards (Current value greater than value of 2 bars ago) %BB Below 2 RSI R2 below 10 Entry Buy current Close Exit When on Close when %BB is Greater than 100 ------------ --------- --------- --------- --------- --------- --------- --------- -- Sell Set up JMA sloping downwards (Current value Less than value of 2 bars ago) %BB Greater than 97 RSI R2 above 90 Entry Sell current bar Close Exit On Close when % BB below 0 ------------ --------- --------- --------- --------- --------- --------- --------- --------- --- Stop Loss Fixed 60 Pips for H1 TF Condition After loss stop trading resume when %bb returns to 50 */ //+------------------------------------------------------------------+ #include #include #define LONG 0 #define SHORT 1 #define FLAT -1 extern string UserComment = "BB Percent_EA"; extern int MagicNumber = 20050610; extern int LSMA_Period = 110; extern int RSI_Period = 2; extern double RSI_High = 90; extern double RSI_Low = 10; extern int Bands_period = 20; extern double Bands_deviation = 2; extern double BB_High = 95.0; extern double BB_Low = 5.0; extern double BB_BuyExit = 100.0; extern double BB_SellExit = 0.0; extern double Lots = 0.1; extern int TakeProfit = 0; 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 = 1; extern string ts12 = "Settings for Type 2"; extern double TrailingStop = 20; double myPoint; int LastTicket = 0; int LastTradeType = -1; bool LastTradeChecked = false; bool LastTradeLost = false; int init() { myPoint = SetPoint(); return(0); } double GetPercentBB(int pos) { double LB, UB, PercentBB; LB = iBands(NULL, 0, Bands_period, Bands_deviation, 0, PRICE_CLOSE, MODE_LOWER, pos); UB = iBands(NULL, 0, Bands_period, Bands_deviation, 0, PRICE_CLOSE, MODE_UPPER, pos); PercentBB = (iClose(NULL, 0, pos) - LB)/(UB - LB) * 100; return(PercentBB); } double iLsma(int LSMAPeriod,int shift) { double wt; double ma1=iMA(NULL,0,LSMAPeriod,0,MODE_SMA ,PRICE_CLOSE,shift); double ma2=iMA(NULL,0,LSMAPeriod,0,MODE_LWMA,PRICE_CLOSE,shift); wt = MathFloor((3.0*ma2-2.0*ma1)/myPoint)*myPoint; wt = NormalizeDouble(wt, Digits); return(wt); } //+------------------------------------------------------------------+ //| Check for open order conditions | //| Buy Set up //| JMA 200 sloping upwards (Current value greater than value of 2 bars ago) //| %BB Below 2 //| RSI R2 below 10 //| Sell Set up //| JMA sloping downwards (Current value Less than value of 2 bars ago) //| %BB Greater than 97 //| RSI R2 above 90 //+------------------------------------------------------------------+ int GetSignal() { double jmaCur, jmaPrev; double bbPercent; double rsi; bbPercent = GetPercentBB(1); rsi = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 1); //---- buy conditions if (bbPercent < BB_Low && rsi < RSI_Low) { // jmaCur = iCustom(NULL, 0, "JMA", JMA_Length, JMA_Phase, 0, 1); // jmaPrev = iCustom(NULL, 0, "JMA", JMA_Length, JMA_Phase, 0, 2); jmaCur = iLsma(LSMA_Period, 1); jmaPrev = iLsma(LSMA_Period, 2); if(jmaCur > jmaPrev) { // Print(" BUY Cur : ", jmaCur, " Prev : ", jmaPrev); return (LONG); } } //---- sell conditions if (bbPercent > BB_High && rsi > RSI_High) { // jmaCur = iCustom(NULL, 0, "JMA", JMA_Length, JMA_Phase, 0, 1); // jmaPrev = iCustom(NULL, 0, "JMA", JMA_Length, JMA_Phase, 0, 2); jmaCur = iLsma(LSMA_Period, 1); jmaPrev = iLsma(LSMA_Period, 2); if(jmaCur < jmaPrev) { // Print(" SELL Cur : ", jmaCur, " Prev : ", jmaPrev); return (SHORT); } } return(FLAT); } void CheckForOpen() { int Signal; int NewTicket; Signal = GetSignal(); //---- buy conditions if(Signal == LONG) { NewTicket = OpenTrade(OP_BUY); if (NewTicket > 0) { LastTradeType = OP_BUY; LastTicket = NewTicket; LastTradeChecked = false; } } //---- sell conditions if(Signal == SHORT) { NewTicket = OpenTrade(OP_SELL); if (NewTicket > 0 ) { LastTradeType = OP_SELL; LastTicket = NewTicket; LastTradeChecked = false; } } //---- } //+------------------------------------------------------------------+ //| Check for close order conditions | //| Buy exit When on Close when %BB is Greater than 100 //| Sell Exit When On Close when % BB below 0 //+------------------------------------------------------------------+ void CheckForClose() { double BBpercent; BBpercent = GetPercentBB(1); //---- for(int i=0;i BB_BuyExit) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White); else HandleTrailingStop(OP_BUY, OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit()); } if(OrderType()==OP_SELL) { if(BBpercent < BB_SellExit) OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White); else HandleTrailingStop(OP_SELL, OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit()); } } //---- } //+------------------------------------------------------------------+ //| LastTradeStoppedOut | //| Check History to see if last trade stopped out | //| Return True if closed for loss, false otherwise | //+------------------------------------------------------------------+ bool LastTradeStoppedOut() { bool Stopped = false; OrderSelect (LastTicket, SELECT_BY_TICKET, MODE_HISTORY); if (OrderProfit() < 0) Stopped = true; return (Stopped); } bool OK2Trade() { double bbPercent; if (!LastTradeChecked) { LastTradeLost = LastTradeStoppedOut(); LastTradeChecked = true; } if (LastTradeLost) { bbPercent = GetPercentBB(1); if (LastTradeType == OP_BUY) { if (bbPercent < 50) return(false); } if (LastTradeType == OP_SELL) { if (bbPercent > 50) return(false); } } return(true); } //+------------------------------------------------------------------+ //| Start function | //+------------------------------------------------------------------+ void start() { //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //---- calculate open orders if (!NewBar()) return(0); // wait until first tick after bar close to take any action; if(CalculateCurrentOrders()==0) { if (OK2Trade()) CheckForOpen(); } else CheckForClose(); //---- } bool NewBar() { static datetime dt = 0; if (Time[0] != dt) { dt = Time[0]; return(true); } return(false); } int OpenTrade(int signal) { int res, err, ticket; double TPprice,STprice; RefreshRates(); if (signal==OP_BUY) { res=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,UserComment,MagicNumber,0,Green); if (res > 0) { ticket = res; OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) TPprice=TakeLong(OrderOpenPrice(), TakeProfit); 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); } ModifyOrder(ticket, OrderOpenPrice(), STprice, TPprice, LightGreen); } } } else if (signal==OP_SELL) { res=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,UserComment,MagicNumber,0,Red); if (res > 0) { ticket = res; OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) TPprice=TakeShort(OrderOpenPrice(),TakeProfit); 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); } ModifyOrder(ticket, OrderOpenPrice(), STprice, TPprice, LightGreen); } } } if(res<0) { err = GetLastError(); Print("OrderSend failed with error(" + err + ") " + ErrorDescription(err) + " Lots:" + DoubleToStr(Lots, 2) ); } return(res); } int ModifyOrder(int ord_ticket,double op, double oSL, double oTP, color mColor) { int CloseCnt, err; double myStop, myTake; CloseCnt=0; while (CloseCnt < 3) { if (OrderModify(ord_ticket,op,oSL,oTP,0,mColor)) { CloseCnt = 3; } else { err=GetLastError(); Print(CloseCnt," Error modifying order : (", err , ") " + ErrorDescription(err)); if (err>0) CloseCnt++; } } } //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders() { int buys=0,sells=0; //---- for(int i=0;i0) return(buys); else return(-sells); } 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; if (SL < 0.1) return(SL); minstop = MarketInfo(Symbol(),MODE_STOPLEVEL); if (Digits == 3 || Digits == 5) minstop = minstop / 10; if (type == OP_BUY) { if((price - SL) < minstop*myPoint) SL = price - minstop*myPoint; } if (type == OP_SELL) { if((SL-price) < minstop*myPoint) SL = price + minstop*myPoint; } return(SL); } //+------------------------------------------------------------------+ //| 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) 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) 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) 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) OrderModify(ticket,op,SellStop,tp,0,DarkOrange); return; } } //+------------------------------------------------------------------+