//
// "00-Tick_v101.mq4" -- draw tick
//
//    Ver. 1.00  2008/10/1(Wed)
//    Ver. 1.02  2010/05/20(Thu)  draw symbol name and price
// 
// 
#property copyright  "00mql4@gmail.com"
#property link       "http://00mql4.blogspot.com/"

//---- defines

//---- indicator settings
#property  indicator_separate_window

#property  indicator_buffers  3

#property  indicator_color1  DimGray
#property  indicator_color2  DodgerBlue
#property  indicator_color3  Crimson

#property  indicator_width1  1
#property  indicator_width2  1
#property  indicator_width3  1

//---- indicator parameters
extern string  symbol           = "";              // symbol name
extern int     nTick            = 100;             // number of ticks
extern int     nGridMinutes     = 5;               // minutes per grid
extern int     nGridBox         = 50;              // number of grid boxes
extern bool    bUpDownColorize  = true;            // colorize
extern color   colGrid          = 0x402010;        // grid color
extern bool    bDrawSymbol      = true;
extern bool    bDrawPrice       = true;
extern color   colSymbol        = Yellow;
extern int     cornerSymbol     = 1;
extern int     xSymbol          = 64;
extern int     ySymbol          = 8;
extern color   colPrice         = Gold;
extern int     cornerPrice      = 1;
extern int     xPrice           = 8;
extern int     yPrice           = 8;
extern string  fontName         = "MS UI Gothic";  // font name
extern int     fontSize         = 10;              // font size
extern int     window           = -1;

//---- indicator buffers
double BufferTick[];
double BufferUp[];
double BufferDown[];

//---- vars
string   sIndicatorName  = "00-Tick_v101";
string   sPrefix;
datetime g_serial;
double   g_Tick[];
datetime g_Time[];
string   g_sIndicatorName;
int      g_window;
double   point;
int      digits;

//----------------------------------------------------------------------
void init()
{
    int i;
    
    if (symbol == "") {
	symbol = Symbol();
    }
    
    SetIndexStyle(0, DRAW_LINE);
    SetIndexStyle(1, DRAW_LINE);
    SetIndexStyle(2, DRAW_LINE);
    
    SetIndexBuffer(0, BufferTick);
    SetIndexBuffer(1, BufferUp);
    SetIndexBuffer(2, BufferDown);
    
    ArrayResize(g_Tick, nTick);
    ArrayResize(g_Time, nTick);
    
    if (StringFind(symbol, "JPY") >= 0) {
	point = 0.01;
    } else {
	point = 0.0001;
    }
    
    digits = MarketInfo(symbol, MODE_DIGITS);
    
    reset();
}

//----------------------------------------------------------------------
void reset()
{
    double v = Close[0];
    
    datetime t = TimeLocal();
    
    for (int i = 0; i < nTick; i++) {
	int shift = iBarShift(symbol, 1, Time[0] - i * 60);
	v = iClose(symbol, 1, shift);
	if (v <= 0 && i > 0) {
	    v = g_Tick[i - 1];
	}
	g_Tick[i] = v;
	g_Time[i] = t - i * 60;
    }
    
    for (i = Bars; i >= 0; i--) {
	BufferTick[i] = EMPTY_VALUE;
	BufferUp[i]   = EMPTY_VALUE;
	BufferDown[i] = EMPTY_VALUE;
    }
}

//----------------------------------------------------------------------
void deinit()
{
    int i;
    int n = ObjectsTotal();
    
    for (i = n - 1; i >= 0; i--) {
	string sName = ObjectName(i);
	if (StringFind(sName, sPrefix) == 0) {
	    ObjectDelete(sName);
	}
    }
}

//----------------------------------------------------------------------
string getBoxName(int i)
{
    return(sPrefix + " " + i);
}

//----------------------------------------------------------------------
void objInit()
{
    for (int i = 0; i < nGridBox; i++) {
	string s = getBoxName(i);
	ObjectCreate(s, OBJ_RECTANGLE, g_window, 0, 0);
	ObjectSet(s, OBJPROP_STYLE, STYLE_SOLID);
    }
}

