Earn up to
$50000
for inviting friends
to get StartUp Bonus
from InstaForex
No investments required!
GET BONUS
55%
from InstaForex
on every deposit
Reply to thread
Results 1 to 1 of 1

Thread: How MQL4 handles files programmatically? part 3

  1. #1 You can automatically minimize the read posts in your account in the 'Forum Settings'
    Will work for pips
    I am:
    ----
     
    Somia2020 is on a distinguished road Somia2020's Avatar
    Join Date
    Oct 2020
    Posts
    93
    Accumulated bonus
    68 USD (What is this?)
    Thanks
    0
    Thanked 242 Times in 51 Posts
    SubscribeSubscribe
    Subsribed 0

    Post How MQL4 handles files programmatically? part 3

    Name: 1.png Views: 44 Size: 135.7 KB

    We complete this critical lesson with some simple examples to apply the previous concepts we learned earlier.

    Example of opening a read-only text file

    In this case, the file must be located on the disk.

    If the file does not exist, an error will occur, and we will not get the number or handle of this file.

    PHP Code:

    Code:
    int handle;
      handle=FileOpen("data.csv",FILE_CSV|FILE_READ,';');
      if(handle<1)
        {
         Print("File not found, the last error is ", GetLastError());
         return(false);
        }
    .....
    Note that the FileOpen function requires three variables:

    - The first variable is the name of the file that we want to open, and we are free to choose the name and extension that we want, and special conditions do not restrict us.

    - The second variable is the open file's properties; is it read-only, read and write, or write-only.

    We define this with the FILE_READ - FILE_WRIT variables. And merging between them by letter |

    We can determine the type of the open file from this same variable, whether it is text or binary.

    The text file we know as FILE_CSV and the binary FILE_BIN. The two cases cannot be combined, meaning we must choose only one case.

    In our previous example, we defined the file type as text and read-only.

    - The third argument is the default separator between the information written on the file, and here is the letter; The semicolon.

    Any other letter can be specified, but it is preferable to leave it because it is standard for other programs such as Excel.


    An example of how to write text information to a text file

    This example shows how to write some of the information we want into a text file with the FileWrite function.

    PHP Code:

    Code:
    int handle;
    handle=FileOpen("data.csv",FILE_CSV|FILE_WRITE,';');
    FileWrite(handle,Time[0],Close[0],Open[0],High[0],Low[0]);
    FileClose(handle);
    In the previous example, we opened the data.csv text file for writing only. In this case, if the file does not already exist, this function will always create it.

    It will delete the old file and create a new file every time it performs this function.

    Next, we used the FileWrite function to write an entire line of information, and here is the last bar information.

    If we run the previous code and open the data.csv file, we will find in it only one line containing the last candle information separating between
    Every piece of information and another letter.

    If we wanted to write all the chart information to the text file, we would use a loop for all the chart candles, and the example would become the figure
    next one :

    PHP Code:

    Code:
    int handle;
    handle=FileOpen("data.csv",FILE_CSV|FILE_WRITE,';');
    for(int i =0;i<=Bars;i++)
    {
      FileWrite(handle,Time[i],Close[i],Open[i],High[i],Low[i]);
    }
    FileClose(handle);
    Note that dealing with large files as writing should be done in the script and not through an index.

    Because it is unreasonable to write the full chart candlestick information into a text file every time the price moves.

    Though trading on financial markets involves high risk, it can still generate extra income in case you apply the right approach. By choosing a reliable broker such as InstaForex you get access to the international financial markets and open your way towards financial independence. You can sign up here.


  2. The Following User Says Thank You to Somia2020 For This Useful Post:

    ghada1996 (07-02-2021)

Reply to thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts