Using inputs defined in common file

 

I have created a logging file, called logging.mqh. Along with the class logic, I have also added the following code...

enum LogLevel
{
   Error,
   Warn,
   Info
};

input group     "Debugging Options"
input LogLevel  Logging_Level = Warn;

The 2 reasons for doing this are...

  1. So I can compile the file stand alone without errors.
  2. I don't have to create the input type for each new EA that wants to use the logging code.

I #include this file into my EA, and when I run the EA I set the log level and everything works as expected.

I also have a custom indicator that includes the logging file.

I call my custom indicator from the EA using iCustom() and pass in the Logging_Level as a parameter.

If I run the EA and change the logging level to Info, the logging level is set correctly for the EA, but the logging level for the custom indicator is still set to the default level of Warn. I thought that passing in the logging level parameter would override (initialize) the default logging level in the custom indicator in the same way that it does when starting the EA and use the value passed to the indicator, but this is not the case.

I could move the input variable into the EA itself, or I could also use a global variable for passing the logging level around, but before I go down that route, is there another simple solution that I may be overlooking?

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
metaRaider:

I have created a logging file, called logging.mqh. Along with the class logic, I have also added the following code...

The 2 reasons for doing this are...

  1. So I can compile the file stand alone without errors.
  2. I don't have to create the input type for each new EA that wants to use the logging code.

I #include this file into my EA, and when I run the EA I set the log level and everything works as expected.

I also have a custom indicator that includes the logging file.

I call my custom indicator from the EA using iCustom() and pass in the Logging_Level as a parameter.

If I run the EA and change the logging level to Info, the logging level is set correctly for the EA, but the logging level for the custom indicator is still set to the default level of Warn. I thought that passing in the logging level parameter would override (initialize) the default logging level in the custom indicator in the same way that it does when starting the EA and use the value passed to the indicator, but this is not the case.

I could move the input variable into the EA itself, or I could also use a global variable for passing the logging level around, but before I go down that route, is there another simple solution that I may be overlooking?

You will need to show the indicator files too part for an answer.

The include statement, the inputs.

And you need to show your iCustom call from the EA.

EDIT:
My guess is, you mixed something up.
 
Dominik Egert #:
You will need to show the indicator files too part for an answer.

The include statement, the inputs.

And you need to show your iCustom call from the EA.

EDIT:
My guess is, you mixed something up.

Here is a snippet of the EA...

#include <Logging.mqh>

int OnInit()
{
        LogError(StringFormat("EA Log Level: %s", EnumToString(Logging_Level)));
}

Here is a snippet of the indicator...

#include <Logging.mqh>

int OnInit()
{
        LogError(StringFormat("Indicator Log Level: %s", EnumToString(Logging_Level)));
}

And here is the call to the custom indicator from the EA...

int handle = iCustom(symbol, PERIOD_M1, "IndicatorTest", Logging_Level);

As you can see, both files are identical (apart from the text message), and they both include the logging class that contains the input statement.

It may be that this will not genuinely work, but I thought that it would!

 
metaRaider #:

Here is a snippet of the EA...

Here is a snippet of the indicator...

And here is the call to the custom indicator from the EA...

As you can see, both files are identical (apart from the text message), and they both include the logging class that contains the input statement.

It may be that this will not genuinely work, but I thought that it would!

it should work, are you sure there is no other input statement in the indicator?

Edit:

Please provide a compilable example of the issue.
 
metaRaider #:

Here is a snippet of the EA...

Here is a snippet of the indicator...

And here is the call to the custom indicator from the EA...

As you can see, both files are identical (apart from the text message), and they both include the logging class that contains the input statement.

It may be that this will not genuinely work, but I thought that it would!

Is this "Logging_Levels" the only input of the indicator? 
 
Dominik Egert #:
it should work, are you sure there is no other input statement in the indicator?

Edit:

Please provide a compilable example of the issue.

I have created the following demo project.

EA code

#property strict

#include "Logging.mqh"

int OnInit()
{
        PrintFormat("EA Log Level: %s", EnumToString(Logging_Level));

        if (iCustom(_Symbol, PERIOD_M1, "Indicator", Logging_Level) == INVALID_HANDLE)
        {
                Print("Failed to load indicator");
        }
        
        return (GetLastError());
}

void OnDeinit(const int reason)
{
}

Indicator code

#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0

#include "Logging.mqh"

int OnInit()
{
        PrintFormat("Indicator Log Level: %s", EnumToString(Logging_Level));
        
        return (GetLastError());
}

void OnDeinit(const int reason)
{
}

int OnCalculate (const int rates_total,      // size of price[] array
                 const int prev_calculated,  // number of bars, calculated at previous call
                 const int begin,            // data begin index
                 const double& price[])      // array for calculation
{
        return (rates_total);
}

Shared logging code

enum LogLevel
{
   Error,
   Warn,
   Info
};

input group     "Debugging Options"
input LogLevel  Logging_Level = Warn;

When I run the ea and change the logging level to Info, the ea shows the correct logging level, but the indicator does not. See following output:

2024.01.23 09:33:08.318 EA (GBPUSD,H1)  EA Log Level: Info
2024.01.23 09:33:08.332 Indicator (GBPUSD,M1)   Indicator Log Level: Warn
 
metaRaider #:

I have created the following demo project.

EA code

Indicator code

Shared logging code

When I run the ea and change the logging level to Info, the ea shows the correct logging level, but the indicator does not. See following output:

OK, I have now got to the bottom of the issue, and unless told otherwise, I'll assume this is a bug. The cause of the problem is the following...

input group     "Debugging Options"



If I remove the input group then it works as expected. So it would appear that the group is also being passed as a parameter (I have proved this!).

I have checked the documentation and I can't see anything in there relating to the use of group with an indicator.

Note: This only affects indicators and not expert advisors!

Can anyone please confirm that this is indeed an issue (I am using MT5 V5.00 build 4150).

 
If this is a genuine bug, how do I report it as I have had a look around the MQL5 Community and nothing seems obvious?
 
metaRaider #:

I have created the following demo project.

EA code

#property strict

#include "Logging.mqh"

Indicator code

#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0

#include "Logging.mqh"

Shared logging code

When I run the ea and change the logging level to Info, the ea shows the correct logging level, but the indicator does not. See following output:

Why are you using 2 copies of the include file?

The quotes indicate that this file should be taken from the same folder where the advisor/indicator is located.

If both the advisor and the indicator are compiled, this means that you have 2 copies of the header file. One copy is located in the same folder where the advisor is, and the second copy is located in the same folder where the indicator.

Including Files (#include)
If the file name is enclosed in quotation marks, the search is made in the current directory (which contains the main source file). The standard directory is not included in the search.

Use angle brackets to include the same header file in both the expert and the indicator

Including Files (#include)
The preprocessor replaces the line #include <file_name> with the content of the file file_name.mqh. Angle brackets indicate that the file_name.mqh file will be taken from the standard directory (usually it is terminal_installation_directory\MQL5\Include). The current directory is not included in the search.

 
metaRaider #:

EA code

#property strict

#property strict does not change anything in mql5

Reason: