RSI-R2 EA Ver1.1-original

Author: Bluto
Profit factor:
0.32
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself It automatically opens orders when conditions are reached
Indicators Used
Moving average indicatorRelative strength indexParabolic Stop and Reverse system
8 Views
0 Downloads
0 Favorites
RSI-R2 EA Ver1.1-original
//+------------------------------------------------------------------+
//|                                                                  |
//|             RSI-R2.mq4 - Ver 1.1 @ 04/10/2007 by Bluto           |
//|                                                                  |
//| Rev 1.1 - Enhanced Money Mgmt & Optimized RSI Periods Per Pairs  |                                                              |
//+------------------------------------------------------------------+


#property copyright "Bluto"
#property link      "None"

extern double LotSize=10;
extern int    Slippage=3;
extern double StopLoss=0;
extern double TakeProfit=700;
extern double RiskPercent=2.0;
extern bool   UseMoneyMgmt=false;
extern bool   BrokerPermitsFractionalLots = true;
extern int    RSI_Period=4;
extern double RSI_Overbought_Value = 75.0; 
extern double RSI_Oversold_Value = 25.0; 
int           MagicNumber=0;
int           ticket;
int           OpenBuyOrders=0;
int           OpenSellOrders=0;
int           BuyCount=0,SellCount=0;
int           i;
bool          Buy_Mode=false, Sell_Mode=false;
double        RSI_Day_1=0, RSI_Day_2=0, RSI_Day_3=0, SMA200_Day3=0;
double        MM_MinLotSize=0;
double        MM_MaxLotSize=0;
double        MM_LotStep=0;
double        MM_Decimals=0;
double        MM_OrderLotSize=0;
int           MM_AcctLeverage=0;
int           MM_CurrencyLotSize=0;


int init()
{
   if (Symbol()=="AUDCADm" || Symbol()=="AUDCAD") {MagicNumber=200001;RSI_Period=5;}
   if (Symbol()=="AUDJPYm" || Symbol()=="AUDJPY") {MagicNumber=200002;RSI_Period=7;}
   if (Symbol()=="AUDNZDm" || Symbol()=="AUDNZD") {MagicNumber=200003;RSI_Period=6;}
   if (Symbol()=="AUDUSDm" || Symbol()=="AUDUSD") {MagicNumber=200004;RSI_Period=4;}
   if (Symbol()=="CHFJPYm" || Symbol()=="CHFJPY") {MagicNumber=200005;RSI_Period=4;}
   if (Symbol()=="EURAUDm" || Symbol()=="EURAUD") {MagicNumber=200006;RSI_Period=2;}
   if (Symbol()=="EURCADm" || Symbol()=="EURCAD") {MagicNumber=200007;RSI_Period=3;}
   if (Symbol()=="EURCHFm" || Symbol()=="EURCHF") {MagicNumber=200008;RSI_Period=3;}
   if (Symbol()=="EURGBPm" || Symbol()=="EURGBP") {MagicNumber=200009;RSI_Period=2;}
   if (Symbol()=="EURJPYm" || Symbol()=="EURJPY") {MagicNumber=200010;RSI_Period=4;}
   if (Symbol()=="EURUSDm" || Symbol()=="EURUSD") {MagicNumber=200011;RSI_Period=4;}
   if (Symbol()=="GBPCHFm" || Symbol()=="GBPCHF") {MagicNumber=200012;RSI_Period=6;}   
   if (Symbol()=="GBPJPYm" || Symbol()=="GBPJPY") {MagicNumber=200013;RSI_Period=4;}
   if (Symbol()=="GBPUSDm" || Symbol()=="GBPUSD") {MagicNumber=200014;RSI_Period=3;}
   if (Symbol()=="NZDJPYm" || Symbol()=="NZDJPY") {MagicNumber=200015;RSI_Period=6;}
   if (Symbol()=="NZDUSDm" || Symbol()=="NZDUSD") {MagicNumber=200016;RSI_Period=7;}
   if (Symbol()=="USDCHFm" || Symbol()=="USDCHF") {MagicNumber=200017;RSI_Period=4;}
   if (Symbol()=="USDJPYm" || Symbol()=="USDJPY") {MagicNumber=200018;RSI_Period=4;}
   if (Symbol()=="USDCADm" || Symbol()=="USDCAD") {MagicNumber=200019;RSI_Period=2;}
   if (MagicNumber==0) {MagicNumber = 200999;}  
 
 return(0);
}

int deinit()
{
 return(0);
}

int start()
{ 


  Calc_Money_Management();
  
  SMA200_Day3 = iMA(NULL,PERIOD_D1,200, 0, MODE_SMA, PRICE_CLOSE, 1);

  RSI_Day_1 = iRSI(NULL, PERIOD_D1, RSI_Period, PRICE_CLOSE, 3);
  RSI_Day_2 = iRSI(NULL, PERIOD_D1, RSI_Period, PRICE_CLOSE, 2);
  RSI_Day_3 = iRSI(NULL, PERIOD_D1, RSI_Period, PRICE_CLOSE, 1);
  
  if (RSI_Day_1 < 65 && RSI_Day_2 < RSI_Day_1 && RSI_Day_3 < RSI_Day_2 && Close[1] > SMA200_Day3) 
   {
    Buy_Mode=true;
   } else {
    Buy_Mode=false;
   } 
   
   if (RSI_Day_1 > 35 && RSI_Day_2 > RSI_Day_1 && RSI_Day_3 > RSI_Day_2 && Close[1] < SMA200_Day3) 
   {
    Sell_Mode=true;
   } else {
    Sell_Mode=false;
   } 

  if (OpenBuyOrders == 1 && iRSI(NULL, PERIOD_D1, RSI_Period, PRICE_CLOSE, 1) > RSI_Overbought_Value)
   {
     CloseLongs(MagicNumber);
     OpenBuyOrders = 0;
     BuyCount=0;   
   }
   
   if (OpenSellOrders == 1 && iRSI(NULL, PERIOD_D1, RSI_Period, PRICE_CLOSE, 1) < RSI_Oversold_Value)
   {
     CloseShorts(MagicNumber);
     OpenSellOrders = 0;
     SellCount=0;   
   }
     
     
//----- Count number of existing open buy & sell orders; update trailing stops.
  
  OpenBuyOrders=0;
  OpenSellOrders=0;
   
      
// Manage Paraolic SAR 

  for (i = 0; i <= OrdersTotal(); i++)
   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == MagicNumber))
     {
      if (OrderType() == OP_BUY)
       {
        OpenBuyOrders++;
        if ((iSAR(NULL,0,0.02,0.2,1) > OrderStopLoss()) && (Bid > iSAR(NULL,0,0.02,0.2,1)) && (OrderOpenPrice() < iSAR(NULL,0,0.02,0.2,1)) && (iSAR(NULL,0,0.02,0.2,1) > iSAR(NULL,0,0.02,0.2,2)))
         {
          OrderModify(OrderTicket(),OrderOpenPrice(),iSAR(NULL,0,0.02,0.2,1),OrderTakeProfit(),0,Blue);
          Print("Order # ",OrderTicket()," updated at ",Hour(),":",Minute(),":",Seconds());
          return(0);
         }
       }
      if (OrderType() == OP_SELL)
       {
        OpenSellOrders++;
        if ((iSAR(NULL,0,0.02,0.2,1) < OrderStopLoss()) && (Ask < iSAR(NULL,0,0.02,0.2,1)) && (OrderOpenPrice() > iSAR(NULL,0,0.02,0.2,1)) && (iSAR(NULL,0,0.02,0.2,1) < iSAR(NULL,0,0.02,0.2,2)))
         {
          OrderModify(OrderTicket(),OrderOpenPrice(),iSAR(NULL,0,0.02,0.2,1),OrderTakeProfit(),0,Blue);
          Print("Order # ",OrderTicket()," updated at ",Hour(),":",Minute(),":",Seconds());
          return(0);
         }
       }
     }
   }


