Reading .CSV file columns int arrays

Examples step by step how to make your own automated strategy or user indicator
Message
Author
forextester
Posts: 33
Joined: Thu Feb 19, 2009 8:40 am

Reading .CSV file columns int arrays

#1 Postby forextester » Sun Mar 08, 2009 8:44 am

Dear Mike,

I hope you are in best of health.

Could you please help me read a csv file into an array.

Example of a csv file(one line)

2008.01.01 22:30;1;146;1;ALL;000000000;22:30 AUD L "AIG Manufacturing Index" 6564;A: 57.6 | P: 53.8

each column is separated by a semicolon. first one is datetime; others include currency, News event name, impact etc.

Metatrader has built in csv mode in it.


Code: Select all

handle = FileOpen(FileName, FILE_CSV|FILE_READ,';');            // Open NEWS.CSV
  if(handle==0)  Comment("File "+FileName+" not found.");

  for(eCount = 0; !FileIsEnding(handle); eCount++)  {             // Read from NEWS.CSV; one line per event
    eTime[eCount]     = StrToTime(FileReadString(handle));        // Event date/time
    eNumber[eCount]   = StrToInteger(FileReadString(handle));     // Event seq# (same date/time have ascending seq#s)
    eWingding[eCount] = StrToInteger(FileReadString(handle));     // Wingding code denoting implicated currency
    eImpact[eCount]   = StrToInteger(FileReadString(handle));     // Impact: 1=low; 2=medium; 3=High
    eCurrency[eCount] = FileReadString(handle);                   // Currency/ies on whose charts object will display; ALL = all currencies
    ePeriods[eCount]  = FileReadString(handle);                   // Time period settings on which object will display (M1,M5,M15,M30,H1,H4,D1,W1,MN; 0=no, 1=yes)
    eText1[eCount]    = FileReadString(handle);                   // First line of description
    eText2[eCount]    = FileReadString(handle);                   // Second line of description


Please provide a solution. It would mean a lot to me.

regards,
Sheraz

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

Re: Reading .CSV file columns int arrays

#2 Postby Terranin » Sun Mar 08, 2009 11:30 am

forextester wrote:Dear Mike,

I hope you are in best of health.

Could you please help me read a csv file into an array.

Example of a csv file(one line)

2008.01.01 22:30;1;146;1;ALL;000000000;22:30 AUD L "AIG Manufacturing Index" 6564;A: 57.6 | P: 53.8

each column is separated by a semicolon. first one is datetime; others include currency, News event name, impact etc.

Metatrader has built in csv mode in it.


Code: Select all

handle = FileOpen(FileName, FILE_CSV|FILE_READ,';');            // Open NEWS.CSV
  if(handle==0)  Comment("File "+FileName+" not found.");

  for(eCount = 0; !FileIsEnding(handle); eCount++)  {             // Read from NEWS.CSV; one line per event
    eTime[eCount]     = StrToTime(FileReadString(handle));        // Event date/time
    eNumber[eCount]   = StrToInteger(FileReadString(handle));     // Event seq# (same date/time have ascending seq#s)
    eWingding[eCount] = StrToInteger(FileReadString(handle));     // Wingding code denoting implicated currency
    eImpact[eCount]   = StrToInteger(FileReadString(handle));     // Impact: 1=low; 2=medium; 3=High
    eCurrency[eCount] = FileReadString(handle);                   // Currency/ies on whose charts object will display; ALL = all currencies
    ePeriods[eCount]  = FileReadString(handle);                   // Time period settings on which object will display (M1,M5,M15,M30,H1,H4,D1,W1,MN; 0=no, 1=yes)
    eText1[eCount]    = FileReadString(handle);                   // First line of description
    eText2[eCount]    = FileReadString(handle);                   // Second line of description


Please provide a solution. It would mean a lot to me.

regards,
Sheraz


I can only provide you solution in Delphi.

Code: Select all

uses
  StrsConv;

type
  TArrayItem = record
     time: TDateTime;
     number: integer;
     winding: integer;
     // ... and so on -  other fields
  end;

var
  f: TextFile;
  s: string;
  index: integer;
  arr: array[0..99] of TArrayItem;
  MyFormatSettings: TFormatSettings;


MyFormatSettings.ThousandSeparator := ',';
MyFormatSettings.DecimalSeparator := '.';
MyFormatSettings.DateSeparator := '.';
MyFormatSettings.TimeSeparator := ':';
MyFormatSettings.ShortDateFormat := 'yyyy.mm.dd';
MyFormatSettings.LongDateFormat := 'yyyy.mm.dd hh:nn';
MyFormatSettings.ShortTimeFormat := 'hh:nn';

AssignFile(f, 'your file');
reset(f);
index := 0;
while not(eof(f)) do
  begin
    readln(f, s);
    arr[index].time := StrToDateTime(GetSubStr(s, ';'), MyFormatSetting);
    arr[index].number := GetStrInt(s, ';');
    arr[index].winding := GetStrInt(s, ';');
    ... and so on

    inc(index);
  end;
CloseFile(f);


This is just an example how to parse text file. I attached used module StrsConv.pas
Attachments
StrsConv.zip
(3.43 KiB) Downloaded 321 times
Hasta la vista
Mike

forextester
Posts: 33
Joined: Thu Feb 19, 2009 8:40 am

#3 Postby forextester » Sun Mar 15, 2009 7:37 pm

great code Mike,

hope you are in best of health,
Thanks for solving the problem, i was having with .CSV file reading.

However I am having following two problems; and would appreciate your help. I have attached the .dpr, log and the csv file.

Problems.
1- The attached program code halts at 12/1/2008 1600, restarts reading the 'Xloutput.csv' from start and halts again at same date/time and then again continues to read the file from start, a couple of times.

2- As you can see in attached file 'Xloutput.csv', we have a new line format; and I'm having difficulty reading "Date and Time" into a single variable: Tdatetime. Instead i am reading Date and Time separately.
How can i read them in a single tdatetime variable?


01/01/2008,21:30,AUD,AIG Manufacturing Index,Low,57.6,,53.8



Looking forward impatiently to your helpful reply.

Regards,
Sheraz
Attachments
to mike.rar
(11.03 KiB) Downloaded 278 times

forextester
Posts: 33
Joined: Thu Feb 19, 2009 8:40 am

#4 Postby forextester » Sun Mar 15, 2009 7:37 pm

great code Mike,

hope you are in best of health,
Thanks for solving the problem, i was having with .CSV file reading.

However I am having following two problems; and would appreciate your help. I have attached the .dpr, log and the csv file.

Problems.
1- The attached program code halts at 12/1/2008 1600, restarts reading the 'Xloutput.csv' from start and halts again at same date/time and then again continues to read the file from start, a couple of times.

2- As you can see in attached file 'Xloutput.csv', we have a new line format; and I'm having difficulty reading "Date and Time" into a single variable: Tdatetime. Instead i am reading Date and Time separately.
How can i read them in a single tdatetime variable?


01/01/2008,21:30,AUD,AIG Manufacturing Index,Low,57.6,,53.8



Looking forward impatiently to your helpful reply.

Regards,
Sheraz
Attachments
to mike.rar
(11.03 KiB) Downloaded 277 times

forextester
Posts: 33
Joined: Thu Feb 19, 2009 8:40 am

#5 Postby forextester » Tue Mar 17, 2009 3:38 am

Dear Mike,

Hope you are in best of health.

a solution to the above posted problems would be very appreciated. With your capability, you could do this in a matter of minutes, of course, if you can spare some.

I am currently trying the trial of Forextester 2, so far so good. You have put a lot of effort into it to make it quite discerningly user friendly. I am hoping that you would bring the support for this product to an even higher level.

it comes with a strategy "Statement processor" which also accepts, as input "a metatrader statement " file. Does it accept any piece of code (a statement) fom a metatrader .mt4 file?

If i need to convert a metatrader indicator into forextester indicator would you help me?

regards,

Sheraz

forextester
Posts: 33
Joined: Thu Feb 19, 2009 8:40 am

Please reply

#6 Postby forextester » Wed Mar 18, 2009 9:06 am

Dear Mike,

I really need your help. Please reply with a solution.

thanks and regards,

Sheraz

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

#7 Postby Terranin » Thu Mar 19, 2009 9:38 am

I am really overloaded with work now, and have no time to read your code and correct mistakes. I will ask another person to look at it.
Hasta la vista

Mike

ghamm
Posts: 39
Joined: Wed Feb 11, 2009 3:02 pm

Re: Please reply

#8 Postby ghamm » Thu Mar 19, 2009 9:54 am

forextester wrote:Dear Mike,

I really need your help. Please reply with a solution.

thanks and regards,

Sheraz


Hi, In looking at your code, you will get an exception if you call StrToDateTime() with a bad date... You will need to either check that the date is correct format, or Trap the exception.

I.E.

Try
StrToDateTime()
Except
On E : Exception Do
Begin
// got an error, deal with it here.. Try again, reject etc..
End;
End;


Maybe Trap the exception and reject the record all together.


Your best bet is to hire someone that knows C or Delphi well. Its different than Metatrader, but many times more powerful for sure.

Gordon

forextester
Posts: 33
Joined: Thu Feb 19, 2009 8:40 am

#9 Postby forextester » Thu Mar 19, 2009 5:18 pm

Thanks Mike and many thanks Gordon,

I will try it and let you know.

thanks again.

regards
Sheraz

PS: Could you please recommend someone who could help me with the code every once in a while, either for a nominal fee, or for free if he/she could spare a few minutes?

forextester
Posts: 33
Joined: Thu Feb 19, 2009 8:40 am

does the object already exist on this date/time?

#10 Postby forextester » Mon Mar 23, 2009 7:02 pm

Dear Mike and Gordon,

I create vertical lines for news events on their date/time. if two or more news events have same date/time, the vertical lines overlap and you could lose important notifications.

How do i know if any object already exists on the same date/time?

would really appreciate the help guys.
Thanks in advance,

Sheraz

forextester
Posts: 33
Joined: Thu Feb 19, 2009 8:40 am

x hours old objects

#11 Postby forextester » Mon Mar 23, 2009 8:43 pm

Dear Mike and Gordon,

I have an increasing series of vertical lines; and i only want to delete the ones which are older than x hours. eg only 24 hours old vertical lines to be deleted

would really appreciate the help you guys.
Thanks and regards,

Sheraz

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

Re: does the object already exist on this date/time?

#12 Postby Terranin » Mon Mar 23, 2009 9:34 pm

forextester wrote:Dear Mike and Gordon,

I create vertical lines for news events on their date/time. if two or more news events have same date/time, the vertical lines overlap and you could lose important notifications.

How do i know if any object already exists on the same date/time?

would really appreciate the help guys.
Thanks in advance,

Sheraz


If you mean how you can know just looking on the chart, I can recommend to create some text objects near this line also. If you mean how to know from your code, the only way is to check by name with function ObjectExists.
Hasta la vista

Mike

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

Re: x hours old objects

#13 Postby Terranin » Mon Mar 23, 2009 9:38 pm

forextester wrote:Dear Mike and Gordon,

I have an increasing series of vertical lines; and i only want to delete the ones which are older than x hours. eg only 24 hours old vertical lines to be deleted

would really appreciate the help you guys.
Thanks and regards,

Sheraz


You can delete objects by name only. The only way is to keep the list of all objects created and delete old. Also you will need to save this list on disk, because after restart ForexTester it will be lost. For optimization reason I would recommend to save this list only when you made some changes in it.
Hasta la vista

Mike


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 11 guests