//+------------------------------------------------------------------+
//|                                                    TickChart.mq4 |
//|                           Copyright © 2005, Инструменты трейдера |
//|                                   http://www.traderstools.h15.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Инструменты трейдера"
#property link      "http://www.traderstools.h15.ru"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern bool       ClearHistory=false;
extern color      ColorChart=Red;
extern bool       Label=true;
extern color      ColorLabel=Red;
extern bool       Line=false;
extern color      ColorLine=Red;

//---- buffers
double TickBuff[];
double Buff[];
double LastClose=0;
int BuffShift=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("Tick Chart");
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,ColorChart);
   SetIndexBuffer(0,TickBuff);
   SetIndexLabel(0,"Tick");
   SetIndexEmptyValue(0,0);
   
   int i,fs,handle;
   ArrayResize(Buff,Bars);
   for(i=0;i<Bars;i++){TickBuff[i]=0;Buff[i]=0;}
   handle=FileOpen(Symbol()+"_tick",FILE_BIN|FILE_READ,";");
   if(handle>0)
     {  
      BuffShift=0;
      if(FileSize(handle)/8>Bars)fs=Bars; 
      else fs=FileSize(handle)/8;
      for(i=0;i<fs;i++)
        {
         Buff[BuffShift]=FileReadDouble(handle,DOUBLE_VALUE);
         BuffShift++;
        } 
      LastClose=Buff[0];  
      FileClose(handle);
     }      
   if(Label)
     {
      ObjectCreate("TickLabel",OBJ_ARROW,WindowFind("Tick Chart"),Time[0],Bid);
      ObjectSet("TickLabel",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
      ObjectSet("TickLabel",OBJPROP_COLOR,ColorLabel);
     } 
   if(Line)
     {
      ObjectCreate("TickLine",OBJ_HLINE,WindowFind("Tick Chart"),0,Bid);
      ObjectSet("TickLine",OBJPROP_COLOR,ColorLine);
     }  
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   int i,handle;
   handle=FileOpen(Symbol()+"_tick",FILE_BIN|FILE_WRITE,";");
   for(i=0;i<BuffShift;i++)FileWriteDouble(handle,Buff[i],DOUBLE_VALUE);
   FileClose(handle);
   ObjectDelete("TickLabel");
   ObjectDelete("TickLine");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i; 
   if(ClearHistory)
     {
      for(i=0;i<Bars;i++)TickBuff[i]=0;
      LastClose=0;
      BuffShift=0;
      Alert("Tick Chart \n Не забудте установить параметр ClearHistory = false, \n иначе график не будет рисоваться.");
     } 
   int bs=ArraySize(Buff);
   for(i=0;i<bs;i++)TickBuff[i]=Buff[i]; 
   if(Label)
        {
         ObjectSet("TickLabel",OBJPROP_TIME1,Time[0]);
         ObjectSet("TickLabel",OBJPROP_PRICE1,Bid);
        }
      if(Line)
        ObjectSet("TickLine",OBJPROP_PRICE1,Bid);
   if(Bid!=LastClose)
     {
      LastClose=Bid;
      for(i=BuffShift;i>=0;i--)Buff[i+1]=Buff[i];
      Buff[0]=LastClose;
      BuffShift++;
      for(i=0;i<BuffShift;i++)TickBuff[i]=Buff[i]; 
      if(Label)
        {
         ObjectSet("TickLabel",OBJPROP_TIME1,Time[0]);
         ObjectSet("TickLabel",OBJPROP_PRICE1,Bid);
        }
      if(Line)
        ObjectSet("TickLine",OBJPROP_PRICE1,Bid);  
     } 
   return(0);
  }
//+------------------------------------------------------------------+