//+------------------------------------------------------------------+ //| AstroFibonacci.mq4 | //| John Taylor. | //| http://financialprogrammingservices.blogspot.com/ | //+------------------------------------------------------------------+ #property copyright "John Taylor." #property link "http://financialprogrammingservices.blogspot.com/" //#property indicator_chart_window #property show_inputs #include // Moving these objects causes the ratio lines to be redrawn ... extern string StartName="AF_Start"; extern string EndName ="AF_End"; extern bool DrawFibos=TRUE; // http://www.stockmarketcompass.com/AstroFibonacciComparedtoFibonacci.htm //Planet Period Ratio Name Ratio Fib. Equiv. //Mercury 87.969 days (.2408 yr) //Venus 224.700 days (.6151 yr) Mar/Ven = .3902 .382 //Earth 365.256 days Ven/Ear = .6151 .618 //Mars 2.135 years Ear/Mars = .4683 //Ceres 4.599 years Mars/Ceres = .4642 //Jupiter 11.860 years Ceres/Jup = .3877 .382 //Saturn 29.450 years Jup/Sat = .4027 .382 //Chiron 50.540 years Sat/Chi = .5827 //Uranus 84.070 years Chi/Ura = .6011 .618 //Neptune 164.88 years Ura/Nep = .5090 .500 //Pluto 248.090 years Nep/Plu = .6645 extern string ___On_Off_Switches___; extern bool Mars_Venus= TRUE; extern bool Venus_Earth= TRUE; extern bool Earth_Mars= FALSE; extern bool Mars_Ceres= TRUE; extern bool Ceres_Jupiter= FALSE; extern bool Jupiter_Saturn= FALSE; extern bool Saturn_Chiron= TRUE; extern bool Chiron_Uranus= FALSE; extern bool Uranus_Neptune= TRUE; extern bool Neptune_Pluto = TRUE; extern string ___Drawing_Options___; extern color TrendColor=Red; extern int TrendStyle=STYLE_DOT; extern int TrendWidth=1; extern color LineColor=Yellow; extern int LineStyle=STYLE_SOLID; extern int LineWidth=1; double CerJup = 0.3877; double MarVen = 0.3902; double JupSat = 0.4027; double MarCer = 0.4642; double EarMar = 0.4683; double UraNep = 0.5090; double SatChi = 0.5827; double ChiUra = 0.6011; double VenEar = 0.6151; double NepPlu = 0.6645; int start() { DeleteObjects(); for(int i=ObjectsTotal()-1;i>=0;i--) { string startObjName=ObjectName(i); if(IsAfStart(startObjName)) { string endObjName=GetAfEnd(startObjName); if(endObjName!="") { //Print ("Found end object ",endObjName); DrawAf(startObjName,endObjName); } } } } bool IsAfStart(string objName) { bool result=FALSE; if(StringSubstr(objName,0,StringLen(StartName))==StartName) { Print("IsAfStart: ",objName, " is a start object"); result=TRUE; } return(result); } // TODO Factor bool IsAfEnd(string objName) { bool result=FALSE; if(StringSubstr(objName,0,StringLen(EndName))==EndName) { Print("IsAfStart: ",objName, " is an end object"); result=TRUE; } return(result); } string GetAfEnd(string startObjName) { string result =""; for(int i=0;iStringLen(StartName)) { string suffix1=StringSubstr(startObjName,StringLen(StartName)); string suffix2=StringSubstr(endObjName, StringLen(EndName)); if(suffix1==suffix2) { result=endObjName; break; } } else { // Must be a single pair with no suffix result=endObjName; break; } } } return(result); } void DrawAf(string startName, string endName) { double price1=ObjectGet(startName,OBJPROP_PRICE1); datetime time1 =ObjectGet(startName,OBJPROP_TIME1); double price2=ObjectGet(endName,OBJPROP_PRICE1); datetime time2 =ObjectGet(endName,OBJPROP_TIME1); //1. Draw trendline between the start and end DrawTrendLine(startName,endName,time1,price1,time2,price2); //2. Draw all the required astrofib lines DrawAstroFibLines(startName,endName,time1,price1,time2,price2); //3. Draw the required fibo lines DrawFiboLines(startName,endName,time1,price1,time2,price2); } static double ObjectCount=0; string GetObjectName(double objNum) { // Return the name of the specified object ... return(StringConcatenate("_afObj_",objNum)); } string AllocateObject() { // Allocate a new object name, and increment the object counter // Return the allocated object name. string result=GetObjectName(ObjectCount); ObjectCount++; return(result); } void DeleteObjects() { for(int i=ObjectsTotal()-1;i>=0;i--) { ObjectDelete(GetObjectName(i)); } ObjectCount=0; } void DrawTrendLine(string startName,string endName, datetime &time1,double &price1,datetime &time2,double &price2) { string trendName=AllocateObject(); if(ObjectFind(trendName)==-1) { if(!ObjectCreate(trendName,OBJ_TREND,0,0,0,0)) { Print("ObjectCreate failed - ",ErrorDescription(GetLastError())); return; } } // Make sure start is before end if(time1>time2) { int tmpi=time1; time1=time2; time2=tmpi; double tmpd=price1; price1=price2; price2=tmpd; } //Adjust the times .... int bar1=iBarShift(NULL,0,time1); time1=Time[bar1]; int bar2=iBarShift(NULL,0,time2); time2=Time[bar2]; if(High[bar1]>High[bar2]) { price1=High[bar1]; price2=Low [bar2]; } else { price1=Low [bar1]; price2=High[bar2]; } // Set/reset trend parameters ... ObjectSet(trendName,OBJPROP_PRICE1,price1); ObjectSet(trendName,OBJPROP_PRICE2,price2); ObjectSet(trendName,OBJPROP_TIME1, time1); ObjectSet(trendName,OBJPROP_TIME2, time2); ObjectSet(trendName,OBJPROP_COLOR, TrendColor); ObjectSet(trendName,OBJPROP_STYLE, MathMin(MathMax(STYLE_SOLID,TrendStyle),STYLE_DASHDOTDOT)); ObjectSet(trendName,OBJPROP_WIDTH, MathMin(MathMax(1,TrendWidth),5)); ObjectSet(trendName,OBJPROP_RAY,FALSE); } void DrawAstroFibLines(string startName,string endName, datetime time1,double price1,datetime time2,double price2) { if(Mars_Venus) DrawAstroFibLine("MarVen",MarVen,time1,price1,time2,price2); if(Venus_Earth) DrawAstroFibLine("VenEar",VenEar,time1,price1,time2,price2); if(Earth_Mars) DrawAstroFibLine("EarMar",EarMar,time1,price1,time2,price2); if(Mars_Ceres) DrawAstroFibLine("MarCer",MarCer,time1,price1,time2,price2); if(Ceres_Jupiter) DrawAstroFibLine("CerJup",CerJup,time1,price1,time2,price2); if(Jupiter_Saturn) DrawAstroFibLine("JupSat",JupSat,time1,price1,time2,price2); if(Saturn_Chiron) DrawAstroFibLine("SatChi",SatChi,time1,price1,time2,price2); if(Chiron_Uranus) DrawAstroFibLine("ChiUra",ChiUra,time1,price1,time2,price2); if(Uranus_Neptune) DrawAstroFibLine("UraNep",UraNep,time1,price1,time2,price2); if(Neptune_Pluto) DrawAstroFibLine("NepPlu",NepPlu,time1,price1,time2,price2); } void DrawAstroFibLine(string objDesc,double ratio,datetime time1,double price1,datetime time2, double price2) { string lineName=AllocateObject(); if(ObjectFind(lineName)==-1) { if(!ObjectCreate(lineName,OBJ_TREND,0,0,0,0)) { Print("ObjectCreate failed - ",ErrorDescription(GetLastError())); return; } } double priceDiff=(1-ratio)*MathAbs(price1-price2),linePrice; if(price1>price2) { linePrice=price1-priceDiff; } else { linePrice=price1+priceDiff; } // Set/reset line parameters ... ObjectSet(lineName,OBJPROP_PRICE1,linePrice); ObjectSet(lineName,OBJPROP_PRICE2,linePrice); ObjectSet(lineName,OBJPROP_TIME1, time1); ObjectSet(lineName,OBJPROP_TIME2, time2); ObjectSet(lineName,OBJPROP_COLOR, LineColor); ObjectSet(lineName,OBJPROP_STYLE, MathMin(MathMax(STYLE_SOLID,LineStyle),STYLE_DASHDOTDOT)); ObjectSet(lineName,OBJPROP_WIDTH, MathMin(MathMax(1,LineWidth),5)); ObjectSet(lineName,OBJPROP_RAY,TRUE); ObjectSetText(lineName,StringConcatenate(objDesc," @ ",DoubleToStr(ratio,4)),14); } void DrawFiboLines(string startName,string endName, datetime time1,double price1,datetime time2,double price2) { // We draw the 3 nearest Fibos: //Venus 224.700 days (.6151 yr) Mar/Ven = .3902 .382 //Jupiter 11.860 years Ceres/Jup = .3877 .382 //Saturn 29.450 years Jup/Sat = .4027 .382 //Neptune 164.88 years Ura/Nep = .5090 .500 //Earth 365.256 days Ven/Ear = .6151 .618 //Uranus 84.070 years Chi/Ura = .6011 .618 if(DrawFibos) DrawAstroFibLine("Fibo",0.382,time1,price1,time2,price2); if(DrawFibos) DrawAstroFibLine("Fibo",0.500,time1,price1,time2,price2); if(DrawFibos) DrawAstroFibLine("Fibo",0.618,time1,price1,time2,price2); }