//+------------------------------------------------------------------+
//|                                         enhanced 3_bar_swing.mq4 |
//|                                     Zen_Leow modified by Mariusz |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Zen_Leow modified by Mariusz"
#property link      ""
#property strict

#property indicator_chart_window
#property indicator_buffers 2

// The color for displaying arrows
#property indicator_color1 clrGreen       // Long signal
#property indicator_color2 clrRed         // Short signal

// Width of the arrows
#property indicator_width1 2  // Long signal arrow
#property indicator_width2 2  // Short signal arrow

extern int ArrowDistance = 30; // Arrow distance from bar in points
extern int BarsToCount = 500; // Calculate on how many bars
extern int CheckSignal = 10; // Check Signal from bar 2 to X
extern bool Alerts = true; // Popup alerts on / off
extern bool PushN = true; // Push notifications on / off
string atxt;
bool newcheck;
// Buffers for the calculations
double Up_Arrow_Buffer[];    // Long buffer for display
double Down_Arrow_Buffer[];   // Short buffer for display
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
	SetIndexStyle(0, DRAW_ARROW);
	SetIndexBuffer(0, Up_Arrow_Buffer);
	SetIndexArrow(0, 233); // Up arrow
	SetIndexStyle(1, DRAW_ARROW);
	SetIndexBuffer(1, Down_Arrow_Buffer);
	SetIndexArrow(1, 234); // Down arrow
	if (CheckSignal < 2) CheckSignal = 2;
	newcheck = true;
	return(0);
	//----
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
	//----

	//----
	return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
	//   i = 2;  // the newest signal bar is suppose to be at index 2
	static datetime start_time = 0;
	if (start_time < iTime(NULL, PERIOD_CURRENT, 0))
	{
		newcheck = true;
		start_time = iTime(NULL, PERIOD_CURRENT, 0);
	}

	for (int i = 1; i <= BarsToCount; i++)
	{
		if (isUpSwingBar(i))
		{
			Up_Arrow_Buffer[i] = Low[i] - (ArrowDistance * Point);
			Down_Arrow_Buffer[i] = EMPTY_VALUE;
			atxt = (Symbol() + " " + IntegerToString(Period()) + " 3Bar Swing Signal Up " + TimeToStr(Time[i]));
			if (newcheck && i <= CheckSignal && i >= 2 && Alerts) Alert(atxt);
			if (newcheck && i <= CheckSignal && i >= 2 && PushN) SendNotification(atxt);
		}
		else if (isDownSwingBar(i))
		{
			Down_Arrow_Buffer[i] = High[i] + (ArrowDistance * Point);
			Up_Arrow_Buffer[i] = EMPTY_VALUE;
			atxt = (Symbol() + " " + IntegerToString(Period()) + " 3Bar Swing Signal Down " + TimeToStr(Time[i]));
			if (newcheck && i <= CheckSignal && i >= 2 && Alerts) Alert(atxt);
			if (newcheck && i <= CheckSignal && i >= 2 && PushN) SendNotification(atxt);
		}
		else
		{
			Up_Arrow_Buffer[i] = EMPTY_VALUE;
			Down_Arrow_Buffer[i] = EMPTY_VALUE;
		}
	}
	newcheck = false;
	//----
	return(0);
}

bool isUpSwingBar(int i)
{
	if (Low[i] < Low[i + 1] && Low[i] < Low[i - 1] && High[i] < High[i + 1] && High[i] < High[i - 1] && Close[i - 1]>High[i + 1])
	{
		return (true);
	}
	return (false);
}

bool isDownSwingBar(int i)
{
	if (Low[i] > Low[i + 1] && Low[i] > Low[i - 1] && High[i] > High[i + 1] && High[i] > High[i - 1] && Close[i - 1] < Low[i + 1])
	{
		return (true);
	}
	return (false);
}
//+------------------------------------------------------------------+