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






Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains open prices of each bar
Series array that contains close prices for each bar
Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar


Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:

Other Features:

It issuies visual alerts to the screen