News blocking module

 

Hi guys,

Im currently build a News blocking module to plugin to my EA so that when there are major news, my EA will stop maybe 5 hours in advance and resume after 5 hours after the news release.

But there isn't timediff or timeadd function to use.

Any smart ideas to go about doing this module of mine?

Thanks

 

Time[] iTime() TimeCurrent() TimeLocal() Hour() Minute() u need more ?

 
qjol:

Time[] iTime() TimeCurrent() TimeLocal() Hour() Minute() u need more ?

is there any way or method to get the date time of time current minus 5 hour?
 

y not ?

 


int timeNow = TimeCurrent();   // time now. it is an integer (or datetime) value.

int five_hour_ago = timeNow - 5*PERIOD_H1*60;  //five hour ago from now

int five_hour_from_now = timeNow + 5*PERIOD_H1*60; 


/// now a computation example

if(TimeHour(five_hour_from_now)>=10 && TimeHour(five_hour_from_now)<17)
{
    /// something here....
}
 

Thanks abstract_mind

That is very helpful, i also figure out the same thing.

much appreciated

 
//+------------------------------------------------------------------+
//|                                                     Template.mq4 |
//|                                                     Version 0.10 |
//+------------------------------------------------------------------+
#property copyright "doshur"
#property link      "http://www.doshur.com"

//---- EXTERNAL VARIABLES

extern int GMT = 0;
extern int NBH = 3;

//---- GLOBAL VARIABLES

string News[15, 2];

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----
   ReadNews();
//----
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   static bool ToTrade_B, ToTrade_S;

//---- NEWS FILTER

   datetime SDT, EDT, NDT;

   if(ToTrade_B == true || ToTrade_S == true)
   {
      for(int i = 0; i < 15; i++)
      {
         if(StringFind(Symbol(), News[i, 1], 0) < 0) break;

         NDT = TimeCurrent();
         SDT = StrToTime(News[i, 0]) - (NBH * 60 * 60) + (GMT * 60 * 60);
         EDT = StrToTime(News[i, 0]) + (NBH * 60 * 60) + (GMT * 60 * 60);

         if(SDT <= NDT && NDT <= EDT)
         {
            ToTrade_B = false;
            ToTrade_S = false;

            break;
         }
      }
   }

   return(0);
}
//+------------------------------------------------------------------+
//| Read News                                                        |
//+------------------------------------------------------------------+
void ReadNews()
{
   int i, Err, Handle;
   string Col_1, Col_2;

   i = 0;

   Handle = FileOpen("News.csv", FILE_CSV|FILE_READ, ";");

   if(Handle < 0)
   {
      Err = GetLastError();

      Print("File Error : ", Err);
      return;
   }

   while(FileIsEnding(Handle) == false)
   {
      Col_1 = FileReadString(Handle);
      Col_2 = FileReadString(Handle);

      if(StringFind(Symbol(), Col_2, 0) >= 0)
      {
         if(StrToTime(Col_1) - TimeCurrent() + 86400 > 0)
         {
            News[i, 0] = Col_1;
            News[i, 1] = Col_2;

            i++;
         }
      }

      if(i + 1 > 15) break;
      if(FileIsEnding(Handle) == true) break;
   }

   FileClose(Handle);
   return;
}
This is my news reading module, please help me to check if there are any bugs or improvement to be made.
 

Hello doshur,

Thank you for the news code. I have found it very useful. I am trying to modify it for use with back testing EA's but I can not get it to work. I have updated it so it can check for news on each tick instead of once. Here is what I have so far:

extern int        GMT = 0;
extern bool       UseOldNews = True;
extern int        MinutesBeforeNews = 120;
extern int        MinutesAfterNews = 420;

string            News[9712, 4];

int start() 
{
   if (UseOldNews)
   {
      ReadNews();
      if (CheckNews()) Print("Trading");
      else {
         Print("Avoiding News");
         return(0);
      }   
   }  
}

bool CheckNews()
{
   datetime StartDateTime, EndDateTime, Now;
   string mySymbol_Pair_1 = StringSubstr(Symbol(), 0, 3);
   string mySymbol_Pair_2 = StringSubstr(Symbol(), 3, 3);
   
   int i = 0;
   
   for(i = 0; i < 9712; i++)
   {
      if(StringFind("H", News[i, 3], 0) > 0)
      {
         Now = TimeCurrent(); // Current Time
         StartDateTime = StrToTime(News[i, 0]+" "+News[i, 1]) - (MinutesBeforeNews * 60) + (GMT * 60 * 60); // Start Time
         EndDateTime = StrToTime(News[i, 0]+" "+News[i, 1]) + (MinutesAfterNews * 60) + (GMT * 60 * 60); // End Time
      }
      
      Print("Now: "+Now);
      Print("Start: "+StartDateTime);
      Print("Finish: "+EndDateTime);   
         
      if(StartDateTime <= Now && Now <= EndDateTime) // News Time
      {
         return(false); // FALSE - News and no trade
      } else return(true); // TRUE - Trading
   }
}
//+------------------------------------------------------------------+
//| Read News                                                        |
//+------------------------------------------------------------------+
void ReadNews()
{
   int i, Err, Handle;
   string Col_1, Col_2, Col_3, Col_4;
   string mySymbol_Pair_1 = StringSubstr(Symbol(), 0, 3);
   string mySymbol_Pair_2 = StringSubstr(Symbol(), 3, 3);
   
   i = 0;
   // ArrayResize(News, 9712);
   Handle = FileOpen("AllOldNews.CSV", FILE_CSV|FILE_READ, ",");

   if(Handle < 0)
   {
      Err = GetLastError();
      Print("File Error : ", Err);
      return;
   }

   while(!FileIsEnding(Handle) && i + 1 > 9712 )
   {
      Col_1 = FileReadString(Handle);
      Col_2 = FileReadString(Handle);
      Col_3 = FileReadString(Handle);
      Col_4 = FileReadString(Handle);

      if(StringFind(mySymbol_Pair_1, Col_3, 0) > 0 || StringFind(mySymbol_Pair_2, Col_3, 0) > 0 )
      {
         if(Col_1 == TimeToStr(TimeCurrent(),TIME_DATE))
         {
            News[i, 0] = Col_1;
            News[i, 1] = Col_2;
            News[i, 2] = Col_3;
            News[i, 3] = Col_4;
            i++;
         }
      }
   }
   FileClose(Handle);
   return;
}   


The time variables in CheckNews() are always coming up empty which suggests it can not find "H" in the array so the array created in ReadNews() might not be working either.

I have attached the csv file I am using. I have reformatted the date from dd/mm/yyyy to yyyy.mm.dd so when we use the StrToTime() function to join date+time the date is in the correct format. I have added arrays because my csv has four columns.


Any help from members here would be greatly appreciated.

Regards,

JonDgls


 
if(StringFind("H", News[i, 3], 0) > 0)
why do u want to find "H"?
 
doshur:
why do u want to find "H"?


Hi doshur,

In my csv the impact is listed as either H,M or L.

Regards,

JonDgls

 

how do you format your CSV file?

for mine, i only have 2 columns - EG : DATETIME, USD

try to use print function to run your script to see where the error comes from

Reason: