How i get value from file to expert advisor?

 

Hi All!

I have a ForexSymbol.txt file and save 28 forex paird. "

AUDCAD,AUDCHF,AUDJPY,AUDNZD,AUDUSD,CADCHF,CADJPY,CHFJPY,EURAUD,EURCAD,EURCHF,EURGBP,EURJPY,EURNZD,EURUSD,GBPAUD,GBPCAD,GBPCHF,GBPJPY,GBPNZD,GBPUSD,NZDCAD,NZDCHF,NZDJPY,NZDUSD,USDCAD,USDCHF,USDJPY

How i get data form it to expert with format

string SYMBOL[30];
for (int i=1; i<=28; i++)
{
        SYMBOL[i]= What i will do to get data from my files?

}

End with

SYMBOL[1]="AUDCAD"

SYMBOL[...]=...

SYMBOL[28]="USDJPY"

 
Nguyễn Thanh Sang :


1. First you need to read the line from the file into a variable

2. Further work according to the example from the documentation ( 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
 

So an example:

Stage number 1:

Let's start with the location of the text file: in the MetaEditor 5 editor in the ' File ' menu, select the item ' Open Data Folder r':

in explorer click on folder ' MQL5 '

in explorer click on the folder ' Files '

in the folder that opens, create our file 'ForexSymbol.txt' and write a string with symbols into it

AUDCAD,AUDCHF,AUDJPY,AUDNZD,AUDUSD,CADCHF,CADJPY,CHFJPY,EURAUD,EURCAD,EURCHF,EURGBP,EURJPY,EURNZD,EURUSD,GBPAUD,GBPCAD,GBPCHF,GBPJPY,GBPNZD,GBPUSD,NZDCAD,NZDCHF,NZDJPY,NZDUSD,USDCAD,USDCHF,USDJPY


Stage number 2:

Let's launch our Expert Advisor:

//+------------------------------------------------------------------+
//|                                           CFileTxt test Read.mq5 |
//|                         Copyright © 2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.000"
//---
#include <Trade\SymbolInfo.mqh>
#include <Files\FileTxt.mqh>
//---
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
CFileTxt       m_file_txt;                   // file txt object
//--- input parameters
input string   InpFileName = "ForexSymbol.txt"; // File name
//---
string   m_array_symbols[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_file_txt.Open(InpFileName,FILE_READ))
     {
      Print("Error open ',",InpFileName,"' #",GetLastError());
      return(INIT_FAILED);
     }
   Print("File ',",InpFileName,"' opened successfully");
   string sep=",";                // A separator as a character
   ushort u_sep;                  // The code of the separator character
//--- Get the separator code
   u_sep=StringGetCharacter(sep,0);
   while(!m_file_txt.IsEnding())
     {
      //--- read and print the string
      string to_split=m_file_txt.ReadString();
      Print(to_split);
      string result[];               // An array to get strings
      //--- Split the string to substrings
      int k=StringSplit(to_split,u_sep,result);
      //--- Show a comment
      PrintFormat("Strings obtained: %d. Used separator '%s' with the code %d",k,sep,u_sep);
      //--- Now output all obtained strings
      if(k>0)
        {
         for(int i=0; i<k; i++)
           {
            ResetLastError();
            if(!m_symbol.Name(Symbol())) // sets symbol name
              {
               Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
               continue;
              }
            PrintFormat("result[%d]=\"%s\"",i,result[i]);
            int size=ArraySize(m_array_symbols);
            ArrayResize(m_array_symbols,size+1,10);
            m_array_symbols[size]=result[i];
           }
        }
     }
   m_file_txt.Close();
   Print("- - -");
   ArrayPrint(m_array_symbols);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+

Result:

File ',ForexSymbol.txt' opened successfully
AUDCAD,AUDCHF,AUDJPY,AUDNZD,AUDUSD,CADCHF,CADJPY,CHFJPY,EURAUD,EURCAD,EURCHF,EURGBP,EURJPY,EURNZD,EURUSD,GBPAUD,GBPCAD,GBPCHF,GBPJPY,GBPNZD,GBPUSD,NZDCAD,NZDCHF,NZDJPY,NZDUSD,USDCAD,USDCHF,USDJPY
Strings obtained: 28. Used separator ',' with the code 44
result[0]="AUDCAD"
result[1]="AUDCHF"
result[2]="AUDJPY"
result[3]="AUDNZD"
result[4]="AUDUSD"
result[5]="CADCHF"
result[6]="CADJPY"
result[7]="CHFJPY"
result[8]="EURAUD"
result[9]="EURCAD"
result[10]="EURCHF"
result[11]="EURGBP"
result[12]="EURJPY"
result[13]="EURNZD"
result[14]="EURUSD"
result[15]="GBPAUD"
result[16]="GBPCAD"
result[17]="GBPCHF"
result[18]="GBPJPY"
result[19]="GBPNZD"
result[20]="GBPUSD"
result[21]="NZDCAD"
result[22]="NZDCHF"
result[23]="NZDJPY"
result[24]="NZDUSD"
result[25]="USDCAD"
result[26]="USDCHF"
result[27]="USDJPY"
- - -
[ 0] "AUDCAD" "AUDCHF" "AUDJPY" "AUDNZD" "AUDUSD" "CADCHF" "CADJPY" "CHFJPY" "EURAUD" "EURCAD" "EURCHF" "EURGBP" "EURJPY" "EURNZD"
[14] "EURUSD" "GBPAUD" "GBPCAD" "GBPCHF" "GBPJPY" "GBPNZD" "GBPUSD" "NZDCAD" "NZDCHF" "NZDJPY" "NZDUSD" "USDCAD" "USDCHF" "USDJPY"
Files:
 

Thanks You @ Vladimir Karputov!

That is great information to me!