#SpudFibo_006

Download script
#SpudFibo_006

This script is designed for MetaTrader 4 to automatically calculate and display Fibonacci retracement levels on a trading chart. Rather than requiring a user to manually click and drag Fibonacci tools based on visual intuition, this tool automates that process by identifying specific price extremes (highs and lows) based on predefined timeframes and sessions.

Core Functionality

1. Automatic Range Identification The script scans historical data to find the "High" and "Low" prices for a specific period. Depending on how you configure it, it can look at:

  • Standard Timeframes: It can calculate ranges based on Daily, Weekly, or Monthly candles.
  • Custom Hourly Sessions: If configured for a specific start hour (like the market open), it can isolate price action within that specific window to determine the range.

2. Intelligent Time Management The script includes logic to handle different time settings accurately:

  • It accounts for Server Time, GMT, and Local Time.
  • It automatically adjusts for time offsets to ensure that if you are looking for a "Daily" high, it captures the correct data regardless of your specific geographic location.
  • It features a specialized logic for Sundays/Fridays to ensure that weekend price gaps do not distort the calculation of previous trading sessions.

3. Multi-Layered Fibonacci Display Once the high and low are identified, the script draws three distinct types of Fibonacci objects simultaneously:

  • Upper Expansion: Projects Fibonacci levels upward from the peak. This helps traders visualize potential extension targets if the price continues to move in a bullish direction.
  • Lower Expansion: Projects Fibonacci levels downward from the trough. This assists in identifying potential target zones for bearish movements.
  • Internal Retracement: Draws a standard retracement between the high and low points. This is used to identify "pullback" zones where the price might pause or reverse before continuing its primary trend.

User Experience

For the end-user, this means the chart automatically updates with three different Fibonacci perspectives based on the market's most recent significant price action. It removes the subjectivity of manual drawing, providing a consistent mathematical reference for support, resistance, and target levels based on historical volatility.

Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains open time of each bar
Miscellaneous
It issuies visual alerts to the screen
10 Views
0 Downloads
0 Favorites
#SpudFibo_006
//+------------------------------------------------------------------+
//|   #SpudFibo.mq4
//+------------------------------------------------------------------+
#property link "http://www.forexfactory.com/showthread.php?t=30109"
#property indicator_chart_window

//+==================================================================+
//| input parameters																	|
//+==================================================================+
extern string	ColorNote		= "--- Fibonacci colors ---";
extern color	UpperFiboColor	= Navy;
extern color	MainFiboColor	= RoyalBlue;
extern color	LowerFiboColor = DodgerBlue;
extern string	TimeFrameNote	= "--- Timeframe for the high and low ---";
extern string	TimeFrameNot2	= "{1=M1, 5=M5, 15=M15, ..., 1440=D1, 10080=W1, 43200=MN1}";
extern int		TimeFrame		= PERIOD_D1;
extern string	OpenTimeNote2	= "--- Open hour for daily timeframe ---";
extern int		OpenTime			= 0;
extern string	TimeZoneNote	= "{0=ServerTime, 1=GMT, 2=LocalTime}";
extern int		TimeZone			= 0;

//+------------------------------------------------------------------+
//| state variables that are used for drawing the fibs.					|
//+------------------------------------------------------------------+
double HiPrice, LoPrice, Range;
datetime StartTime, EndTime;

//+------------------------------------------------------------------+
//| 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";		break;
		case 5:		TimeFrameStr="5 Minute";	break;
		case 15:		TimeFrameStr="15 Minute";	break;
		case 30:		TimeFrameStr="30 Minute";	break;
		case 60:		TimeFrameStr="Hourly";		break;
		case 240:	TimeFrameStr="4 Hourly";	break;
		case 1440:	TimeFrameStr="Daily";		break;
		case 10080:	TimeFrameStr="Weekly";		break;
		case 43200:	TimeFrameStr="Monthly";		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");
   ObjectDelete("FiboDn");
   ObjectDelete("FiboIn");
   return(0);
}


//+------------------------------------------------------------------+
//| Draw Fibo
//+------------------------------------------------------------------+
int DrawFibo()
{
//----
	if(ObjectFind("FiboUp") == -1)
		ObjectCreate("FiboUp",OBJ_FIBO,0,StartTime,HiPrice+Range,StartTime,HiPrice);
	else
	{
		ObjectSet("FiboUp",OBJPROP_TIME2, StartTime);
		ObjectSet("FiboUp",OBJPROP_TIME1, StartTime);
		ObjectSet("FiboUp",OBJPROP_PRICE1,HiPrice+Range);
		ObjectSet("FiboUp",OBJPROP_PRICE2,HiPrice);
	}
   ObjectSet("FiboUp",OBJPROP_LEVELCOLOR,UpperFiboColor);
   ObjectSet("FiboUp",OBJPROP_FIBOLEVELS,7);
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+0,0.0);	ObjectSetFiboDescription("FiboUp",0,TimeFrameStr+" HIGH (100.0%) -  %$"); 
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+1,0.236);	ObjectSetFiboDescription("FiboUp",1,"(123.6%) -  %$"); 
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+2,0.382);	ObjectSetFiboDescription("FiboUp",2,"(138.2%) -  %$"); 
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+3,0.500);	ObjectSetFiboDescription("FiboUp",3,"(150.0%) -  %$"); 
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+4,0.618);	ObjectSetFiboDescription("FiboUp",4,"(161.8%) -  %$"); 
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+5,0.764);	ObjectSetFiboDescription("FiboUp",5,"(176.4%) -  %$"); 
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+6,1.000);	ObjectSetFiboDescription("FiboUp",6,"(200.0%) -  %$"); 
   ObjectSet("FiboUp",OBJPROP_RAY,true);
   ObjectSet("FiboUp",OBJPROP_BACK,true);

