//============================================================================= // RSI_EA.mq4 // Originally by: Robert Hill // //============================================================================= #property copyright "Robert Hill" #include #include #define LONG 1 #define SHORT -1 #define FLAT 0 extern bool AllowNewTrades = true; extern int MagicCode = 5; //+---------------------------------------------------+ //|Indicator inputs | //+---------------------------------------------------+ extern string rs1 = "RSI settings"; extern int RSI_Period = 14; extern int RSI_LowerLevel = 20; extern int RSI_UpperLevel = 80; extern bool ExitAtSignal = true; extern string st1 = "Stochastic settings"; extern int UseStochastic = 1; extern int K_Period = 5; extern int D_Period = 3; extern int Slowing = 3; extern string st2 = "-- Stoch Levels --"; extern int OverSoldLevel = 20; extern int OverBoughtLevel = 80; extern string ma1 = "Moving Average settings"; extern int UseMA = 1; extern int SMMA_Period = 45; // Periodo da media Base //---- Trade Management extern double TakeProfit = 80; extern double StopLoss = 50; extern string ts ="---TrailingStop Settings---"; extern string ts0 = "---TrailingStopLoss---"; extern string ts1 = " 1. Trail immediately"; extern string ts2 = " 2. Standard at input"; extern bool UseTrailingStop = true; extern int TrailingStopType = 2; extern double TrailingStop = 25; //+---------------------------------------------------+ //|Money Management | //+---------------------------------------------------+ extern bool UseMoneyManagement = true; // Change to false if you want to shutdown money management controls. extern bool BrokerIsIBFX = false; extern string m1="Set mini and micro to false for standard account"; extern bool AccountIsMini = true; extern bool AccountIsMicro = false; extern double TradeSizePercent = 2; // Change to whatever percent of equity you wish to risk. extern double Lots = 1; double MaxLots = 100.0; extern int Slippage = 3; extern int SignalCandle = 1; double lotMM; string ExpertName="RSI_EA_"; int MagicNumber; // Magic number of the trades. must be unique to identify string nameEA; // identifies the expert int TradesInThisSymbol; bool YesStop; double myPoint; int totalTries = 5; int retryDelay = 1000; //============================================================================= // expert initialization function //============================================================================= int init() { MagicNumber = MagicCode*1000 + func_Symbol2Val(Symbol())*100 + func_TimeFrame_Const2Val(Period()); nameEA = ExpertName + Symbol() + "_" + func_TimeFrame_Val2String(func_TimeFrame_Const2Val(Period())); myPoint = SetPoint(); return(0); } //============================================================================= // expert deinitialization function //============================================================================= int deinit() { return(0); } //============================================================================= // // GetSignal() // // Function to tell whether or not there is a trade to place. // // RETURN VALUE: // // LONG : If the rules are met to place a long trade // // SHORT : If the rules are met to place a short trade // // FLAT : If the rules are not met // //============================================================================= int GetSignal() { int Stoch_Signal, RSI_Signal; double MA_Signal; MA_Signal = iMA(NULL,0,SMMA_Period,0,MODE_SMMA,PRICE_CLOSE,1); RSI_Signal = GetRSI_Signal(); if (RSI_Signal == FLAT) return(FLAT); if (UseStochastic == 1) { Stoch_Signal = getStoch_Signal(); if (Stoch_Signal == FLAT) return(FLAT); } if (RSI_Signal == LONG) { if (UseStochastic == 1) { if (Stoch_Signal == SHORT) return(FLAT); } if (UseMA == 1) { if (Close[0] <= MA_Signal) return(FLAT); } return(LONG); } if (RSI_Signal == SHORT) { if (UseStochastic == 1) { if (Stoch_Signal == LONG) return(FLAT); } if (UseMA == 1) { if (Close[0] >= MA_Signal) return(FLAT); } return(SHORT); } return (FLAT); } int GetRSI_Signal() { double myRSI, myRSIp; myRSI = iRSI(Symbol(),0,RSI_Period,PRICE_CLOSE,SignalCandle); if (myRSI < RSI_LowerLevel) return(LONG); if (myRSI > RSI_UpperLevel) return(SHORT); // down return (FLAT); // has not changed } int getStoch_Signal() { double stoch; stoch = iStochastic(Symbol(), 0, K_Period, D_Period, Slowing, MODE_SMA, 0, MODE_MAIN, SignalCandle); if ( stoch < OverSoldLevel) { return(LONG); } if (stoch > OverBoughtLevel) { return(SHORT); } return(FLAT); } //============================================================================= // // CheckExitSignals() // // //============================================================================= bool CheckExitSignals(int cmd) { double myRSI, myRSIp; if (ExitAtSignal == false) return(false); myRSI = iRSI(Symbol(),0,RSI_Period,PRICE_CLOSE,SignalCandle); myRSIp = iRSI(Symbol(),0,RSI_Period,PRICE_CLOSE,SignalCandle+1); if (cmd == OP_BUY) { // Check overbought and turning down if (myRSI > RSI_UpperLevel && myRSI < myRSIp) return(true); } if (cmd == OP_SELL) { // Check oversold and turning up if (myRSI < RSI_LowerLevel && myRSI > myRSIp) return(true); } return (false); // has not changed } //============================================================================= // expert start function //============================================================================= int start() { int total, PlaceTrade; total = CheckOpenTrades(); if (total == 0) { if (AllowNewTrades == true) { PlaceTrade = GetSignal(); if (PlaceTrade != FLAT) OpenTrade(PlaceTrade); } } else { RefreshRates(); HandleOpenPositions(); } return(0); } void OpenTrade( int signal) { int res, err; double TPprice,STprice; int ticket; RefreshRates(); lotMM = GetLots(); if (signal == LONG) { res = OrderSend(Symbol(), OP_BUY, lotMM, Ask, Slippage, 0, 0, nameEA, MagicNumber, 0, Green); if (res > 0) { ticket = res; if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("BUY order opened : ", OrderOpenPrice()); 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); } } } if (signal == SHORT) { res = OrderSend(Symbol(), OP_SELL, lotMM, Bid, Slippage, 0, 0, nameEA, MagicNumber, 0, Red); if (res > 0) { ticket = res; if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) { Print("SELL order opened : ", OrderOpenPrice()); 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)); } } void HandleOpenPositions() { int cnt, err, total; bool CloseTrade, result = false; total = OrdersTotal(); for (cnt=0; cnt < total; cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; result = false; // Should it be closed because of a reverse signal? CloseTrade = CheckExitSignals(OrderType()); // We have a long position open if (OrderType() == OP_BUY) { if (CloseTrade == true) { result = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet); if (!result) { err = GetLastError(); Print("OrderClose BUY failed with error(" + err + ") " + ErrorDescription(err)); Alert("Exit of BUY FAILED."); } } } // We have a short position open if (OrderType() == OP_SELL) { if (CloseTrade == true) { result = OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet); if (!result) { err = GetLastError(); Print("OrderClose SELL failed with error(" + err + ") " + ErrorDescription(err)); Alert("Exit of SELL FAILED."); } } } if (!result && UseTrailingStop) // Handle mods to trailing stop HandleTrailingStop(OrderType(), OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit()); } return(0); } //============================================================================= // // HandleTrailingStop() // // Type 1 moves the stoploss without delay. // // Type 2 waits for price to move the amount of the trailStop // before moving stop loss then moves like type 1 // // PARAMETERS: // // type: OP_BUY or OP_SELL // ticket: the ticket number // open_price: the order's open price // cur_sl: the order's current StopLoss value // cur_tp: the order's current TakeProfit value // // RETURN VALUE: // zero for now // // Calling example // HandleTrailingStop(OP_BUY,OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit()); // //============================================================================= void HandleTrailingStop(int type, int ticket, double open_price, double cur_sl, double cur_tp) { double pt, TS = 0; if (type == OP_BUY) { switch (TrailingStopType) { case 1: pt = myPoint * StopLoss; if (Bid - cur_sl > pt) ModifyOrder(ticket, open_price, Bid - pt, cur_tp, Aqua); break; case 2: pt = myPoint * TrailingStop; if (Bid - open_price > pt && (cur_sl < Bid - pt || cur_sl == 0)) ModifyOrder(ticket, open_price, Bid - pt, cur_tp, Aqua); break; } } if (type == OP_SELL) { switch (TrailingStopType) { case 1: pt = myPoint * StopLoss; if (cur_sl - Ask > pt) ModifyOrder(ticket, open_price, Ask+pt, cur_tp, Aqua); break; case 2: pt = myPoint * TrailingStop; if (open_price - Ask > pt && (cur_sl > Ask + pt || cur_sl == 0)) ModifyOrder(ticket, open_price, Ask+pt, cur_tp, Aqua); break; } } } 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++; } } } //============================================================================= // // CheckOpenTrades() // // RETURN VALUE: // // The number of trades this EA has currently open // //============================================================================= int CheckOpenTrades() { int cnt; int NumTrades; // Number of buy and sell trades in this symbol NumTrades = 0; for (cnt=OrdersTotal()-1; cnt>=0; cnt--) { OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; if (OrderType() == OP_BUY) NumTrades++; if (OrderType() == OP_SELL) NumTrades++; } return (NumTrades); } //+------------------------------------------------------------------+ //| Get number of lots for this trade | //+------------------------------------------------------------------+ double GetLots() { double lot; double myMaxLot = MarketInfo(Symbol(), MODE_MAXLOT); if(UseMoneyManagement == false) return(Lots); if (BrokerIsIBFX == true) { lot = Calc_IBFX_Money_Management(); return(lot); } lot=NormalizeDouble((AccountEquity()*TradeSizePercent/10000)/10,2); // 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); } if (lot >= 1.0) lot = MathFloor(lot); else lot = 1.0; if (lot > MaxLots) lot = MaxLots; return(lot); } double Calc_IBFX_Money_Management() { // variables used for money management double lot; double myMaxLot = MarketInfo(Symbol(), MODE_MAXLOT); lot=NormalizeDouble((AccountEquity()*TradeSizePercent/10000)/10,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 lot = MathFloor(lot); if (lot < 1) lot = 1; if (lot > myMaxLot) lot = myMaxLot; return(lot); } 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 (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); } //+------------------------------------------------------------------+ //| 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); } } //+------------------------------------------------------------------+ //| Time frame string appropriation function | //+------------------------------------------------------------------+ string func_TimeFrame_Val2String(int Value ) { switch(Value) { case 1: // M1 return("M1"); case 2: // M1 return("M5"); case 3: return("M15"); case 4: return("M30"); case 5: return("H1"); case 6: return("H4"); case 7: return("D1"); case 8: return("W1"); case 9: return("MN1"); default: return("undefined " + Value); } } 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=="NZDUSD") return(15); if(mySymbol=="USDCAD") return(16); if(mySymbol=="USDCHF") return(17); if(mySymbol=="USDJPY") return(18); return(19); }