Navigation:Home > Content >

Pivot_Daily_Zone.mq4

Time: 2013-09-04 | Download file:Pivot_Daily_Zone.mq4

//+------------------------------------------------------------------+
//|                                             Pivot Daily Zone.mq4 |
//|                                      Copyright © 2006, Eli Hayun |
//|                                          http://www.elihayun.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Eli Hayun"
#property link      "http://www.elihayun.com"

#property indicator_chart_window

#define PIVOT_LINE  "Pivot line"
#define R1          "R1"
#define R2          "R2"
#define R3          "R3"
#define S1          "S1"
#define S2          "S2"
#define S3          "S3"


bool ObjectCreated = false;

extern color  PivotColor = DarkSlateGray;
extern color  R3Color    = NavajoWhite;
extern color  R2Color    = LightSalmon;
extern color  R1Color    = DarkSalmon;
extern color  S3Color    = PowderBlue;
extern color  S2Color    = LightBlue;
extern color  S1Color    = SkyBlue;

bool HaveSunday = false;

int PivotTF = PERIOD_D1;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   if (PivotTF >= PERIOD_D1)
      PivotTF = PERIOD_W1;
      
   for (int ii=0; ii<10; ii++)
   {
      if (TimeDayOfWeek(iTime(NULL, PivotTF, ii)) == 0)
         HaveSunday = true; 
   }
   
   CreatePivotZones();
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   
   DeleteObjects();

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
      
      if (NewBar())
      {
         CreatePivotZones();
      }
      
  }
//+------------------------------------------------------------------+

void CreateObj(string objName, double start, double end, color clr)
{
   int fvb = FirstVisibleBar();
   int bpw = BarsPerWindow();
      
   if (start == end) end -= 2 * Point;
   
   ObjectCreate(objName, OBJ_RECTANGLE, 0, iTime(NULL,PERIOD_D1, 0), start, Time[0], end);
   ObjectSet(objName, OBJPROP_COLOR, clr);
      
}
void DeleteObjects()
{
   ObjectDelete(PIVOT_LINE);
   ObjectDelete(R1);
   ObjectDelete(R2);
   ObjectDelete(R3);
   ObjectDelete(S1);
   ObjectDelete(S2);
   ObjectDelete(S3);
   ObjectDelete("Start Day");

}

bool NewBar()
{
   static datetime dt = 0;
   static datetime dtDay = 0;
   if (dtDay != iTime(NULL, PivotTF, 0))
   {
      ObjectDelete("Start Day");
      ObjectCreate("Start Day", OBJ_VLINE, 0, iTime(NULL, PERIOD_D1, 0),0);
      dtDay = iTime(NULL, PivotTF, 0);
   }
   if (dt != Time[0])
   {
      dt = Time[0];
      return(true);
   }
   return(false);
}

void CreatePivotZones()
{
      
      double yOpen  = GetOpen();
      double yClose = GetClose();
      double yHigh  = GetHigh();
      double yLow   = GetLow();
      
      double pivot = (yHigh + yClose + yLow) / 3;
      double r1    = 2 * pivot - yLow;
      double s1    = 2 * pivot - yHigh;
      double r2    = pivot + (r1 - s1);
      double r3    = yHigh + 2 * (pivot - yLow);
      double s2    = pivot - (r1 - s1);
      double s3    = yLow - 2 * (yHigh - pivot);
      
      DeleteObjects();
      CreateObj(PIVOT_LINE, pivot, pivot, PivotColor);
      CreateObj(R1, pivot, r1, R1Color);
      CreateObj(R2, r1, r2, R2Color);
      CreateObj(R3, r2, r3, R3Color);
      CreateObj(S1, pivot, s1, S1Color);
      CreateObj(S2, s1, s2, S2Color);
      CreateObj(S3, s2, s3, S3Color);
}

double GetHigh()
{
   int dow = TimeDayOfWeek(iTime(NULL, PivotTF, 0));
   int dow1 = TimeDayOfWeek(iTime(NULL, PivotTF, 1));

   int sh = 1;
   if ((HaveSunday) && (dow == 1))
      sh = 2; // skip sunday
   
   if (dow == 2) // Teusday
   {
      return ( Highest(NULL, PivotTF, MODE_HIGH, 2,1) ); 
   }
   
   return( iHigh(NULL, PivotTF, sh) );
   
   
}

double GetLow()
{
   int dow = TimeDayOfWeek(iTime(NULL, PivotTF, 0));
   int dow1 = TimeDayOfWeek(iTime(NULL, PivotTF, 1));

   int sh = 1;
   if ((HaveSunday) && (dow == 1))
      sh = 2; // skip sunday
   
   if (dow == 2) // Teusday
   {
      return ( Lowest(NULL, PivotTF, MODE_LOW,2,1) ); 
   }
   
   return( iLow(NULL, PivotTF, sh) );
}

double GetOpen()
{
   int dow = TimeDayOfWeek(iTime(NULL, PivotTF, 0));
   
   int sh = 1;
   if (HaveSunday && (dow == 1)) sh = 2; // Today is Monday 
   
   return( iOpen(NULL, PivotTF, sh) );

}
double GetClose()
{
   int dow = TimeDayOfWeek(iTime(NULL, PivotTF, 0));
   
   int sh = 1;
   if (HaveSunday && (dow == 1)) sh = 2; // Today is Monday 
   
   return( iClose(NULL, PivotTF, sh) );
}

Recommend