//----
	if(ObjectFind("FiboDn") == -1)
		ObjectCreate("FiboDn",OBJ_FIBO,0,StartTime,LoPrice-Range,StartTime,LoPrice);
	else
	{
		ObjectSet("FiboDn",OBJPROP_TIME2, StartTime);
		ObjectSet("FiboDn",OBJPROP_TIME1, StartTime);
		ObjectSet("FiboDn",OBJPROP_PRICE1,LoPrice-Range);
		ObjectSet("FiboDn",OBJPROP_PRICE2,LoPrice);
	}
   ObjectSet("FiboDn",OBJPROP_LEVELCOLOR,LowerFiboColor); 
   ObjectSet("FiboDn",OBJPROP_FIBOLEVELS,7);
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+0,0.0);	ObjectSetFiboDescription("FiboDn",0,TimeFrameStr+" LOW (0.0%) -  %$"); 
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+1,0.236);	ObjectSetFiboDescription("FiboDn",1,"(-23.6%) -  %$"); 
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+2,0.382);	ObjectSetFiboDescription("FiboDn",2,"(-38.2%) -  %$"); 
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+3,0.500);	ObjectSetFiboDescription("FiboDn",3,"(-50.0%) -  %$"); 
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+4,0.618);	ObjectSetFiboDescription("FiboDn",4,"(-61.8%) -  %$"); 
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+5,0.764);	ObjectSetFiboDescription("FiboDn",5,"(-76.4%) -  %$"); 
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+6,1.000);	ObjectSetFiboDescription("FiboDn",6,"(-100.0%) -  %$"); 
   ObjectSet("FiboDn",OBJPROP_RAY,true);
   ObjectSet("FiboDn",OBJPROP_BACK,true);

//----
	if(ObjectFind("FiboIn") == -1)
		ObjectCreate("FiboIn",OBJ_FIBO,0,StartTime,HiPrice,EndTime,LoPrice);
	else
	{
		ObjectSet("FiboIn",OBJPROP_TIME1, StartTime);
		ObjectSet("FiboIn",OBJPROP_TIME2, StartTime+TimeFrame*60);
		ObjectSet("FiboIn",OBJPROP_PRICE1,HiPrice);
		ObjectSet("FiboIn",OBJPROP_PRICE2,LoPrice);
	}
  	ObjectSet("FiboIn",OBJPROP_LEVELCOLOR,MainFiboColor); 
  	ObjectSet("FiboIn",OBJPROP_FIBOLEVELS,5);
  	ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+0,0.236);	ObjectSetFiboDescription("FiboIn",0,"(23.6"+"\x25"+") -  %$"); 
  	ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+1,0.382);	ObjectSetFiboDescription("FiboIn",1,"(38.2) -  %$"); 
  	ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+2,0.500);	ObjectSetFiboDescription("FiboIn",2,"(50.0) -  %$"); 
  	ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+3,0.618);	ObjectSetFiboDescription("FiboIn",3,"(61.8) -  %$"); 
  	ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+4,0.764);	ObjectSetFiboDescription("FiboIn",4,"(76.4) -  %$"); 
  	ObjectSet("FiboIn",OBJPROP_RAY,true);
  	ObjectSet("FiboIn",OBJPROP_BACK,true);
}

//+------------------------------------------------------------------+
//| Indicator start function
//+------------------------------------------------------------------+

int start()
{
	if(TimeFrame!=PERIOD_D1 || (OpenTime==0 && TimeZone==0))
//	Use daily, weekly, whatever candles
	{
		int shift	= iBarShift(NULL,TimeFrame,Time[0]) + 1;	// yesterday
		HiPrice		= iHigh(NULL,TimeFrame,shift);
		LoPrice		= iLow (NULL,TimeFrame,shift);
		StartTime	= iTime(NULL,TimeFrame,shift);
		EndTime		= StartTime+TimeFrame*60;

		if(TimeFrame==PERIOD_D1 && TimeDayOfWeek(StartTime)==0/*Sunday*/)
		{//Add fridays high and low
			HiPrice = MathMax(HiPrice,iHigh(NULL,PERIOD_D1,shift+1));
			LoPrice = MathMin(LoPrice,iLow(NULL,PERIOD_D1,shift+1));
			StartTime = iTime(NULL,PERIOD_D1,shift+1);
		}
	}
	else
//	Use hourly candles
	{
	//----
	//	find last candle of the period
		shift = 1;
		while(TimeHour(iTime(NULL,PERIOD_H1,shift)) != LastHour)
			shift++;
	//----
	//	find first candle of the period
		int startShift = shift;
		while(TimeHour(iTime(NULL,PERIOD_H1,startShift)) != FirstHour
		||TimeDayOfWeek(iTime(NULL,PERIOD_H1,startShift))==0/*Sunday*/)
			startShift++;
		while(TimeHour(iTime(NULL,PERIOD_H1,startShift)) == FirstHour)
			startShift++;
		startShift--;

	//----
	//	get the highest high and lowest low of the period
		HiPrice		= iHigh(NULL,PERIOD_H1,iHighest(NULL,PERIOD_H1,MODE_HIGH,startShift-shift+1,shift));
		LoPrice		= iLow (NULL,PERIOD_H1,iLowest (NULL,PERIOD_H1,MODE_LOW, startShift-shift+1,shift));
		StartTime	= iTime(NULL,PERIOD_H1,startShift);
		EndTime		= iTime(NULL,PERIOD_H1,shift);
	}

	Range = HiPrice-LoPrice;
	DrawFibo();

	return(0);
}
//+------------------------------------------------------------------+

Comments