ORB Range Indicator

Author: Copyright 2017, Mudrakov Maksim
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
0 Views
0 Downloads
0 Favorites
ORB Range Indicator
//+------------------------------------------------------------------+
//|                                          ORB Range Indicator.mq4 |
//|                                  Copyright 2017, Mudrakov Maksim |
//|                           https://www.mql5.com/ru/users/vmagroup |
//|                                                                  |
//|     Created for Customer:                                        |
//|     https://www.mql5.com/ru/users/ac24365                        |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Mudrakov Maksim"
#property link      "https://www.mql5.com/ru/users/vmagroup"
#property version   "1.00"
#property strict
#define NameIndi "ORBRangeIndi"

#property indicator_chart_window
#property indicator_buffers    4

double ExtBOHigh[],ExtBOLow[],ExtORBHigh[],ExtORBLow[];

datetime expirationTime=D'01.09.2030 12:00';
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

input int               HourStartDay   = 8;                    //Start hours
input int               MinStartDay    = 45;                   //Start minutes
input int               BO_bars        = 3;                    //Number of bars to detect ORB
input color             Text_col       = clrGray;              //Text color               
input bool              Rect_show      = true;                 //Rectangle zone show
input color             Rect_col       = clrBlue;              //Rectangle color      
input bool              BO_show        = true;                 //Show BO lines
input color             BO_highCol     = clrPink;              //Color BO high line
input color             BO_lowCol      = clrPink;              //Color BO low line
input int               BO_width       = 1;                    //Width BO lines
input ENUM_LINE_STYLE   BO_style       = STYLE_DOT;            //Style BO lines
input bool              ORB_show       = true;                 //Show ORB lines
input color             ORB_highCol    = clrGreen;             //Color ORB high line
input color             ORB_lowCol     = clrRed;               //Color ORB low line
input int               ORB_width      = 2;                    //Width ORB lines
input ENUM_LINE_STYLE   ORB_style      = STYLE_SOLID;          //Style ORB lines
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(TimeCurrent()>expirationTime)
     {
      Alert("The time of using the Expert Advisor expired !");
      return(INIT_FAILED);
     }

   ObjectsDeleteAll(0,NameIndi,-1,-1);
   Comment("");
   EventSetMillisecondTimer(100);

   SetIndexBuffer(0,ExtBOHigh);
   SetIndexBuffer(1,ExtBOLow);
   SetIndexBuffer(2,ExtORBHigh);
   SetIndexBuffer(3,ExtORBLow);

   ArraySetAsSeries(ExtBOHigh,true);
   ArraySetAsSeries(ExtBOLow,true);
   ArraySetAsSeries(ExtORBHigh,true);
   ArraySetAsSeries(ExtORBLow,true);

   SetIndexStyle(0,DRAW_LINE,BO_style,BO_width,BO_highCol);
   SetIndexStyle(1,DRAW_LINE,BO_style,BO_width,BO_lowCol);
   SetIndexStyle(2,DRAW_LINE,ORB_style,ORB_width,ORB_highCol);
   SetIndexStyle(3,DRAW_LINE,ORB_style,ORB_width,ORB_lowCol);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
   ObjectsDeleteAll(0,NameIndi,-1,-1);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
