Embed indicator code into an EA

 
Hi all, I'm new with MQL4 and I have this indicator which I should embed inside an EA.

I know I could use the iCustom function but for several reasons I have to embed the code right inside an EA function.

Could you give me some suggestions to avoid modifying whole indicator code? Also some simple explanation would be nice.

I would also be interested in paid private lessons from very experienced MQL4 developers if any of you offer this kind of service. 


Thanks to everyone who can help me


#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Gray
#property indicator_color2 RoyalBlue
extern int MaPriod = 34;
extern double value = 0.5;

double buffer1[];
double buffer2[];
double buffer3[];

double minVolume;
int init() {
    minVolume = Volume[iLowest(NULL, 0,MODE_VOLUME, WHOLE_ARRAY, 0)];
    IndicatorBuffers(3);
    SetIndexStyle(0,DRAW_HISTOGRAM, EMPTY, 3);
    SetIndexStyle(1,DRAW_HISTOGRAM, EMPTY, 3);
    
    SetIndexBuffer(0, buffer1); //Does this function also initialize a dynamic array?
    SetIndexBuffer(1, buffer2);
    SetIndexBuffer(2, buffer3);
    
      
    IndicatorShortName("Volatility("+MaPriod+ ")");
    SetIndexDrawBegin(0,MaPriod);
    
    
    SetIndexLabel(0, "ma");
    SetIndexLabel(1, "value");
    SetIndexLabel(2, "action");

    IndicatorDigits(Digits + 3);    
}


int start() {
    int i;
    int counted_bars=IndicatorCounted(); //How can I develop an IndicatorCounted() equal function to use in an EA?
     if(counted_bars > 0) {
        counted_bars--;
    }
    int limit = Bars-counted_bars-1;
   
    for(i = limit - 1 ; i >= 0; i--) {
        buffer3[i] = (
            MathAbs(High[i+1] - Low[i]) + 
            MathAbs(High[i] - Low[i+1]) + 
            MathAbs(Close[i+1] - Close[i])
        )  
        * (Volume[i] - minVolume);
    }
    
    if(Bars - limit <  MaPriod*15) {
        limit = limit - MaPriod*15;
    }
    
    for(i = limit -1; i >= 0; i--) {
        buffer1[i] = iMAOnArray(buffer3, 0, MaPriod, 0, MODE_EMA, i);
    }
    
    double sum = 0;
    int count = 0;
    for(i = Bars; i >= 0; i--) {
        if(buffer1[i] != EMPTY_VALUE) { 
            sum += buffer1[i];
            count++;
        }
    }
    sum = sum / count;

    for(i = limit - 1 ; i >= 0; i--) {
        if(sum * value < buffer1[i]) {
            buffer2[i] = buffer1[i];
        } else {
            buffer2[i] = EMPTY_VALUE;
        }
    }
    
    return(0);
}

I also did the indicator conversion using OnCalculate(), do you think this is correct?

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Green
extern int MaPriod = 34;
extern double value = 0.5;

double buffer1[];
double buffer2[];
double buffer3[];

double minVolume;
int init() {
    minVolume = Volume[iLowest(NULL, 0,MODE_VOLUME, WHOLE_ARRAY, 0)];
    IndicatorBuffers(3);
    SetIndexStyle(0,DRAW_HISTOGRAM, EMPTY, 3);
    SetIndexStyle(1,DRAW_HISTOGRAM, EMPTY, 3);
    
    SetIndexBuffer(0, buffer1);
    SetIndexBuffer(1, buffer2);
    SetIndexBuffer(2, buffer3);
    
      
    IndicatorShortName("Volatility("+MaPriod+ ")");
    SetIndexDrawBegin(0,MaPriod);
    
    
    SetIndexLabel(0, "ma");
    SetIndexLabel(1, "value");
    SetIndexLabel(2, "action");

    IndicatorDigits(Digits + 3);    
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   
   int i;
   int limit=rates_total-prev_calculated;
   
   for(i = limit; i >= 0; i--) {
        buffer3[i] = (
            MathAbs(High[i+1] - Low[i]) + 
            MathAbs(High[i] - Low[i+1]) + 
            MathAbs(Close[i+1] - Close[i])
        )  
        * (Volume[i] - minVolume);
    }
    
    
    if(Bars - limit <  MaPriod*15) {
      limit = limit - MaPriod*15;
    }
    
    for(i = limit -1; i >= 0; i--) {
      buffer1[i] = iMAOnArray(buffer3, 0, MaPriod, 0, MODE_EMA, i);
    }
    
    double sum = 0;
    int count = 0;
    for(i = Bars; i >= 0; i--) {
        if(buffer1[i] != EMPTY_VALUE) { 
            sum += buffer1[i];
            count++;
        }
    }
    
    sum = sum / count;
    
    for(i = limit - 1 ; i >= 0; i--) {
        if(sum * value < buffer1[i]) {
            buffer2[i] = buffer1[i];
        } else {
            buffer2[i] = EMPTY_VALUE;
        }
    }
      
   
   return(rates_total);
  }
 
Marco De Angelis:
Hi all, I'm new with MQL4 and I have this indicator which I should embed inside an EA.

I know I could use the iCustom function but for several reasons I have to embed the code right inside an EA function.

Could you give me some suggestions to avoid modifying whole indicator code? Also some simple explanation would be nice.
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. 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)

  3. You have several reasons, but do not state them.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem
              The XY Problem

  4. Just embed the other indicator(s) inside your indicator/EA. Add the CI(s) to your code as a resource.
              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)

 
Marco De Angelis: Hi all, I'm new with MQL4 and I have this indicator which I should embed inside an EA. I know I could use the iCustom function but for several reasons I have to embed the code right inside an EA function. Could you give me some suggestions to avoid modifying whole indicator code? Also some simple explanation would be nice. I would also be interested in paid private lessons from very experienced MQL4 developers if any of you offer this kind of service.  I also did the indicator conversion using OnCalculate(), do you think this is correct?
  1. You can embed an indicator in an EA's final executable code, while still using iCustom, by making use of resources.
  2. Also, stop using the old MQL4 event handlers (init, start, etc.) and use the updated MQL4+ ones (OnInit, OnCalculate, etc.), and do not mix them.
  3. Always make use of "#property strict" for better code, and always fix all compiler warnings too. Don't ignore them.
Resources - MQL4 programs - MQL4 Reference
Resources - MQL4 programs - MQL4 Reference
  • docs.mql4.com
Resources - MQL4 programs - MQL4 Reference
 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. 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)

  3. You have several reasons, but do not state them.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem
              The XY Problem

  4. Just embed the other indicator(s) inside your indicator/EA. Add the CI(s) to your code as a resource.
              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)

I apologize for posting in the wrong section.

So if I use the iCustom function and I insert the custom indicator as a resource will it be part of the executable file?

What is the right syntax to insert a custom indicator as a resource?

 
Marco De Angelis #:

I apologize for posting in the wrong section.

So if I use the iCustom function and I insert the custom indicator as a resource will it be part of the executable file?

What is the right syntax to insert a custom indicator as a resource?

  1. OK
  2. Did you bother to read the links provided about resources?
  3. See those blue, underlined, words? They are links. You click on them to see more information.
 
William Roeder #:
  1. OK
  2. Did you bother to read the links provided about resources?
  3. See those blue, underlined, words? They are links. You click on them to see more information.

Thanks for your reply William, sorry for trivial questions.


Thank you too Fernando Carreiro.

Fernando Carreiro
Fernando Carreiro
  • 2023.03.09
  • www.mql5.com
Trader's profile