Create separate_window indicator that displays numbers

 


I've been trying to figure out a way to create an indicator that would be a separate_window at the bottom of chart that would simply display numbers on various bars.  I don't see a way to to do it with buffers and the only object that I can find that might work is the OBJ_TEXT.  The problem is that object requires time/price to place it and the values in a separate_window indicator are considered "levels" so that didn't work for me.  Is there any way to do something like the image above with built-in mql objects/functions?
 
Daniel Lewis:

I've been trying to figure out a way to create an indicator that would be a separate_window at the bottom of chart that would simply display numbers on various bars.  I don't see a way to to do it with buffers and the only object that I can find that might work is the OBJ_TEXT.  The problem is that object requires time/price to place it and the values in a separate_window indicator are considered "levels" so that didn't work for me.  Is there any way to do something like the image above with built-in mql objects/functions?

There's multiple ways of doing it depending on how much control you need over the formatting of the display, but the following example might be sufficient for your needs. Shows the zero-based index of each bar, modulo 10.

What it's doing is creating a sub-window with a fixed value range from zero to 1, and then telling MT4 to display the number labels in the middle of that window (approximately) by telling it to put them at value 0.5.

#property strict
#property indicator_separate_window
#property indicator_maximum 1
#property indicator_minimum 0

#define MY_SHORTNAME    "Example"
#define LABEL_PREFIX    "Example-"

int OnInit()
{
   IndicatorShortName(MY_SHORTNAME); // In order to work out where the subwindow is...
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0, LABEL_PREFIX);
}

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 sub_window_for_indicator = ChartWindowFind(0, MY_SHORTNAME);
   if (sub_window_for_indicator <= 0) return rates_total; // Can't be right; must be because MT4 is initialising

   for (int i = prev_calculated + 1; i <= rates_total; i++) {
      int idx = rates_total - i;
 
      string strName = LABEL_PREFIX + IntegerToString(idx);
      ObjectCreate(0, strName, OBJ_TEXT, sub_window_for_indicator, Time[idx], 0.5);
      ObjectSetString(0, strName, OBJPROP_TEXT, IntegerToString(idx % 10));
      ObjectSetInteger(0, strName, OBJPROP_COLOR, clrRed);
   }  

   return(rates_total);
}
 
JC:

There's multiple ways of doing it depending on how much control you need over the formatting of the display, but the following example might be sufficient for your needs. Shows the zero-based index of each bar, modulo 10.

What it's doing is creating a sub-window with a fixed value range from zero to 1, and then telling MT4 to display the number labels in the middle of that window (approximately) by telling it to put them at value 0.5.

Thanks for the reply.  The funny thing is that is exactly what i tried but it did not work.  I'll go through your code and see what the difference is as your code works.  Thanks again!


*** Edit: My code was pretty much same, I had more ObjectSet stuff to control a few more things.  For some reason my ObjectSetString that set the font was the issue - font name was set correctly so not sure why that didn't work ***

 
Daniel Lewis:

Thanks for the reply.  The funny thing is that is exactly what i tried but it did not work.  I'll go through your code and see what the difference is as your code works.  Thanks again!


*** Edit: My code was pretty much same, I had more ObjectSet stuff to control a few more things.  For some reason my ObjectSetString that set the font was the issue - font name was set correctly so not sure why that didn't work ***

If you want someone to help with your code you should post it.
Reason: