How to include a *.mq4 file into a *.mqh OR *.mqh file and *.mq4 file into EA, being linked to each other

 

Hello together,

Could you please tell me how to include a *.mq4 file into a *.mqh (not EA) file.

Or is it possible to include a *.mqh file (not EA) and *.mq4 file into an EA, whereat the included *.mqh file (not EA) would need input from the included *.mq4 file.

Thank you in advance and have a nice weekend.
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
  1. tylerdd: Could you please tell me how to include a *.mq4 file into a *.mqh (not EA) file.

    Same way you include an mqh into a mq4. The extension is irrelevant.

    #include <filename>
       or 
    #include "filename"
  2. tylerdd: Or is it possible to include a *.mqh file (not EA) and *.mq4 file into an EA,

    Your “into an EA” implies a compiled ex4. Include only applies to source files.

 
William Roeder #:
  1. Same way you include an mqh into a mq4. The extension is irrelevant.

  2. Your “into an EA” implies a compiled ex4. Include only applies to source files.

Thank you a lot William Roeder and have a nice weekend.
 

Hello together,

I could integrate the indicator (*.mq4.file) into the EA. At the same time this leads to the following error within the EA:

'OnInit' - function already defined and has body


Understanding there are 2 'OnInit' function, which seems to cause the error, I do not understand why the indicator and EA(=2 different files) can not have an 'OnInit' function each. Is there any solution available?


That's the 'OnInit' function within the indicator:

int OnInit()
{
     IndicatorDigits (5);
     SetIndexBuffer  (0, BufferUpperLine, INDICATOR_DATA);
     SetIndexBuffer  (1, BufferLowerLine, INDICATOR_DATA);

     SetIndexDrawBegin(0,InpSupertrendPeriod+InpSupertrendShift);
     SetIndexDrawBegin(1,InpSupertrendPeriod+InpSupertrendShift);
     
     SetIndexBuffer  (2, BufferUpperValue, INDICATOR_CALCULATIONS);
     SetIndexLabel   (2, NULL); // incicator will not appear on the screen
     SetIndexBuffer  (3, BufferLowerValue, INDICATOR_CALCULATIONS);
     SetIndexLabel   (3, NULL); // incicator will not appear on the screen
     SetIndexBuffer  (4, BufferTrend,  INDICATOR_CALCULATIONS);
     SetIndexLabel   (4, NULL); // incicator will not appear on the screen
 
        
     return(INIT_SUCCEEDED);
}


That's the 'OnInit' function within the EA:

int OnInit()
{
   // Set magic number
   Trade.SetMagicNumber(MagicNumber);
   Trade.SetSlippage(Slippage);

   return(INIT_SUCCEEDED);
}
 
tylerdd #: I could integrate the indicator (*.mq4.file) into the EA.
  1. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

    Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.
              (MT4) Detailed explanation of iCustom - MQL4 programming forum (2017)

  2. Just embed the other indicator(s) inside your indicator/EA. Add the CI(s) to your code as a resource.

    #resource "\\Indicators… iCustom("::Indicators…
              Use the publicly released code - MQL5 programming forum (2017)
              Resources - MQL4 Reference

    Be aware that using resources is 40x times slower than using CIs directly.
              A custom indicator as a resource - MQL4 programming forum (2019)

    Also make use there are no spaces in the path.
              Getting error 4802 when loading custom indicator that loads another custom indicator with iCustom - Technical Indicators - MQL5 programming forum. (2020)

    Also Use of Resources in MQL5 - MQL5 Articles (2011)

 

hello tylerdd,

the answer from others above are all fine you should read and understand of them.


however,i have a feeling that maybe you are not understand ...something about .mq4/5 ,.ex4(compiled indicator or EA) and .mqh.

you can line a while from a file A into another file B, just write a line of colde like below in B:

#include "A"

no matter A is .mqh or .mq4/5,but one thing is very important:they are MUST BE IN PAIN TEXT FORMAT.

codes are need to be compiled to became an executeable file,which is ex4/5 in MT4/5.

before the compile, there is one more step: pre-process,which will process all MACRO COMMAND(start with #)

#include is one of the MACRO, and the #include "filename"  means the preprocessor will copy all codes from "filename" and insert them in the current position, #include "filename" will be delete .

when all MACRO are processed,the new codes  will go to next step: compile to execute code,the code for the run-time.


in MQL  both pro-processor and compiler are inside MetaEditor.works will be done in one-click so it's quite difficult to understand above for non-programmer.

if you want to under the MACRO, using C-Language develop kit is a choice.


conclusion:

1. include a A.mq4 file into a B.mqh : by writing "#include "A.mq4" in B.mqh

    in pain text format, in right position, no duplicate-defination!

2. is it possible to include a *.mqh file (not EA) and *.mq4 file into an EA

    for EA source code,yes.but please read 1. above ,and beware they cannot be in one just by this way easily,and some function or macros are for indicator not working for EA,if you include in EA,they dont work.

    for compiled EA,it won't work. you cannot expect that you compile an EA with "#include AAA.mqh" and later change aaa.mqh to change the EA feature.aaa.mqh is a base of EA,it need to re-compile EA to make change effected.

tylerdd
tylerdd
  • 2023.11.11
  • www.mql5.com
Trader's profile
 
William Roeder #:
  1. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

    Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.
              (MT4) Detailed explanation of iCustom - MQL4 programming forum (2017)

  2. Just embed the other indicator(s) inside your indicator/EA. Add the CI(s) to your code as a resource.

    #resource "\\Indicators… iCustom("::Indicators…
              Use the publicly released code - MQL5 programming forum (2017)
              Resources - MQL4 Reference

    Be aware that using resources is 40x times slower than using CIs directly.
              A custom indicator as a resource - MQL4 programming forum (2019)

    Also make use there are no spaces in the path.
              Getting error 4802 when loading custom indicator that loads another custom indicator with iCustom - Technical Indicators - MQL5 programming forum. (2020)

    Also Use of Resources in MQL5 - MQL5 Articles (2011)

Hello William Roeder, Thank you so much and sorry for the late reply. The past past few days have been rough. I will check on your links sent. Have a great week.
 
Guo Zheng Feng #:

hello tylerdd,

the answer from others above are all fine you should read and understand of them.


however,i have a feeling that maybe you are not understand ...something about .mq4/5 ,.ex4(compiled indicator or EA) and .mqh.

you can line a while from a file A into another file B, just write a line of colde like below in B:

#include "A"

no matter A is .mqh or .mq4/5,but one thing is very important:they are MUST BE IN PAIN TEXT FORMAT.

codes are need to be compiled to became an executeable file,which is ex4/5 in MT4/5.

before the compile, there is one more step: pre-process,which will process all MACRO COMMAND(start with #)

#include is one of the MACRO, and the #include "filename"  means the preprocessor will copy all codes from "filename" and insert them in the current position, #include "filename" will be delete .

when all MACRO are processed,the new codes  will go to next step: compile to execute code,the code for the run-time.


in MQL  both pro-processor and compiler are inside MetaEditor.works will be done in one-click so it's quite difficult to understand above for non-programmer.

if you want to under the MACRO, using C-Language develop kit is a choice.


conclusion:

1. include a A.mq4 file into a B.mqh : by writing "#include "A.mq4" in B.mqh

    in pain text format, in right position, no duplicate-defination!

2. is it possible to include a *.mqh file (not EA) and *.mq4 file into an EA

    for EA source code,yes.but please read 1. above ,and beware they cannot be in one just by this way easily,and some function or macros are for indicator not working for EA,if you include in EA,they dont work.

    for compiled EA,it won't work. you cannot expect that you compile an EA with "#include AAA.mqh" and later change aaa.mqh to change the EA feature.aaa.mqh is a base of EA,it need to re-compile EA to make change effected.

Hello Guo Zheng Fen, Also to you a big thank you for your reply. Have a great evening.
 

I think your phrasing is what causes the confusion.


If I understand correctly what you are trying to achieve-

You are trying to embed the files into the EA.


Just follow William Roeder's

William Roeder #:

2. Just embed the other indicator(s) inside your indicator/EA. Add the CI(s) to your code as a resource.

#resource "\\Indicators… iCustom("::Indicators…
          Use the publicly released code - MQL5 programming forum (2017)
          Resources - MQL4 Reference

Be aware that using resources is 40x times slower than using CIs directly.
          A custom indicator as a resource - MQL4 programming forum (2019)

Also make use there are no spaces in the path.
          Getting error 4802 when loading custom indicator that loads another custom indicator with iCustom - Technical Indicators - MQL5 programming forum. (2020)

Also Use of Resources in MQL5 - MQL5 Articles (2011)

    Reason: