How to parse retrieving data from text file

 

Hi,

in my txt file I have:


Direction:BUY;Magic Number:432;Price:1.03654
Direction:SELL;Magic Number:44234232;Price:1.05654
Direction:BUY;Magic Number:43233;Price:1.07654



if (Direction==SELL && MagicNumber== 44234232)

{

Alert( Price:1.05654 )

}

I don't have idea how to do this from reading txt file.


Pls help and Thanks!


 
Dejan Krapez: I don't have idea how to do this from reading txt file.
  1. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)

  2. You have a semicolon separator. Read a string, isolate the direction after the colon. Repeat for the other two fields.

 
Dejan Krapez:

Hi,

in my txt file I have:

...

Pls help and Thanks!


StringSplit

Documentation on MQL5: String Functions / StringSplit
Documentation on MQL5: String Functions / StringSplit
  • www.mql5.com
StringSplit - String Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Dejan Krapez:

Hi,

in my txt file I have:


Direction:BUY;Magic Number:432;Price:1.03654
Direction:SELL;Magic Number:44234232;Price:1.05654
Direction:BUY;Magic Number:43233;Price:1.07654
if (Direction==SELL && MagicNumber== 44234232)

{

Alert( Price:1.05654 )

}


Here are the File Functions for MT4.

The first you're interested in is File Open (make sure to read the parameters it takes, the return value, and follow the example that's posted below in the link).

int  FileOpen(
   string  file_name,           // File name
   int     open_flags,          // Combination of flags
   short   delimiter=';',       // Delimiter
   uint    codepage=CP_ACP      // Code page
   );

Make sure to set the proper open_flags parameter. You say your file is a text file, but it appears that the values are stored as CSV. In which case, make sure to set the FILE_CSV flag.

From there, you'll be using FileReadString, FileIsLineEnding, and FileIsEnding. There's examples for all of them in the link I posted

One thing to note is that your text/CSV should use headers.

For example, instead of your text file looking this:

Direction:BUY; Magic Number:432; Price:1.03654  
Direction:SELL;
Magic Number:44234232;
Price:1.05654
Direction:BUY;
Magic Number:43233 Price:1.07654

It should be like this:

Direction
Magic Number Price
BUY
432
1.03654
SELL
4423432
1.05654
BUYY
43223
1.07654

You should also verify the headers and headers count when reading the first line.

Also, you'll probably want to store the values in an array of struct or object since they share a relationship.

Something like,

struct SOrders {
  string direction;
  int magic_number;
  double price;
};

SOrders orders[];

Lastly, don't forget FileClose when you're done reading from the file.