PrevDayAndFloatingPivot_v2
Download scriptThis tool is designed to provide traders with two specific visual references on their charts: a fixed historical price point based on the previous day's activity and a dynamic, "floating" price point based on the current day's live movement. By plotting these two distinct lines, the indicator helps traders identify key pivot zones where market sentiment might shift.
How the Logic Works
The script functions by calculating two separate types of pivots:
1. The Previous Day Pivot (Static Reference) The tool first identifies the correct trading day preceding the current one. It includes a specific logic to skip Sundays, ensuring that the data is pulled from the last actual trading session. Once it finds the correct previous day, it gathers four pieces of information: the highest price reached, the lowest price reached, the closing price, and the opening price of that day. It then calculates a weighted average of these figures to create a single "Pivot" level. This represents a stable price benchmark derived from the previous day's completed market action.
2. The Floating Current Pivot (Dynamic Reference) Unlike the previous day's pivot, this line is designed to move and update in real-time as the current trading day progresses. To do this, the script constantly scans every candle that has occurred since the start of today. It identifies the highest point and the lowest point reached so far in the current session. It then takes these two "extreme" values, combines them with the most recent current price, and calculates a moving average. Because the high, low, and current price are constantly changing as new trades occur, this line "floats" across the chart to show where the balance of power sits in the present moment.
Summary of Visual Output
- Previous Day Pivot: A steady line showing you where the market balanced out during the last full trading session.
- Floating Current Pivot: A reactive line that updates every second, showing you the evolving equilibrium of today’s price action.
//+------------------------------------------------------------------+
//| 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