Plot an event from textfile (Datum;Eventdescription) into Chart

Hello, trying since days to geht Event from csv file plotted into AB Chart. Somehow i don't get the correct way from String MMDDYYY to something like a datenum() or so.

This is the idea but only a rough drawing.

Maybe someboday can help. Thanks in advance

filename = "C:\\AmiBroker\\Events\\events.txt";

function StrToDatenum(YearStr,MonthStr,DayStr)
{
  result =	(10000*YearStr-1900)+
			(100*Monthstr)+
			DayStr;

  return result;
}


fh = fopen(filename, "r");

if (fh)
{
    while (! feof(fh))
    {
        line = fgets(fh);
        if (StrLen(line) > 0)
        {
            // Datum und Event extrahieren
            dateStr  = StrExtract(line, 0, ";"); 
            YearStr  = StrToNum(StrLeft(DateStr,4));
			MonthStr = StrToNum(StrMid(DateStr,4,2));
            DayStr	 = StrToNum(StrRight(DateStr,2));
            
            
            eventText = StrExtract(line, 1, ";");
            eventcategory = StrExtract(line, 2, ";");
			
			// Convert date
			eventDate = StrToDateNum(YearStr,MonthStr,DayStr);

            // find position in Chart
            for (i = 0; i < BarCount; i++)
            {
                if (DateNumOrWhatever[i] == eventDate)
                {
                    // plot eventText vertical
                   PlotSomehowVerticalText(eventText);
                }
            }
        }
    }
    
    fclose(fh);
}

It is better to use AB functions to do the heavy lifting.

Step 1:

Pass your string in date time format

Step 2:
Do lookup on DT from step 1 on Array BarIndex and the Nth Bar number

Step 3:
Plot line where BarIndex() == Nth Bar

Also see this:

Thank you very much. I spent some time to search for existing code but didn't find the post. Thank you again. This helps me out.

Thank you again nsm51. I got it running. PlotText works fine for me. Maybe i code it more sophisticated later.

filename = "C:\\events.csv";
fh = fopen(filename, "r");
biatdate = 0;


if (fh)
{
    while (! feof(fh))
    {
        line = fgets(fh);
        if (StrLen(line) > 0)
        {
            // extract date and event
            dateStr  = StrExtract(line, 0, ';'); 
            eventText = StrExtract(line, 1, ';');
            //eventcategory = StrExtract(line, 2, ";"); later for filtering with toggle
			
            // find position in Chart
            biatdate	= Lookup( BarIndex(), _DT( dateStr ), -1 );
			closeatdate	= Lookup( C,          _DT( dateStr ), -1 );
			 
            PlotText(eventText,biatdate,closeatdate,colorGreen);
            
        }
    }
    
   fclose(fh);
} 


1 Like

Easy in just 2 lines.

The issue now is your AFL will unnecessarily spend time reading the file.

Try and optimize the code by checking if file has been modified.
AFL Function Reference - FGETSTATUS

Store last modified time in persistent static_variable_#1 and read file only if required.

Store file contents as string to another Static Variable(SV) #2, and parse it on each iteration.

Only if file contents are modified, read the file and update the 2nd SV.

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.