Results 1 to 6 of 6
 0 Attachment(s)    

Thread: News filter without any indicator

  1. #1
    Senior Member fxtester's Avatar
    Join Date
    Nov 2008
    Posts
    3,165

    Default News filter without any indicator

    It took me some time to develop this piece of code, one of the most complicated that i have made in my life.

    Before extern parameters :

    HTML Code:
    //----------------------------------------------------------------- news
    
    #define READURL_BUFFER_SIZE   100
    
    #import  "Wininet.dll"
       int InternetOpenW(string, int, string, string, int);
       int InternetConnectW(int, string, int, string, string, int, int, int); 
       int HttpOpenRequestW(int, string, string, int, string, int, string, int); 
       int InternetOpenUrlW(int, string, string, int, int, int);
       int InternetReadFile(int, uchar & arr[], int, int& OneInt[]);
       int InternetCloseHandle(int); 
    #import
    
    //-----------------------------------------------------------------
    Extern parameters :

    Code:
    extern bool AvoidNews          = false;
    extern int MinimumImpact       = 1;
    extern int MinsBeforeNews      = 30;
    extern int MinsAfterNews       = 30;
    extern bool ConsiderUSDForAll  = false;
    extern bool CloseOnNews        = false;
    
    bool ContinueTrading=true;
    Before start :

    Code:
    //----------------------------------------------------------------- news grab xml data
    
    string geturl(string url){   
       int HttpOpen=InternetOpenW(" ",0," "," ",0); 
       int HttpConnect=InternetConnectW(HttpOpen,"",80,"","",3,0,1);
       int HttpRequest=InternetOpenUrlW(HttpOpen,url,NULL,0,0,0);
    
       int read[1];
       uchar Buffer[];
       ArrayResize(Buffer,READURL_BUFFER_SIZE+1);
       string page="";
       while(true){
          InternetReadFile(HttpRequest,Buffer,READURL_BUFFER_SIZE,read);
          string strThisRead=CharArrayToString(Buffer,0,read[0],CP_UTF8);
          if(read[0]>0)page=page+strThisRead;
          else break;
       }
       if(HttpRequest>0)InternetCloseHandle(HttpRequest);
       if(HttpConnect>0)InternetCloseHandle(HttpConnect);
       if(HttpOpen>0)InternetCloseHandle(HttpOpen);
       return page;
    }
    
    //-----------------------------------------------------------------
    Inside start :

    Code:
    ContinueTrading=true;
    
       //------------------------------------------------------ news filter
    
       if(AvoidNews==true){
          string NEWS="",NEWS2="";
          string s1="",s2="";
          s1=StringSubstr(Symbol(),0,3);
          s2=StringSubstr(Symbol(),3,6);
    
          int country1[150];
          int country2[150];
          string country[150];
    
          int date1[150];
          int date2[150];
          string date[150];
          string dateread[150];
          string dateday[150];
          string datemonth[150];
          string dateyear[150];
    
          int time1[150];
          int time2[150];
          string time[150];
          int time3[150];
          string timeread[150];
          string timeap[150];
          string hour[150];
          string hour1[150];
          string hour2[150];
    
          datetime datefinal[150];
    
          int impact1[150];
          int impact2[150];
          string impact[150];
          int impact3[150];
          int impactfinal[150];
    
          int h;
    
          NEWS=geturl("http://www.forexfactory.com/ffcal_week_this.xml");
          NEWS2=StringSubstr(NEWS,0,150);
          
          int BufferLen=StringBufferLen(NEWS);
    
          //country (pair)
          h=0;for(i=0;i<=BufferLen;i++){if(StringFind(NEWS,"<country>",i)!=-1){country1[h]=StringFind(NEWS,"<country>",i);h++;i=StringFind(NEWS,"<country>",i)+1;}}
          h=0;for(i=0;i<=BufferLen;i++){if(StringFind(NEWS,"</country>",i)!=-1){country2[h]=StringFind(NEWS,"</country>",i);h++;i=StringFind(NEWS,"</country>",i)+1;}}
          for(i=0;i<=150;i++){country[i]=StringSubstr(NEWS,country1[i]+9,country2[i]-(country1[i]+9));}
          
          //date
          h=0;for(i=0;i<=BufferLen;i++){if(StringFind(NEWS,"<date>",i)!=-1){date1[h]=StringFind(NEWS,"<date>",i);h++;i=StringFind(NEWS,"<date>",i)+1;}}
          h=0;for(i=0;i<=BufferLen;i++){if(StringFind(NEWS,"</date>",i)!=-1){date2[h]=StringFind(NEWS,"</date>",i);h++;i=StringFind(NEWS,"</date>",i)+1;}}
          for(i=0;i<=150;i++){date[i]=StringSubstr(NEWS,date1[i]+6,date2[i]-(date1[i]+6));}
          for(i=0;i<=150;i++){dateread[i]=StringSubstr(date[i],9,10);}
          for(i=0;i<=150;i++){dateday[i]=StringSubstr(dateread[i],3,2);if(StringSubstr(dateday[i],0,1)=="0")dateday[i]=StringSubstr(dateday[i],1,1);}
          for(i=0;i<=150;i++){datemonth[i]=StringSubstr(dateread[i],0,2);if(StringSubstr(datemonth[i],0,1)=="0")datemonth[i]=StringSubstr(datemonth[i],1,1);}
          for(i=0;i<=150;i++){dateyear[i]=StringSubstr(dateread[i],6,4);}
          
          h=0;for(i=0;i<=BufferLen;i++){if(StringFind(NEWS,"<time>",i)!=-1){time1[h]=StringFind(NEWS,"<time>",i);h++;i=StringFind(NEWS,"<time>",i)+1;}}
          h=0;for(i=0;i<=BufferLen;i++){if(StringFind(NEWS,"</time>",i)!=-1){time2[h]=StringFind(NEWS,"</time>",i);h++;i=StringFind(NEWS,"</time>",i)+1;}}
          for(i=0;i<=150;i++){time[i]=StringSubstr(NEWS,time1[i]+6,time2[i]-(time1[i]+6));}
          for(i=0;i<=150;i++)time3[i]=StringFind(time[i],":",0);
          for(i=0;i<=150;i++){if(time3[i]==10)timeap[i]=StringSubstr(time[i],13,2);else timeap[i]=StringSubstr(time[i],14,2);}
          for(i=0;i<=150;i++){if(time3[i]==10)timeread[i]=StringSubstr(time[i],9,4);else timeread[i]=StringSubstr(time[i],9,5);}
          for(i=0;i<=150;i++){
             if(time3[i]==10){
                if(timeap[i]=="am"){
                   hour1[i]=StringSubstr(timeread[i],0,1);
                   if(hour1[i]=="1")hour1[i]="01";
                   if(hour1[i]=="2")hour1[i]="02";
                   if(hour1[i]=="3")hour1[i]="03";
                   if(hour1[i]=="4")hour1[i]="04";
                   if(hour1[i]=="5")hour1[i]="05";
                   if(hour1[i]=="6")hour1[i]="06";
                   if(hour1[i]=="7")hour1[i]="07";
                   if(hour1[i]=="8")hour1[i]="08";
                   if(hour1[i]=="9")hour1[i]="09";
                   hour2[i]=StringSubstr(timeread[i],2,2);
                   hour[i]=hour1[i]+":"+hour2[i];
                }
                if(timeap[i]=="pm"){
                   hour1[i]=StringSubstr(timeread[i],0,1);
                   if(hour1[i]=="1")hour1[i]="13";
                   if(hour1[i]=="2")hour1[i]="14";
                   if(hour1[i]=="3")hour1[i]="15";
                   if(hour1[i]=="4")hour1[i]="16";
                   if(hour1[i]=="5")hour1[i]="17";
                   if(hour1[i]=="6")hour1[i]="18";
                   if(hour1[i]=="7")hour1[i]="19";
                   if(hour1[i]=="8")hour1[i]="20";
                   if(hour1[i]=="9")hour1[i]="21";
                   hour2[i]=StringSubstr(timeread[i],2,2);
                   hour[i]=hour1[i]+":"+hour2[i];
                }
             }
             else {
                if(timeap[i]=="am"){
                   hour[i]=timeread[i];
                }
                if(timeap[i]=="pm"){
                   hour1[i]=StringSubstr(timeread[i],0,2);
                   if(hour1[i]=="10")hour1[i]="22";
                   if(hour1[i]=="11")hour1[i]="23";
                   hour2[i]=StringSubstr(timeread[i],3,2);
                   hour[i]=hour1[i]+":"+hour2[i];
                }
             }
          }
    
          for(i=0;i<=150;i++)datefinal[i]=StrToTime(dateyear[i]+"."+datemonth[i]+"."+dateday[i]+" "+hour[i]);
    
          //impact
          h=0;for(i=0;i<=BufferLen;i++){if(StringFind(NEWS,"<impact>",i)!=-1){impact1[h]=StringFind(NEWS,"<impact>",i);h++;i=StringFind(NEWS,"<impact>",i)+1;}}
          h=0;for(i=0;i<=BufferLen;i++){if(StringFind(NEWS,"</impact>",i)!=-1){impact2[h]=StringFind(NEWS,"</impact>",i);h++;i=StringFind(NEWS,"</impact>",i)+1;}}
          for(i=0;i<=150;i++){impact[i]=StringSubstr(NEWS,impact1[i]+8,impact2[i]-(impact1[i]+8));}
          for(i=0;i<=150;i++)impact3[i]=StringFind(impact[i],">",0);
          for(i=0;i<=150;i++){if(impact3[i]==14)impactfinal[i]=1;if(impact3[i]==15)impactfinal[i]=3;if(impact3[i]==17)impactfinal[i]=2;}
    
          datetime dategmt=TimeGMT();
    
          int previ=150;
          int nexti=150;
          for(i=0;i<=150;i++){
             if(dategmt<datefinal[i])break;
             if(impactfinal[i]>=MinimumImpact && (s1==country[i] || s2==country[i] || (ConsiderUSDForAll==true && "USD"==country[i])))if(dategmt>datefinal[i] && datefinal[i]!=0)previ=i;
          }
          for(i=0;i<=150;i++){
             if(nexti!=150)break;
             if(impactfinal[i]>=MinimumImpact && (s1==country[i] || s2==country[i] || (ConsiderUSDForAll==true && "USD"==country[i])))if(dategmt<datefinal[i])nexti=i;
          }
    
          int impact0=impactfinal[previ];
          string country0=country[previ];
          string date0=TimeToStr(datefinal[previ]);
    
          int impactx=impactfinal[nexti];
          string countryx=country[nexti];
          string datex=TimeToStr(datefinal[nexti]);
          string dategmtz=TimeToStr(TimeGMT());
    
          string datetest=TimeToStr(datefinal[25]);
    
          if(dategmt<=datefinal[previ]+MinsAfterNews*60 || dategmt>=datefinal[nexti]-MinsBeforeNews*60){
             ContinueTrading=false;
             if(CloseOnNews){
                close(OP_SELL);
                close(OP_BUY);
             }
          }
       }
    
       //------------------------------------------------------
    Before OrderSend functions :

    Code:
    if(ContinueTrading==true)
    Last edited by fxtester; 08-25-2014 at 13:06.

  2. #2
    Senior Member fxtester's Avatar
    Join Date
    Nov 2008
    Posts
    3,165

    Default

    I have found an issue. Corrected.

  3. #3
    Senior Member fxtester's Avatar
    Join Date
    Nov 2008
    Posts
    3,165

    Default

    New version as Forex Factory has updated its xml file :

    Before the start function :

    Code:
    extern string l5               = "----------------- News Filter"; // Unique and complete news filter
    
    extern bool AvoidNews          = true; // Should we enable the news filter ?
    extern int MinimumImpact       = 2; // 1= low impact, 2= medium impact, 3= high impact
    extern int MinsBeforeNews      = 30; // Minutes to consider before news to stop generation of orders
    extern int MinsAfterNews       = 30; // Minutes to consider adter news to stop generation of orders
    extern bool ConsiderUSDForAll  = true; // Should we consider USD even for non USD pairs ?
    //extern bool CloseOnNews        = false; // Close orders during this news range
    
    string innercommentnews="";
    string innercommenttime="";
    string innercommenttimef="";
    
    bool ContinueTrading=true;
    Inside the start function :

    Code:
    if(StartHour!=0 || EndHour!=24 || SpecialFriday==true || TestHour!=24)time();
    Before the Ordersend functions :

    Code:
    if(ContinueTrading==true)
    After the end of the start function :

    Code:
    void news(){
       ContinueTrading=true;
       string NEWS="",NEWS2="";
       string s1="",s2="";
       s1=StringSubstr(Symbol(),0,3);
       s2=StringSubstr(Symbol(),3,3);
    
       int country1[150];
       int country2[150];
       string country[150];
    
       int date1[150];
       int date2[150];
       string date[150];
       string dateread[150];
       string dateday[150];
       string datemonth[150];
       string dateyear[150];
    
       int time1[150];
       int time2[150];
       string time[150];
       int time3[150];
       string timeread[150];
       string timeap[150];
       string hour[150];
       string hour1[150];
       string hour2[150];
    
       datetime datefinal[150];
    
       //int impact1[150];
       //int impact2[150];
       //string impact[150];
       int impact3[150];
       int impactfinal[150];
       
       string impactz[150];
    
       int h;
    
       NEWS=geturl("http://www.forexfactory.com/ffcal_week_this.xml");
       //NEWS2=StringSubstr(NEWS,0,150);
          
       int BufferLen=StringBufferLen(NEWS);
    
       //country (pair)
       h=0;for(n=0;n<=BufferLen;n++){if(StringFind(NEWS,"<country>",n)!=-1){country1[h]=StringFind(NEWS,"<country>",n);h++;n=StringFind(NEWS,"<country>",n)+1;}}
       h=0;for(n=0;n<=BufferLen;n++){if(StringFind(NEWS,"</country>",n)!=-1){country2[h]=StringFind(NEWS,"</country>",n);h++;n=StringFind(NEWS,"</country>",n)+1;}}
       for(n=0;n<=150;n++){country[n]=StringSubstr(NEWS,country1[n]+9,country2[n]-(country1[n]+9));}
          
       //date
       h=0;for(n=0;n<=BufferLen;n++){if(StringFind(NEWS,"<date>",n)!=-1){date1[h]=StringFind(NEWS,"<date>",n);h++;n=StringFind(NEWS,"<date>",n)+1;}}
       h=0;for(n=0;n<=BufferLen;n++){if(StringFind(NEWS,"</date>",n)!=-1){date2[h]=StringFind(NEWS,"</date>",n);h++;n=StringFind(NEWS,"</date>",n)+1;}}
       for(n=0;n<=150;n++){date[n]=StringSubstr(NEWS,date1[n]+15,date2[n]-(date1[n]+18));}
       for(n=0;n<=150;n++){dateread[n]=StringSubstr(date[n],0,10);}
       for(n=0;n<=150;n++){dateday[n]=StringSubstr(dateread[n],3,2);if(StringSubstr(dateday[n],0,1)=="0")dateday[n]=StringSubstr(dateday[n],1,1);}
       for(n=0;n<=150;n++){datemonth[n]=StringSubstr(dateread[n],0,2);if(StringSubstr(datemonth[n],0,1)=="0")datemonth[n]=StringSubstr(datemonth[n],1,1);}
       for(n=0;n<=150;n++){dateyear[n]=StringSubstr(dateread[n],6,4);}
          
       h=0;for(n=0;n<=BufferLen;n++){if(StringFind(NEWS,"<time>",n)!=-1){time1[h]=StringFind(NEWS,"<time>",n);h++;n=StringFind(NEWS,"<time>",n)+1;}}
       h=0;for(n=0;n<=BufferLen;n++){if(StringFind(NEWS,"</time>",n)!=-1){time2[h]=StringFind(NEWS,"</time>",n);h++;n=StringFind(NEWS,"</time>",n)+1;}}
       for(n=0;n<=150;n++){time[n]=StringSubstr(NEWS,time1[n]+15,time2[n]-(time1[n]+18));}
       for(n=0;n<=150;n++)time3[n]=StringFind(time[n],":",0);
       for(n=0;n<=150;n++){if(time3[n]==1)timeap[n]=StringSubstr(time[n],4,2);else timeap[n]=StringSubstr(time[n],5,2);}
       for(n=0;n<=150;n++){if(time3[n]==1)timeread[n]=StringSubstr(time[n],0,4);else timeread[n]=StringSubstr(time[n],0,5);}
       for(n=0;n<=150;n++){
          if(time3[n]==1){
             if(timeap[n]=="am"){
                hour1[n]=StringSubstr(timeread[n],0,1);
                if(hour1[n]=="1")hour1[n]="01";
                if(hour1[n]=="2")hour1[n]="02";
                if(hour1[n]=="3")hour1[n]="03";
                if(hour1[n]=="4")hour1[n]="04";
                if(hour1[n]=="5")hour1[n]="05";
                if(hour1[n]=="6")hour1[n]="06";
                if(hour1[n]=="7")hour1[n]="07";
                if(hour1[n]=="8")hour1[n]="08";
                if(hour1[n]=="9")hour1[n]="09";
                hour2[n]=StringSubstr(timeread[n],2,2);
                hour[n]=hour1[n]+":"+hour2[n];
             }
             if(timeap[n]=="pm"){
                hour1[n]=StringSubstr(timeread[n],0,1);
                if(hour1[n]=="1")hour1[n]="13";
                if(hour1[n]=="2")hour1[n]="14";
                if(hour1[n]=="3")hour1[n]="15";
                if(hour1[n]=="4")hour1[n]="16";
                if(hour1[n]=="5")hour1[n]="17";
                if(hour1[n]=="6")hour1[n]="18";
                if(hour1[n]=="7")hour1[n]="19";
                if(hour1[n]=="8")hour1[n]="20";
                if(hour1[n]=="9")hour1[n]="21";
                hour2[n]=StringSubstr(timeread[n],2,2);
                hour[n]=hour1[n]+":"+hour2[n];
             }
          }
          else {
             if(timeap[n]=="am"){
                hour[n]=timeread[n];
             }
             if(timeap[n]=="pm"){
                hour1[n]=StringSubstr(timeread[n],0,2);
                if(hour1[n]=="10")hour1[n]="22";
                if(hour1[n]=="11")hour1[n]="23";
                hour2[n]=StringSubstr(timeread[n],3,2);
                hour[n]=hour1[n]+":"+hour2[n];
             }
          }
       }
    
       for(n=0;n<=150;n++)datefinal[n]=StrToTime(dateyear[n]+"."+datemonth[n]+"."+dateday[n]+" "+hour[n]);
    
       //impact
       /*h=0;for(n=0;n<=BufferLen;n++){if(StringFind(NEWS,"<impact>",n)!=-1){impact1[n]=StringFind(NEWS,"<impact>",n);h++;n=StringFind(NEWS,"<impact>",n)+1;}}
       h=0;for(n=0;n<=BufferLen;n++){if(StringFind(NEWS,"</impact>",n)!=-1){impact2[n]=StringFind(NEWS,"</impact>",n);h++;n=StringFind(NEWS,"</impact>",n)+1;}}
       for(n=0;n<=150;n++){impact[n]=StringSubstr(NEWS,impact1[n]+17,impact2[n]-(impact1[n]+20));}
       for(n=0;n<=150;n++)impact3[n]=StringFind(impact[n],">",0);
       for(n=0;n<=150;n++){if(impact3[n]==14)impactfinal[n]=1;if(impact3[n]==15)impactfinal[n]=3;if(impact3[n]==17)impactfinal[n]=2;}*/
       
       for(n=0;n<=150;n++){impactz[n]=StringSubstr(NEWS,time2[n]+27,6);}
       for(n=0;n<=150;n++){if(impactz[n]=="Low]]>")impactfinal[n]=1;if(impactz[n]=="Medium")impactfinal[n]=2;if(impactz[n]=="High]]")impactfinal[n]=3;}
    
       datetime dategmt=TimeGMT();
    
       int previ=150;
       int nexti=150;
       for(n=0;n<=150;n++){
          if(dategmt<datefinal[n])break;
          if(impactfinal[n]>=MinimumImpact && (s1==country[n] || s2==country[n] || (ConsiderUSDForAll==true && "USD"==country[n])))if(dategmt>datefinal[n] && datefinal[n]!=0)previ=n;
       }
       for(n=0;n<=150;n++){
          if(nexti!=150)break;
          if(impactfinal[n]>=MinimumImpact && (s1==country[n] || s2==country[n] || (ConsiderUSDForAll==true && "USD"==country[n])))if(dategmt<datefinal[n])nexti=n;
       }
    
       int impact0=impactfinal[previ];
       string country0=country[previ];
       string date0=TimeToStr(datefinal[previ]);
    
       int impactx=impactfinal[nexti];
       string countryx=country[nexti];
       string datex=TimeToStr(datefinal[nexti]);
       string dategmtz=TimeToStr(TimeGMT());
    
       string datetest=TimeToStr(datefinal[25]);
          
       if(dategmt<=datefinal[previ]+MinsAfterNews*60 || dategmt>=datefinal[nexti]-MinsBeforeNews*60){
          ContinueTrading=false;
          /*if(CloseOnNews){
             close(OP_SELL);
             close(OP_BUY);
          }*/
       }
       innercommentnews=
       "\n\nPairs = "+s1+" / "+s2+
       "\n\nPrevious Country = "+country0+
       "\nPrevious Impact = "+impact0+
       "\nPrevious Date = "+date0+
       "\nPrevious Index = "+previ+
       "\n\nNext Country = "+countryx+
       "\nNext Impact = "+impactx+
       "\nNext Date = "+datex+
       "\nNext Index = "+nexti;/*+
       "\ntimeread = "+impactfinal[3];*/
    }

  4. #4
    Senior Member fxtester's Avatar
    Join Date
    Nov 2008
    Posts
    3,165

    Default

    Update issue.

    Replace

    Code:
    NEWS=geturl("http://www.forexfactory.com/ffcal_week_this.xml");
    With :

    Code:
    NEWS=geturl("http://www.forexfactory.com/ffcal_week_this.xml"+"?"+TimeToString(iTime(NULL,10080,0),0));

  5. #5
    Senior Member fxtester's Avatar
    Join Date
    Nov 2008
    Posts
    3,165

    Default

    I don't why it still collecting the previous week data.

    Replace :

    Code:
    NEWS=geturl("http://www.forexfactory.com/ffcal_week_this.xml"+"?"+TimeToString(iTime(NULL,10080,0),0));
    With :

    Code:
    NEWS=geturl("http://www.forexfactory.com/ffcal_week_this.xml"+"?"+TimeToString(iTime(NULL,30,0),0));

  6. #6
    Junior Member
    Join Date
    Jul 2015
    Posts
    2

    Default

    Hello,
    Thanks very much for sharing this brilliant work.
    I tried to add your News Filter to my EA, and I had some issued during compilation, as:

    No declaration of StartHour / EndHour / SpecialFriday / TestHours.
    No function time().

    Can you, please, help me out with this?

    Thanks a lot!

    Quote Originally Posted by fxtester View Post
    I don't why it still collecting the previous week data.

    Replace :

    Code:
    NEWS=geturl("http://www.forexfactory.com/ffcal_week_this.xml"+"?"+TimeToString(iTime(NULL,10080,0),0));
    With :

    Code:
    NEWS=geturl("http://www.forexfactory.com/ffcal_week_this.xml"+"?"+TimeToString(iTime(NULL,30,0),0));

Similar Threads

  1. Add a news filter
    By funyoo in forum MQL programming
    Replies: 16
    Last Post: 03-11-2016, 09:04
  2. New news filter with ffcal
    By fxtester in forum MQL programming
    Replies: 1
    Last Post: 08-20-2014, 14:10
  3. (REQ) News Filter
    By YOAV3211 in forum MQL programming
    Replies: 0
    Last Post: 03-16-2013, 18:42
  4. TSD News filter code
    By dave_rimmer in forum MQL programming
    Replies: 0
    Last Post: 01-29-2011, 20:44

Tags for this Thread

date, indicator, news, time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •