Mq5 RSI BB

 avatar
EBTURK
plain_text
a year ago
6.5 kB
20
Indexable
Never
//+------------------------------------------------------------------+
//|                                             Indicator: RSIBB.mq5 |
//+------------------------------------------------------------------+
#property copyright "EBTURK"
#property link      "https://www.ebturk.com"
#property version   "1.00"
#property description "BB+RSI"

//--- indicator settings ---//
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 4

#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1 3
#property indicator_color1 0xD4D4D4
#property indicator_label1 "BB"

#property indicator_type2 DRAW_LINE
#property indicator_style2 STYLE_SOLID
#property indicator_width2 3
#property indicator_color2 0xD4D4D4
#property indicator_label2 "BB"

#property indicator_type3 DRAW_LINE
#property indicator_style3 STYLE_SOLID
#property indicator_width3 3
#property indicator_color3 0x1111F5
#property indicator_label3 "Sell"

#property indicator_type4 DRAW_LINE
#property indicator_style4 STYLE_SOLID
#property indicator_width4 3
#property indicator_color4 0x4EF511
#property indicator_label4 "Buy"

//--- indicator buffers ---//
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];

input int BB_period = 20;
input int BB_deviatons = 2;
input int RSI_period = 14;
input int RSI_Over_B = 70;
input int RSI_Over_S = 30;
double myPoint;                              //initialized in OnInit
int Bands_handle;
double Bands_Lower[];
double Bands_Upper[];
int RSI_handle;
double RSI[];

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | RSIBB @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   SetIndexBuffer(0, Buffer1);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   SetIndexBuffer(1, Buffer2);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   SetIndexBuffer(2, Buffer3);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   SetIndexBuffer(3, Buffer4);
   PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   Bands_handle = iBands(NULL, PERIOD_CURRENT, BB_period, 0, BB_deviatons, PRICE_CLOSE);
   if(Bands_handle < 0)
     {
      Print("The creation of iBands has failed: Bands_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   RSI_handle = iRSI(NULL, PERIOD_CURRENT, RSI_period, PRICE_CLOSE);
   if(RSI_handle < 0)
     {
      Print("The creation of iRSI has failed: RSI_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total ---//
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   ArraySetAsSeries(Buffer3, true);
   ArraySetAsSeries(Buffer4, true);
   //--- initial zero ---//
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
      ArrayInitialize(Buffer3, EMPTY_VALUE);
      ArrayInitialize(Buffer4, EMPTY_VALUE);
     }
   else
      limit++;
   
   if(BarsCalculated(Bands_handle) <= 0) 
      return(0);
   if(CopyBuffer(Bands_handle, LOWER_BAND, 0, rates_total, Bands_Lower) <= 0) return(rates_total);
   ArraySetAsSeries(Bands_Lower, true);
   if(BarsCalculated(Bands_handle) <= 0) 
      return(0);
   if(CopyBuffer(Bands_handle, UPPER_BAND, 0, rates_total, Bands_Upper) <= 0) return(rates_total);
   ArraySetAsSeries(Bands_Upper, true);
   if(BarsCalculated(RSI_handle) <= 0) 
      return(0);
   if(CopyBuffer(RSI_handle, 0, 0, rates_total, RSI) <= 0) return(rates_total);
   ArraySetAsSeries(RSI, true);
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(true                                //no conditions!
      )
        {
         Buffer1[i] = Bands_Lower[i];        //Set indicator value at Bollinger Bands
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(true //no conditions!
      )
        {
         Buffer2[i] = Bands_Upper[i];        //Set indicator value at Bollinger Bands
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 3
      if(RSI[i] > RSI_Over_B                 //Relative Strength Index > fixed value
      )
        {
         Buffer3[i] = Bands_Upper[i];        //Set indicator value at Bollinger Bands
        }
      else
        {
         Buffer3[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 4
      if(RSI[i] < RSI_Over_S                 //Relative Strength Index < fixed value
      )
        {
         Buffer4[i] = Bands_Lower[i];        //Set indicator value at Bollinger Bands
        }
      else
        {
         Buffer4[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+//