Elcollection: Going through CSV file// Want to understand...  [SOLVED]

Questions about MultiCharts and user contributed studies.
arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Elcollection: Going through CSV file// Want to understand...

Postby arjfca » 19 Dec 2013

Hello

1' Goal needed: Going through a CSV file and reading each individual string.
2' Goal: Understand the ElCollection

I'm reading again & again the ElCollection documentation and still loss. I don't understand the step that need to be done.

How do I assign the ID to the list collection? The ID should be linked to the Filename file.
The file is a CSV file that contain a collection of strings

Var:
FileName = The Path\Filename of the CSV file
TL_String = Individual string to be read from the file
Index = Index of the strings in the file

To go trough each string of the file

Code: Select all

for Index = 1 to ListC.Count(ID) begin
TL_String = ListC.Get(ID, Index);
// Excecute the step with my string
end;
What is a MAP? I don't think that I need to use it because all the work to be done with the CSV string has been already coded.

Each string in the file look like this
TL_String = "57,140.438360,140.438360,41613.5416666667,41627.1666666667,8388736,3,0,False,False";


All the best for this Holiday Season
Any help appreciated

Martin

A section of a CSV file look like
5,132.226120,132.379180,41549.9083333333,41549.9868055556,2124031,1,0,False,False
38,133.690000,133.690000,41578.5416666667,41579.2916666667,2124031,1,2,False,True
51,139.249120,139.249120,41614.0416666667,41615.0416666667,2124031,1,0,False,False
54,141.060430,141.066520,41616.7500000000,41618.0000000000,2124031,1,0,False,False
1,130.810000,572.277778,41487.0000000000,41487.0000000000,32768,1,0,False,False
2,130.085000,69.090278,41487.0000000000,41487.0000000000,32768,1,0,False,False
3,131.785000,572.277778,41490.7500000000,41490.7500000000,16711680,3,0,False,False
4,131.130000,69.090278,41490.7500000000,41490.7500000000,16711680,3,0,False,False
6,128.725000,572.277778,41497.7500000000,41497.7500000000,16711680,3,0,False,False
7,128.120000,69.090278,41497.7500000000,41497.7500000000,16711680,3,0,False,False

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Elcollection: Going through CSV file// Want to understan

Postby furytrader » 20 Dec 2013

ELCollections can be a little confusing and intimidating. It wasn't until I started studying computer science formally that I began to understand the true value of the DLL. Here's a few things to remember:

A collection is either a LIST (a sequential list of data) or a MAP (a dictionary of data):

Lists can be:

ListN = sequential list of numbers
ListS = sequential list of strings
ListC = sequential list of COLLECTIONS (that's right, a list of lists or maps)

A map can be:

MapNN = dictionary of numbers referenced by numeric keyword;
MapSN = dictionary of numbers referenced by string keyword;

MapNS = dictionary of strings referenced by numeric keyword;
MapSS = dictionary of strings referneced by string keyword;

MapNC = dictionary of collections referenced by numeric keyword;
MapSC = dictionary of collections referenced by string keyword;

--------

I recall that when you create a new list, the result of the function is the ID for that list.

So, for example:

Code: Select all

Vars: ListID(0);
ListID = ListN.New;
Creates a new list of numbers (since we used 'ListN' instead of 'ListS' (which would be used for strings)) and assigns the ID number to the variable 'ListID'. When you want to perform some function on that newly created listed, you would have to use that ListID to refer to it. This includes finding values in the list, removing values in the list, etc.

(I am just putting this here so that we understand where the ID for a list comes from.)

------

As I understand it, a Map is like a dictionary - it saves information using a keyword that you can retrieve non-sequentially. For example, I once used a Map to store a set of patterns in the form of "+---+----". Whenever I needed the system to tell me the associated probability for a pattern, I would submit the pattern to the map, it would look it up using the submitted pattern (as a string) and return some numeric value.

-----

For learning how to read a .csv file, I would check out the examples starting on page 13 of the ELCollections.doc file. If you continue to have problems, let me know. (Yes, this sounds like a cop out but the examples in the documentation are pretty good).

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

Re: Elcollection: Going through CSV file// Want to understan

Postby TJ » 20 Dec 2013

This is an example of a LIST

Image


This is an example of a MAP

Image
Attachments
Map.jpg
(5.1 KiB) Downloaded 1615 times
List.jpg
(2.33 KiB) Downloaded 1613 times

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

Re: Elcollection: Going through CSV file// Want to understan

Postby arjfca » 20 Dec 2013

FuryTrader / TJ,

I did succeed to go a little bit further. By reading back your note, I realized that I was using ListC instead of ListS to decode my string

Now, the code compile, but when run i got an error telling me that there is no collection with that name

The code is stopped when reaching ........ Count = ListS.Count(ReadFile_ID) ;

A print of the result for ReadFile_ID = ListS.ReadFile(List_ID, FileName); return 0

A print of FileName: Return : G:\ExcelMUlticharts\Database\TrendLine_USD.JPY.txt
Witch is the proper path and name of the file

Code: Select all

List_ID = ListS.New;

ReadFile_ID = ListS.ReadFile(List_ID, FileName);

Count = ListS.Count(ReadFile_ID) ;
print ("FL: ", Filename, " ", Count:0:0);
TL_String = "57,140.438360,140.438360,41613.5416666667,41627.1666666667,8388736,3,0,False,False";
The complete code

Code: Select all

//Read Trend line file and set them on the chart
[recoverdrawings = false]

Var:
List_ID (0),
StringCounter (0),
ListSize (0),
Index (0),
ReadFile_ID (0),
Count (0),

Intrabarpersist Filename (""),
Intrabarpersist TL_String (""),
Intrabarpersist TL_Num (0),
Intrabarpersist TL_Num_Ex (0),
Intrabarpersist TL_First (0),
Intrabarpersist TL_Last (0),
Intrabarpersist List_String (""),
Intrabarpersist TL_Num_String (0),
Intrabarpersist TL_Price_S (0),
Intrabarpersist TL_Price_E (0),
Intrabarpersist TL_Time_S (0),
Intrabarpersist TL_Time_E (0),
Intrabarpersist TL_Color (0),
Intrabarpersist Tl_Style (0),
Intrabarpersist TL_Size (0),
Intrabarpersist TL_Ext_L (False),
Intrabarpersist TL_Ext_R (False);


Filename = GVGetNamedString("BaseAdress","Error_Adress") +"_" + GetSymbolName + ".txt";
Value1 = GVSetNamedInt("DrawTL" + GetSymbolName,1);

List_ID = ListS.New;

ReadFile_ID = ListS.ReadFile(List_ID, FileName);
print ("FL: ", Filename, " ", list_ID:0:0, " ", readfile_ID:0:0);
Count = ListS.Count(ReadFile_ID) ;

TL_String = "57,140.438360,140.438360,41613.5416666667,41627.1666666667,8388736,3,0,False,False";

For Index = 1 to ListS.Count(ReadFile_ID) begin
TL_String = ListS.Get(ReadFile_ID,Index);

TL_Price_S = StrtoNum(Splitstring(TL_String,2));
TL_Price_E = StrtoNum(Splitstring(TL_String,3));
TL_Time_S = StrtoNum(Splitstring(TL_String,4));
TL_Time_E = StrtoNum(Splitstring(TL_String,5));
TL_Color= StrtoNum(Splitstring(TL_String,6));
TL_Style = StrtoNum(Splitstring(TL_String,7));
TL_Size = StrtoNum(Splitstring(TL_String,8));

If splitstring(TL_string, 9) = "True" then TL_Ext_l = True else TL_Ext_l = False;
If splitstring(TL_string, 10) = "True" then TL_Ext_r = True else TL_Ext_r = False;

TL_NUM = TL_New_DT(TL_Time_S,TL_Price_S,TL_Time_E,TL_Price_E);

TL_SetExtRight(TL_Num,TL_Ext_R ); // Extend to the right
TL_SetExtLeft(TL_Num,TL_Ext_L ); // Extend to the left
TL_SetSize(TL_Num,TL_Size);
TL_SetColor(TL_NUM, TL_Color);
TL_SetSize(TL_Num,TL_Size);
TL_SetStyle(TL_Num,TL_Style);
end;
playsound("G:\Users\Utilisateur\Documents\Bourse\Sound\Trend Line are draw.wav");
Copy of my data to draw trend line on Usd.Jpy in attach

Martin
Attachments
TrendLine_USD.JPY.txt
(405 Bytes) Downloaded 401 times

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

Re: Elcollection: Going through CSV file// Want to understan

Postby arjfca » 20 Dec 2013

Test done with still the same error.. No collection with that ID

Replace the FileName creation code by a Typed variable
G:\ExcelMUlticharts\Database\TrendLine_USD.JPY.txt

Modify my File so it contain like the example One word per line
CSCO
IBM
MSFT
ORCL


Quest continu

Martin

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

Re: Elcollection: Going through CSV file// Want to understan  [SOLVED]

Postby bowlesj3 » 20 Dec 2013

Hi Martin, Your code will not work. You need to take the example in this link and modify it to your application.
viewtopic.php?f=1&t=6901

Here is how you need to think about it to understand it. You need to think of the CSV file being loaded into a special excel spread sheet that is contained inside memory within MultiCharts. Here is how it works.

In my code this command is basically the same as opening the excel spread sheet empty.
ListC_ID = ListC.new

In my code this command is the same as putting all the data from the whole CSV file (that is all rows at once) into the spread sheet that was opened up buy the ListC.new command.
Value1 = ListC.ReadFile(ListC_ID,"C:\EL_CollectionsTestData.txt");

In my code this set of commands is the same as setting pointers to point at each column of this special spread sheet that is inside memory within MultiCharts. There are 3 columns in my example so there are three of these commands. Think of these as the header names for each column. In your example you need 10 of these commands because you have 10 columns rather than 3 columns. You should create names that match the type of data that is in each column. Put a comment in your code indicating these are your column names and maybe put that right in the name. For example ListName_ID could be Column1_Name_ID. ListState_ID could be Column2_State_ID, ListAge_ID could be Column3_Age_ID. This will help you in the future remember that the 1,2,3 are column numbers.

ListName_ID = ListC.Get(ListC_ID,1); {pointing a column 1 of the spread sheet}
ListState_ID = ListC.Get(ListC_ID,2); {pointing a column 2 of the spread sheet}
ListAge_ID = ListC.Get(ListC_ID,3); {pointing a column 3 of the spread sheet}


In my code this set of 3 commands would get the first row from the excel spread sheet.
MyName = ListS.get(ListName_ID,1);
MyState = ListS.get(ListState_ID,1);
MyAge = ListN.get(ListAge_ID,1);

In my code this set of 3commands would get the 2nd row from the excel spread sheet.
MyName = ListS.get(ListName_ID,2);
MyState = ListS.get(ListState_ID,2);
MyAge = ListN.get(ListAge_ID,2);

In my code this set of commands would get the 3rd row from the excel spread sheet.
MyName = ListS.get(ListName_ID,3);
MyState = ListS.get(ListState_ID,3);
MyAge = ListN.get(ListAge_ID,3);

Of course you want to set it up as a loop rather than duplicate these commands over and over to get the column values for all the rows. So instead you use the exact method you are using in your loop (using the index as your loop pointer for processing each row of this special spread sheet inside Multicharts memory). You might want to rename the index to a word such as RowNumber so you can be reminded to think of each loop as processing a new row in what you are picturing in your mind as an excel sheet inside of MultiCharts in memory.

To make it easier to understand still, you could rename the receiving fields shown above better as I have shown below. Make it a standard that you use over and over when processing a CSV file. Here is what I would do.
CurrentRow_MyName = ListS.get(ListName_ID,RowNumber);
CurrentRow_MyState = ListS.get(ListState_ID,RowNumber);
CurrentRow_MyAge = ListN.get(ListAge_ID,RowNumber);
So you know that (during each loop through this special excel sheet you visualize inside of Multicharts memory) the above fields on the left always contain the data for the current row as each loop is processed.

Here is the same code but with all the renaming in place.
CurrentRow_MyName = ListS.get(Column1_Name_ID,RowNumber);
CurrentRow_MyState = ListS.get(Column2_State_ID,RowNumber);
CurrentRow_MyAge = ListN.get(Column3_Age_ID,RowNumber);
Basically you can see how these commands point to specific column/rows in the sheet.

If you expect to be adding and removing columns you could use this naming standard.
CurrentRow_MyName = ListS.get(Name_Column_ID,RowNumber);
CurrentRow_MyState = ListS.get(State_Column_ID,RowNumber);
CurrentRow_MyAge = ListN.get(Age_Column_ID,RowNumber);
By doing this you only need to adjust the column numbering in the one location.


This whole process of selecting descriptive names is called "making your code self documenting". I mention it in the debugging thread of the FAQ section the MultiCharts forum.

Give that a try. This is the way I am able to understand it. Actually maybe I should go back to all my code and do this. That way I would not have to figure it out again every single time I look at it - LOL. I often do this when I go back to my own code. I come up with changes such as this to make it easier to understand and change the next time around (the longer the time until the next change the more important doing this is). It always find that "the process of trying to figure out what my old code is doing" creates the best ideas for improved naming.

So if this is not clear then do this. Pull in my code and rename everything as I have done above until you understand it (until it will compile too). Next go back and apply this new understanding to your code.

John
Last edited by bowlesj3 on 22 Dec 2013, edited 5 times in total.

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

Resolve: Elcollection: Going through CSV file//

Postby arjfca » 22 Dec 2013

Just to let you know that I succeed to use ElCollection

I got a better understanding of it, but I would probably fail a memory test on it at the moment :). As John mention somewhere, we need to use it often to learn it.

A special thank you to JohnB, FuryTrader and ABC

I will publish the code after MC rev 9. This version should allow to create trend line not attach to an indicatior

Happy Xmas
Happy New Year

Martin

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

Re: Elcollection: Going through CSV file// Want to understan

Postby bowlesj3 » 22 Dec 2013

I made an update to my example above and I copied the update here to point it out.

Here is the same code but with all the renaming in place.
CurrentRow_MyName = ListS.get(Column1_Name_ID,RowNumber);
CurrentRow_MyState = ListS.get(Column2_State_ID,RowNumber);
CurrentRow_MyAge = ListN.get(Column3_Age_ID,RowNumber);
Basically you can see how these commands point to specific column/rows in the sheet.

I think this is a good standard to use for processing CSV files with the EL collections.

If you expect to be adding and removing columns you could use this standard.
CurrentRow_MyName = ListS.get(Name_Column_ID,RowNumber);
CurrentRow_MyState = ListS.get(State_Column_ID,RowNumber);
CurrentRow_MyAge = ListN.get(Age_Column_ID,RowNumber);
by doing this you only need to adjust the column numbering in the one location.


Return to “MultiCharts”