Download MetaTrader 5
To add comments, please log in or register
What are the latest MQL5.community news? The Wall section will tell you!
Daniel Petrovai
440
Daniel Petrovai 2014.03.04 02:22 
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "MT4"
#include <stdlib.mqh>
#define R3_NAME "Daily R3"
#define R2_NAME "Daily R2"
#define R1_NAME "Daily R1"
#define PIVOT_NAME "Daily PP"
#define S1_NAME "Daily S1"
#define S2_NAME "Daily S2"
#define S3_NAME "Daily S3"
//
#property indicator_chart_window
#property indicator_buffers 7

//---
input int               Daily_or_Fibonacci_Pivots_12=1;
input string            InpFont="Arial";           // Font
input int               InpFontSize=10;            // Font size
input color             InpColor1=clrRed;          // Color
input color             InpColor2=clrWhite;        // Color
input color             InpColor3=clrYellow;       // Color
input double            InpAngle=0.0;              // Slope angle in degrees
input ENUM_ANCHOR_POINT InpAnchor=ANCHOR_BOTTOM;   // Anchor type
input bool              InpBack=false;             // Background object
input bool              InpSelection=false;        // Highlight to move
input bool              InpHidden=false;           // Hidden in the object list
input long              InpZOrder=0;               // Priority for mouse click
input int               InpLineStyle=0;            // Line style
input int               InpLineWidth=1;            // Input(s)
input int               Shift_Hrs=0;               // Pivot day shift
input int               ChartShift  = 20;
input int               LabelShift  =7;

color InpColor;
// Buffers for levels
double Res3[],Res2[],Res1[],Pivot[],Sup1[],Sup2[],Sup3[];
double pivotArray[8];
string nameArray[]={"Daily R3","Daily R2","Daily R1","Daily PP","Daily S1","Daily S2","Daily S3"};
double PDayHigh,PDayLow;
datetime PivotDayStartTime,objectTextTime,prevTime;
int DayStartBar,LeftMostBar,RightMostBar,ShiftHrs;
bool flag=false;
//--------------------------------------------------------------------
// Initialization
//--------------------------------------------------------------------
int init()
  {
// Attach indicator arrays to buffers
   SetIndexBuffer(0,Res3);
   SetIndexBuffer(1,Res2);
   SetIndexBuffer(2,Res1);
   SetIndexBuffer(3,Pivot);
   SetIndexBuffer(4,Sup1);
   SetIndexBuffer(5,Sup2);
   SetIndexBuffer(6,Sup3);

// Set styles
   SetIndexStyle(0,DRAW_LINE,InpLineStyle,InpLineWidth,InpColor1);
   SetIndexStyle(1,DRAW_LINE,InpLineStyle,InpLineWidth,InpColor1);
   SetIndexStyle(2,DRAW_LINE,InpLineStyle,InpLineWidth,InpColor1);
   SetIndexStyle(3,DRAW_LINE,InpLineStyle,InpLineWidth,InpColor2);
   SetIndexStyle(4,DRAW_LINE,InpLineStyle,InpLineWidth,InpColor3);
   SetIndexStyle(5,DRAW_LINE,InpLineStyle,InpLineWidth,InpColor3);
   SetIndexStyle(6,DRAW_LINE,InpLineStyle,InpLineWidth,InpColor3);

// Set empty values
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexEmptyValue(2,EMPTY_VALUE);
   SetIndexEmptyValue(3,EMPTY_VALUE);
   SetIndexEmptyValue(4,EMPTY_VALUE);
   SetIndexEmptyValue(5,EMPTY_VALUE);
   SetIndexEmptyValue(6,EMPTY_VALUE);

// Catch bad input
   ShiftHrs=Shift_Hrs;
   if(MathAbs(ShiftHrs)>23)
     {
      Alert("ShiftHrs is too large. Reset to 0 hrs. ");
      ShiftHrs=0;
     }
   PivotDayStartTime=Time[Bars-1];         // the first bar
   return(0);
  }
//--------------------------------------------------------------------
//| De-initialization                                                |
//--------------------------------------------------------------------
int deinit()
  {
// Remove texts
   DeleteLabels();
   prevTime=0;
   return(0);
  }
//--------------------------------------------------------------------
//| Main iteration                                                   |
//--------------------------------------------------------------------
int start()
  {
   int i,Count;
   double Range;
   if(ChartShiftSizeGet()<ChartShift)ChartShiftSizeSet(ChartShift);

   i=Bars-IndicatorCounted()-1;

   while(i>=0)
     {
      // If the pivot day changes...
      if(PivotDay(Time[i+1],ShiftHrs)!=PivotDay(Time[i],ShiftHrs))// what day of the week is it ?
        {
         // Determine High & Low for the previous Pivot Day
         Count = iBarShift( NULL, 0, PivotDayStartTime ) - i;           // number of bars in the day
         PDayHigh = High[ iHighest( NULL, 0, MODE_HIGH, Count, i+1 ) ]; // Pivot Day high
         PDayLow = Low[ iLowest( NULL, 0, MODE_LOW, Count, i+1 ) ];     // Pivot Day low

                                                                        // Pivot calculations
         if(Daily_or_Fibonacci_Pivots_12==1)
           {
            Pivot[i]=(PDayHigh+PDayLow+Close[i+1])/3;    // Pivot point
            Range=PDayHigh-PDayLow;
            Res1[i] = 2 * Pivot[i] - PDayLow;                     // R1
            Res2[i] = Pivot[i] + Range;                           // R2
            Res3[i] = Res1[i] + Range;                            // R3
            Sup1[i] = 2 * Pivot[i] - PDayHigh;                    // S1
            Sup2[i] = Pivot[i] - Range;                           // S2
            Sup3[i] = Sup1[i] - Range;                            // S3
            
           }
         else
           {
            Pivot[i]=(PDayHigh+PDayLow+Close[i+1])/3;    // Pivot point
            Range=PDayHigh-PDayLow;
            Res1[i] = Pivot [i]+ (Range  * 0.382);
            Res2[i] = Pivot [i]+ (Range  * 0.618);
            Res3[i] = Pivot [i]+  Range;
            Sup1[i] = Pivot [i]- (Range  * 0.382);
            Sup2[i] = Pivot [i]- (Range  * 0.618);
            Sup3[i] = Pivot [i]-  Range;
            
           }

         // Don't draw the transition between levels
         Res3[i+1] = EMPTY_VALUE;
         Res2[i+1] = EMPTY_VALUE;
         Res1[i+1] = EMPTY_VALUE;
         Pivot[i+1]= EMPTY_VALUE;
         Sup1[i+1] = EMPTY_VALUE;
         Sup2[i+1] = EMPTY_VALUE;
         Sup3[i+1] = EMPTY_VALUE;
         // Remember when the Day changed over
         PivotDayStartTime=Time[i];
        }
      else     // no change to pivot levels
        {
         Res3[i] = Res3[i+1];
         Res2[i] = Res2[i+1];
         Res1[i] = Res1[i+1];
         Pivot[i]= Pivot[i+1];
         Sup1[i] = Sup1[i+1];
         Sup2[i] = Sup2[i+1];
         Sup3[i] = Sup3[i+1];
        }
      //get levels for text objects
      ArrayInitialize(pivotArray,0.0);
      pivotArray[0]=Res3[i];
      pivotArray[1]=Res2[i];
      pivotArray[2]=Res1[i];
      pivotArray[3]=Pivot[i];
      pivotArray[4]=Sup1[i];
      pivotArray[5]=Sup2[i];
      pivotArray[6]=Sup3[i];
      i--;
     }

// Put text on the chart
objectTextTime=Time[0]+(_Period*60*LabelShift);//set shift from Time[0]
   if(CountTextObjects()<7 | prevTime!=Time[0])// if chart time frame has changed or indicator just initialized
     {
     DeleteLabels();
      for(int x=0;x<7;x++)//create  text objects
        {
         if(x==3)InpColor=InpColor2;if(x<3)InpColor=InpColor1;if(x>3)InpColor=InpColor3;// set different colors for text objects
         
         TextCreate(0,nameArray[x],0,objectTextTime,pivotArray[x],nameArray[x],InpFont,InpFontSize,
                    InpColor,InpAngle,InpAnchor,InpBack,InpSelection,InpHidden,InpZOrder);
        }
     prevTime=Time[0];
     }

//---
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CountTextObjects()
  {
   int count=0;
   for(int a=0;a<7;a++)
     {
      if(ObjectFind(0,nameArray[a])<0)continue;
      count++;
     }
   return(count);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteLabels()
  {
   for(int a=0;a<7;a++)
     {
      if(ObjectFind(0,nameArray[a])<0)continue;
      if(TextDelete(0,nameArray[a])==true)Print("1_Deleting "+nameArray[a]);
     }

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int PivotDay(datetime barTime,datetime shiftHrs)
  {
   int PDay=TimeDayOfWeek(barTime+shiftHrs*3600);

   if( PDay == 0 ) PDay = 1;      // Count Sunday as Monday
   if( PDay == 6 ) PDay = 5;      // Count Saturday as Friday

   return( PDay );
  }
//+---------------------------------------------------------------------------+
//| The function receives shift size of the zero bar from the right border    |
//| of the chart in percentage values (from 10% up to 50%).                   |
//+---------------------------------------------------------------------------+
double ChartShiftSizeGet(const long chart_ID=0)
  {
//--- prepare the variable to get the result
   double result=EMPTY_VALUE;
//--- reset the error value
   ResetLastError();
//--- receive the property value
   if(!ChartGetDouble(chart_ID,CHART_SHIFT_SIZE,0,result))
     {
      //--- display the error message in Experts journal
      Print(__FUNCTION__+", Error Code = ",ErrorDescription(GetLastError()));
     }
//--- return the value of the chart property
   return(result);
  }
//+--------------------------------------------------------------------------------------+
//| The function sets the shift size of the zero bar from the right                      |
//| border of the chart in percentage values (from 10% up to 50%). To enable the shift   |
//| mode, CHART_SHIFT property value should be set to                                    |
//| true.                                                                                |
//+--------------------------------------------------------------------------------------+
bool ChartShiftSizeSet(const double value,const long chart_ID=0)
  {
//--- reset the error value
   ResetLastError();
//--- set property value
   if(!ChartSetDouble(chart_ID,CHART_SHIFT_SIZE,value))
     {
      //--- display the error message in Experts journal
      Print(__FUNCTION__+", Error Code = ",ErrorDescription(GetLastError()));
      return(false);
     }
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Creating Text object                                             |
//+------------------------------------------------------------------+
bool TextCreate(const long              chart_ID=0,               // chart's ID
                const string            name="Text",              // object name
                const int               sub_window=0,             // subwindow index
                datetime                time=0,                   // anchor point time
                double                  price=0,                  // anchor point price
                const string            text="Text",              // the text itself
                const string            font="Arial",             // font
                const int               font_size=10,             // font size
                const color             clr=clrRed,               // color
                const double            angle=0.0,                // text slope
                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
                const bool              back=false,               // in the background
                const bool              selection=false,          // highlight to move
                const bool              hidden=true,              // hidden in the object list
                const long              z_order=0)                // priority for mouse click
  {
//--- set anchor point coordinates if they are not set
   ChangeTextEmptyPoint(time,price);
//--- reset the error value
   ResetLastError();
//--- create Text object
   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))
     {
      Print(__FUNCTION__,
            ": failed to create "+name+" object! Error code = ",ErrorDescription(GetLastError()));
      return(false);
     }
//--- set the text
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set the slope angle of the text
   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- set anchor type
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- set color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the object by mouse
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Check anchor point values and set default values                 |
//| for empty ones                                                   |
//+------------------------------------------------------------------+
void ChangeTextEmptyPoint(datetime &time,double &price)
  {
//--- if the point's time is not set, it will be on the current bar
   if(!time)
      time=TimeCurrent();
//--- if the point's price is not set, it will have Bid value
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  }
//+------------------------------------------------------------------+
//| Delete Text object                                               |
//+------------------------------------------------------------------+
bool TextDelete(const long   chart_ID=0,  // chart's ID
                const string name="Text") // object name
  {
//--- reset the error value
   ResetLastError();
//--- delete the object
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": failed to delete "+name+" object! Error code = ",ErrorDescription(GetLastError()));
      return(false);
     }
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
28701
Alain Verleyen 2014.04.26 10:44  
thrdel:


Why are you posting this code here ? Any problem ?
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.04.26 10:55  
Maybe the labels gone wrong and shifted to the right ?
Daniel Petrovai
440
Daniel Petrovai 2014.04.26 11:39  
angevoyageur:
Why are you posting this code here ? Any problem ?

No problem, uploaded in the wrong place and deleted the file instead of the post. All good.
/
To add comments, please log in or register