How to get ELcollections to read a 3 field CSV file.

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

How to get ELcollections to read a 3 field CSV file.

Postby bowlesj3 » 28 Nov 2009

I was having problems figuring this out at the end of the day but I got it to work after a good nights sleep with a rested gray matter. The correct code is below.

The command ListC.ReadFile(ListC_ID, "C:\EL_CollectionsTestData.txt"); is used to read the following file in. C:\EL_CollectionsTestData.txt". The contents of the file are below.

Notice double quotes were used on strings however it turns out you do not really need them. It works either way.

Code: Select all


"John","New York",31
"Jane","Florida",25
"Carlos","Oregon",19
"Leticia","California",42
"EOF","EOF",0
This is the corrected code. There are lots of debugging print statements to show various ID values etc as the code is executed.

Code: Select all


{ A_ELCollectionExample_CSV_read}

{
This is the input file.
It can read it with or without quotes on the string fields.

"C:\EL_CollectionsTestData.txt"

"John","New York",31
"Jane","Florida",25
"Carlos","Oregon",19
"Leticia","California",42
"EOF","EOF",0
}

variables:
MyName(""),
MyState(""),
MyAge(0),

ListName_ID(0),
ListState_ID(0),
ListAge_ID(0),

ListC_ID(0),

RecordNumber(0);

if CurrentBar = 1 then
Begin

ListC_ID = ListC.new;
Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), "ListC_ID", " " , ListC_ID, " " , " ");

Value1 = ListC.ReadFile(ListC_ID,"C:\EL_CollectionsTestData.txt");

Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), "File Is Read Now", " " , " ");
ListName_ID = ListC.Get(ListC_ID,1);
ListState_ID = ListC.Get(ListC_ID,2);
ListAge_ID = ListC.Get(ListC_ID,3);

Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), "Get List IDs", " " , " ");
Print( File("C:\EL_Collections_Test.txt"), "ListName_ID", "=" , ListName_ID, " " ,
"ListState_ID", "=" , ListState_ID, " " , "ListAge_ID", "=" , ListAge_ID, " " , " ");

MyName = ListS.get(ListName_ID,1);
MyState = ListS.get(ListState_ID,1);
MyAge = ListN.get(ListAge_ID,1);

Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), "Print first Row", " " , " ");

Print( File("C:\EL_Collections_Test.txt"), "MyName", "=" , MyName, " " ,
" MyState", "=" , MyState, " " , " MyAge", "=" , MyAge, " " , " ");

Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), "Print all Rows using a while loop", " " , " ");

RecordNumber = 0;
while MyName <> "EOF"
begin
RecordNumber = RecordNumber + 1;
MyName = ListS.get(ListName_ID,RecordNumber);
MyState = ListS.get(ListState_ID,RecordNumber);
MyAge = ListN.get(ListAge_ID,RecordNumber);
if MyName <> "EOF" then
begin
Print( File("C:\EL_Collections_Test.txt"), "MyName", "=" , MyName, " " ,
" MyState", "=" , MyState, " " , " MyAge", "=" , MyAge, " " , " ");
end;

end; {while MyName <> "EOF"}

Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), " ");
Print( File("C:\EL_Collections_Test.txt"), "Print last row to show the EOF (end of file) marker",
" " , " ");
Print( File("C:\EL_Collections_Test.txt"), "MyName", "=" , MyName, " " , " MyState", "=" ,
MyState, " " , " MyAge", "=" , MyAge, " " , " ");

ListC.Clear(ListC_ID);
end; {if CurrentBar = 1 then}

Here is the output of the print statements.

Code: Select all


ListC_ID 91.00


File Is Read Now

Get List IDs
ListName_ID=92.00 ListState_ID=93.00 ListAge_ID=94.00


Print first Row
MyName=John MyState=New York MyAge=31.00


Print all Rows using a while loop
MyName=John MyState=New York MyAge=31.00
MyName=Jane MyState=Florida MyAge=25.00
MyName=Carlos MyState=Oregon MyAge=19.00
MyName=Leticia MyState=California MyAge=42.00


Print last row to show the EOF (end of file) marker
MyName=EOF MyState=EOF MyAge= 0.00
Last edited by bowlesj3 on 29 Nov 2009, edited 16 times in total.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 29 Nov 2009

For anyone who read this last night, I got it to work after a good nights sleep. The whole first post is revised with the correct ELcollections code and the appropriate adjustments here and there to match.

The ELcollections is okay (NOW). The documentation is not that great. I went back and updated my copy of it with appropriate commands from the code above.
Last edited by bowlesj3 on 29 Nov 2009, edited 6 times in total.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Postby TJ » 29 Nov 2009

you are FAST... !

thanks for the update.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 29 Nov 2009

Your welcome.

For those new to the ELcollections, this is the post that had the GV2.2, ELcollections and ADE info.
viewtopic.php?f=5&t=2483

I posted a reply to that thread. It contains an attachement with an update to the ELcollections documentation with a condensed version of the above code just under the ListC.ReadFile command that reads the csv file in.

User avatar
RobotMan
Posts: 375
Joined: 12 Jul 2006
Location: Los Altos, California, USA
Has thanked: 31 times
Been thanked: 13 times
Contact:

Postby RobotMan » 29 Nov 2009

Hi John,

I don't use ELC or ADE, but I wanted to say "thanks!" for posting the zip file cuz I know a lot of people do use it.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 30 Nov 2009

No Problem Bob.

ELcollections is a little strange at times. I have found a few times that the ListC.new command will assign a zero ID but if I put a print statement to a file (any file) before it then it starts working. Once it is working I can take the print out and it keeps working. It has happened 3 times now with new studies. You need a sense of humour when that sort of thing happens.

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: How to get ELcollections to read a 3 field CSV file.

Postby arjfca » 24 Mar 2013

I was having problems figuring this out at the end of the day but I got it to work after a good nights sleep with a rested gray matter. The correct code is below.

The command ListC.ReadFile(ListC_ID, "C:\EL_CollectionsTestData.txt"); is used to read the following file in. C:\EL_CollectionsTestData.txt". The contents of the file are below.

Notice double quotes were used on strings however it turns out you do not really need them. It works either way.

Code: Select all

Hello John
Special thanks to you for this code example

I got a quaestion, I understand that this is an old post.

Actually, you are detection then end of the file using "EOF","EOF",0

Is there another way I could use to detect it.

My goal is to read a CSV file that contain Trade executed on my Interactive Broker broker account.
Once i got the entry Long/Short , qty, date and time, I will be able to plot over a chart where was my entry / exit where placed. Since this file does not contain THE "EOF", I need to find a way to detect the end of a file.

Maybe my solution could be to add the string "EOF" to the last line using a Print File statement, but this mean to modify a working database file and I don't like that

Martin

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: How to get ELcollections to read a 3 field CSV file.

Postby arjfca » 24 Mar 2013

Did resolve my problem

There is a function call:

ListS.Count(ListS_ID) that return the count of how many string line in the list.

recordNumbermax = ListS.Count(ListS_ID) ; When reading my file, I make sure that my ID number does not go further than this value

Martin

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: How to get ELcollections to read a 3 field CSV file.

Postby bowlesj3 » 24 Mar 2013

Hi Martin,

That's Interesting. I never thought of that I guess at the time or maybe I did not know that commands that well back then. I was going to answer earlier today with "you may be out of luck" but I got distracted by other pressing projects. I guess it was better that I did get distracted. It makes sense because at some point I learned that it loads all the data in memory. It is just too bad that they do not give an example of how that count command could be useful. I found the original documentation to be very basic (very limited). That is why I enhanced it a bit. Maybe this is the thread for documenting more examples of how to use the EL-collections.

John

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: How to get ELcollections to read a 3 field CSV file.

Postby arjfca » 21 Dec 2013


Notice double quotes were used on strings however it turns out you do not really need them. It works either way.

"John","New York",31
"Jane","Florida",25
"Carlos","Oregon",19
"Leticia","California",42
"EOF","EOF",0
The double found his importance if your string has a "," in it. An CSV string will be read has a string if and only if the string is double quoted. Otherwise, Elcollection will fail because each segment of the CSV will be consider as an element

For my need, I'm looking for One string per inscription like if one cell in Excel hold a string. Not ten cell, one per element. My CSV decoding is realized using a function that I had already published on this forum

Splitring(String, integer).

For sure, I could modify my logic and have my CSV info decoded by ElCollection, but I like to keep a continuity in my way to decode CSV string. I use them a lot

My next question will be, how to add double quote to a string.

A good weekend to all

Martin :)

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: How to get ELcollections to read a 3 field CSV file.

Postby arjfca » 21 Dec 2013

About the double quote

User ABC (Chris) has published the solution on viewtopic.php?f=1&p=51666#p51666

There is a function DoubleQuote in our arsenal. This will add " to a string

Problem resolve

List_String = doublequote + TL_Num_String + comma + TL_Price_S + comma + TL_Price_E + comma + TL_Time_S + comma + TL_Time_e + comma +
TL_Color + comma + TL_Style + comma + TL_Size + comma + TL_Ext_L + comma +TL_Ext_R + doublequote + newline;

Martin


Return to “User Contributed Studies and Indicator Library”