//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008-2012, EXPFOREX"
#property link      "http://www.expforex.com"
/*
Реальный автор:
 "Сергей (urdala) Рашевский"
     "urdala@mail.ru"
     
     Модификация и улучшение http://www.expforex.com
*/
#property indicator_chart_window
#property indicator_buffers 1
extern string in43="Показывать историю новостей 1-месяц 2-год";
extern int HistoryNewsShow=0;
extern string a1="Минут до новости";
extern int MinAlertPerNews=30; //Срабатывание минут до новости     
extern string a2="Минут после новости";
extern int MinAlertAfterNews=30; //Срабатывание минут после новости
extern string a3="Часовой пояс";
extern int GMT=-1; // часовой пояс. 0-GMT 
extern string a4="Показывать важные новости";
extern bool HighNews=true;//Установка важности новостей
extern string a5="Показывать средние новости";
extern bool MediumNews=true;//Установка важности новостей
extern string a6="Показывать легкие новости";
extern bool LowNews=true;//Установка важности новостей 
extern string a7="Рисовать линии новостей";
extern bool LineCreate=true;// Рисовать линии?
extern string a34="Рисовать текст новостей";
extern bool TextCreate=true;// Рисовать линии?

bool LastWeekCreate=false;// Рисовать только линии будущих новостей?  
extern string a9="Подавать сигналы";
extern bool Alerts=false;// Сигналить о предстоящей новости?
extern string a10="Цвет важных новостей";
extern color high=Red;// цвет важных новостей
extern string a11="Цвет средних новостей";
extern color medium=Yellow;// цвет обычных новостей
extern string a12="Цвет легких новостей";
extern color low=Lime;// цвет незначительных новостей 
extern string a13="Стиль линий";
extern int Style=0;//Стиль линии  
//--------------------------------------------------------------------
#import "wininet.dll"
int InternetAttemptConnect(int x);
int InternetOpenA(string sAgent,int lAccessType,
                  string sProxyName="",string sProxyBypass="",
                  int lFlags=0);
int InternetOpenUrlA(int hInternetSession,string sUrl,
                     string sHeaders="",int lHeadersLength=0,
                     int lFlags=0,int lContext=0);
int InternetReadFile(int hFile,int &sBuffer[],int lNumBytesToRead,
                     int &lNumberOfBytesRead[]);
int InternetCloseHandle(int hInet);
#import
//---------------------------------------------------------------------
double Torg[1];
int TimeNews[300];
string Valuta[300];
string News[300];
string Vazn[300];
string Prognoz[300];
string Prev[300];
string Actual[300];
int Now=0;
int nom;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Stamp2()
  {

   ObjectCreate("Original",OBJ_LABEL,0,0,0);
   ObjectSetText("Original"," ",10,"Arial Bold",Red);
   ObjectSet("Original",OBJPROP_CORNER,1);
   ObjectSet("Original",OBJPROP_XDISTANCE,250);
   ObjectSet("Original",OBJPROP_YDISTANCE,10);

  }
//---------------------------------------------------------------------
int timecur;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   Stamp2();

   IndicatorBuffers(1);
   SetIndexBuffer(0,Torg);
//----------Находим последнее воскресенье(последний загруженый на сайт календарь)
   int DayFile=TimeDay(TimeCurrent()-TimeDayOfWeek(TimeCurrent())*86400);
   if(DayFile<10)string DF="0"+DayFile;
   else DF=DayFile;
   int MonthFile=TimeMonth(TimeCurrent()-TimeDayOfWeek(TimeCurrent())*86400);
   if(MonthFile<10)string MF="0"+MonthFile;
   else MF=MonthFile;
   int YearFile=TimeYear(TimeCurrent()-TimeDayOfWeek(TimeCurrent())*86400);
   string DateFile=MF+"-"+DF+"-"+YearFile;
   string FileName=DateFile+".csv";
   int handle;
   handle=FileOpen(FileName,FILE_CSV|FILE_READ,";");

   if(handle<0)
     {
      //-----Скачиваем календарь ---------------------------------------------------------------------   
      string url="http://www.dailyfx.com/files/Calendar-"+DateFile+".csv";
      //-------Функция читает исходный код страницы сайта в строку------------------------------------
      if(!IsDllsAllowed())
        {
         Alert("Необходимо в настройках разрешить использование DLL");
         return(0);
        }
      int rv = InternetAttemptConnect(0);
      if(rv != 0)
        {
         Alert("Ошибка при вызове InternetAttemptConnect()");
         return(0);
        }
      int hInternetSession=InternetOpenA("Microsoft Internet Explorer",
                                         0,"","",0);
      if(hInternetSession<=0)
        {
         Alert("Ошибка при вызове InternetOpenA()");
         return(0);
        }
      int hURL=InternetOpenUrlA(hInternetSession,
                                url,"",0,0,0);
      if(hURL<=0)
        {
         Alert("Ошибка при вызове InternetOpenUrlA()");
         InternetCloseHandle(hInternetSession);
         return(0);
        }
      //------------------------------------------------------------------------      
      handle=FileOpen(FileName,FILE_CSV|FILE_WRITE,";");
      int dwBytesRead[1],N1,N2,N3,N4;
      string text="";
      string Str[10];
      for(int y=0;y<9;y++)Str[y]="";
      nom=0;
      int x=0;
      while(!IsStopped())
        {
         int cBuffer[256];
         bool bResult=InternetReadFile(hURL,cBuffer,1024,dwBytesRead);
         if(dwBytesRead[0]==0) break;
         for(int i=0; i<256; i++)
           {

            N1=cBuffer[i] & 0x000000FF;
            N2=cBuffer[i] >> 8 & 0x000000FF;
            N3=cBuffer[i] >> 16 & 0x000000FF;
            N4=cBuffer[i] >> 24 & 0x000000FF;
            if(N1!=44 && N1!=10)Str[x]=Str[x]+CharToStr(N1);
            else x++;
            if(N1==10){nom++;x=0;FileWrite(handle,Str[0],Str[1],Str[2],Str[3],Str[4],Str[5],Str[6],Str[7],Str[8]);for(y=0;y<9;y++)Str[y]="";}
            if((i*4)==dwBytesRead[0])break;
            if(N2!=44 && N2!=10)Str[x]=Str[x]+CharToStr(N2);
            else x++;
            if(N2==10){nom++;x=0;FileWrite(handle,Str[0],Str[1],Str[2],Str[3],Str[4],Str[5],Str[6],Str[7],Str[8]);for(y=0;y<9;y++)Str[y]="";}
            if((i*4)+1==dwBytesRead[0])break;
            if(N3!=44 && N3!=10)Str[x]=Str[x]+CharToStr(N3);
            else x++;
            if(N3==10){nom++;x=0;FileWrite(handle,Str[0],Str[1],Str[2],Str[3],Str[4],Str[5],Str[6],Str[7],Str[8]);for(y=0;y<9;y++)Str[y]="";}
            if((i*4)+2==dwBytesRead[0])break;
            if(N4!=44 && N4!=10)Str[x]=Str[x]+CharToStr(N4);
            else x++;
            if(N4==10){nom++;x=0;FileWrite(handle,Str[0],Str[1],Str[2],Str[3],Str[4],Str[5],Str[6],Str[7],Str[8]);for(y=0;y<9;y++)Str[y]="";}
            if((i*4)+3==dwBytesRead[0])break;
           }
        }
      InternetCloseHandle(hInternetSession);
      FileSeek(handle,0,SEEK_SET);
      FileWrite(handle,nom);
     }
   FileClose(handle);
