Three Day Rolling Pivot

Download script
Three Day Rolling Pivot

This script is designed to provide traders with a visual representation of key price levels based on a specific historical timeframe. Its primary purpose is to calculate and display "Pivot Points," which are common technical analysis tools used to identify potential support and resistance levels in the financial markets.

How the Logic Works

1. Defining the Time Window The script operates by looking at a specific period of time defined by the user (the "Pivot Range"). It monitors the market activity between a designated start time and end time. This allows you to isolate specific trading sessions—such as a daily session or a specific overnight move—to determine the price boundaries.

2. Identifying Key Price Data Once the script identifies the required timeframe, it automatically scans the market data to find three critical pieces of information:

  • The High: The highest price reached during that specific window.
  • The Low: The lowest price reached during that specific window.
  • The Close: The final price at which the window ended.

3. Calculating the Pivot Levels After collecting these three data points, the script performs a series of mathematical calculations to project three distinct lines on your chart:

  • The Main Pivot Point: This is calculated by taking the average of the High, Low, and Close prices. It serves as the central balance point for that period.
  • The Top and Bottom Boundaries: The script calculates the "difference" between the midpoint of the range and the main pivot point. It then projects a line upward (Top) and downward (Bottom) from the center. These lines represent the upper and lower boundaries of the price action for that period.

4. Visual Feedback The script translates these calculations into visual elements on your trading platform:

  • It draws three separate lines representing the Pivot Point, the Range Top, and the Range Bottom.
  • It provides a real-time text comment on the chart showing the exact High, Low, and Close values used for the current calculation.

Summary of Benefit

Instead of manually calculating complex averages every time a new session begins, this script automates the process. It identifies where the "center of gravity" was during a specific timeframe and projects those boundaries forward, helping you visualize potential areas where price might encounter resistance (the top) or support (the bottom).

Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
Three Day Rolling Pivot
/**
* ACD_3.mq4
* Three Day Rolling Pivot
**/

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Green 
#property indicator_color2 LimeGreen
#property indicator_color3 LimeGreen

//Input Params
extern string PivotRangeStart = "21:00";
extern string PivotRangeEnd = "21:00";
extern bool DisplayPivotPoint = true;

double Buffer1[];
double Buffer2[];
double Buffer3[];

double pivotRangeHigh;
double pivotRangeLow;
double pivotRangeClose;
double pivotTop=0;
double pivotBottom=0;
    
int init()
{
   SetIndexStyle(0,DRAW_LINE, STYLE_DOT, 1);
   SetIndexBuffer(0,Buffer1);
   SetIndexLabel(0,"Pivot Point");

   SetIndexStyle(1,DRAW_LINE, STYLE_DASH, 1);
   SetIndexBuffer(1,Buffer2);
   SetIndexLabel(1,"Pivot Range Top");

   SetIndexStyle(2,DRAW_LINE, STYLE_DASH, 1);
   SetIndexBuffer(2,Buffer3);
   SetIndexLabel(2,"Pivot Range Bottom");

   return(0);
}

int deinit()
{
   return(0);
}

int start()
{   
   string barTime="", lastBarTime="";    
   string barDay="", lastBarDay="";        
   int openBars[3], tempBars[3];
   int openBar, closeBar;
          
   for(int i=Bars; i>=0; i--)
   {  
      barTime = TimeToStr(Time[i], TIME_MINUTES);
      lastBarTime = TimeToStr(Time[i+1], TIME_MINUTES); 
      barDay = TimeToStr(Time[i],TIME_DATE);
      lastBarDay = TimeToStr(Time[i+1],TIME_DATE);
      
      if ((PivotRangeEnd == "00:00" && barTime>=PivotRangeEnd && barDay>lastBarDay) || (barTime>=PivotRangeEnd && lastBarTime<PivotRangeEnd))     
      {  
         closeBar = i + 1;
         openBar = openBars[2];
         
         ArrayCopy(tempBars, openBars, 1, 0, 2);
         ArrayCopy(openBars, tempBars);
         
         if (openBar>0)
         {
            calculatePivotRangeValues(openBar, closeBar);
         }
      }
      
      if ((PivotRangeStart == "00:00" && barTime>=PivotRangeStart && barDay>lastBarDay) || (barTime>=PivotRangeStart && lastBarTime<PivotRangeStart))
      {
          openBars[0] = i;
      }
      
      if (openBar>0)
      {
          drawIndicators(i);
      }     
   }
   return(0);
}

void calculatePivotRangeValues(int openBar, int closeBar)
{
   pivotRangeHigh = High[Highest(NULL, 0, MODE_HIGH, (openBar - closeBar + 1), closeBar)];
   pivotRangeLow = Low[Lowest(NULL, 0, MODE_LOW, (openBar - closeBar + 1), closeBar)];
   pivotRangeClose = Close[closeBar];
}

void drawIndicators(int curBar)
{
   double pivotPoint, pivotDiff;
   
   if (pivotRangeHigh<=0 || pivotRangeLow <=0 || pivotRangeClose <= 0)
   {
      return(0);
   }
   
   pivotPoint=(pivotRangeHigh + pivotRangeLow + pivotRangeClose)/3;
   pivotDiff = MathAbs(((pivotRangeHigh + pivotRangeLow)/2) - pivotPoint);
   pivotTop = pivotPoint + pivotDiff;
   pivotBottom = pivotPoint - pivotDiff;   

   if (DisplayPivotPoint) Buffer1[curBar]=pivotPoint;
   Buffer2[curBar]=pivotTop;
   Buffer3[curBar]=pivotBottom;
   Comment("H " + DoubleToStr(pivotRangeHigh, Digits) + " L " + DoubleToStr(pivotRangeLow, Digits) + " C " + DoubleToStr(pivotRangeClose,Digits));
}

Comments