CustomCandle_v2

Download script
Author: � 2006, mankurt
CustomCandle_v2

Multi-Timeframe Custom Candle Overview

This script is designed to provide a visual representation of higher timeframes directly on your current chart. It allows you to view "Custom Candles"—meaning bars representing larger blocks of time (such as 10-minute candles on a 1-minute chart)—without having to switch between different charts manually.

How the Logic Works

1. Timeframe Synchronization The script begins by checking your current chart settings to ensure they are compatible with the higher timeframe you wish to view. For example, if you want to see 10-minute candles, your current chart must be set to a period that divides evenly into 10 minutes (like 1-minute or 5-minute charts). It will alert you if the settings are mathematically incompatible.

2. Dynamic Data Calculation Instead of just looking at the standard bars on your screen, the script actively calculates the high, low, open, and close prices for the larger timeframe period.

  • Current Bar: It constantly tracks the price movement of the "live" custom candle as it forms.
  • Previous Bar: Every time a new higher-timeframe candle begins, the script captures the final data of the completed candle and stores it so it can be displayed clearly on your chart.

3. Visual Rendering (Drawing the Candles) The script draws three distinct visual elements for every custom candle:

  • The Body: A rectangular box that represents the price movement from opening to closing.
  • The Wicks: Vertical lines extending from the body to show the highest and lowest prices reached during that timeframe.
  • The Tail: Specific indicators at the bottom of the wick to highlight the lowest point.

4. Automated Styling and Coloring To make the data easy to read at a glance, the script applies logic to color-code the candles based on price action:

  • Bullish Movement: If the price closed higher than it opened, the candle body and wicks are colored (Aqua).
  • Bearish Movement: If the price closed lower than it opened, the candle body and wicks change to a different color (Magenta).
  • Neutral Movement (Doji): If the price opened and closed at almost the same level, the candle is colored Blue.

5. On-Screen Information The script provides real-time text updates in the corner of your screen. It displays the current "Open, High, Low, and Close" prices for the higher timeframe candle currently forming, allowing you to see exactly where the price is relative to that larger period.

Summary of User Experience

When you run this script, you won't see a standard "line" or "moving average." Instead, you will see custom-drawn candles overlaid on your current chart. These candles represent the bigger picture, helping you stay aware of longer-term price trends while you continue to trade on a smaller, more detailed timeframe.

Miscellaneous
It issuies visual alerts to the screen
10 Views
0 Downloads
0 Favorites
CustomCandle_v2

#property link      "mankurt@freemail.ru"
#property copyright "© 2006, mankurt"
#property indicator_chart_window

//+------------------------------------------------------------------------------------------------+
extern int   	TimeFrame 		= 10;
extern color 	UpCandle 		= Aqua;
extern color 	DnCandle 		= Magenta;
extern color 	DojiColor 		= Blue;
extern int   	Width 			= 1;
extern bool  	BGCandle 		= false;
extern bool		DisplayComment	= true;

//+------------------------------------------------------------------------------------------------+
int      i, Nbar, OpenBar, PeriodSecs, TimeframeSecs, Counter, MidBar;
double   HighPrevBar, LowPrevBar, ClosePrevBar;
double   OpenNewBar, HighNewBar, LowNewBar, CloseNewBar;
double   HighCurBar, LowCurBar, CloseCurBar;
double   PriceNewWick, PriceNewTail, PricePrevWick, PricePrevTail, PriceCurWick, PriceCurTail;
string   NameNewCandle, NamePrevCandle;
string   NameNewWick, NameNewTail, NamePrevWick, NamePrevTail;
string   NameBar, NameHigh, NameLow;
datetime TimeOpenNewBar, TimeCloseNewBar, TimeClosePrevBar;
datetime TimeNewShadow, TimePrevShadow;
bool     IsNewBar;

//+------------------------------------------------------------------------------------------------+
int init()
{
	IndicatorShortName("M" + TimeFrame + " íà M" + Period());
	Nbar = TimeFrame / Period();
	PeriodSecs = Period() * 60;
	TimeframeSecs = TimeFrame * 60;
	Counter = 0;
	TimeOpenNewBar = Time[Bars-1];
	OpenNewBar = Open[Bars-1];
	IsNewBar = false;
	NameBar = "Bar M" + TimeFrame + "-";
	NameHigh = "High M" + TimeFrame + "-";
	NameLow = "Low M" + TimeFrame + "-";
	return(0);
}

//+------------------------------------------------------------------------------------------------+
int deinit()
{
	for (int DelOBJ = 1; DelOBJ <= Counter; DelOBJ++)
	{
		ObjectDelete(NameBar + DelOBJ);
		ObjectDelete(NameHigh + DelOBJ);
		ObjectDelete(NameLow + DelOBJ);
	}
	Comment("");
	return(0);
}

//+------------------------------------------------------------------------------------------------+
int start()
{
	if (TimeFrame > 1440)
	{
		Alert("CustomCandle: TimeFrame more than D1 is not supported!!!");
		return(0);
	}
	if (Period() > 240)
	{
		Alert("CustomCandle: Period more than H4 is not supported!!!");
		return(0);
	}
	if (TimeFrame <= Period() || MathMod(TimeFrame, Period()) != 0)
	{
		Alert("CustomCandle: TimeFrame setting should be greater than " + Period() + ", and evenly divisible by it.");
		return(0);
	}
	
	
	i = Bars - IndicatorCounted();
	while (i > 0)
	{
		i--;
		while (i >= 0) 
		{
			if (Time[i] == TimeOpenNewBar || BarNew(i, TimeFrame) == false)
				i--;
			else
			{
				IsNewBar = true;
				Counter++;
				break;
			}
		}
		
		if (i < 0) 
			i = 0;
			
		if (IsNewBar == true)
		{
			//+--------------------------- Previous Bar ------------------------------------------+
			OpenBar = iBarShift(NULL, 0, TimeOpenNewBar, false);
			TimeClosePrevBar = Time[i+1];
			ClosePrevBar = Close[i+1];
			HighPrevBar = High[Highest(0, 0, MODE_HIGH, OpenBar-i, i+1)];
			LowPrevBar = Low[Lowest(0, 0, MODE_LOW, OpenBar-i, i+1)];
			NamePrevCandle = NameBar + (Counter - 1);
			MidBar = OpenBar - MathRound((OpenBar - i) / 2);
			TimePrevShadow = Time[MidBar];
			PricePrevWick = PriceShadow(OpenNewBar, ClosePrevBar, 0);
			PricePrevTail = PriceShadow(OpenNewBar, ClosePrevBar, 1);
			NamePrevWick = NameHigh + (Counter - 1);
			NamePrevTail = NameLow + (Counter - 1);
			
			//+------------------- Modify Previous Bar & Shadow ----------------------------------+

			// Candle body
			if (ObjectFind(NamePrevCandle) == 0)
			{
				ObjectMove(NamePrevCandle, 1, TimeClosePrevBar, ClosePrevBar);
				PropBar(OpenNewBar, ClosePrevBar, NamePrevCandle);
				if (OpenBar == i + 1) ObjectSet(NamePrevCandle, OBJPROP_WIDTH, Width*3);
			}
			
			// Candle wick
			if (ObjectFind(NamePrevWick) == 0)
			{
				if (PricePrevWick == HighPrevBar) 
					ObjectDelete(NamePrevWick);
				else
				{
					ObjectMove(NamePrevWick, 0, TimePrevShadow, PricePrevWick);
					ObjectMove(NamePrevWick, 1, TimePrevShadow, HighPrevBar);
					ColorShadow(OpenNewBar, ClosePrevBar, NamePrevWick);
					ObjectSetText(NamePrevWick, "High=" + DoubleToStr(HighPrevBar, Digits), 7, "Tahoma");
				}
			}
			
			// Candle tail
			if (ObjectFind(NamePrevTail) == 0)
			{
				if (PricePrevTail == LowPrevBar) 
					ObjectDelete(NamePrevTail);
				else
				{
					ObjectMove(NamePrevTail, 0, TimePrevShadow, PricePrevTail);
					ObjectMove(NamePrevTail, 1, TimePrevShadow, LowPrevBar);
					ColorShadow(OpenNewBar, ClosePrevBar, NamePrevTail);
					ObjectSetText(NamePrevTail, "Low=" + DoubleToStr(LowPrevBar, Digits), 7, "Tahoma");
				}
			}
			
			//+---------------------------- New Bar ----------------------------------------------+
			OpenNewBar = Open[i];
			TimeOpenNewBar = Time[i];
			HighNewBar = High[i];
			LowNewBar = Low[i];
			CloseNewBar = Close[i];
			TimeCloseNewBar = Time[i] + TimeframeSecs - PeriodSecs;
			NameNewCandle = NameBar + Counter;
			TimeNewShadow = Time[i] + MathRound(Nbar / 2) * PeriodSecs;
			PriceNewWick = PriceShadow(OpenNewBar, CloseNewBar, 0);
			PriceNewTail = PriceShadow(OpenNewBar, CloseNewBar, 1);
			NameNewWick = NameHigh + Counter;
			NameNewTail = NameLow + Counter;
			IsNewBar = false;
		}
		else
		{
			//+--------------------------- Current Bar -------------------------------------------+
			OpenBar = iBarShift(NULL, 0, TimeOpenNewBar, false);
			
			CloseNewBar = Close[i];
			HighNewBar = High[Highest(0, 0, MODE_HIGH, OpenBar+1, i)];
			LowNewBar = Low[Lowest(0, 0, MODE_LOW, OpenBar+1, i)];
			PriceNewWick = PriceShadow(OpenNewBar, CloseNewBar, 0);
			PriceNewTail = PriceShadow(OpenNewBar, CloseNewBar, 1);
		}
		
		//+------------------- Create New Object & Modifi Current --------------------------------+

		// Derk 4Jan08 - Just delete the objects every time
		ObjectDelete(NameNewCandle);
		ObjectDelete(NameNewWick);
		ObjectDelete(NameNewTail);

		// Candle body
		ObjectCreate(NameNewCandle, OBJ_RECTANGLE, 0, TimeOpenNewBar, OpenNewBar, TimeCloseNewBar, CloseNewBar);
		ObjectSet(NameNewCandle, OBJPROP_STYLE, STYLE_SOLID);
		PropBar(OpenNewBar, CloseNewBar, NameNewCandle);
		
		// Candle wick
		ObjectCreate(NameNewWick, OBJ_TREND, 0, TimeNewShadow, PriceNewWick, TimeNewShadow, HighNewBar);
		ObjectSet(NameNewWick, OBJPROP_STYLE, STYLE_SOLID);
		ObjectSet(NameNewWick, OBJPROP_WIDTH, Width);
		ObjectSet(NameNewWick, OBJPROP_RAY, false);
		ColorShadow(OpenNewBar, CloseNewBar, NameNewWick);
		
		// Candle tail
		ObjectCreate(NameNewTail, OBJ_TREND, 0, TimeNewShadow, PriceNewTail, TimeNewShadow, LowNewBar);
		ObjectSet(NameNewTail, OBJPROP_STYLE, STYLE_SOLID);
		ObjectSet(NameNewTail, OBJPROP_WIDTH, Width);
		ObjectSet(NameNewTail, OBJPROP_RAY, false);
		ColorShadow(OpenNewBar, CloseNewBar, NameNewTail);
	}
	
	//+------------------------------- Comment OHLC ----------------------------------------------+
	if (DisplayComment)
		Comment(Symbol() + ", M" + TimeFrame + "     O = " + 
				DoubleToStr(OpenNewBar, Digits) + ",   H = " + 
				DoubleToStr(HighNewBar, Digits) + ",   L = " + 
				DoubleToStr(LowNewBar, Digits) + ",   C = " + 
				DoubleToStr(CloseNewBar, Digits) + "\n");
			
	return(0);
}

//+--------------------- Main Function "New Bar or Old Bar" --------------------------------------+
bool BarNew (int j, int tmf)
{
	int t0 = 1440 * (TimeDayOfWeek(Time[j]) - 1) + 60 * TimeHour(Time[j]) + TimeMinute(Time[j]);
	int t1 = 1440 * (TimeDayOfWeek(Time[j+1]) - 1) + 60 * TimeHour(Time[j+1]) + TimeMinute(Time[j+1]);
	
	if (MathMod(t0, tmf) - MathMod(t1, tmf) == t0 - t1)
		return(false);
	else 
		return(true);
}

//+--------------------- Function "Price Shadow" -------------------------------------------------+
double PriceShadow (double OpnB, double ClsB, int swt)
{
	double prH, prL;
	if (OpnB < ClsB)
	{
		prH = ClsB;
		prL = OpnB;
	}
	if (OpnB > ClsB)
	{
		prH = OpnB;
		prL = ClsB;
	}
	if (OpnB == ClsB)
	{
		prH = ClsB;
		prL = ClsB;
	}
	switch (swt)
	{
		case 0:
			return(prH);
			break;
		case 1:
			return(prL);
			break;
	}
}

//+--------------------- Function "Property Bars" ------------------------------------------------+
void PropBar (double OpPr, double ClPr, string NmOBJ)
{
	string Cl = " Close=" + DoubleToStr(ClPr, Digits);
	string Op = " Open=" + DoubleToStr(OpPr, Digits);
	if (OpPr == ClPr)
	{
		ObjectSet(NmOBJ, OBJPROP_BACK, false);
		ObjectSet(NmOBJ, OBJPROP_COLOR, DojiColor);
		ObjectSetText(NmOBJ, "Doji " + Op + Cl, 7, "Tahoma");
	}
	if (OpPr < ClPr)
	{
		ObjectSet(NmOBJ, OBJPROP_COLOR, UpCandle);
		ObjectSet(NmOBJ, OBJPROP_BACK, BGCandle);
		ObjectSetText(NmOBJ, "UpBar " + Op + Cl, 7, "Tahoma");
	}
	if (OpPr > ClPr)
	{
		ObjectSet(NmOBJ, OBJPROP_COLOR, DnCandle);
		ObjectSet(NmOBJ, OBJPROP_BACK, BGCandle);
		ObjectSetText(NmOBJ, "DnBar " + Op + Cl, 7, "Tahoma");
	}
	ObjectSet(NmOBJ, OBJPROP_WIDTH, Width);
}

//+---------------------- Function "Color Shadow" ------------------------------------------------+
void ColorShadow (double OP, double CP, string NOBJ)
{
	if (OP == CP)	ObjectSet(NOBJ, OBJPROP_COLOR, DojiColor);
	if (OP < CP)	ObjectSet(NOBJ, OBJPROP_COLOR, UpCandle);
	if (OP > CP)	ObjectSet(NOBJ, OBJPROP_COLOR, DnCandle);
}


Comments