//
// "00-ShowPips_v104.mq4" -- 
// 
//    Ver. 0.01  2008/11/28(Fri)  initial version
//    Ver. 1.00  2008/11/28(Fri)  release version
//    Ver. 1.01  2008/12/01(Mon)  added bShowVolume, server name
//    Ver. 1.02  2009/04/13(Mon)  adapted to 121sec, added "." to symbol
//    Ver. 1.03  2009/05/17(Sun)  adapted bSortBySpread
//    Ver. 1.04  2010/05/26(Wed)  Digits
// 
// 
#property copyright  "00mql4@gmail.com"
#property link       "http://00mql4.blogspot.com/"

//---- indicator settings
#property  indicator_chart_window

#property  indicator_buffers  0

//---- defines
#define N_FX_MAX    8
#define N_PAIR_MAX  28  // = 8C2

//---- indicator parameters
extern bool   bCheckMinorPair  = true;             // check minor pairs
extern bool   bUseServerSpread = true;             // use spread taken from MarketInfo() (or g_sPair[][1])
extern bool   bSortBySpread    = true;             // sort by spread
extern bool   bSortByRatio     = true;             // sort by ratio or pips when bSortBySpread is false
extern bool   bShowVolume      = true;             // show Volume[]
extern int    nCheckBar        = 20;               // check hi/lo range
extern int    nShowPair        = 0;                // number of pair to show
extern int    corner           = 1;                // corner point
extern int    xStart           = 8;                // x start
extern int    yStart           = 20;               // y start
extern int    yMargin          = 2;                // margin between lines
extern string fontName         = "FixedSys";  // font name
extern int    fontSize         = 14;               // font size
extern color  fontColor        = Orange;           // font color
extern string serverFontName   = "Arial Black";    // font name for server name
extern int    serverFontSize   = 10;               // font size for server name
extern color  serverFontColor  = DarkOrange;       // font color for server name
extern string sSymPrefix       = "";               // symbol prefix, "xx" for "xxUSDJPY"
extern string sSymPostfix      = "";               // symbol postfix, "yy" for "USDJPYyy"

//---- indicator buffers

//---- vars
string sIndicatorName = "00-ShowPips_v103";
string sPrefix;
int    N_FX;
int    N_PAIR;
color  colJPY = Red;
color  colUSD = White;
color  colEUR = Aqua;
color  colGBP = Yellow;
color  colCHF = Magenta;
color  colAUD = Lime;
color  colNZD = Green;
color  colCAD = DodgerBlue;
string g_sFx[N_FX_MAX]  = {
    "JPY",
    "USD",
    "EUR",
    "GBP",
    "CHF",
    "AUD",
    "NZD",
    "CAD"
};
string g_sPair[N_PAIR_MAX][2] = {    // pair and spread
    // major pairs
    // JPY
    "USDJPY", "2.0",  //  0
    "EURJPY", "3.5",  //  1
    "GBPJPY", "6.0",  //  2
    "CHFJPY", "3.0",  //  3
    // USD		   
    "EURUSD", "3.0",  //  7
    "GBPUSD", "4.0",  //  8
    "USDCHF", "4.0",  //  9
    // EUR		   
    "EURGBP", "3.0",  // 13
    "EURCHF", "3.0",  // 14
    // GBP		   
    "GBPCHF", "7.0",  // 18
    // CHF
    // none
    
    // minor pairs
    // JPY
    "AUDJPY", "4.0",  //  4
    "NZDJPY", "6.0",  //  5
    "CADJPY", "4.0",  //  6
    // USD
    "AUDUSD", "3.0",  // 10
    "NZDUSD", "4.0",  // 11
    "USDCAD", "4.0",  // 12
    // EUR
    "EURAUD", "7.0",  // 15
    "EURNZD", "8.0",  // 16
    "EURCAD", "8.0",  // 17
    // GBP
    "GBPAUD", "8.0",  // 19
    "NZDGBP", "4.0",  // 20
    "GBPCAD", "8.0",  // 21
    // CHF
    "AUDCHF", "6.0",  // 22
    "NZDCHF", "7.0",  // 23
    "CADCHF", "6.0",  // 24
    // AUD
    "AUDNZD", "9.0",  // 25
    "AUDCAD", "6.0",  // 26
    // NZD
    "NZDCAD", "8.0"  // 27
    // CAD
    // none
};
double g_pips[N_PAIR_MAX];
double g_spread[N_PAIR_MAX];
double g_point[N_PAIR_MAX];
double g_digits[N_PAIR_MAX];
double g_hi[N_PAIR_MAX];
double g_lo[N_PAIR_MAX];

//----------------------------------------------------------------------
void init()
{
    sPrefix = sIndicatorName;
    
    IndicatorShortName(sIndicatorName);
    
    if (bCheckMinorPair) {
	N_FX = 8;     // 8 pairs, JPY/USD/EUR/GBP/CHF/AUD/NZD/CAD
	N_PAIR = 28;  // 8C2
    } else {
	N_FX = 5;     // 5 pairs, JPY/USD/EUR/GBP/CHF
	N_PAIR = 10;  // 5C2
    }
    
    nCheckBar = MathMax(nCheckBar, 1);
    nShowPair = MathMin(MathMax(nShowPair, 0), N_PAIR);
    
    if (sSymPrefix == "" && sSymPostfix == "") {
	// for 121sec
	if (StringFind(AccountServer(), "121") >= 0) {
	    for (int i = 0; i < N_PAIR; i++) {
		g_sPair[i][0] = g_sPair[i][0] + ".";
	    }
	}
    } else {
	for (i = 0; i < N_PAIR; i++) {
	    g_sPair[i][0] = sSymPrefix + g_sPair[i][0] + sSymPostfix;
	}
    }
}

//----------------------------------------------------------------------
void deinit()
{
    int n = ObjectsTotal();
    for (int i = n - 1; i >= 0; i--) {
	string sName = ObjectName(i);
	if (StringFind(sName, sPrefix) == 0) {
	    ObjectDelete(sName);
	}
    }
}

//----------------------------------------------------------------------
void objLabel(string sName, int corner, int x, int y, string text, color col, int size = 0, string font = "")
{
    sName = sPrefix + sName;
    
    if (size == 0) {
	size = fontSize;
    }
    if (font == "") {
	font = fontName;
    }
    
    ObjectCreate(sName, OBJ_LABEL, 0, 0, 0);
    ObjectSetText(sName, text, size, font, col);
    ObjectSet(sName, OBJPROP_CORNER, corner);
    ObjectSet(sName, OBJPROP_XDISTANCE, x);
    ObjectSet(sName, OBJPROP_YDISTANCE, y);
}

//----------------------------------------------------------------------
void checkPips()
{
    bool bDigits35 = false;
    if (Digits == 3 || Digits == 5) {
	bDigits35 = true;
    }
    
    for (int i = 0; i < N_PAIR; i++) {
	string sPair = g_sPair[i][0];
	double point = MarketInfo(sPair, MODE_POINT);
	double hi = iHigh(sPair, 0, iHighest(sPair, 0, MODE_HIGH, nCheckBar, 0));
	double lo = iLow(sPair, 0, iLowest(sPair, 0, MODE_LOW, nCheckBar, 0));
	double range = hi - lo;
	int digits = MarketInfo(sPair, MODE_DIGITS);
	if (bDigits35) {
	    digits -= 1;
	    point *= 10;
	}
	
	g_hi[i] = hi;
	g_lo[i] = lo;
	g_pips[i] = range;
	g_point[i] = point;
	g_digits[i] = digits;
	if (point != 0) {
	    g_pips[i] /= point;
	}
	if (bUseServerSpread) {
	    //double spread = MarketInfo(sPair, MODE_ASK) - MarketInfo(sPair, MODE_BID);
	    //double spread = MarketInfo(sPair, MODE_ASK) - iClose(sPair, 0, 0);
	    double spread = MarketInfo(sPair, MODE_SPREAD);
	    if (bDigits35) {
		spread *= 0.1;
	    }
	    if (point == 0) {
		g_spread[i] = spread * 0.01;
	    } else {
		g_spread[i] = spread;
	    }
	} else {
	    g_spread[i] = StrToDouble(g_sPair[i][1]);
	}
    }
    
    for (; i < N_PAIR_MAX; i++) {
	g_pips[i] = 0;
    }
}

//----------------------------------------------------------------------
double getVolume(string sPair)
{
    double v = 0;
    for (int i = 0; i < nCheckBar; i++) {
	v += iVolume(sPair, 0, i);
    }
    
    return(v / nCheckBar);
}

//----------------------------------------------------------------------
void start()
{
    int i;
    
    checkPips();
    
    double ratio[N_PAIR_MAX];
    double pips[N_PAIR_MAX];
    double spread[N_PAIR_MAX];
    for (i = 0; i < N_PAIR; i++) {
	pips[i] = g_pips[i] - g_spread[i] * 0.0001;
	if (g_spread[i] != 0) {
	    ratio[i] = g_pips[i] / g_spread[i] - g_spread[i] * 0.0001;
	} else {
	    ratio[i] = g_pips[i];
	}
	spread[i] = g_spread[i] - ratio[i] * 0.0001;
    }
    for (; i < N_PAIR_MAX; i++) {
	ratio[i] = -1;
    }

    int tx = xStart;
    int ty = yStart;
    string sHeader = StringConcatenate("ratio pips(spread) n=", nCheckBar);
    string sSepa   =                  "------------------------";
    if (bShowVolume) {
	sHeader = StringConcatenate("[vol/1] ", sHeader);
	sSepa   = StringConcatenate("---------", sSepa);
    }
    
    objLabel("server", corner, tx, ty, AccountServer(), serverFontColor, serverFontSize, serverFontName);
    ty += fontSize + yMargin;
    objLabel("header", corner, tx, ty, sHeader, fontColor);
    ty += fontSize + yMargin;
    objLabel("sepa", corner, tx, ty, sSepa, fontColor);
    ty += fontSize + yMargin;
    
    int n = 0;
    for (int c = 0; c < N_PAIR; c++) {
	if (!bSortBySpread) {
	    if (bSortByRatio) {
		i = ArrayMaximum(ratio);
	    } else {
		i = ArrayMaximum(pips);
	    }
	} else {
	    i = ArrayMinimum(spread);
	}
	if (MarketInfo(g_sPair[i][0], MODE_BID) != 0.0) {
	    string sPair   = g_sPair[i][0];
	    string sRatio  = DoubleToStr(ratio[i], 1);
	    string sPips   = DoubleToStr(g_pips[i], 0);
	    string sSpread = DoubleToStr(g_spread[i], 1);
	    string sHi     = DoubleToStr(g_hi[i], g_digits[i]);
	    string sLo     = DoubleToStr(g_lo[i], g_digits[i]);
	    string sVolume = DoubleToStr(getVolume(sPair), 1);
	    int len = StringLen(sPips);
	    for (int x = len; x < 4; x++) {
		sPips = StringConcatenate(" ", sPips);
	    }
	    len = StringLen(sSpread);
	    for ( x = len; x < 4; x++) {
		sSpread = StringConcatenate(" ", sSpread);
	    }
	    sRatio = StringConcatenate("x", sRatio);
	    len = StringLen(sRatio);
	    for ( x = len; x < 5; x++) {
		sRatio = StringConcatenate(" ", sRatio);
	    }
	    string s = StringConcatenate(sRatio, " ", sPips, "(", sSpread, ")", " ", sPair);
	    if (bShowVolume) {
		len = StringLen(sVolume);
		for ( x = len; x < 5; x++) {
		    sVolume = StringConcatenate(" ", sVolume);
		}
		s = StringConcatenate("[", sVolume, "] ", s);
	    }
	    //Print("c= ", c, ", sPair= ", sPair, ", s= ", s);
	    objLabel(sPair, corner, tx, ty, s, fontColor);
	    ty += fontSize + yMargin;
	    n++;
	    if (nShowPair > 0 && n >= nShowPair) {
		break;
	    }
	}
	if (!bSortBySpread) {
	    if (bSortByRatio) {
		ratio[i] = -999;
	    } else {
		pips[i] = -999;
	    }
	} else {
	    spread[i] = 999;
	}
    }
}
