Implement a Indicator in a EA?

 

Hey Guys,

I know how to get specific data from a indicator in a EA. For example I use iMA to get the current data for the moving average.
But I also want to visualize those data for the user.

So my question is, how can I implement the moving average so that the moving average is appering on the chart?
Also I want to visualize the Supertrend Indicator. Has anyone a idea? - Because in the EA there is no OnStart()-methode or OnCalculate()-methode...

My code for and moving average Indicator:

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 C'240,192,192'
#property indicator_color2 C'192,240,192'
#property indicator_color3 clrRed
#property indicator_color4 clrBlue
#property indicator_width1 5
#property indicator_width2 5
#property indicator_width3 2
#property indicator_width4 2

extern int                  MA1Period=21;
extern ENUM_MA_METHOD       MA1Method=MODE_EMA;
extern ENUM_APPLIED_PRICE   MA1Price =PRICE_CLOSE;
extern int                  MA2Period=50;
extern ENUM_MA_METHOD       MA2Method=MODE_SMA;
extern ENUM_APPLIED_PRICE   MA2Price =PRICE_CLOSE;


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


int OnInit()
{
   SetIndexBuffer(0,buffer0); 
   SetIndexBuffer(1,buffer1); 
   SetIndexBuffer(2,buffer2);
   SetIndexBuffer(3,buffer3);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   return(INIT_SUCCEEDED);
}

int start()
{
   int counted_bars=IndicatorCounted();
   int limit,i;
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;

   for(i=limit-1; i>=0; i--)
   {
      buffer0[i] = iMA(NULL,0,MA1Period,0,MA1Method,MA1Price,i);
      buffer1[i] = iMA(NULL,0,MA2Period,0,MA2Method,MA2Price,i);
      buffer2[i] = buffer0[i];
      buffer3[i] = buffer1[i];
   }
   return(0);
}

Thanks! 

 
Attach the indicator to the chart with the same settings as in the EA
 
How can I attach an indicator in a EA?
 
FrazeColder:
How can I attach an indicator in a EA?
You can't. Attach it to the chart.
 

Hi, I am new to mql myself but as far as I know you can not use indicators on EA's...You will need to use the icustom function if you want to use your indicator data for your EA. Look here https://docs.mql4.com/indicators/icustom

This will only use the data from the indicator, if you want to see indicators on the chart in real time then you will need to attach the indicator on a separate chart. When you backrest the EA on strategy tester and then open the chart then the indicators will show up on the chart, well does for me but I am just using a simple moving average. 

If you are struggling with icustom (I was) then Look in code base for similar code or this might help you understand the icustom function https://www.mql5.com/en/forum/138577/page3#627521 

 
alexlearnmql:

This will only use the data from the indicator, if you want to see indicators on the chart in real time then you will need to attach the indicator on a separate chart. When you backrest the EA on strategy tester and then open the chart then the indicators will show up on the chart,


You do not need to attach the indicator to a separate chart, you can attach it to the same chart that has the EA running.

if you are using strategy tester visual mode for an EA named "MyEA"

If you load indicators onto a chart and save the template as "MyEA" (or whatever the EA is named)

Whenever you test that EA in visual mode, the template will be loaded and you will see the indicators on the chart. Note that the settings for the indicator will be the same as when the template was saved. So if you save the template with eg. an MA with the period set to 20 , changing the settings of the EA to use an MA period of 10 will not change the settings for the indicator that is being displayed.

Reason: