How to use ELCollection to read last row from a file?  [SOLVED]

Questions about MultiCharts and user contributed studies.
rc76
Posts: 51
Joined: 14 Oct 2020
Has thanked: 17 times

How to use ELCollection to read last row from a file?

Postby rc76 » 24 Oct 2023

I have the following txt file: Read123.txt
"Date","Time","Open","High","Low","Close","Volume",
2018-01-01, 17:01:00, 6405.00, 6409.75, 6401.50, 6407.75, 538
2018-01-01, 17:02:00, 6408.00, 6410.25, 6407.50, 6410.00, 117
2018-01-01, 17:03:00, 6409.75, 6412.00, 6409.50, 6411.25, 101
2018-01-01, 17:04:00, 6411.50, 6412.00, 6411.00, 6411.25, 31
2018-01-01, 17:05:00, 6411.00, 6411.50, 6411.00, 6411.50, 11
I wish to:
(1) read the last row of the file
(2) from the last row, read the data (first comma separated value), and the time (2nd comma separated value)

How can I achieve that?

I tried to device the following indicator code:

Code: Select all

Vars: FileName(""); FileName = "C:\Read123.txt"; Vars: ListC_ID(ListC.New), Date_ID(0); if CurrentBar = 1 then Begin ClearPrintLog; // Read entire file ? Value1 = ListC.ReadFile(ListC_ID, Filename); // Determine number of rows in the file ? Value2 = ListC.Count(ListC_ID); // Read the last value on the file ? Value3 = ListC.Back(ListC_ID); // ---------------------------- // Check Output // ---------------------------- Print("ReadFile: ", Value1); Print("Count: ", Value2); Print("Back: ", Value3); // Clean the List from memory ListC.Clear(ListC_ID); end;
The output results I have is:

ReadFile: 0.00
Count: 8.00
Back: 18.00

Which doesn’t make any sense.

Any advice or help will be truly appreciated!

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: How to use ELCollection to read last row from a file?

Postby rrams » 24 Oct 2023

Hi rc76,
I don't have the ELCollections documentation with me to look at but I think you want this:

Code: Select all

vars: FileName("C:\Read123.txt"), ListC_ID(ListC.New), ListDate_ID(0), ListTime_ID(0), MyDate(""), MyTime(""); if CurrentBar = 1 then begin // Read entire file Value1 = ListC.ReadFile(ListC_ID, Filename); // Determine columns to read into list ListDate_ID = ListC.Get(ListC_ID, 2); ListTime_ID = ListC.Get(ListC_ID, 3); // Read the last record (row) of the file MyDate = ListS.Get(ListDate_ID, ListS.Count(ListDate_ID)); MyTime = ListS.Get(ListTime_ID, ListS.Count(ListTime_ID)); // Check Output Print("Rows: ", ListS.Count(ListDate_ID):0:0); Print("Date: ", MyDate); Print("Time: ", MyTime); Print(""); // Clean the List from memory ListC.Clear(ListC_ID); end;
I don't know why it needs column 2 & 3 instead of 1 & 2. I probably missed something.

JensOhle
Posts: 9
Joined: 24 Jul 2018
Has thanked: 3 times
Been thanked: 2 times

Re: How to use ELCollection to read last row from a file?

Postby JensOhle » 25 Oct 2023

Hey rc76 ... this is not how ELC stores data ... you should not think like importing a csv file into excel row by row ... please read carefully the section below "Value1 = ListC.ReadFile(ID, FileName);" on page 14 in the ELC documentation (attached) ... instead of rows, ELC uses columns ... it's more simple if your script also writes the data file via EL ... hope that helps, Jens
ELCollections.doc
(90.5 KiB) Downloaded 57 times

rc76
Posts: 51
Joined: 14 Oct 2020
Has thanked: 17 times

Re: How to use ELCollection to read last row from a file?  [SOLVED]

Postby rc76 » 29 Oct 2023

rrams - Yes it works. Thank you so much rrams! Could it be column 1 is its internal index for the file?

JensOhle - Thank you JensOhle! Any chance you may have any idea about rrams's question on column 1?

JensOhle
Posts: 9
Joined: 24 Jul 2018
Has thanked: 3 times
Been thanked: 2 times

Re: How to use ELCollection to read last row from a file?

Postby JensOhle » 30 Oct 2023

rrams - Yes it works. Thank you so much rrams! Could it be column 1 is its internal index for the file?
JensOhle - Thank you JensOhle! Any chance you may have any idea about rrams's question on column 1?
@TJ: I learned so much from the "TJ style of responses" that I try to imitate you here ;-)

@rc76: Have you looked into the part of ELC documentation mentioned in my post above? If yes, what is ELC doing with the provided code? Ask yourself this question (once, but hard) to understand how ELC really functions.

Code: Select all

If you are willing to do this, DON'T READ FURTHER - but instead come back later and check it out :)
1) What does the code with the ListC.ReadFile method? It reads the comma delimited text file into a list of lists. 2) How many lists? With n comma > n+1 lists - right? 3) In the presented file there are seven comma in the first line. Therefore 8 lists are generated. In the following lines there are only six comma and seven values. Therefore ELC has to fill one list with NIL values. 4) Seemingly it does that from the last (most right) value forward (to the left; this wasn't obvious, it's just how the creator coded it). Thus, the NIL value is brought into the first list and the others in the following. In short: The first line of the file contains an additional comma at the very end. Delete this and change 2/3 to 1/2. Also, to use headings properly use MapSC.ReadFile (see page 15 of the docu for an example). Code well, Jens.


Return to “MultiCharts”