How to programmatically get list of input parameters

 

Hi there,

I was wondering if it was possible to get a list of input parameters given to an EA or Indicator?

Right now I'm logging my EA's settings to a new file automatically whenever it detects that I'm using different settings so that I can look back and see what I was using in the past.  However, this relies on me hard coding the input variables into a function which was ok at first but sometimes I forget to update the function when I add a new input and then of course it's settings aren't logged to the file.

Thanks

 

I'm using a simple code like ...

input bool ShowInfoOnChart = true;

Then, use Comment( ) to display all settings on chart.

This way you cans switch On and Off the setting show on the chart the easy way.

 
Marc Dixon:

Hi there,

I was wondering if it was possible to get a list of input parameters given to an EA or Indicator?

Right now I'm logging my EA's settings to a new file automatically whenever it detects that I'm using different settings so that I can look back and see what I was using in the past.  However, this relies on me hard coding the input variables into a function which was ok at first but sometimes I forget to update the function when I add a new input and then of course it's settings aren't logged to the file.

Thanks

Sadly there are no built in methods to programmatically load/save the input variables. You must manage them yourself.
 

That's unfortunate.


Thanks for your help.

 

Use ChartSaveTemplate + parsing of new tpl-file.

Example for buffer's color

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Как в индикаторе узнать текущий цвет индикаторной линии?

fxsaber, 2017.05.12 13:45

#property strict

#property indicator_chart_window
#property indicator_buffers 2

#define PATH "MQL4\\indicators\\"

#include <TypeToBytes.mqh> // https://www.mql5.com/ru/code/16280

string GetIndicatorName( void )
{
  const string StrName = ::MQLInfoString(MQL_PROGRAM_PATH);
  const int Pos = ::StringFind(StrName, PATH) + ::StringLen(PATH);
  
  return(::StringSubstr(StrName, Pos, ::StringLen(StrName) - Pos - 4));
}

void SeekToString( const int handle, const string Str )
{
  while (!::FileIsEnding(handle))
    if (::FileReadString(handle) == Str)
      break;
  
  return;
}  

struct BUFFER_STRUCT
{
  int Shift;
  int Type;
  color Color;
  ENUM_LINE_STYLE Style;
  int Width;
};

const BUFFER_STRUCT GetBufferProperties( const uint Num = 0, const bool FlagSave = true )
{
  BUFFER_STRUCT Res = {0};
  
  const string FileName = ::WindowExpertName() + ".tpl";

  if (FlagSave ? ::ChartSaveTemplate(0, "..\\MQL4\\Files\\" + FileName) : true)
  {
    const int handle = ::FileOpen(FileName, ::FILE_READ|::FILE_CSV);

    if (handle > 0)
    {
      ::SeekToString(handle, "name=" + ::GetIndicatorName());
      
      if (Num == 0)
        ::SeekToString(handle, "</expert>");
      else
      {
        const string TmpStr = "weight_" + (string)(Num - 1);
        
        while (!::FileIsEnding(handle))
          if (::StringFind(::FileReadString(handle), TmpStr) == 0)
            break;
      }
            
      if (!::FileIsEnding(handle))
      {
        static const string Property[] = {"shift", "draw", "color", "style", "weight"};
        const string StrNum = "_" + (string)Num + "=";
              
        for (int i = 0; i < ::ArraySize(Property); i++)
          _W(Res)[i * sizeof(int)] = (int)::StringToInteger(::StringSubstr(::FileReadString(handle), ::StringLen(Property[i] + StrNum)));
      }
      
      ::FileClose(handle);
    }
  }
  
  return(Res);
}  

void OnInit()
{  
  string Str = "Colors:";
  
  for (int i = 0; i < indicator_buffers; i++)
    Str += " " + (string)i + "-" + (string)::GetBufferProperties(i).Color;
    
  Alert(Str);
}

void start()
{
}
 
Marc Dixon:

I was wondering if it was possible to get a list of input parameters given to an EA or Indicator?

#include <fxsaber\Expert.mqh> // https://www.mql5.com/ru/code/19003

void OnStart()
{
  MqlParam Parameters[];
  string Names[];

  if (EXPERT::Parameters(0, Parameters, Names))
    ArrayPrint(Names);
}
 
fxsaber:

This is MT4 section. The library supports MT5 only.

 
Stanislav Korotky:

This is MT4 section. The library supports MT5 only.

It was an example. The library can be made to work in MT4 by adding a few lines. TPL-format is the same in MT4/5.

 

hello!

How do I get the parameters from the input of an indicator to put it into ea in mql4?

Thank

 
skyskyusd LUONG: How do I get the parameters from the input of an indicator to put it into ea in mql4?

Asked and answered. #4

You really don't need to. The EA calls the indicator with iCustom. Put the inputs in the EA.
 

I have written yesterday a pyhton file, which parses the mq5 file and generates code, which returns all the input parameters and its values.


"""
this script collects all inputs of a mql EA
and stores them in a generated header file of an MQL
for convinient access.
usage: 
#include "GeneratedFile.mqh"
string param_strings[];
getParams(param_strings);
//now print or write them to a log file.

"""

#testing computing grid TP
import numpy as np
import argparse
import logging
import time
import re
from jinja2 import Template

parser = argparse.ArgumentParser()


parser.add_argument("mq5_file", help="The EA mq5 file")
parser.add_argument("--loglevel",  default="info", help="error, warning or info")
args = parser.parse_args()


log_level=logging.ERROR
if args.loglevel=="info":
    log_level=logging.INFO
if args.loglevel=="warning":
    log_level=logging.WARNING

recfmt = "%(threadName)s || %(levelname)s: %(asctime)s.%(msecs)03d - [%(filename)s:%(lineno)s:%(funcName)s ] %(message)s" #'(%(threadName)s) %(asctime)s.%(msecs)03d %(levelname)s %(filename)s:%(lineno)d %(message)s'
timefmt = '%y%m%d_%H:%M:%S'
logging.basicConfig(filename=time.strftime("./collect_inputs.log"), #%y%m%d_%H%M%S.log"),
                        filemode="w",
                        level=log_level,
                        format=recfmt, datefmt=timefmt)

def type2converter(type_name):
    """
    generates a function like IntegerToString if type_name is int
    """
    out={"int":"IntegerToString",
        "string":"string",
        "double":"DoubleToString",
        "bool":"IntegerToString"}
    types = []
    if not type_name in out:
        return "IntegerToString" #might be a struct or enum
    return out[type_name]



lines=[]
with open(args.mq5_file) as f:
    lines = f.readlines()

inputs_str=[]

for li in lines:
    if (li.startswith("input") and not "group" in li):
        inputs_str.append(li)

print(inputs_str)
params = {}
for inp in inputs_str:
    #now parse the line.
    m = re.findall(r"input (\w+)\ +(\w+)", inp)[0]
    param_name = m[1]
    param_type = m[0]
    params[param_name]={'type':param_type, 'converter':type2converter(param_type)}

#now generate the code
tpl="""
void getParams(string & out[]) 
{
    //returns all param=value of the inputs
    string p = "";
    {% for param, info in params.items() %}
        p = \"{{param}} = \"+{{info['converter']}}({{param}}); 
        arrayPush(out, p);
    {% endfor %}
}

"""

j2_template = Template(tpl)
#print(params)
code = j2_template.render({'params':params})
print(code)

out_filename = args.mq5_file.replace(".mq5", "")+"_inputs.mqh"
with open(out_filename, 'w') as f:
    f.write(code)



I hope it helps folks.