How to plot indicators on chart using MQL4?


Can we plot the indicator using an EA, for example if I have an EA trading 5-MA 15 MA cross, can I plot those indicator lines in EA- I badly need this way for some testing purposes

Please share some example or a tutorial, I tried for days but couldnt do it in MT4; Thanks


You can obtain the values of 5-MA (for example) using codes like these:

int P = 5; // Period of Moving Average
int S = 0; // Shift of Moving Average
int B = 1; // Bar that you're interested in

double MA5 = iMA(_Symbol,_Period,P,S,MODE_SMA,PRICE_CLOSE,B);

There is no need for your EA to plot those lines on the chart, since it should be even more straightforward to just drop the moving average indicator on the chart.

Tk u, I do have an EA and an Indicator developed however when I back test I see the Indicator crossover and the EA open trades differing and the values when printed differs as well, so I’m trying to draw the indicator thru the EA to visualize the buy and sell, is there a way that we can do it? like using dll or some other way

You can always draw trendlines without ray, from within your EA to depict the exact point that the crossover happened:

            ObjectCreate(ChartID(),<Name1>,OBJ_TREND,0,T1,P1,T2,P2); // Draws '/'
            ObjectCreate(ChartID(),<Name2>,OBJ_TREND,0,T1,P3,T2,P4); // draws '\'

Just have to generate appropriate names dynamically.

But I think the right way, given that you’ve already printed the values from the EA and they differ from what the indicators provided, is to check through your code for all parameters used to calculate the moving averages, rather than to plot out the lines via your EA - it serves no real purpose.

Thank you, I will try this code to draw the lines, and I did check the code, its all exactly the same, I even tried the iMA function- as below (Please check the code and kindly guide me if there is something wrong… because the difference between both Indi and EA MAs are varies and sometimes really huge upto 400 points difference… ex if Indi MA5 value is 1.32300 the EA MA5 prints 1.32700 I’m totally lost and have no clue why… I tried with different forex companies demo accounts, still the same issue


//INIT Function
   int shift_begin=int(MathSqrt(period)+period+1);



//onCal function

   int limit=rates_total-prev_calculated;
   else  limit++;

   for(i=0; i<limit && !IsStopped(); i++)
                MA5[i]  =       iMA(Symbol(), PERIOD_CURRENT, 5,0, 3, 0,Shift);
   for(i=0; i<limit && !IsStopped(); i++)
                MA10[i] =       iMAOnArray(MA5,0,(int)MathRound(period),0,3,i);

//EA Code

//Calc MA Function

   double MA5[],MA10[];



   for(int i=max-1; i>=0; i--)
                MA5[i]  =       iMA(Symbol(), PERIOD_CURRENT, 5, 0, 3, 0,Shift);
   for(int i=max-1; i>=0; i--)
                MA10[i] =       NormalizeDouble(iMAOnArray(MA5,0,(int)MathRound(period),0,3,i),_Digits);

Since you have the indicator, you can just use iCustom in your EA to retrieve the values calculated by it, as follows:

  double MA5 = iCustom(Symbol(),PERIOD_CURRENT,<Indicator Name>,<Indicator parameters>,0,Bar);
  double MA10 = iCustom(Symbol(),PERIOD_CURRENT,<Indicator Name>,<Indicator parameters>,1,Bar);

This way, you can just focus on checking that the calculations within your indicator are exactly what you expected.

I have tested, and these lines are able to retrieve the MA5 and MA10 values from your indicator, without any discrepancy.