bool histCalc=true;
bool newCalc=false;
int iteratorRect=0;
int indexORBhigh=-1, indexORBlow=-1;
datetime endORB=0,timeText=0;
double pointText;
string textVal,textPerc;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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 startCalc=1;
   if(histCalc) startCalc=Bars-2;
   if(startCalc>rates_total)startCalc=rates_total;
   for(int i=startCalc; i>0; i--)
     {
      histCalc=false;
      int dayStartIndex=iBarShift(Symbol(),PERIOD_D1,Time[i],false);
      datetime dayStart=iTime(Symbol(),PERIOD_D1,dayStartIndex);
      datetime startORB=dayStart+HourStartDay*3600+MinStartDay*60;
      endORB=startORB+PeriodSeconds(0)*(BO_bars-1);
      if(Time[i]<endORB && Time[i]>startORB && endORB!=0)newCalc=true;
      if(Time[i]>endORB && newCalc)
        {
         newCalc=false;

         int indexBar=iBarShift(Symbol(),0,endORB,false);
         indexORBlow=iLowest(Symbol(),0,MODE_LOW,BO_bars,indexBar);
         indexORBhigh=iHighest(Symbol(),0,MODE_HIGH,BO_bars,indexBar);
         int indexHighestOpen=iHighest(Symbol(),0,MODE_OPEN,BO_bars,indexBar);
         int indexHighestClose=iHighest(Symbol(),0,MODE_CLOSE,BO_bars,indexBar);
         int indexLowestOpen=iLowest(Symbol(),0,MODE_OPEN,BO_bars,indexBar);
         int indexLowestClose=iLowest(Symbol(),0,MODE_CLOSE,BO_bars,indexBar);
         double priceBO_h=Open[indexHighestOpen];
         if(Close[indexHighestClose]>priceBO_h)priceBO_h=Close[indexHighestClose];
         double priceBO_l=Open[indexLowestOpen];
         if(Close[indexLowestClose]<priceBO_l)priceBO_l=Close[indexLowestClose];

         timeText=Time[indexORBhigh];
         pointText=High[indexORBhigh];
         textVal=DoubleToStr(High[indexORBhigh]-Low[indexORBlow],Digits);
         textPerc=DoubleToStr(((High[indexORBhigh]-Low[indexORBlow])*100)/Low[indexORBlow],2)+" %";

         if(Rect_show)RectangleCreate(NameIndi+"_rect_"+(string)iteratorRect,startORB,endORB,Low[indexORBlow],High[indexORBhigh],Rect_col,true,true,1);
         iteratorRect++;

         for(int j=i; j<i+BO_bars+1; j++)
           {
            if(BO_show)
              {
               ExtBOHigh[j]=priceBO_h;
               ExtBOLow[j]=priceBO_l;
              }
            if(ORB_show)
              {
               ExtORBHigh[j]=High[indexORBhigh];
               ExtORBLow[j]=Low[indexORBlow];
              }
           }
        }
      else
        {
         ExtBOHigh[i]=ExtBOHigh[i+1];
         ExtBOLow[i]=ExtBOLow[i+1];
         ExtORBHigh[i]=ExtORBHigh[i+1];
         ExtORBLow[i]=ExtORBLow[i+1];
        }
     }

   if(BO_show)
     {
      HLineCreate(NameIndi+"bo_h",ExtBOHigh[1],BO_highCol,BO_style,BO_width);
      HLineCreate(NameIndi+"bo_l",ExtBOLow[1],BO_lowCol,BO_style,BO_width);
     }
   if(ORB_show)
     {
      HLineCreate(NameIndi+"orb_h",ExtORBHigh[1],ORB_highCol,ORB_style,ORB_width);
      HLineCreate(NameIndi+"orb_l",ExtORBLow[1],ORB_lowCol,ORB_style,ORB_width);
     }
   return(rates_total);
  }

//--------------------------------------------------------------------+
//      Horisontal Line Create                                        |
//--------------------------------------------------------------------+
void HLineCreate(const string          name="HLine",
                 double                price=0,
                 const color           clr=clrRed,
                 const ENUM_LINE_STYLE style=STYLE_SOLID,
                 const int             width=1)
  {

   if(ObjectFind(0,name)!=-1)
     {
      if(!ObjectMove(0,name,0,0,price))
        {
         Print(__FUNCTION__," ",GetLastError());
         return;
        }
     }
   else
     {
      if(!ObjectCreate(0,name,OBJ_HLINE,0,0,price))
        {
         Print(__FUNCTION__," ",GetLastError());
         return;
        }
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,name,OBJPROP_STYLE,style);
      ObjectSetInteger(0,name,OBJPROP_WIDTH,width);
      ObjectSetInteger(0,name,OBJPROP_BACK,false);
      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
      ObjectSetInteger(0,name,OBJPROP_SELECTED,false);
      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
      ObjectSetInteger(0,name,OBJPROP_ZORDER,0);
     }
  }
//--------------------------------------------------------------------+
//      Create Rectangle                                              |
//--------------------------------------------------------------------+
void RectangleCreate(const string          name="Rectangle",
                     datetime              time1=0,
                     datetime              time2=0,
                     double                price1=0,
                     double                price2=0,
                     const color           clr=clrRed,
                     bool                  back=false,
                     bool                  fill=false,
                     int                   width=3)
  {

   if(ObjectFind(0,name)!=-1)
     {
      ObjectMove(0,name,0,time1,price1);
      ObjectMove(0,name,1,time2,price2);
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
     }
   else
     {
      if(!ObjectCreate(0,name,OBJ_RECTANGLE,0,time1,price1,time2,price2))
        {
         Print(__FUNCTION__," ",GetLastError());
         return;
        }
      ObjectSetInteger(0,name,OBJPROP_FILL,fill);
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_SOLID);
      ObjectSetInteger(0,name,OBJPROP_WIDTH,width);
      ObjectSetInteger(0,name,OBJPROP_BACK,back);
      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
      ObjectSetInteger(0,name,OBJPROP_SELECTED,false);
      ObjectSetInteger(0,name,OBJPROP_HIDDEN,false);
      ObjectSetInteger(0,name,OBJPROP_ZORDER,0);
     }
  }

Comments