//----------------------------------------------------------------------
void objLabel(string sName, int corner, int x, int y, string text, color col, int window,
	      int size = 0, string font = "")
{
    sName = sPrefix + " " + sName;
    
    if (size == 0) {
	size = fontSize;
    }
    if (font == "") {
	font = fontName;
    }
    
    ObjectCreate(sName, OBJ_LABEL, window, 0, 0);
    ObjectSetText(sName, text, size, font, col);
    ObjectSet(sName, OBJPROP_CORNER, corner);
    ObjectSet(sName, OBJPROP_XDISTANCE, x);
    ObjectSet(sName, OBJPROP_YDISTANCE, y);
}

//----------------------------------------------------------------------
void start()
{
    if (g_serial == 0) {
	g_serial = TimeLocal();
    
	if (window == -1) {
	    g_window = WindowFind(sIndicatorName);
	    if (g_window <= 0) {
		g_window = WindowOnDropped();
	    }
	} else {
	    g_window = window;
	}
    
	g_sIndicatorName = sIndicatorName + "(" + symbol + ")";
	sPrefix = g_sIndicatorName + " " + g_serial + " " + g_window;
	
	IndicatorShortName(g_sIndicatorName);
	
	objInit();
    }
    
    for (int i = nTick - 1; i > 0; i--) {
	g_Tick[i] = g_Tick[i - 1];
	g_Time[i] = g_Time[i - 1];
    }
    double p;
    if (symbol != Symbol()) {
	p = iClose(symbol, 1, 0);
    } else {
	p = Close[0];
    }
    g_Tick[0] = p;
    g_Time[0] = TimeLocal();
    
    for (i = nTick + 10; i >= nTick; i--) {
	BufferTick[i] = EMPTY_VALUE;
	BufferUp[i]   = EMPTY_VALUE;
	BufferDown[i] = EMPTY_VALUE;
    }
    for (i = nTick - 1; i >= 0; i--) {
	double v0 = g_Tick[i + 0];
	double v1 = g_Tick[i + 1];
	    
	BufferTick[i] = v0;
	BufferUp[i]   = EMPTY_VALUE;
	BufferDown[i] = EMPTY_VALUE;
	    
	if (bUpDownColorize) {
	    if (v0 > v1) {
		BufferUp[i] = v0;
	    } else if (v0 < v1) {
		BufferDown[i] = v0;
	    }
	}
    }
	
    if (bDrawSymbol) {
	string s;
	if (bDrawSymbol) {
	    s = symbol;
	} else {
	    s = "";
	}
	objLabel("symbol", cornerSymbol, xSymbol, ySymbol, s, colSymbol, g_window);
	    
	if (bDrawPrice) {
	    double price;
	    if (symbol != Symbol()) {
		price = iClose(symbol, 1, 0);
	    } else {
		price = Close[0];
	    }
	    s = DoubleToStr(price, digits);
	} else {
	    s = "";
	}
	objLabel("price", cornerPrice, xPrice, yPrice, s, colPrice, g_window);
    }
    
    // grid
    {
	datetime ts = MathFloor(g_Time[0] / (nGridMinutes * 60));
	datetime te;
	int n = 0;
	int idx[];
	ArrayResize(idx, nGridBox * 2);
	if ((g_Time[0] % (2 * nGridMinutes * 60)) < nGridMinutes * 60) {
	    idx[0] = 0;
	    n = 1;
	}
	for (i = 0; i < nTick; i++) {
	    te = MathFloor(g_Time[i] / (nGridMinutes * 60));
	    if (te == ts) {
		continue;
	    }
	    ts = te;
	    idx[n] = i;
	    n++;
	    if (n >= nGridBox * 2) {
		break;
	    }
	}
	    
	for (i = 0; i < nGridBox; i++) {
	    s = getBoxName(i);
	    ts = Time[idx[i * 2 + 0]];
	    te = Time[idx[i * 2 + 1]];
	    double lo = 0;
	    double hi = 1000;
	    if (i * 2 >= n) {
		lo = 0;
		hi = 0;
	    }
	    ObjectMove(s, 0, ts, lo);
	    ObjectMove(s, 1, te, hi);
	    ObjectSet(s, OBJPROP_COLOR, colGrid);
	}
	    
	WindowRedraw();
    }
}
