//+------------------------------------------------------------------+ //| PreviousFibs.mq4 //+------------------------------------------------------------------+ #property link "http://www" #property indicator_chart_window //+==================================================================+ //| input parameters | //+==================================================================+ extern string ColorNote = "--- Fibonacci colors ---"; extern color UpperFiboColor = Red; extern color MainFiboColor = Black; extern color LowerFiboColor = Blue; extern string TimeFrameNote = "--- Timeframe for the high and low ---"; extern string TimeFrameNote2 = "{1=M1, 5=M5, 15=M15}"; extern string TimeFrameNote3 = "{30=M30, 60=H1, 240=H4}"; extern string TimeFrameNote4 = "{1440=D1, 10080=W1, 43200=MN1}"; extern int TimeFrame = PERIOD_D1; extern string TimeZoneNote1 = "{To Set Properly, 1st Set...}"; extern string TimeZoneNote2 = "{Whether using Server, GMT or Local...}"; extern string TimeZoneNote3 = "{Time as Base Time}"; extern string TimeZoneNote = "{Use: 0=ServerTime, 1=GMT, 2=LocalTime}"; extern int TimeZone = 0; extern string OpenTimeNote1 = "{Then Set the Day Open Time...}"; extern string OpenTimeNote2 = "{Relative to Base Time}"; extern string OpenTimeNote = "{Open hour for daily Timeframe}"; extern int OpenTime = 0; //extern string Note1="->use Fib 786 vs 764?<-"; //extern bool Fib786 = true; extern string Note2="-> set fib levels below <-"; extern double LongTarget2 = 1.55; extern double LongTarget1 = 1.34; extern double HighPoint = 1.00; extern double Resistance = 0.809; extern double BuySell1 = 0.618; extern double FibPivot = 0.5; extern double BuySell2 = 0.382; extern double Support = 0.191; extern double LowPoint = 0.0; //extern string Note3="-> for short targets use no neg sign <-"; extern double ShortTarget1 = -0.34; extern double ShortTarget2 = -0.55; extern int LineThickness= 1; //+------------------------------------------------------------------+ //| state variables that are used for drawing the fibs. | //+------------------------------------------------------------------+ double HiPrice, LoPrice, Range; datetime StartTime, EndTime, HiTime, LoTime; // Added by Robert to allow drawing of other periods string ObjectID; //+------------------------------------------------------------------+ //| global constants; get initialized in the init function | //+------------------------------------------------------------------+ string TimeFrameStr; // Used to label High and Low int FirstHour, // First hour that is used for the range calculation in server time LastHour; // Last hour that is included in the range calculation in server time //+------------------------------------------------------------------+ #import "kernel32.dll" int GetTimeZoneInformation(int& TZInfoArray[]); #import //+------------------------------------------------------------------+ int init() { //---- name for DataWindow and indicator subwindow label switch(TimeFrame) { case 1: TimeFrameStr="Minute"; ObjectID = "M1"; break; case 5: TimeFrameStr="5 Minute"; ObjectID = "M5"; break; case 15: TimeFrameStr="15 Minute"; ObjectID = "M15"; break; case 30: TimeFrameStr="30 Minute"; ObjectID = "M30"; break; case 60: TimeFrameStr="Hourly"; ObjectID = "H1"; break; case 240: TimeFrameStr="4 Hourly"; ObjectID = "H4"; break; case 1440: TimeFrameStr="Daily"; ObjectID = "D1"; break; case 10080: TimeFrameStr="Weekly"; ObjectID = "W1"; break; case 43200: TimeFrameStr="Monthly"; ObjectID = "Mn1"; break; default: TimeFrameStr="Unknown Timeframe"; } //---- Calculate timeshift int timeShift = 0; int ServerLocalOffset = RoundClosest(TimeCurrent()-TimeLocal(),3600) / 60; if(TimeZone==2) timeShift = ServerLocalOffset/60; // local time -> server time if(TimeZone==1) { if(IsDllsAllowed()) { int GmtLocalOffset, ServerGmtOffset, TZInfoArray[43], result = GetTimeZoneInformation(TZInfoArray); if(result!=0) GmtLocalOffset=TZInfoArray[0]; // Difference between your local time and GMT in minutes (winter time) if(result==2) GmtLocalOffset+=TZInfoArray[42]; // Current difference between your local time and GMT in minutes ServerGmtOffset = ServerLocalOffset-GmtLocalOffset; timeShift = ServerGmtOffset/60; // GMT -> server time } else Alert("For GMT to work, DLLs must be enabled."); } //---- FirstHour = OpenTime%24; if(FirstHour>0) LastHour = FirstHour-1; else LastHour = 23; LastHour += 24+timeShift; LastHour %=24; FirstHour+= 24+timeShift; FirstHour%=24; Print("FirstHour (server time) = "+FirstHour); Print("LastHour (server time) = "+LastHour); return(0); } //+------------------------------------------------------------------+ int RoundClosest(int n, int step) { if(n > 0) n += step/2; else n -= step/2; return(n - n%step); } //+------------------------------------------------------------------+ int deinit() { ObjectDelete("FiboUp" + ObjectID); ObjectDelete("FiboDn" + ObjectID); ObjectDelete("FiboIn" + ObjectID); return(0); } //+------------------------------------------------------------------+ //| Draw Fibo //+------------------------------------------------------------------+ int DrawFibo() { //---- if(ObjectFind("FiboUp" + ObjectID) == -1) ObjectCreate("FiboUp" + ObjectID,OBJ_FIBO,0,StartTime,HiPrice+Range,StartTime,HiPrice); else { ObjectSet("FiboUp" + ObjectID,OBJPROP_TIME2, StartTime); ObjectSet("FiboUp" + ObjectID,OBJPROP_TIME1, StartTime); ObjectSet("FiboUp" + ObjectID,OBJPROP_PRICE1,HiPrice+Range); ObjectSet("FiboUp" + ObjectID,OBJPROP_PRICE2,HiPrice); } ObjectSet("FiboUp" + ObjectID,OBJPROP_LEVELCOLOR,UpperFiboColor); ObjectSet("FiboUp" + ObjectID,OBJPROP_FIBOLEVELS,3); ObjectSet("FiboUp" + ObjectID,OBJPROP_FIRSTLEVEL+0,LowPoint); ObjectSetFiboDescription("FiboUp" + ObjectID,0,TimeFrameStr+" HIGH (100.0%) - %$"); ObjectSet("FiboUp" + ObjectID,OBJPROP_FIRSTLEVEL+1,LongTarget1-1); ObjectSetFiboDescription("FiboUp" + ObjectID,1,"T1 - %$"); ObjectSet("FiboUp" + ObjectID,OBJPROP_FIRSTLEVEL+2,LongTarget2-1); ObjectSetFiboDescription("FiboUp" + ObjectID,2,"T2 - %$"); // ObjectSet("FiboUp" + ObjectID,OBJPROP_FIRSTLEVEL+3,0.500); ObjectSetFiboDescription("FiboUp" + ObjectID,3,"(150.0%) - %$"); // ObjectSet("FiboUp" + ObjectID,OBJPROP_FIRSTLEVEL+4,0.618); ObjectSetFiboDescription("FiboUp" + ObjectID,4,"(161.8%) - %$"); /* if(Fib786 == true) ObjectSet("FiboUp" + ObjectID,OBJPROP_FIRSTLEVEL+5,0.786); if(Fib786 == true) ObjectSetFiboDescription("FiboUp" + ObjectID,5,"(178.6%) - %$"); if(Fib786 == false) ObjectSet("FiboUp" + ObjectID,OBJPROP_FIRSTLEVEL+5,0.764); if(Fib786 == false) ObjectSetFiboDescription("FiboUp" + ObjectID,5,"(176.4%) - %$"); */ // ObjectSet("FiboUp" + ObjectID,OBJPROP_FIRSTLEVEL+6,1.000); ObjectSetFiboDescription("FiboUp" + ObjectID,6,"(200.0%) - %$"); ObjectSet("FiboUp" + ObjectID,OBJPROP_RAY,true); ObjectSet("FiboUp" + ObjectID,OBJPROP_BACK,true); //---- if(ObjectFind("FiboDn" + ObjectID) == -1) ObjectCreate("FiboDn" + ObjectID,OBJ_FIBO,0,StartTime,LoPrice-Range,StartTime,LoPrice); else { ObjectSet("FiboDn" + ObjectID,OBJPROP_TIME2, StartTime); ObjectSet("FiboDn" + ObjectID,OBJPROP_TIME1, StartTime); ObjectSet("FiboDn" + ObjectID,OBJPROP_PRICE1,LoPrice-Range); ObjectSet("FiboDn" + ObjectID,OBJPROP_PRICE2,LoPrice); } ObjectSet("FiboDn" + ObjectID,OBJPROP_LEVELCOLOR,LowerFiboColor); ObjectSet("FiboDn" + ObjectID,OBJPROP_FIBOLEVELS,3); ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+0,LowPoint); ObjectSetFiboDescription("FiboDn" + ObjectID,0,TimeFrameStr+" LOW (0.0%) - %$"); ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+1,LowPoint-ShortTarget1); ObjectSetFiboDescription("FiboDn" + ObjectID,1,"T1 - %$"); ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+2,LowPoint-ShortTarget2); ObjectSetFiboDescription("FiboDn" + ObjectID,2,"T2 - %$"); //ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+3,FibPivot); ObjectSetFiboDescription("FiboDn" + ObjectID,3,FibPivot+" - %$"); //ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+4,BuySell1); ObjectSetFiboDescription("FiboDn" + ObjectID,4,BuySell1+" - %$"); //ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+5,Resistance); ObjectSetFiboDescription("FiboDn" + ObjectID,5,Resistance+" - %$"); /* if(Fib786 == false) ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+5,0.764); if(Fib786 == false) ObjectSetFiboDescription("FiboDn" + ObjectID,5,"(-76.4%) - %$"); */ // ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+6,1.000); ObjectSetFiboDescription("FiboDn" + ObjectID,6,"(-100.0%) - %$"); ObjectSet("FiboDn" + ObjectID,OBJPROP_RAY,true); ObjectSet("FiboDn" + ObjectID,OBJPROP_BACK,true); //---- if(ObjectFind("FiboIn" + ObjectID) == -1) ObjectCreate("FiboIn" + ObjectID,OBJ_FIBO,0,HiTime,HiPrice,LoTime,LoPrice); else { ObjectSet("FiboIn" + ObjectID,OBJPROP_TIME1, HiTime); ObjectSet("FiboIn" + ObjectID,OBJPROP_TIME2, LoTime); ObjectSet("FiboIn" + ObjectID,OBJPROP_PRICE1,HiPrice); ObjectSet("FiboIn" + ObjectID,OBJPROP_PRICE2,LoPrice); } ObjectSet("FiboIn" + ObjectID,OBJPROP_LEVELCOLOR,MainFiboColor); ObjectSet("FiboIn" + ObjectID,OBJPROP_FIBOLEVELS,5); //------------if price moved high to low-------------- // Eaxample: ObjectSet("FiboDn" + ObjectID,OBJPROP_FIRSTLEVEL+1,LowPoint-ShortTarget1); ObjectSetFiboDescription("FiboDn" + ObjectID,1,"T1 - %$"); if(LoTime > HiTime) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+0,Support); if(LoTime > HiTime) ObjectSetFiboDescription("FiboIn" + ObjectID,0,"S/R - %$"); if(LoTime > HiTime) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+1,BuySell2); if(LoTime > HiTime) ObjectSetFiboDescription("FiboIn" + ObjectID,1,"B/S - %$"); if(LoTime > HiTime) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+2,FibPivot); if(LoTime > HiTime) ObjectSetFiboDescription("FiboIn" + ObjectID,2,"Pivot - %$"); if(LoTime > HiTime) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+3,BuySell1); if(LoTime > HiTime) ObjectSetFiboDescription("FiboIn" + ObjectID,3,"B/S - %$"); ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+4,Resistance); ObjectSetFiboDescription("FiboIn",4,"S/R - %$"); // if(LoTime > HiTime && Fib786 == false) // ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+4,0.764); // if(LoTime > HiTime && Fib786 == false) // ObjectSetFiboDescription("FiboIn" + ObjectID,4,"(76.4) - %$"); //------------if price moved low to high-------------- /*if(LoTime < HiTime && Fib786 == true) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+0,0.214); if(LoTime < HiTime && Fib786 == true) ObjectSetFiboDescription("FiboIn" + ObjectID,0,"(78.6"+"\x25"+") - %$"); if(LoTime < HiTime && Fib786 == false) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+0,0.236); if(LoTime < HiTime && Fib786 == false) ObjectSetFiboDescription("FiboIn" + ObjectID,0,"(76.4"+"\x25"+") - %$"); */ if(LoTime < HiTime) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+1,BuySell1); if(LoTime < HiTime) ObjectSetFiboDescription("FiboIn" + ObjectID,1,"B/S - %$"); if(LoTime < HiTime) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+2,FibPivot); if(LoTime < HiTime) ObjectSetFiboDescription("FiboIn" + ObjectID,2,"Pivot - %$"); if(LoTime < HiTime) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+3,BuySell2); if(LoTime < HiTime) ObjectSetFiboDescription("FiboIn" + ObjectID,3,"B/S - %$"); if(LoTime < HiTime) ObjectSet("FiboIn" + ObjectID,OBJPROP_FIRSTLEVEL+4,Resistance); if(LoTime < HiTime) ObjectSetFiboDescription("FiboIn" + ObjectID,4,"S/R - %$"); ObjectSet("FiboIn" + ObjectID,OBJPROP_RAY,true); ObjectSet("FiboIn" + ObjectID,OBJPROP_BACK,true); } //+------------------------------------------------------------------+ //| Indicator start function //+------------------------------------------------------------------+ int start() { if(TimeFrame!=PERIOD_D1 || (OpenTime==0 && TimeZone==0) || iBars(NULL,PERIOD_H1) < 50) // Use daily, weekly, whatever candles { int shift = iBarShift(NULL,TimeFrame,Time[0]) + 1; // yesterday StartTime = iTime(NULL,TimeFrame,shift); if(TimeFrame==PERIOD_D1 && TimeDayOfWeek(StartTime)==0/*Sunday*/) shift++; // take fridays high and low HiPrice = iHigh(NULL,TimeFrame,shift); LoPrice = iLow (NULL,TimeFrame,shift); StartTime = iTime(NULL,TimeFrame,shift); EndTime = StartTime+TimeFrame*60; //---- // falling or rising? if(iOpen(NULL,TimeFrame,shift) > iClose(NULL,TimeFrame,shift)) { HiTime = StartTime; LoTime = EndTime; } else { HiTime = EndTime; LoTime = StartTime; } } else // Use hourly candles { //---- // find last candle of the period shift = 1; while(TimeHour (iTime(NULL,PERIOD_H1,shift)) != LastHour && TimeDayOfWeek(iTime(NULL,PERIOD_H1,shift)) != 0/*Sunday*/) shift++; //---- // find first candle of the period int startShift = shift; while(TimeHour(iTime(NULL,PERIOD_H1,startShift)) != FirstHour) startShift++; while(TimeHour(iTime(NULL,PERIOD_H1,startShift)) == FirstHour) startShift++; startShift--; //---- // get the highest high and lowest low of the period int highest = iHighest(NULL,PERIOD_H1,MODE_HIGH,startShift-shift+1,shift), lowest = iLowest (NULL,PERIOD_H1,MODE_LOW, startShift-shift+1,shift); HiPrice = iHigh(NULL,PERIOD_H1,highest); LoPrice = iLow (NULL,PERIOD_H1,lowest); StartTime = iTime(NULL,PERIOD_H1,startShift); EndTime = StartTime+TimeFrame*60; HiTime = iTime(NULL,PERIOD_H1,highest); LoTime = iTime(NULL,PERIOD_H1,lowest); } Range = HiPrice-LoPrice; DrawFibo(); return(0); } //+------------------------------------------------------------------+