//===================================================================
//                                                        Spread mq4
//                                                   SpreadIndicator
//
//                Copyright © 2010 MeinMetatrader.de 
// Mods:
// 05/20/10 handle 5 digit pipettes
// 05/20/10 use static vars for real time processing
// 05/20/10 show max spread
// 05/25/10 show max ask per bar
// 09/15/10 remove all the above
// 09/15/10 show spread, max spread, min spread, max range
// 09/16/10 reformat output
// 04/24/13 Added text color (DerkWehler - V2_0917)
//===================================================================

#property copyright ""
#property link      ""
#property indicator_chart_window
#property indicator_buffers 1
//#property indicator_color1 Yellow // not used

// Inputs
extern color LabelColor = Yellow;

// Global Vars
double MaxAskBuf[];
bool bHasPipettes = false;
string sVer = "V2_0917";
string sObjName1 = "SpreadNow";
string sObjName2 = "SpreadMax";
string sObjName3 = "SpreadMin";
string sObjName4 = "SpreadMR";   // max range
string sOutLine1 = "x";
string sOutLine2 = "x";
string sOutLine3 = "x";
string sOutLine4 = "x";
double dThsSpr = 0;
double dMaxSpr = 0;
double dMinSpr = 9999.0;
double dThsRng = 0;
double dMaxRng = -9999.0;
int iDigits=0;
double dPoint=0;


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
int init()
{
	SetIndexStyle(0,DRAW_ARROW,0,2);
	SetIndexBuffer(0,MaxAskBuf); 
	SetIndexArrow(0,158);  //159=small dot  158=tiny dot (too tiny to see)
	SetIndexLabel(0,"NotUsed");

	GetPoint(Symbol());
	Print("Spread entered, ver: " + sVer + ", point: " + DoubleToStr(dPoint,iDigits));
	return(0);
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
void deinit()
{
	ObjectDelete(sObjName1);
	ObjectDelete(sObjName2);
	ObjectDelete(sObjName3);
	ObjectDelete(sObjName4);         
	return;      
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
int start()
{
	dThsSpr = Ask - Bid;
	if (dThsSpr > dMaxSpr) dMaxSpr = dThsSpr;
	if (dThsSpr < dMinSpr) dMinSpr = dThsSpr;
	dThsRng = dMaxSpr - dMinSpr;
	if (dThsRng > dMaxRng) dMaxRng = dThsRng;
	sOutLine1 = "SPR: " + DoubleToStr(dThsSpr/dPoint,1);
	sOutLine2 = "MAX: " + DoubleToStr(dMaxSpr/dPoint,1);
	sOutLine3 = "MIN: " + DoubleToStr(dMinSpr/dPoint,1);
	sOutLine4 = "RNG: " + DoubleToStr(dMaxRng/dPoint,1);

	if (ObjectFind(sObjName1) < 0)
	{
		ObjectCreate(sObjName1, OBJ_LABEL, 0, 0, 0);
		ObjectSet(sObjName1, OBJPROP_CORNER, 3);   // Upper Left=0; Upper Right=1; Lower Left=2; Lower Right=3
		ObjectSet(sObjName1, OBJPROP_YDISTANCE, 42);
		ObjectSet(sObjName1, OBJPROP_XDISTANCE, 10);
		ObjectSetText(sObjName1, sOutLine1, 9, "Arial", LabelColor);
	}
	ObjectSetText(sObjName1, sOutLine1);

	if (ObjectFind(sObjName2) < 0)
	{
		ObjectCreate(sObjName2, OBJ_LABEL, 0, 0, 0);
		ObjectSet(sObjName2, OBJPROP_CORNER, 3);   // Upper Left=0; Upper Right=1; Lower Left=2; Lower Right=3
		ObjectSet(sObjName2, OBJPROP_YDISTANCE, 29);
		ObjectSet(sObjName2, OBJPROP_XDISTANCE, 10);
		ObjectSetText(sObjName2, sOutLine2, 9, "Arial", LabelColor);
	}
	ObjectSetText(sObjName2, sOutLine2);

	if (ObjectFind(sObjName3) < 0)
	{
		ObjectCreate(sObjName3, OBJ_LABEL, 0, 0, 0);
		ObjectSet(sObjName3, OBJPROP_CORNER, 3);   // Upper Left=0; Upper Right=1; Lower Left=2; Lower Right=3
		ObjectSet(sObjName3, OBJPROP_YDISTANCE, 16);
		ObjectSet(sObjName3, OBJPROP_XDISTANCE, 10);
		ObjectSetText(sObjName3, sOutLine3, 9, "Arial", LabelColor);
	}
	ObjectSetText(sObjName3, sOutLine3);

	if (ObjectFind(sObjName4) < 0)
	{
		ObjectCreate(sObjName4, OBJ_LABEL, 0, 0, 0);
		ObjectSet(sObjName4, OBJPROP_CORNER, 3);   // Upper Left=0; Upper Right=1; Lower Left=2; Lower Right=3
		ObjectSet(sObjName4, OBJPROP_YDISTANCE, 3);
		ObjectSet(sObjName4, OBJPROP_XDISTANCE, 10);
		ObjectSetText(sObjName4, sOutLine3, 9, "Arial", LabelColor);
	}
	ObjectSetText(sObjName4, sOutLine4);   
	WindowRedraw();
	return(0);   
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
void GetPoint(string sym)
{
	// this only works on fx, not futures
	iDigits = MarketInfo(sym, MODE_DIGITS);

	int iLoc = StringFind(sym,"JPY",0);
	if (iLoc < 0)  // not a yen cross
	{
		dPoint = 0.0001;
		if (iDigits == 4)
			bHasPipettes = false;
		if (iDigits == 5)
			bHasPipettes = true;
	}
	else  // ok it is a yen
	{
		dPoint = 0.01;
		iDigits = 2;
		if (iDigits == 2)
			bHasPipettes = false;
		if (iDigits == 3)
			bHasPipettes = true;
	}
	return;
}

