Displaying value of moving average on chart window

 

Hi I would like to modify the "Moving Averages.mq4" code given in the indicators folder.

Such that in addition to drawing the moving average line, I would like to display the value of the moving average line

on top of each bar.

I saw from the mq4 code that the buffer which contain all these values is from "SetIndexBuffer(0,ExtMapBuffer)".

Any method on how I can display values from this buffer on the chart window, on top of each bar ?

Many thanks.

 
hip280:
I would like to display the value of the moving average line on top of each bar.
So add a text object.
 
hip280:

Hi I would like to modify the "Moving Averages.mq4" code given in the indicators folder.

Such that in addition to drawing the moving average line, I would like to display the value of the moving average line

on top of each bar.

Do you want to do this on every bar in the history or just the latest bar?

Do you realize that you can run your mouse onto the bar in question and the HLOC values will display in the data Window?

It seems to me that you will have to have very few bars on the chart (heavily zoomed in) in order for the text to not conflict with the adjacent text.

 

open the data window and then move the mouse over the chart, it will show you all values of all indicators.

 

Oh yes, I am aware that moving the mouse over the indicator will show the indicator value. But as I want to do this on every bar in the history, I have to move the mouse bar by bar for many bars.

So I want to code it to simply display it on screen. To minimise adjacent texts conflicting, I thought of displaying the value on top of the high of each bar, so each text will have a different y-value.

Hmm...ok I will go look up about using text objects. If anyone can furnish some code example to display the moving average buffer values as text on screen, it will be most helpful.

 

Hi, instead of modifying "Moving Averages.mq4". I decided to create a separate indicator to display the MA values at each bar on the chart window.

Below is what I came up with, but unfortunately it does not work, can anyone tell me where is the error ?

#property indicator_chart_window

int init()
  {
   return(0);
  }

int deinit()
  {
   ObjectsDeleteAll();
   return(0);
  }

int start()
  {
   int counted_bars=IndicatorCounted();
   int i;
   double currentMA ;

   //find index of bar shown at leftmost side of screen
   i = Bars-counted_bars-1;
   
   //work from leftmost bar to rightmost bar 
   while(i>=0)
   {
      //find current MA value, will also be used as y-coordinate
      currentMA = iMA(0,PERIOD_H1,20,0,MODE_SMA,PRICE_TYPICAL,i);
      ObjectCreate("maValue"+i,OBJ_LABEL,0,Time[i],currentMA+0.0005);
      ObjectSetText("maValue"+i,currentMA,8,"Tahoma",Gold);
   i--;
   }
   

   return(0);
  }
//+------
 
Press Ctrl + B and you will see your object list. You must use ObjectCreate() with OBJ_TEXT as parameter not OBJ_LABEL, you also need to add, 2 ObjectSet() function with OBJPROP_TIME1, OBJPROP_PRICE1,
 
#property indicator_chart_window

int init(){
   return(0);
}

int deinit(){
   ObjectsDeleteAll();
   return(0);
}

int start(){
   //find index of bar shown at leftmost side of screen
   int i = Bars-IndicatorCounted()-1;
   if( i > 1000 )
       i = 1000;
   
   //work from leftmost bar to rightmost bar 
   while(i>=0){
      //find current MA value, will also be used as y-coordinate
      double currentMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_TYPICAL,i);
      ObjectCreate( "maValue"+i,OBJ_TEXT,0,Time[i],currentMA+5*Point);
      ObjectSetText("maValue"+i,DoubleToStr(currentMA,Digits),8,"Tahoma",Gold);
      i--;
   }
   
   return(0);
}
 

Thank you everyone for your helpful comments !

dabbler, what you did there is really cool ! Just what I imagined !!

 
hip280:

dabbler, what you did there is really cool ! Just what I imagined !!

I didn't have time this morning to explain why I did what I did.

I limited the number of labels to 1000, a pretty arbitrary number. But if you have 100,000 bars in your chart it could generate more objects than is reasonable.

I changed the offset from the moving average to 5*Point. That is pretty lousy programming, but better than 0.0001 in the sense that mine adapts to JPY pairs. However it doesn't adjust for 4/5 digit brokers so it is still a poor example.

The DoubleToStr call just presents the correct number of digits, rather than whatever value MQL4 decides to use.

Notice that I removed the explicit calls to the H1 bars. You can't call up H1 bar data and then plot it on an M15 chart using the same bar number. You could put some code in the init function to insist on the chart being on an H1 timeframe, if that is what you want.

You still have to zoom the chart quite heavily to prevent the text running into itself.

Thanks primarily to onewithZachy, whose suggested use of OBJ_TEXT was the key to success here :-)