load input parameter from ini file

ezraluandre

Trader
Jun 17, 2020
33
1
24
31
Hi,

I want to set an input parameter using INI file. For a single symbol it works find but I want to use it for multi-currency purpose. Here's the code that I use,
MQL4:
input string InpSettingsFile = "Lybra/Test/external file.ini";    //input file name, blank = not used
 
int StoplossPoint, TakeProfitPoint;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() {
//---
   if(LoadFileMultiCurrency()) {
      Print("stoploss point: ", StoplossPoint);
      Print("take profit point: ", TakeProfitPoint);
   }
}
//+------------------------------------------------------------------+
bool LoadFileMultiCurrency() {
   bool result = false;
//for(int SymbolLoop=0; SymbolLoop < NumberOfTradeableSymbols; SymbolLoop++) {
 
   if(InpSettingsFile == "")
      return result;
   if(!FileIsExist(InpSettingsFile))
      return result;
 
   static long lastModDate = 0;
   long        modDate     = FileGetInteger(InpSettingsFile, FILE_MODIFY_DATE);
 
   if(modDate == lastModDate)
      return (result);
   lastModDate = modDate;
 
   string key     = "";
   string svalue  = "";
   string line    = "";
   string parts[];
 
   int handle = FileOpen(InpSettingsFile, FILE_SHARE_READ | FILE_TXT | FILE_ANSI);
   if(handle == INVALID_HANDLE)
      return (result);
   FileSeek(handle, 0, SEEK_SET);
 
   while(!FileIsEnding(handle)) {
      line = FileReadString(handle);
      StringSplit(line, '=', parts);
      key    = "";
      svalue = "";
      int size = ArraySize(parts);
      if(size > 0)
         key = parts[0];
      if(size > 1)
         svalue = parts[1];
      StringToLower(key);
 
      //evaluate the inputs
      if(key == "stoplosspoint")
         result |= SetValue(StoplossPoint, (int) StringToInteger(svalue));
      if(key == "takeprofitpoint")
         result |= SetValue(TakeProfitPoint, (int) StringToInteger(svalue));
   }
   FileClose(handle);
 
   return(result);
}
 
template <typename T> bool SetValue(T& currentValue, T newValue) {
   if(currentValue != newValue) {
      currentValue = newValue;
      return (true);
   }
   return (false);
}
//+------------------------------------------------------------------+

INI file content:
Code:
StoplossPoint=40
TakeProfitPoint=40

What I want to do is something like this:
Code:
[EURUSD]
StoplossPoint=40
TakeProfitPoint=40
[USDJPY]
StoplossPoint=40
TakeProfitPoint=40
[EURCAD]
StoplossPoint=40
TakeProfitPoint=40

How can I do that?
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,166
1,494
144
Odesa
www.earnforex.com
  1. You need to define the structure/array where you will be storing the info. You cannot store info for multiple symbols in just two plain variables.
  2. Inside your file-reading cycle, you need to check whether the line that you've just read is a currency definition or variable value. It's easy to do by checking whether the first character is a bracket ([).
  3. If you read a new symbol, you resize the data array and store the symbol name there. Then, after reading the variables, you store their values there.
  4. Once the cycle is over, you need to run an output cycle inside your OnStart(), going through the entire array of data (ArraySize() can help here) and printing the symbol and the variable info.