//-----------Заносим данные из файла в массивы.---------------------------------
   handle=FileOpen(FileName,FILE_CSV|FILE_READ,";");
   nom=StrToInteger(FileReadString(handle));
   string Hand;
   for(i=1;i<=8;i++)Hand=FileReadString(handle);
   string data,time,month,poyas,valuta;

   for(i=1;i<=nom;i++)
     {
      data=FileReadString(handle);
      time=FileReadString(handle);
      month=StringSubstr(data,4,3);
      if(month=="Jan") month="01";
      if(month=="Feb") month="02";
      if(month=="Mar") month="03";
      if(month=="Apr") month="04";
      if(month=="May") month="05";
      if(month=="Jun") month="06";
      if(month=="Jul") month="07";
      if(month=="Aug") month="08";
      if(month=="Sep") month="09";
      if(month=="Oct") month="10";
      if(month=="Nov") month="11";
      if(month=="Dec") month="12";
     if(time!="") TimeNews[i]=StrToTime(YearFile+"."+month+"."+StringSubstr(data,8,2)+" "+time)+GMT*3600;
     if(time=="") TimeNews[i]=0;
      
      text=FileReadString(handle);

      valuta=FileReadString(handle);
      if(valuta=="eur" ||valuta=="EUR")Valuta[i]="EUR";
      if(valuta=="usd" ||valuta=="USD")Valuta[i]="USD";
      if(valuta=="jpy" ||valuta=="JPY")Valuta[i]="JPY";
      if(valuta=="gbp" ||valuta=="GBP")Valuta[i]="GBP";
      if(valuta=="chf" ||valuta=="CHF")Valuta[i]="CHF";
      if(valuta=="cad" ||valuta=="CAD")Valuta[i]="CAD";
      if(valuta=="aud" ||valuta=="AUD")Valuta[i]="AUD";
      if(valuta=="nzd" ||valuta=="NZD")Valuta[i]="NZD";

      News[i]=FileReadString(handle);

      Vazn[i]=FileReadString(handle);
      if(Vazn[i]!="High" && Vazn[i]!="HIGH" && Vazn[i]!="Medium" && Vazn[i]!="MEDIUM" && Vazn[i]!="Low" && Vazn[i]!="LOW")Vazn[i]=FileReadString(handle);
      if(Vazn[i]!="High" && Vazn[i]!="HIGH" && Vazn[i]!="Medium" && Vazn[i]!="MEDIUM" && Vazn[i]!="Low" && Vazn[i]!="LOW")Vazn[i]=FileReadString(handle);
      if(Vazn[i]!="High" && Vazn[i]!="HIGH" && Vazn[i]!="Medium" && Vazn[i]!="MEDIUM" && Vazn[i]!="Low" && Vazn[i]!="LOW")Vazn[i]=FileReadString(handle);



      Actual[i]=FileReadString(handle);
      Prognoz[i]=FileReadString(handle);
      Prev[i]=FileReadString(handle);

      while(!IsStopped())
        {
         text=FileReadString(handle);
         if(FileIsLineEnding(handle))break;
        }
     }

//------------Выход из функции--------------------------------------------------
   FileClose(handle);
   return(0);
  }
//------------------------------------------------------------------------------
int deinit()
  {
   Stamp2();
   ObjectsDeleteAll(0,OBJ_ARROW);
   ObjectsDeleteAll(0,OBJ_VLINE);
   ObjectsDeleteAll(0,OBJ_LABEL);

   return(0);
  }

//-------------------------------------------------------------------------------







int start()
  {


   int i=1;           color Col;

//---Рисуем линии новостей на графике--------------------------------------------
   if(LineCreate)
     {
      int na=0;
      int bartime[];
      ArrayResize(bartime,nom);
      for(int o=nom;o>=1;o--)
        {
         if(News[o]=="")continue;
         if(StringSubstr(Symbol(),0,3)!=Valuta[o] && StringSubstr(Symbol(),3,3)!=Valuta[o])continue;
         if((Vazn[o]=="High" || Vazn[o]=="HIGH") && HighNews==false)continue;
         if((Vazn[o]=="Medium" || Vazn[o]=="MEDIUM") && MediumNews==false)continue;
         if((Vazn[o]=="Low" || Vazn[o]=="LOW") && LowNews==false)continue;

         bartime[o]=TimeNews[o];
        }
      int predTime=-1;
      for(i=1;i<nom;i++)
        {

         if(News[i]=="")continue;
         if(TimeNews[i]==0)continue;
         
         // if (ObjectFind(News[i])==0)continue;
         if(StringSubstr(Symbol(),0,3)!=Valuta[i] && StringSubstr(Symbol(),3,3)!=Valuta[i])continue;
         if((Vazn[i]=="High" || Vazn[i]=="HIGH") && HighNews==false)continue;
         if((Vazn[i]=="Medium" || Vazn[i]=="MEDIUM") && MediumNews==false)continue;
         if((Vazn[i]=="Low" || Vazn[i]=="LOW") && LowNews==false)continue;
         if(Vazn[i]=="High"|| Vazn[i]=="HIGH")Col=high;
         if(Vazn[i]=="Medium"|| Vazn[i]=="MEDIUM")Col=medium;
         if(Vazn[i]=="Low"|| Vazn[i]=="LOW")Col=low;

         ObjectCreate("line"+News[i],OBJ_VLINE,0,TimeNews[i],0);
         ObjectSet("line"+News[i],OBJPROP_STYLE,1);
         ObjectSet("line"+News[i],OBJPROP_COLOR,Col);

         if(bartime[ArrayBsearch(bartime,TimeNews[i])]!=bartime[i]){na++;bartime[i]=-1; }else na=0;
         if(TimeDayOfWeek(TimeNews[i])!=predTime){na=0;predTime=TimeDayOfWeek(TimeNews[i]);}
         double raz=WindowPriceMax(0)-WindowPriceMin(0);
         ObjectCreate(News[i],OBJ_ARROW,0,0,0);
         ObjectSet(News[i],OBJPROP_TIME1,TimeNews[i]);
         ObjectSet(News[i],OBJPROP_PRICE1,High[iBarShift(Symbol(),0,TimeNews[i])]+na*raz/42);



         if(Vazn[i]=="Low"|| Vazn[i]=="LOW")ObjectSet(News[i],OBJPROP_ARROWCODE,140);
         if(Vazn[i]=="Medium"|| Vazn[i]=="MEDIUM")ObjectSet(News[i],OBJPROP_ARROWCODE,144);
         if(Vazn[i]=="High"|| Vazn[i]=="HIGH")ObjectSet(News[i],OBJPROP_ARROWCODE,149);
         ObjectSet(News[i],OBJPROP_COLOR,Col);
         ObjectSet(News[i],OBJPROP_STYLE,Style);
         ObjectSet(News[i],OBJPROP_RAY,false);

        }
     }
//---------------Обработка событий------------------------------------
   for(i=1;i<nom;i++)
     {
      if((Vazn[i]=="High" || Vazn[i]=="HIGH") && HighNews==false)continue;
      if((Vazn[i]=="Medium" || Vazn[i]=="MEDIUM") && MediumNews==false)continue;
      if((Vazn[i]=="Low" || Vazn[i]=="LOW") && LowNews==false)continue;
      if(TimeCurrent()+MinAlertPerNews*60>TimeNews[i] && TimeCurrent()-MinAlertAfterNews*60<TimeNews[i] && (StringSubstr(Symbol(),0,3)==Valuta[i] || StringSubstr(Symbol(),3,3)==Valuta[i]))
        {
         Torg[0]=1;
         break;
        }
      else Torg[0]=0;

     }
   if(Torg[0]==1 && i!=Now && Alerts) {Alert("Через ",(TimeNews[i]-TimeCurrent())/60," минут будет новость ",News[i]);Now=i;}

   int t=0;
//------------------Коментарий-------------------------- 
   if(TextCreate)
     {
      int lastperty;
      for(i=1;i<nom;i++)
        {
         if(TimeNews[i]>TimeCurrent())break;
         lastperty=i;
        }

      for(i=lastperty-10;i<=lastperty+20;i++)
        {
         if(StringSubstr(Symbol(),0,3)!=Valuta[i] && StringSubstr(Symbol(),3,3)!=Valuta[i])continue;
         if((Vazn[i]=="High" || Vazn[i]=="HIGH") && HighNews==false)continue;
         if((Vazn[i]=="Medium" || Vazn[i]=="MEDIUM") && MediumNews==false)continue;
         if((Vazn[i]=="Low" || Vazn[i]=="LOW") && LowNews==false)continue;

         if(Vazn[i]=="High")Col=high;
         if(Vazn[i]=="Medium")Col=medium;
         if(Vazn[i]=="Low")Col=low;

         bool linet;
         if(TimeCurrent()>TimeNews[i]){SetLabel("NewInfos"+t,t,TimeToStr(TimeNews[i],TIME_DATE|TIME_MINUTES)," "+News[i]," Пред:"+Prev[i]+" Прогноз:"+Prognoz[i]+" Факт:"+Actual[i]," Прошло:"+((TimeCurrent()-TimeNews[i])/60)+" минут",Col,10);linet=true;}
         if(TimeCurrent()<=TimeNews[i] && linet){SetLabel("NAMERAZD",t,"======================================================================================================================================");t++;linet=false;}
         if(TimeCurrent()<=TimeNews[i])SetLabel("NewInfos"+t,t,TimeToStr(TimeNews[i],TIME_DATE|TIME_MINUTES)," "+News[i], " Пред:"+Prev[i]+" Прогноз:"+Prognoz[i]+" Факт:"+Actual[i], " Осталось:"+((TimeNews[i]-TimeCurrent())/60)+" минут",Col,10);


         t++;
        }
        for(int h=t;h<100;h++)
        {if(ObjectFind("NewInfos"+h)!=-1)ObjectDelete("NewInfos"+h);}

     }
        if(HistoryNewsShow!=0)initHistory(HistoryNewsShow);

//-------------------------------------------------------------------------------
   return(0);
  }
//+------------------------------------------------------------------+







void SetLabel(string nm,int yt,string time="-",string tx="-",string par="-",string left="-",color cl=Blue,int xd=20,int cr=2,int fs=9) 
  {
   int yc;
   yc=yt*(fs+3);

   if(ObjectFind(nm+"time")<0) ObjectCreate(nm+"time",OBJ_LABEL,0,0,0);
   ObjectSetText(nm+"time",time,fs);
   ObjectSet(nm+"time",OBJPROP_COLOR,cl);
   ObjectSet(nm+"time",OBJPROP_XDISTANCE,xd);
   ObjectSet(nm+"time",OBJPROP_YDISTANCE,yc);
   ObjectSet(nm+"time",OBJPROP_CORNER,cr);
   ObjectSet(nm+"time",OBJPROP_FONTSIZE,fs);

   if(ObjectFind(nm)<0) ObjectCreate(nm,OBJ_LABEL,0,0,0);
   ObjectSetText(nm,tx,fs);
   ObjectSet(nm,OBJPROP_COLOR,cl);
   ObjectSet(nm,OBJPROP_XDISTANCE,xd+StringLen(time)*7);
   ObjectSet(nm,OBJPROP_YDISTANCE,yc);
   ObjectSet(nm,OBJPROP_CORNER,cr);
   ObjectSet(nm,OBJPROP_FONTSIZE,fs);

   if(ObjectFind(nm+"prog")<0) ObjectCreate(nm+"prog",OBJ_LABEL,0,0,0);
   ObjectSetText(nm+"prog",par,fs);
   ObjectSet(nm+"prog",OBJPROP_COLOR,cl);
   ObjectSet(nm+"prog",OBJPROP_XDISTANCE,xd+550);
   ObjectSet(nm+"prog",OBJPROP_YDISTANCE,yc);
   ObjectSet(nm+"prog",OBJPROP_CORNER,cr);
   ObjectSet(nm+"prog",OBJPROP_FONTSIZE,fs);

   if(ObjectFind(nm+"left")<0) ObjectCreate(nm+"left",OBJ_LABEL,0,0,0);
   ObjectSetText(nm+"left",left,fs);
   ObjectSet(nm+"left",OBJPROP_COLOR,cl);
   ObjectSet(nm+"left",OBJPROP_XDISTANCE,xd+750);
   ObjectSet(nm+"left",OBJPROP_YDISTANCE,yc);
   ObjectSet(nm+"left",OBJPROP_CORNER,cr);
   ObjectSet(nm+"left",OBJPROP_FONTSIZE,fs);

  }
