//+------------------------------------------------------------------+
//|                                                   ^X_Sensors.mq4 |
//|                                                    Version 2.0.1 |
//|------------------------------------------------------------------|
//|                     Copyright © 2007, Mr.WT, Senior Linux Hacker |
//|                                     http://w-tiger.narod.ru/wk2/ |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2007, Mr.WT, Senior Linux Hacker"
#property link      "http://w-tiger.narod.ru/wk2/"

#property indicator_chart_window
extern int _Shift = 30;
extern color _Color = LightSlateGray;
extern color _P_Color = Gold;
extern color _R_Color = OrangeRed;
extern color _S_Color = RosyBrown;
int _N_Time, MAX_HISTORY, _My_Period, STEP, ObjectId;
string OBJECT_PREFIX = "LEVELS";
//-------------------------------------------------------------------------------------------
int init() {

_My_Period = PERIOD_W1;
if(Period() > PERIOD_H4) {
  _My_Period = PERIOD_MN1;
} else if(Period() < PERIOD_H4) {
  _My_Period = PERIOD_D1;
}

MAX_HISTORY = 1440;
if(Period() == PERIOD_MN1) {
   MAX_HISTORY = 120;
} else if(Period() == PERIOD_W1) {
   MAX_HISTORY = 240;
} else if(Period() == PERIOD_D1) {
   MAX_HISTORY = 240;
} else if(Period() == PERIOD_H4) {
   MAX_HISTORY = 360;
}

_N_Time = 0;

ObjectCreate("S1 line", OBJ_TREND, 0, Time[_Shift], 0, Time[0], 0);
ObjectCreate("S2 line", OBJ_TREND, 0, Time[_Shift], 0, Time[0], 0);
ObjectCreate("S3 line", OBJ_TREND, 0, Time[_Shift], 0, Time[0], 0);
ObjectCreate("R1 line", OBJ_TREND, 0, Time[_Shift], 0, Time[0], 0);
ObjectCreate("R2 line", OBJ_TREND, 0, Time[_Shift], 0, Time[0], 0);
ObjectCreate("R3 line", OBJ_TREND, 0, Time[_Shift], 0, Time[0], 0);
ObjectCreate( "P line", OBJ_TREND, 0, Time[_Shift], 0, Time[0], 0);

ObjectSet("S1 line", OBJPROP_RAY, 1);
ObjectSet("S1 line", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("S1 line", OBJPROP_COLOR, _S_Color);

ObjectSet("S2 line", OBJPROP_RAY, 1);
ObjectSet("S2 line", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("S2 line", OBJPROP_COLOR, _S_Color);

ObjectSet("S3 line", OBJPROP_RAY, 1);
ObjectSet("S3 line", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("S3 line", OBJPROP_COLOR, _S_Color);

ObjectSet("R1 line", OBJPROP_RAY, 1);
ObjectSet("R1 line", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("R1 line", OBJPROP_COLOR, _R_Color);

ObjectSet("R2 line", OBJPROP_RAY, 1);
ObjectSet("R2 line", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("R2 line", OBJPROP_COLOR, _R_Color);

ObjectSet("R3 line", OBJPROP_RAY, 1);
ObjectSet("R3 line", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("R3 line", OBJPROP_COLOR, _R_Color);

ObjectSet("P line", OBJPROP_RAY, 1);
ObjectSet("P line", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("P line", OBJPROP_COLOR, _P_Color);

ObjectCreate("R1 label", OBJ_TEXT, 0, Time[0], 0);
ObjectCreate("R2 label", OBJ_TEXT, 0, Time[0], 0);
ObjectCreate("R3 label", OBJ_TEXT, 0, Time[0], 0);
ObjectCreate("S1 label", OBJ_TEXT, 0, Time[0], 0);
ObjectCreate("S2 label", OBJ_TEXT, 0, Time[0], 0);
ObjectCreate("S3 label", OBJ_TEXT, 0, Time[0], 0);
ObjectCreate( "P label", OBJ_TEXT, 0, Time[0], 0);

STEP = 1;
ObjectId = 0;
return(0);
}
//-------------------------------------------------------------------------------------------
int deinit() {

Comment("");
ObjectDelete("R1 label");
ObjectDelete("R1 line");
ObjectDelete("R2 label");
ObjectDelete("R2 line");
ObjectDelete("R3 label");
ObjectDelete("R3 line");
ObjectDelete("S1 label");
ObjectDelete("S1 line");
ObjectDelete("S2 label");
ObjectDelete("S2 line");
ObjectDelete("S3 label");
ObjectDelete("S3 line");
ObjectDelete("P label");
ObjectDelete("P line");
ObDeleteObjectsByPrefix(OBJECT_PREFIX);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {

if ( _N_Time == Time[0] ) return(0);

double _close, _high, _low, P, R1, R2, R3, S1, S2, S3;
double _rates[][6];

ArrayCopyRates(_rates, NULL, _My_Period);
int err = GetLastError();
if(err == 4066) {
  Sleep(1000);
  if(iClose(NULL, _My_Period, 0) != Close[0]) {
    Sleep(1000);
    return(0);
  }
}
_close = _rates[1][4];
_high = _rates[1][3];
_low = _rates[1][2];
P = (_high + _low + _close) / 3;
R1 = 2*P - _low;
S1 = 2*P - _high;
R2 = P + _high - _low;
S2 = P - _high + _low;
R3 = (P - _low)*2 + _high;
S3 = (P - _high)*2 + _low;
string s1,s2,s3,r1,r2,r3,p;
if(Point == 0.01) {
  s1 = DoubleToStr(S1, 2);
  s2 = DoubleToStr(S2, 2);
  s3 = DoubleToStr(S3, 2);
  r1 = DoubleToStr(R1, 2);
  r2 = DoubleToStr(R2, 2);
  r3 = DoubleToStr(R3, 2);
  p = DoubleToStr(P, 2);
} else {
  s1 = DoubleToStr(S1, 4);
  s2 = DoubleToStr(S2, 4);
  s3 = DoubleToStr(S3, 4);
  r1 = DoubleToStr(R1, 4);
  r2 = DoubleToStr(R2, 4);
  r3 = DoubleToStr(R3, 4);
  p = DoubleToStr(P, 4);
}
//---- Pivot Lines
ObjectSetText("R1 label", "              R1 "+r1+"", 8, "Fixedsys", DimGray);
ObjectSetText("R2 label", "              R2 "+r2+"", 8, "Fixedsys", DimGray);
ObjectSetText("R3 label", "              R3 "+r3+"", 8, "Fixedsys", DimGray);
ObjectSetText("S1 label", "              S1 "+s1+"", 8, "Fixedsys", DimGray);
ObjectSetText("S2 label", "              S2 "+s2+"", 8, "Fixedsys", DimGray);
ObjectSetText("S3 label", "              S3 "+s3+"", 8, "Fixedsys", DimGray);
ObjectSetText( "P label", "              P "+p+"", 8, "Fixedsys", DimGray);

ObjectMove("R1 label", 0, Time[0], R1);
ObjectMove("R2 label", 0, Time[0], R2);
ObjectMove("R3 label", 0, Time[0], R3);
ObjectMove("P label", 0, Time[0], P);
ObjectMove("S1 label", 0, Time[0], S1);
ObjectMove("S2 label", 0, Time[0], S2);
ObjectMove("S3 label", 0, Time[0], S3);

ObjectMove("S1 line", 0, Time[_Shift], S1);
ObjectMove("S1 line", 1, Time[0], S1);

ObjectMove("S2 line", 0, Time[_Shift], S2);
ObjectMove("S2 line", 1, Time[0], S2);

ObjectMove("S3 line", 0, Time[_Shift], S3);
ObjectMove("S3 line", 1, Time[0], S3);

ObjectMove("P line", 0, Time[_Shift], P);
ObjectMove("P line", 1, Time[0], P);

ObjectMove("R1 line", 0, Time[_Shift], R1);
ObjectMove("R1 line", 1, Time[0], R1);

ObjectMove("R2 line", 0, Time[_Shift], R2);
ObjectMove("R2 line", 1, Time[0], R2);

ObjectMove("R3 line", 0, Time[_Shift], R3);
ObjectMove("R3 line", 1, Time[0], R3);
//====
   string S;
   ObDeleteObjectsByPrefix(OBJECT_PREFIX);
   double HH = 0;
   double LL = 1000000;
   int History = MathMin(Bars,MAX_HISTORY);
   int i, j, k;
   for(i = 1; i < History; i++)
     {
       HH = MathMax(HH, CSH(i));
       LL = MathMin(LL, CSL(i));
     }
   int NumberOfPoints = (HH - LL) / (1.0*Point*STEP) + 1;
   int Count[];
   ArrayResize(Count, NumberOfPoints);
   for(i = 0; i < NumberOfPoints; i++) 
       Count[i] = 0;
   for(i = 1; i < History; i++)
     {
       double C = CSL(i);
       while(C < CSH(i))
         {
           int Index = (C-LL) / (1.0*Point*STEP);
           Count[Index]++;    
           C += 1.0*Point*STEP;
         }
     }
   for(i = 0; i < NumberOfPoints; i++)
     {
       double StartX = Time[_Shift];
       double StartY = LL + 1.0*Point*STEP*i;
       double EndX   = Time[_Shift + Count[i]];
       double EndY   = StartY;
       string ObjName = ObGetUniqueName(OBJECT_PREFIX);
       ObjectDelete(ObjName);
       ObjectCreate(ObjName, OBJ_TREND, 0, StartX, StartY, EndX, EndY);
       ObjectSet(ObjName, OBJPROP_RAY, 0);
       ObjectSet(ObjName, OBJPROP_COLOR, _Color);
     }
//====
_N_Time = Time[0];
//---- End Of Program
return(0);
}
//+------------------------------------------------------------------+

double CSH(int shift)
  {
   return (MathMax(Open[shift], Close[shift]));
  }

double CSL(int shift)
  {
   return (MathMin(Open[shift],Close[shift]));
  }

string IntToStr(int X)
  {
    return (DoubleToStr(X, 0));
  }

string ObGetUniqueName(string Prefix)
  {
   ObjectId++;
   return (Prefix+" "+IntToStr(ObjectId));
  }

void ObDeleteObjectsByPrefix(string Prefix)
  {
   int L = StringLen(Prefix);
   int i = 0; 
   while(i < ObjectsTotal())
     {
       string ObjName = ObjectName(i);
       if(StringSubstr(ObjName, 0, L) != Prefix) 
         { 
           i++; 
           continue;
         }
       ObjectDelete(ObjName);
     }
  }
//-------------------------------------------------------------------------------------------

