//+------------------------------------------------------------------+ //| iPivot.mq4 | //| Copyright © 2015, Awran5. | //| awran5@yahoo.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2015, Awran5." #property link "awran5@yahoo.com" #property version "1.00" #property description "Draw Pivot Point with 4 Support and Resistance lines based on most popular Formulas:\n" #property description "- Standard, \n- Fibonacci, \n- Camarilla, \n- Woody’s, \n- DeMark" #property strict #property indicator_chart_window #property indicator_buffers 9 //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum Pivot { Standard, // Standard Fibonacci, // Fibonacci Camarilla, // Camarilla Woodie, // Woodie DeMark // DeMark }; //|--------------------------------------------------------------------------------------------------------------------| //| E X T E R N A L V A R I A B L E S | //|--------------------------------------------------------------------------------------------------------------------| input Pivot PivotMethod = Fibonacci; // Pivot Forumlas input bool ShowPivot = true; // Show Pivot input color pColor = clrSilver; // Pivot Color input int pWidth = 2; // Pivot Width input ENUM_LINE_STYLE pStyle = 0; // Pivot style input string lb_0 = ""; // -------------------------------------------------------------- input bool Show_SR = true; // Show S/R input color rColor = clrFireBrick; // Resistance Color input ENUM_LINE_STYLE rStyle = 0; // Resistance style input int rWidth = 2; // Resistance Width input color sColor = clrDarkGreen; // Support Color input ENUM_LINE_STYLE sStyle = 0; // Support style input int sWidth = 2; // Support Width input color lColor = clrDarkGray; // Text color input bool UseAlert = true; // Enable Alert input bool UseEmail = false; // Enable Email input bool UseSound = false; // Enable Sound input string SoundName = "alert2.wav"; // Sound Name //|--------------------------------------------------------------------------------------------------------------------| //| I N T E R N A L V A R I A B L E S | //|--------------------------------------------------------------------------------------------------------------------| double P,R1,R2,R3,R4,S1,S2,S3,S4, PBuffer[],S1Buffer[],S2Buffer[],S3Buffer[],S4Buffer[], R1Buffer[],R2Buffer[],R3Buffer[],R4Buffer[], //--- DayHigh,DayLow,DayClose,DayOpen,Range; //--- string names[9]={"Daily Pivot","Resistance 1","Resistance 2","Resistance 3","Resistance 4","Support 1","Support 2","Support 3","Support 4"}, labels[9]={"DPV","DR1","DR2","DR3","DR4","DS1","DS2","DS3","DS4"}, space=" ",formula; //--- int MaxBars=500; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,PBuffer); SetIndexBuffer(1,R1Buffer); SetIndexBuffer(2,R2Buffer); SetIndexBuffer(3,R3Buffer); SetIndexBuffer(4,R4Buffer); SetIndexBuffer(5,S1Buffer); SetIndexBuffer(6,S2Buffer); SetIndexBuffer(7,S3Buffer); SetIndexBuffer(8,S4Buffer); // Set styles SetIndexStyle(0,12,pStyle,pWidth,pColor); SetIndexStyle(1,12,rStyle,rWidth,rColor); SetIndexStyle(2,12,rStyle,rWidth,rColor); SetIndexStyle(3,12,rStyle,rWidth,rColor); SetIndexStyle(4,12,rStyle,rWidth,rColor); SetIndexStyle(5,12,sStyle,sWidth,sColor); SetIndexStyle(6,12,sStyle,sWidth,sColor); SetIndexStyle(7,12,sStyle,sWidth,sColor); SetIndexStyle(8,12,sStyle,sWidth,sColor); // Set labels SetIndexLabel(0,"Pivot"); SetIndexLabel(1,"Resistance 1"); SetIndexLabel(2,"Resistance 2"); SetIndexLabel(3,"Resistance 3"); SetIndexLabel(4,"Resistance 4"); SetIndexLabel(5,"Support 1"); SetIndexLabel(6,"Support 2"); SetIndexLabel(7,"Support 3"); SetIndexLabel(8,"Support 4"); // Set Empty valus SetIndexEmptyValue (0, 0.0); SetIndexEmptyValue (1, 0.0); SetIndexEmptyValue (2, 0.0); SetIndexEmptyValue (3, 0.0); SetIndexEmptyValue (4, 0.0); SetIndexEmptyValue (5, 0.0); SetIndexEmptyValue (6, 0.0); SetIndexEmptyValue (7, 0.0); SetIndexEmptyValue (8, 0.0); //---- indicator short name IndicatorShortName("iPivot"); //---- force daily data load iBars(NULL,PERIOD_D1); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- for(int i=0; i<9; i++) { if(ObjectFind(0,names[i]) >=0) ObjectDelete(names[i]); if(ObjectFind(0,space+labels[i]) >=0) ObjectDelete(space+labels[i]); } if(ObjectFind(0,"com")>=0) ObjectDelete("com"); Comment(""); //--- } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate (const int rates_total, // size of input time series const int prev_calculated, // bars handled in previous call const datetime& time[], // Time const double& open[], // Open const double& high[], // High const double& low[], // Low const double& close[], // Close const long& tick_volume[], // Tick Volume const long& volume[], // Real Volume const int& spread[]) // Spread { //--- if(Period() >= PERIOD_D1) return(rates_total); int limit,counted_bars=prev_calculated; if(counted_bars < 0) return(rates_total); if(counted_bars>0) counted_bars--; limit=rates_total-counted_bars; if(limit>MaxBars) limit=MaxBars; for(int i=limit -1; i>=0; i--) { int iYesterday=i+1; datetime yesterday=iTime(NULL,PERIOD_D1,iYesterday); if(TimeDayOfWeek(yesterday)==0) iYesterday++; // Data from Friday not Sunday DayHigh = iHigh (NULL, PERIOD_D1, iYesterday); DayLow = iLow (NULL, PERIOD_D1, iYesterday); DayOpen = iOpen (NULL, PERIOD_D1, iYesterday); DayClose = iClose(NULL, PERIOD_D1, iYesterday); Range=(DayHigh-DayLow); if(PivotMethod==Standard) { P=(DayHigh+DayLow+DayClose)/3; R1 = P * 2 - DayLow; R2 = P + Range; R3 = P * 2 + DayHigh - (DayLow * 2); //--- S1 = P * 2 - DayHigh; S2 = P - Range; S3 = P * 2 - (DayHigh * 2 - DayLow); //--- formula="Standard"; } else if(PivotMethod==Fibonacci) { P=(DayHigh+DayLow+DayClose)/3; R1 = P + Range * 0.382; R2 = P + Range * 0.618; R3 = P + Range * 1.000; R4 = P + Range * 1.618; //--- S1 = P - Range * 0.382; S2 = P - Range * 0.618; S3 = P - Range * 1.000; S4 = P - Range * 1.618; //--- formula="Fibonacci"; } else if(PivotMethod==Camarilla) { //P = (DayHigh + DayLow + DayClose) / 3; R1 = DayClose + Range * 1.1/12; R2 = DayClose + Range * 1.1/6; R3 = DayClose + Range * 1.1/4; R4 = DayClose + Range * 1.1/2; //--- S1 = DayClose - Range * 1.1/12; S2 = DayClose - Range * 1.1/6; S3 = DayClose - Range * 1.1/4; S4 = DayClose - Range * 1.1/2; P=(R1+S1)/2; //--- formula="Camarilla"; } else if(PivotMethod==Woodie) { P=(DayHigh+DayLow+2*DayClose)/4; R1 = (2 * P) - DayLow; R2 = P + Range; R3 = DayHigh + 2 * (P - DayLow); R4 = R3 + Range; //--- S1 = (2 * P) - DayHigh; S2 = P - Range; S3 = DayLow - 2 * (DayHigh - P); S4 = S3 - Range; //--- formula="Woodie"; } else if(PivotMethod==DeMark) { double x=0.0; if(Close[i] < Open[i]) x = DayHigh + (DayLow * 2) + DayClose; if(Close[i] > Open[i]) x = (DayHigh * 2) + DayLow + DayClose; if(Close[i]==Open[i]) x=DayHigh+DayLow+(DayClose*2); P=x/4; R1 = (x/2) - DayLow; S1 = (x/2) - DayHigh; //--- formula="DeMark"; } //---- Buffers PBuffer[i] = P; R1Buffer[i] = R1; R2Buffer[i] = R2; R3Buffer[i] = R3; R4Buffer[i] = R4; S1Buffer[i] = S1; S2Buffer[i] = S2; S3Buffer[i] = S3; S4Buffer[i] = S4; } //--- Draw Line if(ShowPivot) { DrawLines(names[0],space+labels[0],pStyle,pWidth,pColor,P); } if(Show_SR) { DrawLines(names[1],space+labels[1],rStyle,rWidth,rColor,R1); DrawLines(names[2],space+labels[2],rStyle,rWidth,rColor,R2); DrawLines(names[3],space+labels[3],rStyle,rWidth,rColor,R3); DrawLines(names[4],space+labels[4],rStyle,rWidth,rColor,R4); //--- DrawLines(names[5],space+labels[5],sStyle,sWidth,sColor,S1); DrawLines(names[6],space+labels[6],sStyle,sWidth,sColor,S2); DrawLines(names[7],space+labels[7],sStyle,sWidth,sColor,S3); DrawLines(names[8],space+labels[8],sStyle,sWidth,sColor,S4); } //--- Show comment ObjectDelete("com"); ObjectCreate("com",OBJ_LABEL,0,0,0); ObjectSetText("com","Pivot Formula = "+formula,8,"Arial",clrDarkGray); ObjectSet("com",OBJPROP_CORNER,3); ObjectSet("com",OBJPROP_XDISTANCE,10); ObjectSet("com",OBJPROP_YDISTANCE,10); //--- Alert Notifications(); //--- return value of prev_calculated for next call return(rates_total); } //|--------------------------------------------------------------------------------------------------------------------| //| I N T E R N A L F U N C T I O N S | //|--------------------------------------------------------------------------------------------------------------------| //+------------------------------------------------------------------+ //| Draw lines //+------------------------------------------------------------------+ void DrawLines(string name,string label,int style,int width,color clr,double price) { //--- datetime startline = iTime(NULL, 1440, 0)/* + 3600*/; datetime stopline = iTime(NULL, 240, 0) + 43200; ObjectDelete(name); ObjectCreate(name,OBJ_TREND,0,startline,price,stopline,price); ObjectSet(name,OBJPROP_COLOR,clr); ObjectSet(name,OBJPROP_STYLE,style); ObjectSet(name,OBJPROP_WIDTH,width); ObjectSet(name,OBJPROP_RAY,false); if(ObjectFind(label)!=0) { ObjectDelete(label); ObjectCreate(label,OBJ_TEXT,0,startline,price,stopline,price); ObjectSetText(label,label,7,"Verdana",lColor); } else { ObjectMove(label,0,startline,price); } //--- } //+------------------------------------------------------------------+ //| Notifications function //+------------------------------------------------------------------+ void Notifications() { //---- if(Low[0]
P)
doAlert("Pivot Tocuh! - Price has tocuhed the Pivot line @ "+Symbol()+" @ "+PeriodToStr(Period())+" time frame");
if(Low[0]