MS_Pentagon_002
Download scriptTechnical Logic Overview: MS_Pentagon
The MS_Pentagon script is a specialized technical analysis tool designed for the MetaTrader platform. It combines geometric price action theory with astronomical data to provide traders with a visual framework of market structure and potential significance dates.
Below is a breakdown of the logic used by this script in non-technical terms:
1. Geometric Price Action (The Pentagon Structure)
The core functionality of the script involves identifying and drawing "Pentagon" structures on your price charts. Unlike standard trendlines, these are calculated based on specific proportions.
- Dynamic Expansion: The script analyzes two user-defined dates to establish a baseline range. It then uses mathematical constants (specifically involving the Golden Ratio) to project geometric shapes onto the chart.
- Breakout Detection: The logic constantly monitors the current price movement. If the price breaks through specific boundaries of the established geometry—such as moving above or below predicted "points"—the script automatically calculates and draws a new pentagon shape.
- Visual Feedback: These shapes are rendered using two different styles: solid lines for the primary structure and dotted lines for internal geometric connections. This helps traders visualize the "path" of price movement and potential zones of expansion or contraction.
2. Astronomical Data Integration (Eclipses)
One of the unique features of this script is its inclusion of astronomical events as markers on the chart.
- Solar and Lunar Eclipses: The script contains a pre-programmed database of significant solar and lunar eclipses spanning several years.
- Automatic Mapping: When viewing the Daily (D1) timeframe, the script automatically scans for these dates. If an eclipse falls within your current view, it places specific markers on the chart:
- Solar Eclipses are marked with an "S" in a pink color.
- Lunar Eclipses are marked with an "L" in an aqua color.
- Placement Logic: The script intelligently positions these labels based on the price action of that specific day (e.g., placing them above the high or below the low of the candle) to ensure they remain visible and relevant to the price movement.
3. Dynamic Time Calculation
To ensure the geometric shapes and eclipse markers align perfectly with your current chart, the script includes a sophisticated time-shifting engine. Because different trading platforms handle "time" differently across various timeframes (from 1-minute charts to Monthly charts), the script manually calculates the correct leap years, month lengths, and hour shifts. This ensures that the shapes remain anchored to the correct historical data points regardless of which timeframe you are currently viewing.
Summary for the Trader
In short, this tool serves as a hybrid "map." It uses geometry to project where price might go based on previous expansion patterns, and it uses astronomy to highlight specific dates that many unconventional traders believe may influence market volatility. It automates the heavy lifting of calculating complex ratios and searching through historical eclipse data, presenting them as a clean visual overlay for your analysis.
//+------------------------------------------------------------------+
//| MS_Pentagon |
//+------------------------------------------------------------------+
#property copyright "*"
#property link "*"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_color1 Red
extern string EX_P_Bpoint="2008.03.17 00:00";
extern string EX_P_Dpoint="2008.08.15 00:00";
extern double EX_P_Epips=14000;
extern int EX_p_max=20;
extern color EX_color1=Blue;
extern color EX_color2=Blue;
extern bool EX_show_astrology=true;
double h5;
datetime Tb,Td; int Xb,Xd;
int xsa,xsb,xsc,xsd,xse;
double ysa,ysb,ysc,ysd,yse;
datetime px; double py; int pd;
double pi=3.1415926535;
int pno=0,max_pno=20;
datetime ST_X[101]; double ST_Y[101]; int ST_D[101];
string solareclipse01="2005.04.09 00:00"; // The South Pacific
string solareclipse02="2005.10.03 00:00"; // Europe
string solareclipse03="2006.03.29 00:00"; // Africa
string solareclipse04="2006.09.22 00:00"; // Africa
string solareclipse05="2007.03.19 00:00"; // Alaska
string solareclipse06="2007.09.11 00:00"; // South America
string solareclipse07="2008.02.07 00:00"; // The South Pacific
string solareclipse08="2008.08.01 00:00"; // Russia
string solareclipse09="2009.01.26 00:00"; // India
string solareclipse10="2009.07.22 00:00"; // The South Pacific
string solareclipse11="2010.01.15 00:00"; // Africa
string solareclipse12="2010.07.11 00:00"; // Polynesia
string solareclipse13="2011.01.04 00:00"; // Europe
string solareclipse14="2011.06.02 00:00"; // Iceland
string solareclipse15="2011.11.25 00:00"; // Antarctica
string solareclipse16="2012.05.21 00:00"; // ú{
string solareclipse17="2012.11.14 00:00"; // Australia
string solareclipse18="2013.05.10 00:00"; //
string lunareclipse01="2005.10.17 00:00"; //
string lunareclipse02="2006.09.08 00:00"; //
string lunareclipse03="2007.03.04 00:00"; //
string lunareclipse04="2007.08.28 00:00"; //
string lunareclipse05="2008.08.17 00:00"; //
string lunareclipse06="2010.01.01 00:00"; //
string lunareclipse07="2010.06.26 00:00"; //
string lunareclipse08="2010.12.21 00:00"; //
string lunareclipse09="2011.06.11 00:00"; //
string lunareclipse10="2011.12.10 00:00"; //
string lunareclipse11="2012.06.04 00:00"; //
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{IndicatorShortName("MS_Pentagon");
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{DelObject();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{ int i,ic=IndicatorCounted(),fr,ix;
double ph;
if(ic==0)
{max_pno=EX_p_max; if(EX_p_max>100) max_pno=100;
Tb=StrToTime(EX_P_Bpoint);
Td=StrToTime(EX_P_Dpoint);
Xb=iBarShift(NULL,0,Tb);
Xd=iBarShift(NULL,0,Td);
if(EX_P_Epips==0.0) h5=MathAbs((Close[Xb]-Close[Xd])*1.5); else h5=EX_P_Epips*Point;
SetSize(Tb,Xb,Td,Xd);
px=Time[Xb+xsb];
if(Close[Xb]<Close[Xd]) {py=Close[Xb]+ysb; pd=1;} else {py=Close[Xb]-ysb; pd=-1;}
DrawPentagon(px,py,pd); PushST(px,py,pd);
if(Close[Xb]<Close[Xd]) ph=py-ysb; else ph=py+ysb;
DrawText("MS_P00",Time[Xb-(xsc-xsb)/2],"High:"+DoubleToStr(h5,Digits),ph,White);
}
if(ic==0) fr=Xb-1; else fr=Bars-ic;
for(i=fr; i>=0; i--)
{ix=iBarShift(NULL,0,px);
if(pd==1)
{if(ix>=i && i>ix-xsb && Close[i]<py-ysb*(ix-i)/xsb) // AB break
{px=Time[ix+xsc]; py=py-ysb; pd=-1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
else if(ix>=i && i>ix-xse && Close[i]>py+yse*(ix-i)/xse) // AE break
{px=Time[ix+xse]; py=py+yse; pd=-1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
else if(ix-xsb>=i && i>ix-xsc && Close[i]<py-ysb) // BC break
{px=Time[ix]; py=py-ysb*2; pd=-1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
else if(ix-xse>=i && i>=ix-xsd && Close[i]>py+yse-yse*((ix-xse)-i)/(xsd-xse)) // DE break
{px=Time[ix-xse]; py=py+yse; pd=-1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
else if(ix-xsc>=i && i>=ix-xsd && Close[i]<py-ysc+ysc*((ix-xsc)-i)/(xsd-xsc)) // CD break
{px=Time[ix-xsc]; py=py-ysc; pd=-1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
}
else
{if(ix>=i && i>ix-xsb && Close[i]>py+ysb*(ix-i)/xsb) // AB break
{px=Time[ix+xsc]; py=py+ysb; pd=1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
else if(ix>=i && i>ix-xse && Close[i]<py-yse*(ix-i)/xse) // AE break
{px=Time[ix+xse]; py=py-yse; pd=1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
else if(ix-xsb>=i && i>ix-xsc && Close[i]>py+ysb) // BC break
{px=Time[ix]; py=py+ysb*2; pd=1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
else if(ix-xse>=i && i>=ix-xsd && Close[i]<py-yse+yse*((ix-xse)-i)/(xsd-xse)) // DE break
{px=Time[ix-xse]; py=py-yse; pd=1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
else if(ix-xsc>=i && i>=ix-xsd && Close[i]>py+ysc-ysc*((ix-xsc)-i)/(xsd-xsc)) // CD break
{px=Time[ix-xsc]; py=py+ysc; pd=1;
if(SearchST(px,py,pd)!=0) {DrawPentagon(px,py,pd); PushST(px,py,pd);}
}
}
}
if(Period()==PERIOD_D1 && EX_show_astrology) ProcEclipse();
return(0);
}
//+------------------------------------------------------------------+
//| SetSize |
//+------------------------------------------------------------------+
void SetSize(datetime tb,int xb,datetime td,int xd)
{
int x1=(xb-xd)*1.0/(0.618+(1-0.618)*1.0/2.0);
xsa=0;
xsb=x1*(1.0-0.618)*1.0/2.0;
xsc=x1-xsb;
xsd=x1;
xse=x1*1.0/2.0;
ysa=0.0;
ysb=h5*(1.0-(1.0-0.618)*1.0/2.0)*MathTan((pi/2.0)*36.0/90.0)/(0.618*1.0/2.0*MathTan((pi/2.0)*72.0/90.0));
ysc=ysb;
ysd=0.0;
yse=h5-ysb;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int DrawPentagon(datetime x,double y,int direction)
{int wx=iBarShift(NULL,0,x);
datetime ta=x;
datetime tb=SiftXTime(wx,xsb);
datetime tc=SiftXTime(wx,xsc);
datetime td=SiftXTime(wx,xsd);
datetime te=SiftXTime(wx,xse);
double pa=y;
double pb=y-ysb; if(direction==-1) pb=y+ysb;
double pc=y-ysc; if(direction==-1) pc=y+ysc;
double pd=y;
double pe=y+yse; if(direction==-1) pe=y-yse;
DrawTLine("MS_P01"+pno,ta,pa,tb,pb,EX_color1,STYLE_SOLID);
DrawTLine("MS_P02"+pno,tb,pb,tc,pc,EX_color1,STYLE_SOLID);
DrawTLine("MS_P03"+pno,tc,pc,td,pd,EX_color1,STYLE_SOLID);
DrawTLine("MS_P04"+pno,td,pd,te,pe,EX_color1,STYLE_SOLID);
DrawTLine("MS_P05"+pno,te,pe,ta,pa,EX_color1,STYLE_SOLID);
DrawTLine("MS_P06"+pno,ta,pa,tc,pc,EX_color2,STYLE_DOT);
DrawTLine("MS_P07"+pno,tb,pb,td,pd,EX_color2,STYLE_DOT);
DrawTLine("MS_P08"+pno,tc,pc,te,pe,EX_color2,STYLE_DOT);
DrawTLine("MS_P09"+pno,td,pd,ta,pa,EX_color2,STYLE_DOT);
DrawTLine("MS_P10"+pno,te,pe,tb,pb,EX_color2,STYLE_DOT);
pno++; pno=MathMod(pno,max_pno);
}
//+------------------------------------------------------------------+
//| PushSTACK |
//+------------------------------------------------------------------+
int PushST(datetime x,int y,int d)
{ int i;
for(i=max_pno-1-1; i>=0; i--)
{ST_X[i+1]=ST_X[i]; ST_Y[i+1]=ST_Y[i]; ST_D[i+1]=ST_D[i];}
ST_X[0]=x; ST_Y[0]=y; ST_D[0]=d;
return(0);
}
//+------------------------------------------------------------------+
//| SearchSTACK |
//+------------------------------------------------------------------+
int SearchST(datetime x,int y,int d)
{ int rc,i;
rc=-1;
for(i=0; i<=max_pno-1 ;i++)
if(ST_X[i]==x && ST_Y[i]==y && ST_D[i]==d) {rc=0; break;}
return(rc);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int DelObject()
{ int i;
ObjectDelete("MS_P00");
for(i=0; i<=max_pno; i++)
{ObjectDelete("MS_P01"+i); ObjectDelete("MS_P02"+i); ObjectDelete("MS_P03"+i);
ObjectDelete("MS_P04"+i); ObjectDelete("MS_P05"+i); ObjectDelete("MS_P06"+i);
ObjectDelete("MS_P07"+i); ObjectDelete("MS_P08"+i); ObjectDelete("MS_P09"+i);
ObjectDelete("MS_P10"+i);
}
ObjectDelete("MS_PS"+solareclipse01); ObjectDelete("MS_PS"+solareclipse02); ObjectDelete("MS_PS"+solareclipse03);
ObjectDelete("MS_PS"+solareclipse04); ObjectDelete("MS_PS"+solareclipse05); ObjectDelete("MS_PS"+solareclipse06);
ObjectDelete("MS_PS"+solareclipse07); ObjectDelete("MS_PS"+solareclipse08); ObjectDelete("MS_PS"+solareclipse09);
ObjectDelete("MS_PS"+solareclipse10); ObjectDelete("MS_PS"+solareclipse11); ObjectDelete("MS_PS"+solareclipse12);
ObjectDelete("MS_PS"+solareclipse13); ObjectDelete("MS_PS"+solareclipse14); ObjectDelete("MS_PS"+solareclipse15);
ObjectDelete("MS_PS"+solareclipse16); ObjectDelete("MS_PS"+solareclipse17); ObjectDelete("MS_PS"+solareclipse18);
ObjectDelete("MS_PL"+lunareclipse01); ObjectDelete("MS_PL"+lunareclipse02); ObjectDelete("MS_PL"+lunareclipse03);
ObjectDelete("MS_PL"+lunareclipse04); ObjectDelete("MS_PL"+lunareclipse05); ObjectDelete("MS_PL"+lunareclipse06);
ObjectDelete("MS_PL"+lunareclipse07); ObjectDelete("MS_PL"+lunareclipse08); ObjectDelete("MS_PL"+lunareclipse09);
ObjectDelete("MS_PL"+lunareclipse10); ObjectDelete("MS_PL"+lunareclipse11);
}
//+------------------------------------------------------------------+
//| ProcEclipse |
//+------------------------------------------------------------------+
void ProcEclipse()
{
SolarEclipse(solareclipse01);
SolarEclipse(solareclipse02);
SolarEclipse(solareclipse03);
SolarEclipse(solareclipse04);
SolarEclipse(solareclipse05);
SolarEclipse(solareclipse06);
SolarEclipse(solareclipse07);
SolarEclipse(solareclipse08);
SolarEclipse(solareclipse09);
SolarEclipse(solareclipse10);
SolarEclipse(solareclipse11);
SolarEclipse(solareclipse12);
SolarEclipse(solareclipse13);
SolarEclipse(solareclipse14);
SolarEclipse(solareclipse15);
SolarEclipse(solareclipse16);
SolarEclipse(solareclipse17);
SolarEclipse(solareclipse18);
LunarEclipse(lunareclipse01);
LunarEclipse(lunareclipse02);
LunarEclipse(lunareclipse03);
LunarEclipse(lunareclipse04);
LunarEclipse(lunareclipse05);
LunarEclipse(lunareclipse06);
LunarEclipse(lunareclipse07);
LunarEclipse(lunareclipse08);
LunarEclipse(lunareclipse09);
LunarEclipse(lunareclipse10);
LunarEclipse(lunareclipse11);
}
//+------------------------------------------------------------------+
//| SolarEclipse |
//+------------------------------------------------------------------+
int SolarEclipse(string tt)
{ int ib;
datetime dt=StrToTime(tt);
datetime bf=Time[Bars-1]; datetime bt=Time[0];
if(bf<=dt && dt<=bt)
{ib=iBarShift(NULL,0,dt);
DrawText("MS_PS"+tt,dt,"S",High[ib]+(High[ib]-Low[ib])*1.5,HotPink);
}
else if(dt>Time[0]) DrawText("MS_PS"+tt,dt,"S",Close[0],HotPink);
}
//+------------------------------------------------------------------+
//| LunarEclipse |
//+------------------------------------------------------------------+
int LunarEclipse(string tt)
{ int ib;
datetime dt=StrToTime(tt);
datetime bf=Time[Bars-1]; datetime bt=Time[0];
if(bf<=dt && dt<=bt)
{ib=iBarShift(NULL,0,dt);
DrawText("MS_PL"+tt,dt,"L",Low[ib]-(High[ib]-Low[ib]),Aqua);
}
else if(dt>Time[0]) DrawText("MS_PL"+tt,dt,"L",Close[0],Aqua);
}
//+------------------------------------------------------------------+
//| DrawTLine |
//+------------------------------------------------------------------+
void DrawTLine(string no,datetime t1,double p1,datetime t2,double p2,color cl,int st)
{
if(ObjectFind(no)<0) ObjectCreate(no,OBJ_TREND,0,0,0,0,0);
ObjectSet(no,OBJPROP_STYLE ,st);
ObjectSet(no,OBJPROP_COLOR ,cl);
ObjectSet(no,OBJPROP_WIDTH ,1);
ObjectSet(no,OBJPROP_TIME1 ,t1);
ObjectSet(no,OBJPROP_PRICE1,p1);
ObjectSet(no,OBJPROP_TIME2 ,t2);
ObjectSet(no,OBJPROP_PRICE2,p2);
ObjectSet(no,OBJPROP_RAY,false);
ObjectSet(no,OBJPROP_BACK,false);
}
//+------------------------------------------------------------------+
//| DrawText |
//+------------------------------------------------------------------+
void DrawText(string no,datetime tb,string tx,double p,color cl)
{ ObjectCreate(no,OBJ_TEXT,0,0,0);
ObjectSet(no,OBJPROP_TIME1 ,tb);
ObjectSet(no,OBJPROP_PRICE1 ,p);
ObjectSet(no,OBJPROP_COLOR ,cl);
ObjectSet(no,OBJPROP_FONTSIZE,9);
ObjectSetText(no,tx);
}
//+------------------------------------------------------------------+
// SiftXTime |
//+------------------------------------------------------------------+
datetime SiftXTime(int x,int sf)
{ datetime dt;
if(x-sf>=0)
{dt=Time[x-sf];}
else
{dt=Timeshift(Time[0],sf-x);}
return(dt);
}
//+------------------------------------------------------------------+
// Timeshift |
//+------------------------------------------------------------------+
datetime Timeshift (datetime dt,int sf) {
int ty=TimeYear(dt);
int tm=TimeMonth(dt);
int td=TimeDay(dt);
int th=TimeHour(dt);
int ti=TimeMinute(dt);
int i;
for(i=1; i<=sf; i++)
{if (Period()==PERIOD_M1 ) ti=ti+1;
else if(Period()==PERIOD_M5 ) ti=ti+5;
else if(Period()==PERIOD_M15) ti=ti+15;
else if(Period()==PERIOD_M30) ti=ti+30;
else if(Period()==PERIOD_H1 ) th=th+1;
else if(Period()==PERIOD_H4 ) th=th+4;
else if(Period()==PERIOD_D1 ) td=td+1;
else if(Period()==PERIOD_W1 ) td=td+7;
else if(Period()==PERIOD_MN1) tm=tm+1;
if(ti>60) {ti=ti-60; th++;}
if(th>23) {th=th-24; td++;}
if(tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) {if(td>31) {tm++; td=td-31;}}
else if(tm==2) {if(MathMod(ty,4)==0) // 09.11.03 update
{if(td>29) {tm++; td=td-29;}}
else
{if(td>28) {tm++; td=td-28;}}
}
else if(tm==4 || tm==6 || tm==9 || tm==11) if(td>30) {tm++; td=td-30;}
if(tm>12) {tm=1; ty++;}
}
return(StrToTime(ty+"."+tm+"."+td+" "+th+":"+ti));
}
Comments