Clock – library MetaTrader 5

The library (Clock.mqh) allows creating analog clocks as graphical resources.

Features

The use of the library is shown in the following source code:

// MQL4&5-code
// An example of a cross-platform Expert Advisor, which creates an analog clock on the chart

#property strict

#include <fxsaber\Clock.mqh> // 

const string ObjName = __FILE__;            // The name of the Bitmap object to display the clock
const string ResourceName = "::" + ObjName; // The name of the resource where the clock will be formed

// Creating the clock from the settings of the corresponding INI file
CLOCK Clock("Clocks\\Clock01\\Clock01.INI", ResourceName);

void OnINIt()
{
  // Creating a graphical object for displaying the clock
  ObjectCreate(0, ObjName, OBJ_BITMAP_LABEL, 0, 0, 0);

  // Specifying the name of the resource where the clock is located
  ObjectSetString(0, ObjName, OBJPROP_BMPFILE, 0, ResourceName);

  // Adding the possibility to select the object to drag it with the mouse
  ObjectSetInteger(0, ObjName, OBJPROP_SELECTABLE, true);

  // Enabling the second timer to update the clock
  EventSetTimer(1);
}

void OnTimer()
{
  // Setting the current time on the clock
  Clock.SetTime(TimeLocal());

  // Refreshing the chart to make changes visible
  ChartRedraw();
}

The following clock appears on the chart after refresh:

Indicator

The attached cross-platform Clock.mq5 indicator enables the immediate “out of the box” use of the clock.

The indicator implements an interactive change of skins: clock versions (unpack the ZIP archive) are switched using the UP/DOWN keys on the keyboard.

Features

  • Raster (not vector) clock;
  • Hour hands move without smoothing;
  • The library can be used in MetaTrader 4/5.

How the clock is formed

The originals of clock are taken from the web by searching for flash clock (swf files are available in the ZIP), which are convenient because they contain required graphical representations of clock face, hands, etc. You can use any other sources. For example, you can find ready vector files or draw them yourself.

Alternative:  Range Oscillator + Bands (Smoothed) - indicator MetaTrader 5

INI files (see examples) set the parameters of clock formation layer by layer. The below settings can be edited in any text editor

ImageName = ClockFace.bmp
BoundX = 0 // the X coordinate of the zero point inside the bmp image (layer)
BoundY = 0 // the Y coordinate of the zero point inside the bmp image (layer)
X = 0      // the X coordinate of the zero point inside the image being formed of layers
Y = 0      // the Y coordinate of the zero point inside the image being formed of layers
Rotate = 0 // Speed of rotation
A = 100%   // The value of the Alpha channel used
R = 100%   // The value of the Red channel used
G = 100%   // The value of the Green channel used
B = 100%   // The value of the Blue channel used

The lower the position of a layer description in the list, the higher it is than previously described layers. There is no special limitation on the number of layers.

This image construction method allows using the library for forming any static/dynamic objects, not only the clock. To provide the clock dynamics, we only needed to set rotation. However, other types of movement (such as shift, scaling, etc.) can be added to the code in just a few lines. The attached source code provides an example of how to implement this approach when describing images.

As for the clock, the layer-by-layer operation allowed us to implement shadows of hands and illumination (as is done in flash clocks).

If you want to make the dial transparent, you should set the Alpha channel in the corresponding INI file less than 100%:

Alternative:  DSSBressert_HTF - indicator MetaTrader 5

Alpha channel of the dial 90% (A = 90)

At the time of publication, there were no analog clocks in the Market. I’ve found this variant in the Code Base.

The new feature of this library is LAYER::Rotate used for rotation. Probably, this function will lead to the creation of third-party solutions based on dynamic graphical resources similar to the clock.


📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks