//+-------------------------------------------------------------------------+
//|                                      Copyright © 2012, Vladimir Hlystov |
//|                                                http://cmillion.narod.ru |
//|выдает сигнал если цена приближается к линии мах мин цены с заданной даты|
//+-------------------------------------------------------------------------+
#property copyright "Copyright © 2012, Vladimir Hlystov"
#property link      "http://cmillion.narod.ru"
#property indicator_chart_window
//+------------------------------------------------------------------+
extern datetime Start = D'20.08.2012';
extern int      LevelPercent  = 70;
extern int      approximation = 2;
extern int      font_size     = 10;
extern color    color1        = Red;
extern color    color2        = Orange;
extern color    color3        = Blue;
string NaneLine[5];
double PriceLine[5];
color  ColorLine[5];
datetime SignalTime;
int T1;
//+------------------------------------------------------------------+
int init()
{
   ColorLine[0] = color1;
   ColorLine[1] = color1;
   ColorLine[2] = color2;
   ColorLine[3] = color3;
   ColorLine[4] = color3;
   PriceLine[0] = iHigh(NULL,1,iHighest(NULL,1,MODE_HIGH,iBarShift(NULL,1,Start,false),0)); 
   PriceLine[1] = iLow(NULL,1,iLowest(NULL,1,MODE_LOW,iBarShift(NULL,1,Start,false),0)); 
   NaneLine[0] = "100 %   "+DoubleToStr(PriceLine[0],Digits);
   NaneLine[1] = "-100 %   "+DoubleToStr(PriceLine[1],Digits);
   NaneLine[2] = "0 %   "+DoubleToStr((PriceLine[0]-PriceLine[1])/Point,0)+" п";
}
//+------------------------------------------------------------------+
int deinit()
{
   ObjectDelete("Start");
   for(int i=0; i<5; i++)
   {
      ObjectDelete(NaneLine[i]);
      ObjectDelete(NaneLine[i]+"line");
   }
   Comment("");
}
//+------------------------------------------------------------------+
int start()
{
   int j;
   if (ObjectFind("Start")!=-1)
   {
      if (Start!=NormalizeDouble(ObjectGet("Start",OBJPROP_TIME1),Digits))
      {
         Start=NormalizeDouble(ObjectGet("Start",OBJPROP_TIME1),Digits);
         for(j=0; j<5; j++)
         {
            ObjectDelete(NaneLine[j]);
            ObjectDelete(StringConcatenate(NaneLine[j],"line"));
         }
         PriceLine[0] = iHigh(NULL,1,iHighest(NULL,1,MODE_HIGH,iBarShift(NULL,1,Start,false),0)); 
         PriceLine[1] = iLow(NULL,1,iLowest(NULL,1,MODE_LOW,iBarShift(NULL,1,Start,false),0)); 
         NaneLine[0] = "100 %   "+DoubleToStr(PriceLine[0],Digits);
         NaneLine[1] = "-100 %   "+DoubleToStr(PriceLine[1],Digits);
         NaneLine[2] = "0 %   "+DoubleToStr((PriceLine[0]-PriceLine[1])/Point,0)+" п";
      }
   }
   else
   {
      ObjectDelete("Start");
      ObjectCreate("Start", OBJ_VLINE, 0,Start,0,0,0);
      ObjectSet   ("Start", OBJPROP_COLOR, Red);
      ObjectSet   ("Start", OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet   ("Start", OBJPROP_WIDTH, 1);
      ObjectSet   ("Start", OBJPROP_BACK,  false);
   }
   //---
   for(int i=0; i<2; i++)
   {
      if (ObjectFind(StringConcatenate(NaneLine[i],"line"))!=-1)
      {
         if (PriceLine[i]!=NormalizeDouble(ObjectGet(StringConcatenate(NaneLine[i],"line"),OBJPROP_PRICE1),Digits))
         {
            PriceLine[i]=NormalizeDouble(ObjectGet(StringConcatenate(NaneLine[i],"line"),OBJPROP_PRICE1),Digits);
            for(j=i; j<5; j++)
            {
               ObjectDelete(NaneLine[j]);
               ObjectDelete(StringConcatenate(NaneLine[j],"line"));
            }
            NaneLine[0] = "100 %   "+DoubleToStr(PriceLine[0],Digits);
            NaneLine[1] = "-100 %   "+DoubleToStr(PriceLine[1],Digits);
            NaneLine[2] = "0 %   "+DoubleToStr((PriceLine[0]-PriceLine[1])/Point,0)+" п";
         }
      }
   }
   //---
   string txt;
   T1 = WindowFirstVisibleBar()/2;
   PriceLine[2] = (PriceLine[0]+PriceLine[1])/2;
   PriceLine[3] = (PriceLine[2]+(PriceLine[0]-PriceLine[2])*LevelPercent/100);
   PriceLine[4] = (PriceLine[2]-(PriceLine[0]-PriceLine[2])*LevelPercent/100);
   NaneLine[3] = StringConcatenate(DoubleToStr(LevelPercent,0),"%   ",DoubleToStr(PriceLine[3],Digits),"   ",DoubleToStr((PriceLine[0]-PriceLine[3])*LevelPercent/100/Point,0)," п");
   NaneLine[4] = StringConcatenate("-",DoubleToStr(LevelPercent,0),"%   ",DoubleToStr(PriceLine[4],Digits),"   ",DoubleToStr((PriceLine[4]-PriceLine[1])*LevelPercent/100/Point,0)," п");
   for(i=0; i<5; i++)
   {
      drawtext(NaneLine[i], ColorLine[i], Time[T1] , PriceLine[i]);
      drawline(StringConcatenate(NaneLine[i],"line"), ColorLine[i] , PriceLine[i]);
      if (i>2)
      {
         if (MathAbs(Bid-PriceLine[i])/Point<approximation)
         {
            txt=StringConcatenate(Symbol(),"  Цена коснулась линии ",NaneLine[i],"    в ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
            if (SignalTime!=Time[0]) {Alert(txt);SignalTime=Time[0];}
         }
      }
   }
   Comment(txt);
   return(0);
}
//+------------------------------------------------------------------+
int drawline(string NT, color col, double Y1)
{
   if (ObjectFind(NT)!=-1)
   {
      if (Y1==NormalizeDouble(ObjectGet(NT,OBJPROP_PRICE1),Digits)) return;
      ObjectDelete(NT);
   }
   ObjectCreate(NT, OBJ_HLINE, 0,0,Y1,0,0);
   ObjectSet   (NT, OBJPROP_COLOR, col);
   ObjectSet   (NT, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet   (NT, OBJPROP_WIDTH, 1);
   ObjectSet   (NT, OBJPROP_BACK,  false);
   return;
}
//--------------------------------------------------------------------
int drawtext(string NT, color col, datetime t1, double p1)
{
   if (ObjectFind(NT)!=-1)
   {
      if (p1==NormalizeDouble(ObjectGet(NT,OBJPROP_PRICE1),Digits) && t1==ObjectGet(NT,OBJPROP_TIME1)) return;
      ObjectDelete (NT);
   }
   ObjectCreate (NT, OBJ_TEXT,0,t1,p1,0,0,0,0);
   ObjectSetText(NT, NT ,font_size,"Arial");
   ObjectSet    (NT, OBJPROP_COLOR, col);
}
//--------------------------------------------------------------------