Daily-Fibo

This script is designed to automate the plotting of Fibonacci retracement levels on a trading chart based on daily price action. Instead of requiring you to manually identify highs and lows and draw lines, the tool automatically calculates and visualizes these key zones to help with technical analysis.

Core Functionality

The logic follows a systematic process to determine the range of movement for the previous trading day:

  • Data Retrieval: Every time the chart updates, the script looks back at the data from the previous day. It identifies the highest price (High) and the lowest price (Low) reached during that period.
  • Weekend Adjustment: To ensure accuracy during market holidays or weekends, the script includes a specific check for Sundays. If the previous candle was a Sunday, it intelligently incorporates the high and low prices from Friday as well. This prevents "gaps" in the data and ensures the Fibonacci levels are based on the most recent continuous trading session.
  • Range Calculation: Once the high and low points are established, the script calculates the total price range (the distance between the top and bottom).

Visual Output

The script then generates three distinct types of Fibonacci objects on your chart:

  1. Upper Fibonacci Levels: These are drawn starting from the previous day's high. They are customized with specific colors and include labels for key targets like "TP" (Take Profit) and "EC" (Equilibrium).
  2. Lower Fibonacci Levels: These are drawn starting from the previous day's low. They mirror the upper levels but focus on the downward price movement, also featuring distinct colors and specific descriptive labels.
  3. Inner Fibonacci Levels (Optional): Depending on your settings, the script can draw a third "Inner" set of lines. These represent the primary range between the daily high and low, highlighting the most critical retracement points (like the 50% "PP" or Pivot Point) within that specific day's volatility.

Key Features for Users

  • Automatic Updates: You do not need to redraw these lines manually; the script refreshes the positions automatically as new days begin.
  • Customizable Aesthetics: The logic allows for different colors for the upper, main, and lower levels, making it easy to distinguish between bullish and bearish zones at a glance.
  • Infinite Extension: The lines are set to "ray" mode, meaning they extend forward into the current price action, allowing you to see how the current market is reacting to yesterday's levels in real-time.
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
17 Views
0 Downloads
0 Favorites
Daily-Fibo
//+------------------------------------------------------------------+
//| Daily-Fibo.mq4 originally modified from
//| #SpudFibo.mq4
//+------------------------------------------------------------------+
#property  indicator_chart_window

extern string note1 = "Fibonacci colors";
extern color UpperFiboColor = MediumSeaGreen;
extern color MainFiboColor = MediumBlue;
extern color LowerFiboColor = OrangeRed;
extern string note2 = "Draw main Fibonacci lines?";
extern bool  InnerFibs = true;

double HiPrice, LoPrice, Range;
datetime StartTime;

int init()
{
   return(0);
}

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,13);
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+0,0.0);	ObjectSetFiboDescription("FiboUp",0,"Y-day (H) -  %$"); 
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+2,0.382);	ObjectSetFiboDescription("FiboUp",2,"TP -  %$"); 
   ObjectSet("FiboUp",OBJPROP_FIRSTLEVEL+4,0.618);	ObjectSetFiboDescription("FiboUp",4,"EC -  %$"); 
   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,19);
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+0,0.0);	ObjectSetFiboDescription("FiboDn",0,"Y-day (L) -  %$"); 
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+2,0.382);	ObjectSetFiboDescription("FiboDn",2,"-TP -  %$"); 
   ObjectSet("FiboDn",OBJPROP_FIRSTLEVEL+4,0.618);	ObjectSetFiboDescription("FiboDn",4,"-EC -  %$"); 
   ObjectSet("FiboDn",OBJPROP_RAY,true);
   ObjectSet("FiboDn",OBJPROP_BACK,true);

	if(InnerFibs)
	{
		if(ObjectFind("FiboIn") == -1)
			ObjectCreate("FiboIn",OBJ_FIBO,0,StartTime,HiPrice,StartTime+PERIOD_D1*60,LoPrice);
		else
		{
			ObjectSet("FiboIn",OBJPROP_TIME2, StartTime);
			ObjectSet("FiboIn",OBJPROP_TIME1, StartTime+PERIOD_D1*60);
			ObjectSet("FiboIn",OBJPROP_PRICE1,HiPrice);
			ObjectSet("FiboIn",OBJPROP_PRICE2,LoPrice);
		}
   	ObjectSet("FiboIn",OBJPROP_LEVELCOLOR,MainFiboColor); 
   	ObjectSet("FiboIn",OBJPROP_FIBOLEVELS,7);
   	//ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+0,0.0);	ObjectSetFiboDescription("FiboDn",0,"Y-day (L) -  %$"); 
     	ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+2,0.382);	ObjectSetFiboDescription("FiboIn",2,"Dz L -  %$"); 
   	ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+3,0.500);	ObjectSetFiboDescription("FiboIn",3,"PP -  %$"); 
   	ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+4,0.618);	ObjectSetFiboDescription("FiboIn",4,"Dz H -  %$"); 
     	//ObjectSet("FiboIn",OBJPROP_FIRSTLEVEL+6,1.000);	ObjectSetFiboDescription("FiboUp",6,"Y-day (H) -  %$"); 
   	ObjectSet("FiboIn",OBJPROP_RAY,true);
   	ObjectSet("FiboIn",OBJPROP_BACK,true);
   }
   else
	   ObjectDelete("FiboIn");
}

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

int start()
{
	int shift	= iBarShift(NULL,PERIOD_D1,Time[0]) + 1;	// yesterday
	HiPrice		= iHigh(NULL,PERIOD_D1,shift);
	LoPrice		= iLow (NULL,PERIOD_D1,shift);
	StartTime	= iTime(NULL,PERIOD_D1,shift);

	if(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));
	}

	Range = HiPrice-LoPrice;

	DrawFibo();

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

Comments