Yearly_Pivots
Download scriptOverview
This script is designed to automatically identify and display "Yearly Pivot Points" on your trading charts. Pivot points are significant price levels used by traders to determine potential support (floors) and resistance (ceilings). By calculating these based on annual data, the tool helps you visualize the major price boundaries for the current year.
How It Works
The script operates through a systematic process of gathering historical data and performing mathematical calculations to project future price levels:
1. Data Gathering The script automatically scans the previous year's monthly data. It looks specifically for three key pieces of information from that past period:
- The highest price reached (High)
- The lowest price reached (Low)
- The closing price at the end of December (Close)
2. Core Pivot Calculations Once it has gathered the High, Low, and Close prices from the previous year, it uses standard financial formulas to calculate several key levels:
- The Pivot Point (P): The central equilibrium price for the year, calculated as the average of the High, Low, and Close.
- Resistance Levels (R1, R2, R3): These represent prices above the Pivot Point where the market may face selling pressure.
- Support Levels (S1, S2, S3): These represent prices below the Pivot Point where the market may find buying interest.
3. Mid-Point Calculations For traders who require more granular detail, the script also calculates "Mid-Pivots." These are the halfway points between the main levels (e.g., the midpoint between the central Pivot and Resistance 1). These serve as secondary zones where price action might slow down or reverse.
Visual Features
To make this data easy to read on your chart, the script handles all the visual heavy lifting:
- Automatic Drawing: It draws horizontal lines across your chart for every calculated level (P, R1-R3, S1-S3, and Mid-Pivots).
- Color Coding: Resistance levels are typically highlighted in red tones, while Support levels are shown in blue tones, allowing for instant visual identification of market "danger" or "opportunity" zones.
- Dynamic Labeling: The script places text labels next to each line, displaying the exact price value so you don't have to guess the coordinates manually.
- Clean Up: The script includes a "housekeeping" feature that ensures old lines are deleted when the tool is removed or refreshed, keeping your chart clean and uncluttered.
Summary of Benefits
Instead of manually calculating complex annual averages, this tool provides an instant visual map of the year's most important price levels. It allows you to see where big "walls" (Resistance) and "floors" (Support) are located, helping you make more informed decisions on where to place your trades.
//+------------------------------------------------------------------+
//| Yearly_Pivots.mq4 |
//| Copyright © 2008, Siro |
//+------------------------------------------------------------------+
#property indicator_chart_window
extern int LineWidth=2;
extern int MidLineWidth=0;
extern string Style="LineStyle should be 0-5";
extern int LineStyle=0;
extern int MidLineStyle=3;
extern color P_color=Navy;
extern color S_color=Blue;
extern color MS_color=Blue;
extern color R_color=Red;
extern color MR_color=Red;
extern bool ShowMidPivots=true;
double P,R1,R2,R3,S1,S2,S3;
double MR1,MR2,MR3,MS1,MS2,MS3;
int actualyear;
double high=-999999,low=999999,close;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
if(ObjectFind("Yearly_Pivot_P")!=-1)
ObjectDelete("Yearly_Pivot_P");
if(ObjectFind("Yearly_Pivot_R1")!=-1)
ObjectDelete("Yearly_Pivot_R1");
if(ObjectFind("Yearly_Pivot_R2")!=-1)
ObjectDelete("Yearly_Pivot_R2");
if(ObjectFind("Yearly_Pivot_R3")!=-1)
ObjectDelete("Yearly_Pivot_R3");
if(ObjectFind("Yearly_Pivot_S1")!=-1)
ObjectDelete("Yearly_Pivot_S1");
if(ObjectFind("Yearly_Pivot_S2")!=-1)
ObjectDelete("Yearly_Pivot_S2");
if(ObjectFind("Yearly_Pivot_S3")!=-1)
ObjectDelete("Yearly_Pivot_S3");
if(ObjectFind("Yearly_Pivot_LABEL_P")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_P");
if(ObjectFind("Yearly_Pivot_LABEL_R1")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_R1");
if(ObjectFind("Yearly_Pivot_LABEL_R2")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_R2");
if(ObjectFind("Yearly_Pivot_LABEL_R3")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_R3");
if(ObjectFind("Yearly_Pivot_LABEL_S1")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_S1");
if(ObjectFind("Yearly_Pivot_LABEL_S2")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_S2");
if(ObjectFind("Yearly_Pivot_LABEL_S3")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_S3");
if(ObjectFind("Yearly_Pivot_MR1")!=-1)
ObjectDelete("Yearly_Pivot_MR1");
if(ObjectFind("Yearly_Pivot_MR2")!=-1)
ObjectDelete("Yearly_Pivot_MR2");
if(ObjectFind("Yearly_Pivot_MR3")!=-1)
ObjectDelete("Yearly_Pivot_MR3");
if(ObjectFind("Yearly_Pivot_MS1")!=-1)
ObjectDelete("Yearly_Pivot_MS1");
if(ObjectFind("Yearly_Pivot_MS2")!=-1)
ObjectDelete("Yearly_Pivot_MS2");
if(ObjectFind("Yearly_Pivot_MS3")!=-1)
ObjectDelete("Yearly_Pivot_MS3");
if(ObjectFind("Yearly_Pivot_LABEL_MR1")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MR1");
if(ObjectFind("Yearly_Pivot_LABEL_MR2")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MR2");
if(ObjectFind("Yearly_Pivot_LABEL_MR3")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MR3");
if(ObjectFind("Yearly_Pivot_LABEL_MS1")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MS1");
if(ObjectFind("Yearly_Pivot_LABEL_MS2")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MS2");
if(ObjectFind("Yearly_Pivot_LABEL_MS3")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MS3");
IndicatorShortName("Yearly Pivots");
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
if(ObjectFind("Yearly_Pivot_P")!=-1)
ObjectDelete("Yearly_Pivot_P");
if(ObjectFind("Yearly_Pivot_R1")!=-1)
ObjectDelete("Yearly_Pivot_R1");
if(ObjectFind("Yearly_Pivot_R2")!=-1)
ObjectDelete("Yearly_Pivot_R2");
if(ObjectFind("Yearly_Pivot_R3")!=-1)
ObjectDelete("Yearly_Pivot_R3");
if(ObjectFind("Yearly_Pivot_S1")!=-1)
ObjectDelete("Yearly_Pivot_S1");
if(ObjectFind("Yearly_Pivot_S2")!=-1)
ObjectDelete("Yearly_Pivot_S2");
if(ObjectFind("Yearly_Pivot_S3")!=-1)
ObjectDelete("Yearly_Pivot_S3");
if(ObjectFind("Yearly_Pivot_LABEL_P")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_P");
if(ObjectFind("Yearly_Pivot_LABEL_R1")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_R1");
if(ObjectFind("Yearly_Pivot_LABEL_R2")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_R2");
if(ObjectFind("Yearly_Pivot_LABEL_R3")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_R3");
if(ObjectFind("Yearly_Pivot_LABEL_S1")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_S1");
if(ObjectFind("Yearly_Pivot_LABEL_S2")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_S2");
if(ObjectFind("Yearly_Pivot_LABEL_S3")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_S3");
if(ObjectFind("Yearly_Pivot_MR1")!=-1)
ObjectDelete("Yearly_Pivot_MR1");
if(ObjectFind("Yearly_Pivot_MR2")!=-1)
ObjectDelete("Yearly_Pivot_MR2");
if(ObjectFind("Yearly_Pivot_MR3")!=-1)
ObjectDelete("Yearly_Pivot_MR3");
if(ObjectFind("Yearly_Pivot_MS1")!=-1)
ObjectDelete("Yearly_Pivot_MS1");
if(ObjectFind("Yearly_Pivot_MS2")!=-1)
ObjectDelete("Yearly_Pivot_MS2");
if(ObjectFind("Yearly_Pivot_MS3")!=-1)
ObjectDelete("Yearly_Pivot_MS3");
if(ObjectFind("Yearly_Pivot_LABEL_MR1")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MR1");
if(ObjectFind("Yearly_Pivot_LABEL_MR2")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MR2");
if(ObjectFind("Yearly_Pivot_LABEL_MR3")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MR3");
if(ObjectFind("Yearly_Pivot_LABEL_MS1")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MS1");
if(ObjectFind("Yearly_Pivot_LABEL_MS2")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MS2");
if(ObjectFind("Yearly_Pivot_LABEL_MS3")!=-1)
ObjectDelete("Yearly_Pivot_LABEL_MS3");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int pos=0;
int counted_bars=IndicatorCounted();
actualyear=TimeYear(iTime(NULL,PERIOD_MN1,0));
if(counted_bars < 0)
return(-1);
//---- last counted bar will be recounted
if(counted_bars > 0)
counted_bars--;
pos = 0;
while(actualyear-TimeYear(iTime(NULL, PERIOD_MN1, pos))<=1)
{
if(actualyear-TimeYear(iTime(NULL, PERIOD_MN1, pos))==1)
{
if(iHigh(NULL,PERIOD_MN1,pos)>high)
high=iHigh(NULL,PERIOD_MN1,pos);
if(iLow(NULL,PERIOD_MN1,pos)<low)
low=iLow(NULL,PERIOD_MN1,pos);
if(TimeMonth(iTime(NULL, PERIOD_MN1, pos))==12)
close=iClose(NULL,PERIOD_MN1,pos);
}
pos++;
}
P=(high+low+close)/3;
R1 = (2*P)-low;
S1 = (2*P)-high;
R2 = P + (R1-S1);
S2 = P - (R1-S1);
R3 = high+2*(P-low);
S3 = low-2*(high-P);
MR1=(P+R1)/2;
MR2=(R1+R2)/2;
MR3=(R2+R3)/2;
MS1=(P+S1)/2;
MS2=(S1+S2)/2;
MS3=(S2+S3)/2;
//**************************PIVOTS!!!********************************
ObjectCreate("Yearly_Pivot_P", OBJ_HLINE, 0, 0, P);
ObjectSet("Yearly_Pivot_P", OBJPROP_COLOR, P_color);
ObjectSet("Yearly_Pivot_P", OBJPROP_STYLE, LineStyle);
ObjectSet("Yearly_Pivot_P", OBJPROP_WIDTH, LineWidth);
ObjectCreate("Yearly_Pivot_R1", OBJ_HLINE, 0, 0, R1);
ObjectSet("Yearly_Pivot_R1", OBJPROP_COLOR, R_color);
ObjectSet("Yearly_Pivot_R1", OBJPROP_STYLE, LineStyle);
ObjectSet("Yearly_Pivot_R1", OBJPROP_WIDTH, LineWidth);
ObjectCreate("Yearly_Pivot_R2", OBJ_HLINE, 0, 0, R2);
ObjectSet("Yearly_Pivot_R2", OBJPROP_COLOR, R_color);
ObjectSet("Yearly_Pivot_R2", OBJPROP_STYLE, LineStyle);
ObjectSet("Yearly_Pivot_R2", OBJPROP_WIDTH, LineWidth);
ObjectCreate("Yearly_Pivot_R3", OBJ_HLINE, 0, 0, R3);
ObjectSet("Yearly_Pivot_R3", OBJPROP_COLOR, R_color);
ObjectSet("Yearly_Pivot_R3", OBJPROP_STYLE, LineStyle);
ObjectSet("Yearly_Pivot_R3", OBJPROP_WIDTH, LineWidth);
ObjectCreate("Yearly_Pivot_S1", OBJ_HLINE, 0, 0, S1);
ObjectSet("Yearly_Pivot_S1", OBJPROP_COLOR, S_color);
ObjectSet("Yearly_Pivot_S1", OBJPROP_STYLE, LineStyle);
ObjectSet("Yearly_Pivot_S1", OBJPROP_WIDTH, LineWidth);
ObjectCreate("Yearly_Pivot_S2", OBJ_HLINE, 0, 0, S2);
ObjectSet("Yearly_Pivot_S2", OBJPROP_COLOR, S_color);
ObjectSet("Yearly_Pivot_S2", OBJPROP_STYLE, LineStyle);
ObjectSet("Yearly_Pivot_S2", OBJPROP_WIDTH, LineWidth);
ObjectCreate("Yearly_Pivot_S3", OBJ_HLINE, 0, 0, S3);
ObjectSet("Yearly_Pivot_S3", OBJPROP_COLOR, S_color);
ObjectSet("Yearly_Pivot_S3", OBJPROP_STYLE, LineStyle);
ObjectSet("Yearly_Pivot_S3", OBJPROP_WIDTH, LineWidth);
ObjectCreate("Yearly_Pivot_LABEL_P", OBJ_TEXT, 0, Time[0], P);
ObjectSet("Yearly_Pivot_LABEL_P", OBJPROP_COLOR, P_color);
ObjectSetText("Yearly_Pivot_LABEL_P", "Pivot:"+DoubleToStr(P,Digits));
ObjectCreate("Yearly_Pivot_LABEL_R1", OBJ_TEXT, 0, Time[0], R1);
ObjectSet("Yearly_Pivot_LABEL_R1", OBJPROP_COLOR, R_color);
ObjectSetText("Yearly_Pivot_LABEL_R1", "R1:"+DoubleToStr(R1,Digits));
ObjectCreate("Yearly_Pivot_LABEL_R2", OBJ_TEXT, 0, Time[0], R2);
ObjectSet("Yearly_Pivot_LABEL_R2", OBJPROP_COLOR, R_color);
ObjectSetText("Yearly_Pivot_LABEL_R2", "R2:"+DoubleToStr(R2,Digits));
ObjectCreate("Yearly_Pivot_LABEL_R3", OBJ_TEXT, 0, Time[0], R3);
ObjectSet("Yearly_Pivot_LABEL_R3", OBJPROP_COLOR, R_color);
ObjectSetText("Yearly_Pivot_LABEL_R3", "R3:"+DoubleToStr(R3,Digits));
ObjectCreate("Yearly_Pivot_LABEL_S1", OBJ_TEXT, 0, Time[0], S1);
ObjectSet("Yearly_Pivot_LABEL_S1", OBJPROP_COLOR, S_color);
ObjectSetText("Yearly_Pivot_LABEL_S1", "S1:"+DoubleToStr(S1,Digits));
ObjectCreate("Yearly_Pivot_LABEL_S2", OBJ_TEXT, 0, Time[0], S2);
ObjectSet("Yearly_Pivot_LABEL_S2", OBJPROP_COLOR, S_color);
ObjectSetText("Yearly_Pivot_LABEL_S2", "S2:"+DoubleToStr(S2,Digits));
ObjectCreate("Yearly_Pivot_LABEL_S3", OBJ_TEXT, 0, Time[0], S3);
ObjectSet("Yearly_Pivot_LABEL_S3", OBJPROP_COLOR, S_color);
ObjectSetText("Yearly_Pivot_LABEL_S3", "S3:"+DoubleToStr(S3,Digits));
//***********************************************************************
//****************************Midpivots*************************************
if(ShowMidPivots==true)
{
ObjectCreate("Yearly_Pivot_MR1", OBJ_HLINE, 0, 0, MR1);
ObjectSet("Yearly_Pivot_MR1", OBJPROP_COLOR, MR_color);
ObjectSet("Yearly_Pivot_MR1", OBJPROP_STYLE, MidLineStyle);
ObjectSet("Yearly_Pivot_MR1", OBJPROP_WIDTH, MidLineWidth);
ObjectCreate("Yearly_Pivot_MR2", OBJ_HLINE, 0, 0, MR2);
ObjectSet("Yearly_Pivot_MR2", OBJPROP_COLOR, MR_color);
ObjectSet("Yearly_Pivot_MR2", OBJPROP_STYLE, MidLineStyle);
ObjectSet("Yearly_Pivot_MR2", OBJPROP_WIDTH, MidLineWidth);
ObjectCreate("Yearly_Pivot_MR3", OBJ_HLINE, 0, 0, MR3);
ObjectSet("Yearly_Pivot_MR3", OBJPROP_COLOR, MR_color);
ObjectSet("Yearly_Pivot_MR3", OBJPROP_STYLE, MidLineStyle);
ObjectSet("Yearly_Pivot_MR3", OBJPROP_WIDTH, MidLineWidth);
ObjectCreate("Yearly_Pivot_MS1", OBJ_HLINE, 0, 0, MS1);
ObjectSet("Yearly_Pivot_MS1", OBJPROP_COLOR, MS_color);
ObjectSet("Yearly_Pivot_MS1", OBJPROP_STYLE, MidLineStyle);
ObjectSet("Yearly_Pivot_MS1", OBJPROP_WIDTH, MidLineWidth);
ObjectCreate("Yearly_Pivot_MS2", OBJ_HLINE, 0, 0, MS2);
ObjectSet("Yearly_Pivot_MS2", OBJPROP_COLOR, MS_color);
ObjectSet("Yearly_Pivot_MS2", OBJPROP_STYLE, MidLineStyle);
ObjectSet("Yearly_Pivot_MS2", OBJPROP_WIDTH, MidLineWidth);
ObjectCreate("Yearly_Pivot_MS3", OBJ_HLINE, 0, 0, MS3);
ObjectSet("Yearly_Pivot_MS3", OBJPROP_COLOR, MS_color);
ObjectSet("Yearly_Pivot_MS3", OBJPROP_STYLE, MidLineStyle);
ObjectSet("Yearly_Pivot_MS3", OBJPROP_WIDTH, MidLineWidth);
ObjectCreate("Yearly_Pivot_LABEL_MR1", OBJ_TEXT, 0, Time[0], MR1);
ObjectSet("Yearly_Pivot_LABEL_MR1", OBJPROP_COLOR, MR_color);
ObjectSetText("Yearly_Pivot_LABEL_MR1", "MR1:"+DoubleToStr(MR1,Digits));
ObjectCreate("Yearly_Pivot_LABEL_MR2", OBJ_TEXT, 0, Time[0], MR2);
ObjectSet("Yearly_Pivot_LABEL_MR2", OBJPROP_COLOR, MR_color);
ObjectSetText("Yearly_Pivot_LABEL_MR2", "MR2:"+DoubleToStr(MR2,Digits));
ObjectCreate("Yearly_Pivot_LABEL_MR3", OBJ_TEXT, 0, Time[0], MR3);
ObjectSet("Yearly_Pivot_LABEL_MR3", OBJPROP_COLOR, MR_color);
ObjectSetText("Yearly_Pivot_LABEL_MR3", "MR3:"+DoubleToStr(MR3,Digits));
ObjectCreate("Yearly_Pivot_LABEL_MS1", OBJ_TEXT, 0, Time[0], MS1);
ObjectSet("Yearly_Pivot_LABEL_MS1", OBJPROP_COLOR, MS_color);
ObjectSetText("Yearly_Pivot_LABEL_MS1", "MS1:"+DoubleToStr(MS1,Digits));
ObjectCreate("Yearly_Pivot_LABEL_MS2", OBJ_TEXT, 0, Time[0], MS2);
ObjectSet("Yearly_Pivot_LABEL_MS2", OBJPROP_COLOR, MS_color);
ObjectSetText("Yearly_Pivot_LABEL_MS2", "MS2:"+DoubleToStr(MS2,Digits));
ObjectCreate("Yearly_Pivot_LABEL_MS3", OBJ_TEXT, 0, Time[0], MS3);
ObjectSet("Yearly_Pivot_LABEL_MS3", OBJPROP_COLOR, MS_color);
ObjectSetText("Yearly_Pivot_LABEL_MS3", "MS3:"+DoubleToStr(MS3,Digits));
}
//**************************************************************************
//----
//----
return(0);
}
//+------------------------------------------------------------------+
Comments