// Variables to track whether news is currently happening and the path to the .csv file
bool IsCurrentlyNewsGoing=false;
int event_counter=0;
string CsvFilePath = "news.csv";
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CheckForNewsEvents()
{
IsCurrentlyNewsGoing = false;
//Print("Opening file: ", CsvFilePath);
int file_handle = FileOpen(CsvFilePath, FILE_READ);
if(file_handle != INVALID_HANDLE)
{
string line;
string date;
string time;
string event_name;
string event_priority;
string event_currency;
int date_pos;
int time_pos;
int name_pos;
int priority_pos;
int currency_pos;
int year, month, day;
while(!FileIsEnding(file_handle))
{
line = FileReadString(file_handle);
date_pos = StringFind(line, ",", 0);
if(date_pos != -1)
{
time_pos = StringFind(line, ",", date_pos + 1);
if(time_pos != -1)
{
date = StringSubstr(line, date_pos + 1, 10);
time = StringSubstr(line, date_pos + 11, 9);
name_pos = StringFind(line, ",", time_pos + 1);
priority_pos = StringFind(line, ",", name_pos + 1);
currency_pos = StringFind(line, ",", priority_pos + 1);
event_name = StringSubstr(line, time_pos + 1, name_pos - time_pos - 1);
event_priority = StringSubstr(line, name_pos + 1, priority_pos - name_pos - 1);
event_currency = StringSubstr(line, priority_pos + 1, StringLen(line) - priority_pos - 1);
year = StringToInteger(StringSubstr(date, 6, 4));
month = StringToInteger(StringSubstr(date, 0, 2));
day = StringToInteger(StringSubstr(date, 3, 2));
date = IntegerToString(year) + "." + IntegerToString(month) + "." + IntegerToString(day);
datetime event_time = StrToTime(date + " " + time);
//Print("Line: ", line);
//Print("Date: ", date);
//Print("Time: ", time);
//Print("event_name: ", event_name);
//Print("event_priority: ", event_priority);
//Print("event_currency: ", event_currency);
//Print("Event Time: ", TimeToStr(event_time));
//Print("Current Time: ", TimeToStr(TimeCurrent()));
if(event_time - offset_before_sec == TimeCurrent())
{
IsCurrentlyNewsGoing = true;
string obj_name = date + " " + time + " " + event_name + " " + event_priority + " " + event_currency;
int obj_handle = ObjectCreate(obj_name, OBJ_VLINE, 0, TimeCurrent(), 0, TimeCurrent(), 0);
if(obj_handle == -1)
{
Print("Error creating news line object. Error code: ", GetLastError());
}
else
{
ObjectSet(obj_handle, OBJPROP_COLOR, clrRed);
event_counter++;
}
}
}
}
}
FileClose(file_handle);
}
else
{
Print("Error opening file. Error code: ", GetLastError());
}
Comment("Total News Event : ", event_counter);
return IsCurrentlyNewsGoing;
}