xPivotMultiday
Download scriptOverview of the Tool Logic
This script is designed to automate the calculation and visual display of various significant price levels on a trading chart. Instead of requiring a trader to manually calculate historical data or identify key support and resistance areas, the tool automatically scans past market data to project specific levels based on established financial theories.
Key Functional Components
1. Historical Data Analysis The script looks back over a user-defined number of days (the "LookBack" period). It identifies the high, low, and opening/closing prices for each day in this range. By processing this historical data, it establishes a baseline for current price behavior.
2. Multi-Theory Level Generation The tool calculates several types of technical levels simultaneously:
- Pivot Points: Standard support and resistance levels derived from the previous day's price action. These are typically used to identify where price might stall or reverse.
- Fibonacci Levels: Projects specific percentage-based retracement levels (such as 38.2%, 50%, and 62%) based on yesterday's price range.
- Camarilla Levels: A specialized set of levels often used by day traders to identify potential breakout points.
- Session Highlights: The script marks the current day's opening price, as well as the high and low prices reached so far during the current session.
3. Historical Tracking The indicator doesn't just show today's data; it draws horizontal lines across the chart for the past several days. This allows a visitor to see how these levels behaved in previous sessions, helping to identify recurring patterns or "zones" where the market frequently reacts.
4. Dynamic Real-Time Updates The logic includes an automated update system that refreshes the "Today" levels in real-time. As the current price moves and the day progresses, the script automatically adjusts the current High/Low markers to reflect the most recent price action without needing to restart the tool.
5. Visual Aids and Customization To make the chart readable, the script:
- Assigns unique colors and line styles to different types of data (e.g., making Pivot points distinct from Fibonacci levels).
- Automatically places text labels on the lines so the user knows exactly what price level they are looking at.
- Includes "Sweet Spots," which identify specific round-number price intervals based on the current market quote.
Summary for the User
Essentially, this script acts as an automated "architect" for your chart. It looks at the past few days of history, applies three different mathematical theories of support and resistance, and draws them out clearly for you to see. This allows you to visualize where the "walls" in the market are located instantly, helping you make more informed decisions on where to enter or exit trades.
//+------------------------------------------------------------------+
//| xPivotMultiday.mq4 |
//| Based on SDX-Pivots|
//| Coded by xecret |
//+------------------------------------------------------------------+
#property indicator_chart_window
extern int LookBackDays=5, ServerTimeZone=2,LabelShift=9;
extern bool ShowPivot=true;
extern color R_color=Red; extern int R_style=1, R_width=1;
extern color S_color=Blue; extern int S_style=1, S_width=1;
extern color Pivot_color=Blue; extern int Pivot_style=0, Pivot_width=2;
extern bool ShowFibo=true;
extern color Fibo_color=Blue;
extern int Fibo_style=2,Fibo_width=1;
extern bool ShowCamarilla=true;
extern color Camarilla_color=Black;
extern int Camarilla_style=2,Camarilla_width=1;
extern bool ShowOpen=true;
extern color Open_color=Black;
extern int Open_style=1,Open_width=1;
extern bool ShowTodayHighLow=True;
extern color TodayHighLow_color=Gray;
extern int TodayHighLow_style=1,TodayHighLow_width=1;
extern bool ShowYesterdayHighLow=true;
extern color YesterdayHighLow_color=Black;
extern int YesterdayHighLow_style=0,YesterdayHighLow_width=1;
extern bool ShowSweetSpots=true;
extern color SweetSpots_color=Orange;
extern int SweetSpots_style=2,SweetSpots_width=1;
extern string LabelFont="Arial";
extern int LabelSize=6;
extern color LabelColor=Black;
extern int UpdateInterval=10;
double H[100],L[100],O[100],C[100];
int IdxOpen_H1[100],IdxClose_H1[100],Weekday[100],YesterdayOffset[100];
datetime TimeOpen_0[100],TimeClose_0[100];
string Daymark[100];
datetime lasttime=0,timelastupdate=0;
int TD, lastweekday=8;
int RealLookBackDays;
int PipDelta=1;
int init()
{
if(Digits==5 || Digits==3) PipDelta=10;
TD=ServerTimeZone*3600;
RealLookBackDays=MathMin(LookBackDays,MathFloor(Bars*Period()/1440)-2);
int newsize=RealLookBackDays+3;
ArrayResize(H,newsize);ArrayResize(L,newsize);ArrayResize(O,newsize);ArrayResize(C,newsize);
ArrayResize(IdxOpen_H1,newsize);ArrayResize(IdxClose_H1,newsize);ArrayResize(Weekday,newsize);ArrayResize(YesterdayOffset,newsize);
ArrayResize(TimeOpen_0,newsize);ArrayResize(TimeClose_0,newsize);ArrayResize(Daymark,newsize);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
int obj_total= ObjectsTotal();
for (int i= obj_total; i>=0; i--) {
string name= ObjectName(i);
if (StringSubstr(name,0,7)=="[PIVOT]")
ObjectDelete(name);
}
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if(TimeDayOfWeek(Time[0]-TD)!=lastweekday){
lastweekday=TimeDayOfWeek(Time[0]-TD);
ComputeData(RealLookBackDays);
double today_high,today_low,today_open,yesterday_high,yesterday_low,yesterday_close;
double d,q,p,r1,r2,r3,s1,s2,s3,h3,h4,l3,l4;
for(int i=0;i<=RealLookBackDays;i++){
today_high=H[i];today_low=L[i];today_open=O[i];
yesterday_high=H[i+YesterdayOffset[i]];yesterday_low=L[i+YesterdayOffset[i]];yesterday_close=C[i+YesterdayOffset[i]];
if(ShowPivot){
d = (today_high - today_low);
q = (yesterday_high - yesterday_low);
p = (yesterday_high + yesterday_low + yesterday_close) / 3;
r1 = (2*p)-yesterday_low;
r2 = p+(yesterday_high - yesterday_low); // r2 = p-s1+r1;
r3 = (2*p)+(yesterday_high-(2*yesterday_low));
s1 = (2*p)-yesterday_high;
s2 = p-(yesterday_high - yesterday_low); // s2 = p-r1+s1;
s3 = (2*p)-((2* yesterday_high)-yesterday_low);
SetLevel("R1", r1, R_color, R_style, TimeOpen_0[i],TimeClose_0[i],i,R_width);
SetLevel("R2", r2, R_color, R_style, TimeOpen_0[i],TimeClose_0[i],i,R_width);
SetLevel("R3", r3, R_color, R_style, TimeOpen_0[i],TimeClose_0[i],i,R_width);
SetLevel("Pivot", p, Pivot_color, Pivot_style, TimeOpen_0[i],TimeClose_0[i],i,Pivot_width);
SetLevel("S1", s1, S_color, S_style, TimeOpen_0[i],TimeClose_0[i],i,S_width);
SetLevel("S2", s2, S_color, S_style, TimeOpen_0[i],TimeClose_0[i],i,S_width);
SetLevel("S3", s3, S_color, S_style, TimeOpen_0[i],TimeClose_0[i],i,S_width);
}
if(ShowFibo){
SetLevel("L-62%", yesterday_low - q*0.618, Fibo_color, Fibo_style, TimeOpen_0[i],TimeClose_0[i],i,Fibo_width);
SetLevel("L-38%", yesterday_low - q*0.382, Fibo_color, Fibo_style, TimeOpen_0[i],TimeClose_0[i],i,Fibo_width);
SetLevel("L+38%", yesterday_low + q*0.382, Fibo_color, Fibo_style, TimeOpen_0[i],TimeClose_0[i],i,Fibo_width);
SetLevel("LH50%", yesterday_low + q*0.5, Fibo_color, Fibo_style, TimeOpen_0[i],TimeClose_0[i],i,Fibo_width);
SetLevel("H-38%", yesterday_high - q*0.382, Fibo_color, Fibo_style, TimeOpen_0[i],TimeClose_0[i],i,Fibo_width);
SetLevel("H+38%", yesterday_high + q*0.382, Fibo_color, Fibo_style, TimeOpen_0[i],TimeClose_0[i],i,Fibo_width);
SetLevel("H+62%", yesterday_high + q*0.618, Fibo_color, Fibo_style, TimeOpen_0[i],TimeClose_0[i],i,Fibo_width);
}
if (ShowCamarilla==true) {
h4 = (q*0.55)+yesterday_close;
h3 = (q*0.27)+yesterday_close;
l3 = yesterday_close-(q*0.27);
l4 = yesterday_close-(q*0.55);
SetLevel("H3", h3, Camarilla_color, Camarilla_style, TimeOpen_0[i],TimeClose_0[i],i,Camarilla_width);
SetLevel("H4", h4, Camarilla_color, Camarilla_style, TimeOpen_0[i],TimeClose_0[i],i,Camarilla_width);
SetLevel("L3", l3, Camarilla_color, Camarilla_style, TimeOpen_0[i],TimeClose_0[i],i,Camarilla_width);
SetLevel("L4", l4, Camarilla_color, Camarilla_style, TimeOpen_0[i],TimeClose_0[i],i,Camarilla_width);
}
if (ShowOpen) {
SetLevel("T Open", today_open, Open_color, Open_style, TimeOpen_0[i],TimeClose_0[i],i,Open_width);
}
if (ShowTodayHighLow){
SetLevel("T High", today_high, TodayHighLow_color, TodayHighLow_style, TimeOpen_0[i],TimeClose_0[i],i,TodayHighLow_width);
SetLevel("T Low", today_low, TodayHighLow_color, TodayHighLow_style, TimeOpen_0[i],TimeClose_0[i],i,TodayHighLow_width);
}
if (ShowYesterdayHighLow){
SetLevel("Y High", yesterday_high, YesterdayHighLow_color, YesterdayHighLow_style, TimeOpen_0[i],TimeClose_0[i],i,YesterdayHighLow_width);
SetLevel("Y Low", yesterday_low, YesterdayHighLow_color, YesterdayHighLow_style, TimeOpen_0[i],TimeClose_0[i],i,YesterdayHighLow_width);
}
SetTimeLine("Start"+i, WeekDayToChar(Weekday[i]), TimeOpen_0[i], CadetBlue, O[i] - 10*Point*PipDelta);
}
}
UpdateToday();
return(0);
}
//+------------------------------------------------------------------+
void ComputeData(int lookbackdays)
{
int i,j;
IdxClose_H1[0]=0;
//TimeClose_0[0]=Time[0];
TimeClose_0[0]=Time[0]+Period()*60;
for(i=0;i<=lookbackdays+2;i++){
Weekday[i]=TimeDayOfWeek(iTime(NULL,PERIOD_H1,IdxClose_H1[i])-TD);
for(j=IdxClose_H1[i]+1;j<=IdxClose_H1[i]+24;j++){
if(TimeDayOfWeek(iTime(NULL,PERIOD_H1,j)-TD)!=Weekday[i]){
IdxOpen_H1[i]=j-1;
IdxClose_H1[i+1]=j;
break;
}
}
C[i]=iClose(NULL,PERIOD_H1,IdxClose_H1[i]);
O[i]=iOpen(NULL,PERIOD_H1,IdxOpen_H1[i]);
H[i]=iHigh(NULL,PERIOD_H1,iHighest(NULL,PERIOD_H1,MODE_HIGH,IdxOpen_H1[i]-IdxClose_H1[i]+1,IdxClose_H1[i]));
L[i]=iLow(NULL,PERIOD_H1,iLowest(NULL,PERIOD_H1,MODE_LOW,IdxOpen_H1[i]-IdxClose_H1[i]+1,IdxClose_H1[i]));
TimeOpen_0[i]=iTime(NULL,PERIOD_H1,IdxOpen_H1[i]);
TimeClose_0[i+1]=iTime(NULL,0,iBarShift(NULL,0,TimeOpen_0[i])+1);
}
for(i=0;i<=lookbackdays;i++){
for(j=1;j<=3;j++){
if(Weekday[i+j]!=0 && Weekday[i+j]!=6){
YesterdayOffset[i]=j;
break;
}
}
Daymark[i]=WeekDayToChar(Weekday[i]);
}
Daymark[0]="";
Daymark[YesterdayOffset[0]]=Daymark[YesterdayOffset[0]] + "(Y)";
}
void SetLevel(string text, double level, color col1, int linestyle, datetime startofday, datetime endofday=0,int IdxDay=1,int Width=1)
{
if(endofday==0) endofday=Time[0];
int digits= Digits;
text= Daymark[IdxDay] + " " + text;
string labelname= "[PIVOT] " + text + " Label"+IdxDay,
linename= "[PIVOT] " + text + " Line"+IdxDay,
pricelabel;
// create or move the horizontal line
if (ObjectFind(linename) != 0) {
ObjectCreate(linename, OBJ_TREND, 0, startofday, level, endofday,level);
ObjectSet(linename, OBJPROP_STYLE, linestyle);
ObjectSet(linename, OBJPROP_COLOR, col1);
ObjectSet(linename, OBJPROP_WIDTH, Width);
if(IdxDay!=0) ObjectSet(linename, OBJPROP_RAY, false);
}
else {
ObjectMove(linename, 1, endofday,level);
ObjectMove(linename, 0, startofday, level);
}
// put a label on the line
datetime lableposition;
if(IdxDay<1) lableposition=Time[0]+Period()*60*LabelShift;else lableposition=startofday+Period()*60*LabelShift;
if (ObjectFind(labelname) != 0) {
ObjectCreate(labelname, OBJ_TEXT, 0, lableposition, level);
}
else {
ObjectMove(labelname, 0, lableposition, level);
}
pricelabel=text;
//if (ShowLevelPrices && StrToInteger(text)==0)
// pricelabel= pricelabel + ": "+DoubleToStr(level, Digits);
ObjectSetText(labelname, pricelabel, LabelSize, LabelFont, LabelColor);
}
void SetTimeLine(string objname, string text, datetime x, color col1, double vleveltext)
{
string name= "[PIVOT] " + objname;
//int x= iTime(NULL, PERIOD_H1, idx);
if (ObjectFind(name) != 0)
ObjectCreate(name, OBJ_TREND, 0, x, 0, x, 100);
else {
ObjectMove(name, 0, x, 0);
ObjectMove(name, 1, x, 100);
}
ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
ObjectSet(name, OBJPROP_COLOR, DarkGray);
if (ObjectFind(name + " Label") != 0)
ObjectCreate(name + " Label", OBJ_TEXT, 0, x, vleveltext);
else
ObjectMove(name + " Label", 0, x, vleveltext);
ObjectSetText(name + " Label", text, 8, "Arial", col1);
}
void UpdateToday(){
if(Time[0]!=lasttime){
lasttime=Time[0];
if(ShowPivot){
MoveToday("R3");MoveToday("R2");MoveToday("R1"); MoveToday("Pivot");
MoveToday("S1");MoveToday("S2");MoveToday("S3");
}
if(ShowFibo){
MoveToday("L-62%");MoveToday("L-38%");MoveToday("L+38%"); MoveToday("LH50%");
MoveToday("H-38%");MoveToday("H+38%");MoveToday("H+62%");
}
if(ShowCamarilla){
MoveToday("H3");MoveToday("H4");MoveToday("L3");MoveToday("L4");
}
if(ShowYesterdayHighLow){
MoveToday("Y High");MoveToday("Y Low");MoveToday("T Open");
}
if(ShowOpen){
MoveToday("T Open");
}
}
if (TimeCurrent()-timelastupdate < UpdateInterval) return (0);
timelastupdate= TimeCurrent();
if (ShowTodayHighLow) {
IdxOpen_H1[0]=iBarShift(NULL,PERIOD_H1,TimeOpen_0[0]);
IdxClose_H1[0]=0;
H[0]=iHigh(NULL,PERIOD_H1,iHighest(NULL,PERIOD_H1,MODE_HIGH,IdxOpen_H1[0]-IdxClose_H1[0]+1,IdxClose_H1[0]));
L[0]=iLow(NULL,PERIOD_H1,iLowest(NULL,PERIOD_H1,MODE_LOW,IdxOpen_H1[0]-IdxClose_H1[0]+1,IdxClose_H1[0]));
SetLevel("T High", H[0], Black, STYLE_DASH, TimeOpen_0[0],TimeClose_0[0],0);
SetLevel("T Low", L[0], Black, STYLE_DASH, TimeOpen_0[0],TimeClose_0[0],0);
}
if (ShowSweetSpots) {
int ssp1, ssp2;
double ds1, ds2;
ssp1= Bid / Point;
ssp1= ssp1 - ssp1%(50*PipDelta);
ssp2= ssp1 + 50*PipDelta;
ds1= ssp1*Point;
ds2= ssp2*Point;
SetLevel(DoubleToStr(ds1,Digits), ds1, SweetSpots_color, SweetSpots_style, Time[10],Time[0]+Period()*60*LabelShift,-1,SweetSpots_width);
SetLevel(DoubleToStr(ds2,Digits), ds2, SweetSpots_color, SweetSpots_style, Time[10],Time[0]+Period()*60*LabelShift,-1,SweetSpots_width);
}
}
string WeekDayToChar(int weekday){
switch(weekday){
case 0: return("Sun");
case 1: return("Mon");
case 2: return("Tue");
case 3: return("Wed");
case 4: return("Thu");
case 5: return("Fri");
case 6: return("Sat");
default: return("Err");
}
}
void MoveToday(string text){
ObjectSet("[PIVOT] " + text + " Label0",OBJPROP_TIME1,Time[0]+Period()*60*LabelShift);
}
Comments