//----- Generic order handler.
//----- If we have a new buy signal, close existing sell orders; if we have a new sell signal, close existing buy orders; reset order counters.
//----- Next, create new buy or sell order.
         
  if (Buy_Mode==true && OpenBuyOrders==0) 
   {
    if(OpenSellOrders > 0)
     {
      CloseShorts(MagicNumber);
      OpenSellOrders = 0; 
     } 
    if(OpenBuyOrders == 0)
     {  
      ticket = OpenPendingOrder(OP_BUY,MM_OrderLotSize,Ask,Slippage,Bid,StopLoss,TakeProfit,"RSI-R2",MagicNumber,0,Lime);
      if(ticket<0)
       {
        Print("OrderSend failed with error #",GetLastError());
        return(0);
       }
      else
       {
        OpenBuyOrders++;
       }    
     }
   }
   
  if (Sell_Mode==true && OpenSellOrders==0) 
   {
    if(OpenBuyOrders > 0)
     {
      CloseLongs(MagicNumber);
      OpenBuyOrders = 0;
     }  
    if (OpenSellOrders == 0) 
     {
      ticket = OpenPendingOrder(OP_SELL,MM_OrderLotSize,Bid,Slippage,Ask,StopLoss,TakeProfit,"RSI-R2",MagicNumber,0,HotPink);
      if(ticket<0)
       {
        Print("OrderSend failed with error #",GetLastError());
        return(0);
       }
      else
       {
        OpenSellOrders++;
       }    
     }
   }
   
  return(0);
}

//----- Order Processing Functions

void CloseLongs(int MagicNumber)
{
 int trade;
 for(trade=OrdersTotal()-1;trade>=0;trade--)
 {
  if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false)
   continue;

  if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
   continue;
   
  if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
  if(OrderType()==OP_BUY)
   OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);
 }//for
}

void CloseShorts(int MagicNumber)
{
 int trade;
 for(trade=OrdersTotal()-1;trade>=0;trade--)
 {
  if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false)
   continue;

  if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
   continue;
   
  if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
  if(OrderType()==OP_SELL)
   OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
 }//for
}

int OpenPendingOrder(int pType,double pLots,double pLevel,int sp, double pr, int sl, int tp,string pComment,int pMagic,datetime pExpiration,color pColor)
{
  int ticket=0;
  int err=0;
  int c = 0;
  int NumberOfTries = 10;
  switch (pType)
  {
      case OP_BUY:
         for(c = 0 ; c < NumberOfTries ; c++)
         {  
            RefreshRates();
            ticket=OrderSend(Symbol(),OP_BUY,pLots,Ask,sp,StopLong(Bid,sl),TakeLong(Bid,tp),pComment,pMagic,pExpiration,pColor);
            if (ticket > 0) break;
            err=GetLastError();
            if(err==0)
            { 
               break;
            }
            else
            {
               if(err==4 || err==137 ||err==146 || err==136) //Busy errors
               {
                  Sleep(5000);
                  continue;
               }
               else //normal error
               {
                  Print("Error Code= ", err);
                  break;
               }  
            }
         } 
         break;
      case OP_SELL:
         for(c = 0 ; c < NumberOfTries ; c++)
         {
            RefreshRates();
            ticket=OrderSend(Symbol(),OP_SELL,pLots,Bid,sp,StopShort(Ask,sl),TakeShort(Ask,tp),pComment,pMagic,pExpiration,pColor);
            if (ticket > 0) break;
            err=GetLastError();
            if(err==0)
            { 
               break;
            }
            else
            {
               if(err==4 || err==137 ||err==146 || err==136) //Busy errors
               {
                  Sleep(5000);
                  continue;
               }
               else //normal error
               {
                  Print("Error Code= ", err);
                  break;
               }  
            }
         } 
         break;
  } 
  
  return(ticket);
}  

double StopLong(double price,int stop)
{
 if(stop==0)
  return(0);
 else
  return(price-(stop*Point));
}

double StopShort(double price,int stop)
{
 if(stop==0)
  return(0);
 else
  return(price+(stop*Point));
}

double TakeLong(double price,int take)
{
 if(take==0)
  return(0);
 else
  return(price+(take*Point));
}

double TakeShort(double price,int take)
{
 if(take==0)
  return(0);
 else
  return(price-(take*Point));
}


void Calc_Money_Management()
 {
  MM_AcctLeverage = AccountLeverage();
  MM_MinLotSize = MarketInfo(Symbol(),MODE_MINLOT);
  MM_MaxLotSize = MarketInfo(Symbol(),MODE_MAXLOT);
  MM_LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
  MM_CurrencyLotSize = MarketInfo(Symbol(),MODE_LOTSIZE);

  if(MM_LotStep == 0.01) {MM_Decimals = 2;}
  if(MM_LotStep == 0.1) {MM_Decimals = 1;}

  if(BrokerPermitsFractionalLots == true)
   {
    if (UseMoneyMgmt == true)
     {
      MM_OrderLotSize = AccountEquity() * (RiskPercent * 0.01) / (MM_CurrencyLotSize / MM_AcctLeverage);
      MM_OrderLotSize = StrToDouble(DoubleToStr(MM_OrderLotSize,MM_Decimals));
      if (MM_OrderLotSize < MM_MinLotSize) {MM_OrderLotSize = MM_MinLotSize;}
      if (MM_OrderLotSize > MM_MaxLotSize) {MM_OrderLotSize = MM_MaxLotSize;}
     }
    else
     {
      MM_OrderLotSize = LotSize;
      if (MM_OrderLotSize < MM_MinLotSize) {MM_OrderLotSize = MM_MinLotSize;}
      if (MM_OrderLotSize > MM_MaxLotSize) {MM_OrderLotSize = MM_MaxLotSize;}
     }
   }
  else
   {
    if (UseMoneyMgmt == true)
     {
      MM_OrderLotSize = AccountEquity() * (RiskPercent * 0.01) / (MM_CurrencyLotSize / MM_AcctLeverage);
      MM_OrderLotSize = MathRound(MM_OrderLotSize);
      if (MM_OrderLotSize < MM_MinLotSize) {MM_OrderLotSize = MM_MinLotSize;}
      if (MM_OrderLotSize > MM_MaxLotSize) {MM_OrderLotSize = MM_MaxLotSize;}
     }
    else
     {
      MM_OrderLotSize = LotSize;
      if (MM_OrderLotSize < MM_MinLotSize) {MM_OrderLotSize = MM_MinLotSize;}
      if (MM_OrderLotSize > MM_MaxLotSize) {MM_OrderLotSize = MM_MaxLotSize;}
     }
   }   
  return(0);   
 }

Profitability Reports

USD/JPY Jul 2025 - Sep 2025
0.00
Total Trades 7
Won Trades 7
Lost trades 0
Win Rate 100.00 %
Expected payoff 4665.17
Gross Profit 32656.20
Gross Loss 0.00
Total Net Profit 32656.20
-100%
-50%
0%
50%
100%
USD/CHF Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -5011.25
Gross Profit 0.00
Gross Loss -5011.25
Total Net Profit -5011.25
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.16
Total Trades 2
Won Trades 1
Lost trades 1
Win Rate 50.00 %
Expected payoff -3752.82
Gross Profit 1476.27
Gross Loss -8981.91
Total Net Profit -7505.64
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.00
Total Trades 7
Won Trades 7
Lost trades 0
Win Rate 100.00 %
Expected payoff 1491.43
Gross Profit 10440.00
Gross Loss 0.00
Total Net Profit 10440.00
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
0.05
Total Trades 2
Won Trades 1
Lost trades 1
Win Rate 50.00 %
Expected payoff -3130.00
Gross Profit 330.00
Gross Loss -6590.00
Total Net Profit -6260.00
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -6256.55
Gross Profit 0.00
Gross Loss -6256.55
Total Net Profit -6256.55
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.41
Total Trades 2
Won Trades 1
Lost trades 1
Win Rate 50.00 %
Expected payoff -3131.08
Gross Profit 4328.75
Gross Loss -10590.91
Total Net Profit -6262.16
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.00
Total Trades 1
Won Trades 0
Lost trades 1
Win Rate 0.00 %
Expected payoff -3010.00
Gross Profit 0.00
Gross Loss -3010.00
Total Net Profit -3010.00
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.00
Total Trades 5
Won Trades 5
Lost trades 0
Win Rate 100.00 %
Expected payoff 2048.00
Gross Profit 10240.00
Gross Loss 0.00
Total Net Profit 10240.00
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
0.65
Total Trades 5
Won Trades 4
Lost trades 1
Win Rate 80.00 %
Expected payoff -1501.25
Gross Profit 14203.05
Gross Loss -21709.31
Total Net Profit -7506.26
-100%
-50%
0%
50%
100%

Comments