How to make a for loop through column of a csv file ?

To add comments, please log in or register
Florent
179
Florent  

Hello MQL5 community,


I generate a trading signal from an external application and I would like to use these signals as input for me EA. There can be no more than one signal per single day.

As soon as the external application generate a signal a new line is added at the end of the file containing all the information needed to open a position.

The CSV file to which informations are recorder looks like this :

2015.02.02;EURUSD;1;1.1290
2015.02.05;USDCHF;-1;0.9552
2015.02.10;EURJPY;1;134.620
2015.02.18;EURGBP;-1;0.7404

As you can see there is date, a currency pair, the type of position (i.e. 1 for long and -1 for short) and the limited price.

My first objective is to backtest this strategy in Metatrader, so I would like a condition that would say :

if (date of the bar is found in the csv file {

    - then close existing the position (if any)

    - and go long or go short the currencies pair at the limited price

}

This condition should works with historical data (backtesting) and live trade.

With the code bellow I'm able to read the different values in the file but I'm not so familiar with MQL langage and can't figure out how to handle CSV in an elegant manner to perform such verification.

Should I put all dates present in the csv into an array ? or is there a possibility to loop through them once a day with, for example :

for (date in dateCSV ) {

   test if date == date of the current bar

}
int init()                                      // Spec. funct. init()
   {
   
   Alert ("Function init() triggered at start");// Alert
   
   int Handle;                         // File descriptor
   
   string File_Name="currencies.csv",   // Name of the file
          Pair,                    // Pair
          Str_DtTm;                    // Date and time of the event (line)
          
   datetime Dat_DtTm;                  // Date and time of the event (date)
   
   char   Type;                   
   double Price;
   
   Handle=FileOpen(File_Name,FILE_CSV|FILE_READ,";");// File opening
   if(Handle<0)                        // File opening fails
     {
      if(GetLastError()==4103)         // If the file does not exist,..
         Alert("No file named ",File_Name);//.. inform trader
      else                             // If any other error occurs..
         Alert("Error while opening file ",File_Name);//..this message
      PlaySound("Bzrrr.wav");          // Sound accompaniment
      return;                          // Exit start()      
     }

   while(FileIsEnding(Handle)==false)     // While the file pointer..
     {                                    // ..is not at the end of the file
      
      Str_DtTm =FileReadString(Handle);   // Read the data
      Dat_DtTm =StrToTime(Str_DtTm);      // Transformation of data type
      Print("Date du signal:", Dat_DtTm);
      
      Pair =FileReadString(Handle); 
      Print("Pair :", Pair);
      
      Type =FileReadString(Handle); 
      Print("Type :", Type);
      
      Price =FileReadString(Handle); 
      Print("Prix :", Price);
      
      if(FileIsEnding(Handle)==true) {    // File pointer is at the end
      
         Print("File ends here !!!!");
         break;                           // Exit reading and drawing
      }
     
     }
   return;                                      // Exit init()
   }

Thank you,

Diane Minshew
381
Diane Minshew  
Florew:

Hello MQL5 community,


I generate a trading signal from an external application and I would like to use these signals as input for me EA. There can be no more than one signal per single day.

As soon as the external application generate a signal a new line is added at the end of the file containing all the information needed to open a position.

The CSV file to which informations are recorder looks like this :

As you can see there is date, a currency pair, the type of position (i.e. 1 for long and -1 for short) and the limited price.

My first objective is to backtest this strategy in Metatrader, so I would like a condition that would say :

This condition should works with historical data (backtesting) and live trade.

With the code bellow I'm able to read the different values in the file but I'm not so familiar with MQL langage and can't figure out how to handle CSV in an elegant manner to perform such verification.

Should I put all dates present in the csv into an array ? or is there a possibility to loop through them once a day with, for example :

Thank you,

1.The signal for trading no more that one signal per day would have to be a function that checks the deal history.

2.What are the CSV file properties.

3. Should I put all dates present in the csv into an array ? , If there is ever a question that you should then do it. You never know in the future if you are going to need it.

Its better have it and not need it than need it and not have it. 

 4. possibility to loop through them once a day.    Yes you can loop through them. there are examples in the code base also if you need help there are other programming languages that offer the capabilities just rewrite the code in C++ and rewrite some values for MQL. 

Florent
179
Florent  

Hello,

I've create a job service for this task.

https://www.mql5.com/en/job/24761

Freelance service at MQL5.com: I need an EA that read signal from a csv file
Freelance service at MQL5.com: I need an EA that read signal from a csv file
  • www.mql5.com
I need an Expert Advisor that works in strategy tester and in live trading on a Metatrader 4 terminal. I would like this job to be done through the "Job service". The task will be to read signal from a csv file and to execute orders when one or several new signals are detected. The file is updated daily but the EA must performs its check every 5 minutes. How it works ? Let's take an example to see how it works. Content of the file the 2015-01-01: Date,Pair,PriceLimit,PositionType File is empty,...
To add comments, please log in or register