Chart object

The chart object OBJ_CHART allows you to create thumbnails of other charts inside the chart for other instruments and timeframes. Chart objects are included in the general chart list, which we obtained programmatically using the ChartFirst and ChartNext functions. As mentioned in the section on Checking the main window status, the special chart property CHART_IS_OBJECT allows you to find out by identifier whether it is a full-fledged window or a chart object. In the latter case, calling ChartGetInteger(id, CHART_IS_OBJECT) will return true.

The chart object has a set of properties specific only to it.

Identifier

Description

Type

OBJPROP_CHART_ID

Chart ID (r/o)

long

OBJPROP_PERIOD

Chart Period

ENUM_TIMEFRAMES

OBJPROP_DATE_SCALE

Show the time scale

bool

OBJPROP_PRICE_SCALE

Show the price scale

bool

OBJPROP_CHART_SCALE

Scale (value in the range 0 - 5)

int

OBJPROP_SYMBOL

Symbol

string

The identifier obtained through the OBJPROP_CHART_ID property allows you to manage the object like a regular chart using the functions from the chapter Working with charts. However, there are some limitations:

By default, all properties (except OBJPROP_CHART_ID) are equal to the corresponding properties of the current window.

The demonstration of chart objects is implemented as a bufferless indicator ObjectChart.mq5. It creates a subwindow with two chart objects for the same symbol as the current chart but with adjacent timeframes above and below the current one.

Objects snap to the upper right corner of the subwindow and have the same predefined sizes:

#define SUBCHART_HEIGHT 150
#define SUBCHART_WIDTH  200

Of course, the height of the subwindow must match the height of the objects, until we can respond adaptively to resize events.

#property indicator_separate_window
#property indicator_height SUBCHART_HEIGHT
#property indicator_buffers 0
#property indicator_plots   0

One mini-chart is configured in the SetupSubChart function, which takes the number of the object, its dimensions, and the required timeframe as inputs. The result of SetupSubChart is the identifier of the chart object, which we just output into the log for reference.

void OnInit()
{
   Print(SetupSubChart(0SUBCHART_WIDTHSUBCHART_HEIGHTPeriodUp(_Period)));
   Print(SetupSubChart(1SUBCHART_WIDTHSUBCHART_HEIGHTPeriodDown(_Period)));
}

Macros PeriodUp and PeriodDown use the helper function PeriodRelative.

#define PeriodUp(PPeriodRelative(P, +1)
#define PeriodDown(PPeriodRelative(P, -1)
   
ENUM_TIMEFRAMES PeriodRelative(const ENUM_TIMEFRAMES tfconst int step)
{
   static const ENUM_TIMEFRAMES stdtfs[] =
   {
      PERIOD_M1,  // =1 (1)
      PERIOD_M2,  // =2 (2)
      ...
      PERIOD_W1,  // =32769 (8001)
      PERIOD_MN1// =49153 (C001)
   };
   const int x = ArrayBsearch(stdtfstf == PERIOD_CURRENT ? _Period : tf);
   const int needle = x + step;
   if(needle >= 0 && needle < ArraySize(stdtfs))
   {
      return stdtfs[needle];
   }
   return tf;
}

Here is the main working function SetupSubChart.

long SetupSubChart(const int nconst int dxconst int dy,
   ENUM_TIMEFRAMES tf = PERIOD_CURRENTconst string symbol = NULL)
{
   // create an object
   const string name = Prefix + "Chart-"
      + (symbol == NULL ? _Symbol : symbol) + PeriodToString(tf);
   ObjectCreate(0nameOBJ_CHARTChartWindowFind(), 00);
   
   // anchor to the top right corner of the subwindow
   ObjectSetInteger(0nameOBJPROP_CORNERCORNER_RIGHT_UPPER);
   // position and size
   ObjectSetInteger(0nameOBJPROP_XSIZEdx);
   ObjectSetInteger(0nameOBJPROP_YSIZEdy);
   ObjectSetInteger(0nameOBJPROP_XDISTANCE, (n + 1) * dx);
   ObjectSetInteger(0nameOBJPROP_YDISTANCE0);
   
   // specific chart settings
   if(symbol != NULL)
   {
      ObjectSetString(0nameOBJPROP_SYMBOLsymbol);
   }
   
   if(tf != PERIOD_CURRENT)
   {
      ObjectSetInteger(0nameOBJPROP_PERIODtf);
   }
   // disable the display of lines
   ObjectSetInteger(0nameOBJPROP_DATE_SCALEfalse);
   ObjectSetInteger(0nameOBJPROP_PRICE_SCALEfalse);
   // add the MA indicator to the object by its id just for demo
   const long id = ObjectGetInteger(0nameOBJPROP_CHART_ID);
   ChartIndicatorAdd(id0iMA(NULLtf100MODE_EMAPRICE_CLOSE));
   return id;
}

For a chart object, the anchor point is always fixed in the upper left corner of the object, so when anchoring to the right corner of the window, you need to add the width of the object (this is done by +1 in the expression(n+1)*dx for OBJPROP_XDISTANCE).

The following screenshot shows the result of the indicator on the XAUUSD,H1 chart.

Two chart objects in the indicator subwindow

Two chart objects in the indicator subwindow

As we can see, the mini-charts display the M30 and H2 timeframes.

It is important to note that you can add indicators to chart objects and apply tpl templates, including those with Expert Advisors. However, you cannot create objects inside chart objects.

When the chart object is hidden due to disabled visualization on the current timeframe or on all timeframes, the CHART_WINDOW_IS_VISIBLE property for the internal chart still returns true.