//
//
#property  copyright "00mql4@gmail.com"
#property  link      "http://www.mql4.com/"

#property indicator_chart_window

// corner: 0
//   10 100 170
//
// corner: 1
//             145  70  10
// 
// 

extern string symbol     = "";          // "" for current symbol, ex. "USDJPY"
extern int corner        = 1;           // anchor point, 0: top left, 1: top right, 2: bottom left, 3: bottom right
extern int yPosCount     = -1;          // counter for y position, -1: auto
extern int window        = 0;           // window, 0: main, 1: 2nd, 2: 3rd,...
extern double point      = 0;           // 0: auto, ex. 0.001 for 3 decimal places
extern int xSymbol       = -1;          // x position of the symbol
extern int ySymbol       = -1;          // y position of the symbol
extern int dxPrice       = 75;          // distance from the sympol in x direction
extern int dyPrice       = 0;           // distance from the sympol in y direction
extern int dxChange      = 144;         // distance from the sympol in x direction
extern int dyChange      = 0;           // distance from the sympol in x direction
extern color colSymbol   = PowderBlue;  // color for the symbol
extern color colCur      = White;       // color for the symbol
extern color colUp       = Lime;        // color for the change in case of raise
extern color colDown     = Red;         // color for the change in case of fall
extern color colUnchange = Yellow;      // color for the change in case of no change
extern string fontName   = "Arial";     // font name
extern int fontSize      = 16;          // font size
extern int yGap          = 4;           // gap pixels to next symbol

int nChart            = 0;
int nSerial           = 0;
bool bInc             = false;
string sIndicatorName = "00-NetChange";
string sVarSerial     = "00-NetChange nSerial";
string sVarChart      = "00-NetChange nChart";
string sVarPosCount   = "00-NetChange yPosCount";
string sObjBase       = "00-NetChange ";
string sym;
string sObjSymbol;
string sObjCur;
string sObjChange;

//----------------------------------------------------------------------
int init()
{
    nSerial = GlobalVariableGet(sVarSerial);
    GlobalVariableSet(sVarChart, nSerial + 1);
    
    nChart = GlobalVariableGet(sVarChart);
    GlobalVariableSet(sVarChart, nChart + 1);
    
    if (yPosCount == -1) {
	yPosCount = GlobalVariableGet(sVarPosCount);
	GlobalVariableSet(sVarPosCount, yPosCount + 1);
	bInc = true;
    }
    
    sym = symbol;
    if (sym == "") {
	sym = Symbol();
    }
    
    IndicatorShortName(sObjBase + sym);
    
    // name of objects
    sObjSymbol = sObjBase + sym + " " + nSerial + " symbol";
    sObjCur    = sObjBase + sym + " " + nSerial + " cur";
    sObjChange = sObjBase + sym + " " + nSerial + " change";
    
    // create objects
    ObjectCreate(sObjSymbol, OBJ_LABEL, window, 0, 0);
    ObjectCreate(sObjCur,    OBJ_LABEL, window, 0, 0);
    ObjectCreate(sObjChange, OBJ_LABEL, window, 0, 0);
    
    // dir
    int sx = 1;
    int sy = 1;
    if (corner == 1 || corner == 3) {
	sx = -1;
    }
    if (corner == 2 || corner == 3) {
	sy = -1;
    }
    
    // symbol
    int px = xSymbol;
    int py = ySymbol;
    if (px == -1) {
	if (corner == 0 || corner == 2) {
	    px = 10;
	} else {
	    px = 145;
	}
    }
    if (py == -1) {
	py = yPosCount * (fontSize + yGap);
    }
    ObjectSet(sObjSymbol, OBJPROP_CORNER, corner);
    ObjectSet(sObjSymbol, OBJPROP_XDISTANCE, px);
    ObjectSet(sObjSymbol, OBJPROP_YDISTANCE, py);
    ObjectSetText(sObjSymbol, sym, fontSize, fontName, colSymbol);
    
    // current price
    int x = px + dxPrice * sx;
    int y = py + dyPrice * sy;
    ObjectSet(sObjCur, OBJPROP_CORNER, corner);
    ObjectSet(sObjCur, OBJPROP_XDISTANCE, x);
    ObjectSet(sObjCur, OBJPROP_YDISTANCE, y);
    
    // change
    x = px + dxChange * sx;
    y = py + dyChange * sy;
    ObjectSet(sObjChange, OBJPROP_CORNER, corner);
    ObjectSet(sObjChange, OBJPROP_XDISTANCE, x);
    ObjectSet(sObjChange, OBJPROP_YDISTANCE, y);
    
    return(0);
}
  
//-------------------------------------------------------- 
int deinit()
{
    ObjectDelete(sObjSymbol);
    ObjectDelete(sObjCur);
    ObjectDelete(sObjChange);

    if (bInc) {
	double yPosCount = GlobalVariableGet(sVarPosCount);
	GlobalVariableSet(sVarPosCount, yPosCount - 1);
    }
    
    nChart = GlobalVariableGet(sVarChart);
    GlobalVariableSet(sVarChart, nChart - 1);
    if (nChart == 1) {
	GlobalVariableDel(sVarPosCount);
	GlobalVariableDel(sVarChart);
	GlobalVariableDel(sVarSerial);
    }
    
    return(0);
}

//----------------------------------------------------------------------
int start()
{
    int dayMinute = 60 * 24;
    double price[][6];
    
    ArrayCopyRates(price, sym, dayMinute);   // [0]time, [1]open, [2]low, [3]high, [4]close, [5]volume
    
    int n;
    if (point == 0) {
	point = MarketInfo(sym, MODE_POINT);
    }
    n = MathLog(1.0 / point) / MathLog(10);
    
    double pPrev = price[1][4];
    string sPrev = DoubleToStr(pPrev, n);
    
    double pCur = iClose(sym, 0, 0);
    string sCur = DoubleToStr(pCur, n);
    
    double pChange = pCur - pPrev;
    string sChange = DoubleToStr(pChange, n);
    
    color c;
    
    if (pChange > 0) {
	sChange = "+" + sChange;
	c = colUp;
    } else if (pChange < 0) {
	c = colDown;
    } else {
	c = colUnchange;
    }
    
    ObjectSetText(sObjCur,    sCur,    fontSize, fontName, colCur);
    ObjectSetText(sObjChange, sChange, fontSize, fontName, c);
}
