PrevDayAndFloatingPivot

Download script
Author: Copyright � 2006, mbkennel@gmail.com
PrevDayAndFloatingPivot

This script is designed to provide traders with two distinct types of visual price levels on their charts: a static reference point based on historical data and a dynamic, moving reference point based on current market activity.

Core Functionality

The primary purpose of this tool is to help identify key "Pivot" points—levels where price might experience a change in direction or significant interaction. It distinguishes between the established balance of the previous trading day and the evolving balance of the current session.

How the Logic Works

1. Historical Reference (Previous Day Pivot) The script first looks back at the most recent completed trading day to establish a baseline. To ensure accuracy, it specifically ignores Sundays to avoid skewed data from non-standard trading sessions. It calculates a "Pivot" level by taking four key price points from the previous day:

  • The High price
  • The Low price
  • The Closing price
  • The Opening price of the current day

By averaging these values, the script generates a stable line on your chart that represents the historical equilibrium of the market.

2. Dynamic Tracking (Floating Current Pivot) While the first point is fixed, the second is "floating." This part of the logic continuously monitors the highest and lowest prices reached during the current trading day up to the present moment. It combines these live extremes with the current market price to calculate a moving pivot.

Because it uses the live "High" and "Low" of the day, this line will shift and adjust in real-time as the market moves, providing a more immediate reflection of today's volatility and price range.

Summary for the User

By using this script, you are viewing two different perspectives of the market simultaneously:

  • The Red Line (Previous Day Pivot): Shows you where the market was balanced yesterday. It acts as a "memory" of the previous session's price action.
  • The Magenta Line (Floating Current Pivot): Shows you where the market is balancing right now. It adjusts dynamically to reflect today’s unique price swings and current momentum.
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains open prices of each barSeries array that contains close prices for each bar
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
PrevDayAndFloatingPivot
//+------------------------------------------------------------------+
//|                                      PrevDayAndFloatingPivot.mq4 |
//|                             Copyright © 2006, mbkennel@gmail.com |
//|                                        http://www.metatrader.org |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, mbkennel@gmail.com"
#property link      "http://www.metatrader.org"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Magenta
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   
   SetIndexLabel(0,"Previous Day Pivot");
   SetIndexLabel(1,"Floating current pivot"); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

  
int TodaysBarShift(int shift) {
   // return the bar shift for today. 
   // i.e. not today.
   
   datetime timenow= iTime(NULL,Period(),shift); // shift of current bar on lower TF chart.
   int idaybarshift = iBarShift(NULL,PERIOD_D1,timenow,false); 
   datetime timedaybegin = iTime(NULL,PERIOD_D1,idaybarshift);
   
   if ((timedaybegin) > timenow) idaybarshift++; 
   
   return(idaybarshift);    
}  

int PreviousNonSundayBarShift(int shift) {
   int tbs = TodaysBarShift(shift); 
   int ybs = tbs+1; 
   datetime yesterdaybegin = iTime(NULL,PERIOD_D1,ybs);
   if (TimeDayOfWeek(yesterdaybegin) == 0) ybs++; // we found a Sunday bar so screw it. 
   return(ybs); 
}

void TodaysHighestLowest(double& H, double& L, int shift) {
   // return the higest and lowest so far today.
   datetime now = iTime(NULL,Period(),shift); // time value of bar.
   int tbs = TodaysBarShift(shift);
   datetime daybegin = iTime(NULL,PERIOD_D1,tbs); 
   
   
   H = High[shift];
   L = Low[shift]; 
   int j = shift+1;
   while ( iTime(NULL,Period(),j) >= daybegin) {
      double Ht = High[j];
      double Lt = Low[j];
      H = MathMax(H,Ht);
      L = MathMin(L,Lt);
      j++;
      if ((j-shift) > 24) {
         Print ("Shit!");
         break;
      } 
   }
   return; 
}

  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   int limit = Bars-counted_bars;
   
   for (int i=limit; i >= 0; i--) {
      // Get the prev days pivot. Today's pivot.
      int ybs = PreviousNonSundayBarShift(i); 
      int tdbar = TodaysBarShift(i);
      
      // Prev day's pivot:
      double p = (iHigh(NULL,PERIOD_D1,ybs)+iLow(NULL,PERIOD_D1,ybs)+
         iClose(NULL,PERIOD_D1,ybs) + iOpen(NULL,PERIOD_D1,tdbar))*0.25;
      
      double TH,TL;
      TodaysHighestLowest(TH,TL,i);
      
      double flp = (TH+TL+Close[i])*0.33333;   
      ExtMapBuffer1[i] = p;
      ExtMapBuffer2[i] = flp; 
      // 
   
   }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments