Pivots_Daily_M_SR_AIMEfx
Download scriptLogic Overview: Pivot Point Medians Indicator
This script is designed to automatically identify and plot specific price levels on a trading chart. These levels, known as "Pivot Points," are used by traders to gauge the equilibrium of a market based on the price action of the previous trading day.
The logic follows a structured process to calculate these values:
1. Time and Data Filtering
The script begins by defining the scope of its work. It allows the user to specify a "Start Hour" and "Start Minute" to determine when the trading day begins for calculation purposes. It also includes a "DaysToPlot" feature, which lets you limit how many days of history the indicator will display. This is a performance optimization; by limiting the data to only what you need to see (e.g., the last 15 days), the chart remains responsive and fast.
2. Identifying Key Price Data
To calculate the current day's pivot levels, the script must first "look back" at the previous trading day. It performs a search to find:
- The High price reached during the previous day.
- The Low price reached during the previous day.
- The Closing price of the previous day.
To ensure accuracy, the script includes a robust searching mechanism. Instead of simply grabbing the nearest bar, it intelligently scans for specific timestamps to account for gaps in data (such as holidays or weekends) and ensures that the prices retrieved belong strictly to the intended timeframe.
3. Pivot Point Calculations
Once the previous day's High, Low, and Close are confirmed, the script applies mathematical formulas to generate six distinct price levels (labeled M0 through M5). These levels represent different "medians" or balance points:
- Support Levels (M0, M1, M2): Generally positioned at lower price points where buyers might step in.
- Resistance Levels (M3, M4, M5): Generally positioned at higher price points where sellers might take control.
4. Visual Plotting and Labeling
After the levels are calculated, the script performs the following visual actions:
- Line Drawing: It plots horizontal lines across the chart for each of the six levels.
- Dynamic Labeling: It automatically creates text labels on the right side of the chart (or a specified shift). These labels display the exact price of each level (e.g., "M0 1.2345").
- Color Coding: To make the chart easier to read at a glance, it distinguishes between support and resistance by using different colors (DeepSkyBlue for support and Orange for resistance).
Summary of Operation
In simple terms: The script looks at yesterday's price extremes, runs them through a set of formulas to find "balance points," draws lines on your current chart at those prices, and labels them clearly so you can see where the market's major support and resistance zones are located for the day.
//+------------------------------------------------------------------+
//| Original Indicator Auto-Pivot Plotter Labels.mq4 Elton Treloar |
//| I called this one Pivot Point Medians + GMT correction |
//| and was made by Linuxser |
//| Thanks to Rosh form metaquotes forum for fix the redraw problem |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepSkyBlue
#property indicator_color3 DeepSkyBlue
#property indicator_color4 Orange
#property indicator_color5 Orange
#property indicator_color6 Orange
#define IND_NAME "Pivot Point Medians"
//---- input parameters
extern int StartHour=0;
extern int StartMinute=0;
extern int DaysToPlot=15;
extern color SupportLabelColor=DeepSkyBlue;
extern color ResistanceLabelColor=Orange;
extern int fontsize=6;
extern int LabelShift = 0;
//---- buffers
double M0Buffer[];
double M1Buffer[];
double M2Buffer[];
double M3Buffer[];
double M4Buffer[];
double M5Buffer[];
datetime LabelShiftTime;
string Pivot="Pivot",Sup1="S 1", Res1="R 1";
string Sup2="S 2", Res2="R 2", Sup3="S 3", Res3="R 3";
double M0,M1,M2,M3,M4,M5;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE,2,0);
SetIndexStyle(1,DRAW_LINE,2,0);
SetIndexStyle(2,DRAW_LINE,2,0);
SetIndexStyle(3,DRAW_LINE,2,0);
SetIndexStyle(4,DRAW_LINE,2,0);
SetIndexStyle(5,DRAW_LINE,2,0);
SetIndexStyle(6,DRAW_LINE,2,0);
SetIndexBuffer(0,M0Buffer);
SetIndexBuffer(1,M1Buffer);
SetIndexBuffer(2,M2Buffer);
SetIndexBuffer(3,M3Buffer);
SetIndexBuffer(4,M4Buffer);
SetIndexBuffer(5,M5Buffer);
IndicatorShortName(IND_NAME);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ObjectDelete("M0");
ObjectDelete("M1");
ObjectDelete("M2");
ObjectDelete("M3");
ObjectDelete("M4");
ObjectDelete("M5");
ObjectDelete("M6");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//---- indicator calculation
{
if (counted_bars==0)
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
}
int limit=Bars-counted_bars;
int StartMinutesIntoDay=(StartHour*60)+StartMinute; // 8' o'clock x 60 = 480
int CloseMinutesIntoDay=StartMinutesIntoDay-Period(); //
// ****************************************************
// Check That cloes time isn't now a negative number.
// Correct if it is by adding a full day's worth
// of minutes.
// ****************************************************
if (CloseMinutesIntoDay<0)
{
CloseMinutesIntoDay=CloseMinutesIntoDay+1440;
}
// ****************************************************
// Establish the nuber of bars in a day.
// ****************************************************
int BarsInDay=1440/Period();
// ******************************************************************************************
// ******************************************************************************************
// Main Loop
// ******************************************************************************************
// ******************************************************************************************
for(int i=0; i<limit; i++)
{
// ***************************************************************************
// Only do all this if we are within the plotting range we want.
// i.e. DaysToPlot
// If DaysToPlot is "0" (zero) we plot ALL available data. This can be
// VERY slow with a large history and at small time frames. Expect to
// wait if plotting a year or more on a say a five minute chart. On my PC
// two years of data at Five Minutes takes around 30 seconds per indicator.
// i.e. 30 seconds for main pivot levels indicator then another 30 seconds
// to plot the seperate mid-levels indicator.
// ***************************************************************************
if ( (i<((DaysToPlot+1)*BarsInDay))||DaysToPlot==0) // Used to limit the number of days
{ // that are mapped out. Less waiting ;)
// *****************************************************
// Find previous day's opening and closing bars.
// *****************************************************
int PreviousClosingBar = FindLastTimeMatchFast(CloseMinutesIntoDay,i+1);
int PreviousOpeningBar = FindLastTimeMatchFast(StartMinutesIntoDay,PreviousClosingBar+1);
double PreviousHigh= High[PreviousClosingBar];
double PreviousLow = Low [PreviousClosingBar];
double PreviousClose = Close[PreviousClosingBar];
// *****************************************************
// Find previous day's high and low.
// *****************************************************
for (int SearchHighLow=PreviousClosingBar;SearchHighLow<(PreviousOpeningBar+1);SearchHighLow++)
{
if (High[SearchHighLow]>PreviousHigh) PreviousHigh=High[SearchHighLow];
if (Low[SearchHighLow]<PreviousLow) PreviousLow=Low[SearchHighLow];
}
// ************************************************************************
// Calculate Pivot lines and map into indicator buffers.
// ************************************************************************
{
double M0 = (3*PreviousLow-2*PreviousHigh+PreviousClose)/2;
double M1 = (2*PreviousLow-PreviousHigh+PreviousClose)/2;
double M2 = (PreviousLow+PreviousClose)/2;
double M3 = (PreviousHigh+PreviousClose)/2;
double M4 = (2*PreviousHigh-PreviousLow+PreviousClose)/2;
double M5 = (3*PreviousHigh-2*PreviousLow+PreviousClose)/2;
LabelShiftTime = Time[LabelShift];
if (i==0)
{
Print(i);
ObjectCreate("M0", OBJ_TEXT, 0, LabelShiftTime, 0);
ObjectSetText("M0", " M0 " +DoubleToStr(M0,4),fontsize,"tahoma ",SupportLabelColor);
SetIndexLabel(0, "M0");
ObjectCreate("M1", OBJ_TEXT, 0, LabelShiftTime, 0);
ObjectSetText("M1", " M1 " +DoubleToStr(M1,4),fontsize,"tahoma ",SupportLabelColor);
SetIndexLabel(1, "M1");
ObjectCreate("M2", OBJ_TEXT, 0, LabelShiftTime, 0);
ObjectSetText("M2", " M2 " +DoubleToStr(M2,4),fontsize,"tahoma ",SupportLabelColor);
SetIndexLabel(2, "M2");
ObjectCreate("M3", OBJ_TEXT, 0, LabelShiftTime, 0);
ObjectSetText("M3", " M3 " +DoubleToStr(M3,4),fontsize,"tahoma ",ResistanceLabelColor);
SetIndexLabel(3, "M3");
ObjectCreate("M4", OBJ_TEXT, 0, LabelShiftTime, 0);
ObjectSetText("M4", " M4 " +DoubleToStr(M4,4),fontsize,"tahoma ",ResistanceLabelColor);
SetIndexLabel(4, "M4");
ObjectCreate("M5", OBJ_TEXT, 0, LabelShiftTime, 0);
ObjectSetText("M5", " M5 " +DoubleToStr(M5,4),fontsize,"tahoma ",ResistanceLabelColor);
SetIndexLabel(5, "M5");
Print("ObjectSet Done");
ObjectMove("M0", 0, LabelShiftTime,M0);
ObjectMove("M1", 0, LabelShiftTime,M1);
ObjectMove("M2", 0, LabelShiftTime,M2);
ObjectMove("M3", 0, LabelShiftTime,M3);
ObjectMove("M4", 0, LabelShiftTime,M4);
ObjectMove("M5", 0, LabelShiftTime,M5);
Print("ObjectMove Done");
ObjectsRedraw();
Print("ObjectsRedraw Done");
}
}
}
M0Buffer[i]=M0;
M1Buffer[i]=M1;
M2Buffer[i]=M2;
M3Buffer[i]=M3;
M4Buffer[i]=M4;
M5Buffer[i]=M5;
}
// Extra1[i]=OpenPriceAt+i;
// **********************************************
// Calculate the mid-levels in to the buffers.
// (for mid-levels version)
// **********************************************(Twe)
// Res1[i] =((R1-P)/2)+P; //M3
// Res2[i] =((R2-R1)/2)+R1; //M4
// Res3[i] =((R3-R2)/2)+R2;
// Supp1[i]=((P-S1)/2)+S1; //M2
// Supp2[i]=((S1-S2)/2)+S2; //M1
// Supp3[i]=((S2-S3)/2)+S3;
//End of 'DaysToPlot 'if' statement.
// ***************************************************************************************
// End of Main Loop
// ***************************************************************************************
// *****************************************
// Return from Start() (Main Routine)
return(0);
}
//+-------------------------------------------------------------------------------------------------------+
// END Custom indicator iteration function
//+-------------------------------------------------------------------------------------------------------+
// *****************************************************************************************
// *****************************************************************************************
// -----------------------------------------------------------------------------------------
// The following routine will use "StartingBar"'s time and use it to find the
// general area that SHOULD contain the bar that matches "TimeToLookFor"
// -----------------------------------------------------------------------------------------
int FindLastTimeMatchFast(int TimeToLookFor,int StartingBar)
{
int StartingBarsTime=(TimeHour(Time[StartingBar])*60)+TimeMinute(Time[StartingBar]);
// ***************************************************
// Check that our search isn't on the otherside of
// the midnight boundary and correct for calculations
// ***************************************************
if (StartingBarsTime<TimeToLookFor)
{
StartingBarsTime=StartingBarsTime+1440;
}
// ***************************************************
// Find out where the bar SHOULD be in time.
// ***************************************************
int HowFarBack=StartingBarsTime-TimeToLookFor;
// ***************************************************
// Translate that into Bars
// ***************************************************
int HowManyBarsBack=HowFarBack/Period();
// ***************************************************
// We've found our starting point
// Calculate it's relative time in minutes.
// ***************************************************
int SuggestedBar=StartingBar+HowManyBarsBack;
int SuggestedBarsTime=(TimeHour(Time[SuggestedBar])*60)+TimeMinute(Time[SuggestedBar]);
// ***************************************************
// If we have what we're after let's just skip the
// crap and return the position of the bar in the
// chart to our main routine
// ***************************************************
if (SuggestedBarsTime==TimeToLookFor)
{
return(SuggestedBar);
}
// ***************************************************
// Else find the difference in time from where we
// were supposed to find the bar.
// ***************************************************
int DeltaTimeFound=TimeToLookFor-SuggestedBarsTime;
int DeltaBarFound=DeltaTimeFound/Period();
// ***************************************************
// I'd be worried if Delta was below zero. Would mean
// that we have EXTRA bars in that day. Unlikely in
// the extreme.
// So I'm only checking for the other case.
// ...now let's search in a narrow range to find that
// bar we are after...
// ***************************************************
if (DeltaTimeFound>0)
{
for (int SearchCount=SuggestedBar ;SearchCount>(SuggestedBar-DeltaBarFound-2) ;SearchCount--)
{
// ******************************************************************************
// Find time (in minutes) of the current bar AND the two bars either side of it.
// This is done to allow for any missing bars in the data. i.e. If THE bar you
// were after were to be missing you'd never get a valid close/open time.
// This is the same reason for searching rather than just looking back a certain
// number of bars from the close time. Missing bars upset the count back and
// screw up all pivot calculations. There is still room for error but it's improved.
// The best calculations will come from having the best data. ...of course. :)
// ******************************************************************************
//
int PreviousBarsTime=(TimeHour(Time[SearchCount+1])*60)+TimeMinute(Time[SearchCount+1]);
int CurrentBarsTime=(TimeHour(Time[SearchCount])*60)+TimeMinute(Time[SearchCount]);
int NextBarsTime=(TimeHour(Time[SearchCount-1])*60)+TimeMinute(Time[SearchCount-1]);
if (CurrentBarsTime==TimeToLookFor)
{
return(SearchCount); // *** If current bar is what we are after than lets get out.
// *** without mucking about with the rest of the checks.
}
// **********************************
// Check that previous bar doesn't
// lay on a different day.
// Adjust if it is
// **********************************
if(PreviousBarsTime>CurrentBarsTime)
{
PreviousBarsTime=PreviousBarsTime-1440;
}
// **********************************
// Check that following bar doesn't
// lay on a different day.
// Adjust if it is.
// **********************************
if(NextBarsTime<CurrentBarsTime)
{
NextBarsTime=NextBarsTime+1440;
}
// ******************************************
// If this is the best we can get
// to the actual bar we are after, then
// exit, returning current bar number.
// *******************************************
if(PreviousBarsTime<TimeToLookFor)
{
if( TimeToLookFor<NextBarsTime)
{
return(SearchCount);
}
}
}
}
return (SuggestedBar);
}
Comments