//===========================================================================================================================================
//===========================================================================================================================================
//===========================================================================================================================================
//===========================================================================================================================================
//===========================================================================================================================================
//===========================================================================================================================================

double raznica;
int initHistory(int type)
  {
   int timecurentnew;
if(raznica==WindowPriceMax(0)-WindowPriceMin(0))return(-1);
raznica=WindowPriceMax(0)-WindowPriceMin(0);
int do;
if(type==1)do=TimeDay(TimeCurrent());
if(type==2)do=TimeDayOfYear(TimeCurrent());

   for(int ye=1;ye<do;ye++)

     {
      timecurentnew=TimeCurrent()-24*60*60*ye;
      if(type==1)if(TimeMonth(TimeCurrent())!=TimeMonth(timecurentnew))break;
      if(type==2)if(TimeYear(TimeCurrent())!=TimeYear(timecurentnew))break;
      //----------Находим последнее воскресенье(последний загруженый на сайт календарь)
      int DayFile=TimeDay(timecurentnew-TimeDayOfWeek(timecurentnew)*86400);
      if(DayFile<10)string DF="0"+DayFile;
      else DF=DayFile;
      int MonthFile=TimeMonth(timecurentnew-TimeDayOfWeek(timecurentnew)*86400);
      if(MonthFile<10)string MF="0"+MonthFile;
      else MF=MonthFile;
      int YearFile=TimeYear(timecurentnew-TimeDayOfWeek(timecurentnew)*86400);
      string DateFile=MF+"-"+DF+"-"+YearFile;
      string FileName=DateFile+".csv";
      int handle;
      handle=FileOpen(FileName,FILE_CSV|FILE_READ,";");

      if(handle<0)
        {
         //-----Скачиваем календарь ---------------------------------------------------------------------   
         string url="http://www.dailyfx.com/files/Calendar-"+DateFile+".csv";
         //-------Функция читает исходный код страницы сайта в строку------------------------------------
         if(!IsDllsAllowed())
           {
            Alert("Необходимо в настройках разрешить использование DLL");
            return(0);
           }
         int rv = InternetAttemptConnect(0);
         if(rv != 0)
           {
            Alert("Ошибка при вызове InternetAttemptConnect()");
            return(0);
           }
         int hInternetSession=InternetOpenA("Microsoft Internet Explorer",
                                            0,"","",0);
         if(hInternetSession<=0)
           {
            Alert("Ошибка при вызове InternetOpenA()");
            return(0);
           }
         int hURL=InternetOpenUrlA(hInternetSession,
                                   url,"",0,0,0);
         if(hURL<=0)
           {
            Alert("Ошибка при вызове InternetOpenUrlA()");
            InternetCloseHandle(hInternetSession);
            return(0);
           }
         //------------------------------------------------------------------------      
         handle=FileOpen(FileName,FILE_CSV|FILE_WRITE,";");
         int dwBytesRead[1],N1,N2,N3,N4;
         string text="";
         string Str[10];
         for(int y=0;y<9;y++)Str[y]="";
         nom=0;
         int x=0;
         while(!IsStopped())
           {
            int cBuffer[256];
            bool bResult=InternetReadFile(hURL,cBuffer,1024,dwBytesRead);
            if(dwBytesRead[0]==0) break;
            for(int i=0; i<256; i++)
              {

               N1=cBuffer[i] & 0x000000FF;
               N2=cBuffer[i] >> 8 & 0x000000FF;
               N3=cBuffer[i] >> 16 & 0x000000FF;
               N4=cBuffer[i] >> 24 & 0x000000FF;
               if(N1!=44 && N1!=10)Str[x]=Str[x]+CharToStr(N1);
               else x++;
               if(N1==10){nom++;x=0;FileWrite(handle,Str[0],Str[1],Str[2],Str[3],Str[4],Str[5],Str[6],Str[7],Str[8]);for(y=0;y<9;y++)Str[y]="";}
               if((i*4)==dwBytesRead[0])break;
               if(N2!=44 && N2!=10)Str[x]=Str[x]+CharToStr(N2);
               else x++;
               if(N2==10){nom++;x=0;FileWrite(handle,Str[0],Str[1],Str[2],Str[3],Str[4],Str[5],Str[6],Str[7],Str[8]);for(y=0;y<9;y++)Str[y]="";}
               if((i*4)+1==dwBytesRead[0])break;
               if(N3!=44 && N3!=10)Str[x]=Str[x]+CharToStr(N3);
               else x++;
               if(N3==10){nom++;x=0;FileWrite(handle,Str[0],Str[1],Str[2],Str[3],Str[4],Str[5],Str[6],Str[7],Str[8]);for(y=0;y<9;y++)Str[y]="";}
               if((i*4)+2==dwBytesRead[0])break;
               if(N4!=44 && N4!=10)Str[x]=Str[x]+CharToStr(N4);
               else x++;
               if(N4==10){nom++;x=0;FileWrite(handle,Str[0],Str[1],Str[2],Str[3],Str[4],Str[5],Str[6],Str[7],Str[8]);for(y=0;y<9;y++)Str[y]="";}
               if((i*4)+3==dwBytesRead[0])break;
              }
           }
         InternetCloseHandle(hInternetSession);
         FileSeek(handle,0,SEEK_SET);
         FileWrite(handle,nom);
        }
      FileClose(handle);
      //-----------Заносим данные из файла в массивы.---------------------------------
      handle=FileOpen(FileName,FILE_CSV|FILE_READ,";");
      nom=StrToInteger(FileReadString(handle));
      string Hand;
      for(i=1;i<=8;i++)Hand=FileReadString(handle);
      string data,time,month,poyas,valuta;

      for(i=1;i<=nom;i++)
        {
         data=FileReadString(handle);
         time=FileReadString(handle);
         month=StringSubstr(data,4,3);
         if(month=="Jan") month="01";
         if(month=="Feb") month="02";
         if(month=="Mar") month="03";
         if(month=="Apr") month="04";
         if(month=="May") month="05";
         if(month=="Jun") month="06";
         if(month=="Jul") month="07";
         if(month=="Aug") month="08";
         if(month=="Sep") month="09";
         if(month=="Oct") month="10";
         if(month=="Nov") month="11";
         if(month=="Dec") month="12";
     if(time!="") TimeNews[i]=StrToTime(YearFile+"."+month+"."+StringSubstr(data,8,2)+" "+time)+GMT*3600;
     if(time=="") TimeNews[i]=0;
     
         text=FileReadString(handle);

         valuta=FileReadString(handle);
         if(valuta=="eur" ||valuta=="EUR")Valuta[i]="EUR";
         if(valuta=="usd" ||valuta=="USD")Valuta[i]="USD";
         if(valuta=="jpy" ||valuta=="JPY")Valuta[i]="JPY";
         if(valuta=="gbp" ||valuta=="GBP")Valuta[i]="GBP";
         if(valuta=="chf" ||valuta=="CHF")Valuta[i]="CHF";
         if(valuta=="cad" ||valuta=="CAD")Valuta[i]="CAD";
         if(valuta=="aud" ||valuta=="AUD")Valuta[i]="AUD";
         if(valuta=="nzd" ||valuta=="NZD")Valuta[i]="NZD";

         News[i]=FileReadString(handle);

         Vazn[i]=FileReadString(handle);
         if(Vazn[i]!="High" && Vazn[i]!="HIGH" && Vazn[i]!="Medium" && Vazn[i]!="MEDIUM" && Vazn[i]!="Low" && Vazn[i]!="LOW")Vazn[i]=FileReadString(handle);
         if(Vazn[i]!="High" && Vazn[i]!="HIGH" && Vazn[i]!="Medium" && Vazn[i]!="MEDIUM" && Vazn[i]!="Low" && Vazn[i]!="LOW")Vazn[i]=FileReadString(handle);
         if(Vazn[i]!="High" && Vazn[i]!="HIGH" && Vazn[i]!="Medium" && Vazn[i]!="MEDIUM" && Vazn[i]!="Low" && Vazn[i]!="LOW")Vazn[i]=FileReadString(handle);



         Actual[i]=FileReadString(handle);
         Prognoz[i]=FileReadString(handle);
         Prev[i]=FileReadString(handle);

         while(!IsStopped())
           {
            text=FileReadString(handle);
            if(FileIsLineEnding(handle))break;
           }
        }

      //------------Выход из функции--------------------------------------------------
      FileClose(handle);


      i=1;           color Col;

      //---Рисуем линии новостей на графике--------------------------------------------
      if(LineCreate)
        {

         int na=0;
         int bartime[];
         ArrayResize(bartime,nom);
         for(int o=nom;o>=1;o--)
           {
            if(News[o]=="")continue;
            if(StringSubstr(Symbol(),0,3)!=Valuta[o] && StringSubstr(Symbol(),3,3)!=Valuta[o])continue;
            if((Vazn[o]=="High" || Vazn[o]=="HIGH") && HighNews==false)continue;
            if((Vazn[o]=="Medium" || Vazn[o]=="MEDIUM") && MediumNews==false)continue;
            if((Vazn[o]=="Low" || Vazn[o]=="LOW") && LowNews==false)continue;

            bartime[o]=TimeNews[o];
           }
         int predTime=-1;
         for(i=1;i<nom;i++)
           {

         if(News[i]=="")continue;
         if(TimeNews[i]==0)continue;
         
         // if (ObjectFind(News[i])==0)continue;
         if(StringSubstr(Symbol(),0,3)!=Valuta[i] && StringSubstr(Symbol(),3,3)!=Valuta[i])continue;
         if((Vazn[i]=="High" || Vazn[i]=="HIGH") && HighNews==false)continue;
         if((Vazn[i]=="Medium" || Vazn[i]=="MEDIUM") && MediumNews==false)continue;
         if((Vazn[i]=="Low" || Vazn[i]=="LOW") && LowNews==false)continue;
         if(Vazn[i]=="High"|| Vazn[i]=="HIGH")Col=high;
         if(Vazn[i]=="Medium"|| Vazn[i]=="MEDIUM")Col=medium;
         if(Vazn[i]=="Low"|| Vazn[i]=="LOW")Col=low;

         ObjectCreate("line"+News[i],OBJ_VLINE,0,TimeNews[i],0);
         ObjectSet("line"+News[i],OBJPROP_STYLE,1);
         ObjectSet("line"+News[i],OBJPROP_COLOR,Col);

         if(bartime[ArrayBsearch(bartime,TimeNews[i])]!=bartime[i]){na++;bartime[i]=-1; }else na=0;
         if(TimeDayOfWeek(TimeNews[i])!=predTime){na=0;predTime=TimeDayOfWeek(TimeNews[i]);}
         double raz=WindowPriceMax(0)-WindowPriceMin(0);
         ObjectCreate(News[i],OBJ_ARROW,0,0,0);
         ObjectSet(News[i],OBJPROP_TIME1,TimeNews[i]);
         ObjectSet(News[i],OBJPROP_PRICE1,High[iBarShift(Symbol(),0,TimeNews[i])]+na*raz/42);



         if(Vazn[i]=="Low"|| Vazn[i]=="LOW")ObjectSet(News[i],OBJPROP_ARROWCODE,140);
         if(Vazn[i]=="Medium"|| Vazn[i]=="MEDIUM")ObjectSet(News[i],OBJPROP_ARROWCODE,144);
         if(Vazn[i]=="High"|| Vazn[i]=="HIGH")ObjectSet(News[i],OBJPROP_ARROWCODE,149);
         ObjectSet(News[i],OBJPROP_COLOR,Col);
         ObjectSet(News[i],OBJPROP_STYLE,Style);
         ObjectSet(News[i],OBJPROP_RAY,false);

           }
        }


     }

   return(0);
  }
//+------------------------------------------------------------------+
 