Create Your Own Watermark In Charts

A watermark is text you can add to the background of your chart to help annotate any piece of information you deem necessary for the chart’s background. Some useful purposes of a watermark on a chart could be to annotate: the period, or symbol, or current time of the chart.

By using a watermark and controlling its position on your chart it can easily display this essential information without being too obtrusive.

For example, here is a watermark I created to help display the time and symbol of the chart:

Watermark on MT4 Chart
Watermark on MQL4 chart displays time period and symbol

How can you display a watermark on your chart?

There are several functions needed to display a watermark on your chart. You initially start with the ObjectCreate method, and then apply additional properties to that object. Much of what you need can be found from the [example]({{% relref “redirect/obj_label-mql4.md” %}}) on OBJ_LABEL on MetaTrader’s website. If you certainly need more detail than the code provided below be sure to check it out.

To code your watermark on your charts you need to determine what information you would like to display.

For the purposes of this example, I will create a watermark that is static – it will not change as price changes. Therefore, because of its nature I will create a function and then call this function in the onInit() method.

Watermark function

Here is the function in its entirety, for you to use it alongside your existing code you would need to extract the function createWatermark() and then call it within your onInit() method. Lastly, you would need to create the three external variables height_pixels, width_pixels, and font_size.

The reason for those three variables being external is that it makes it easier for you to change the values from the chart directly by right clicking and then selecting Expert Advisors > Properties > Inputs.

Therefore, the following code places a watermark feature of the chart’s time and symbol and places it in the centre of the chart:

//+------------------------------------------------------------------+
//| watermark-charts.mq4 |
//| Copyright 2020, Ryan Sheehy. |
//| https://robottradingforex.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Ryan Sheehy"
#property link "https://robottradingforex.com/watermark-mql4/"
#property version "1.00"
#property strict
extern int height_pixels=640;
extern int width_pixels=1280;
extern int font_size=80;
int OnInit() {
createWatermark();
return(INIT_SUCCEEDED);
}
bool createWatermark() {
// unique name given to the object label
string name = "watermark";
// check if we already have it on the chart, if so delete it
ObjectDelete( 0, name );
string txt = "";
// change the periods to a string label
if (Period() == PERIOD_MN1) txt = "MONTH";
if (Period() == PERIOD_W1) txt = "WEEK";
if (Period() == PERIOD_D1) txt = "DAY";
if (Period() == PERIOD_H4) txt = "4 HOUR";
if (Period() == PERIOD_H1) txt = "1 HOUR";
if (Period() == PERIOD_M30) txt = "30 MIN";
if (Period() == PERIOD_M15) txt = "15 MIN";
if (Period() == PERIOD_M5) txt = "5 MIN";
if (Period() == PERIOD_M1) txt = "1 MIN";
// preparing for error
ResetLastError();
// create the object shell and report any errors
if (!ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0)) {
Print(__FUNCTION__, ": failed to create text label! Error code = ",GetLastError());
return(false);
}
// add the symbol to the text
txt = txt + " " + Symbol();
// set the text
ObjectSetString(0, name, OBJPROP_TEXT, txt);
// set the corner reference of X & Y axis
ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
// set the anchor reference of the label
ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_CENTER);
// set the X & Y co-ordinates
ObjectSetInteger(0, name, OBJPROP_XDISTANCE, width_pixels/2);
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, height_pixels/2);
// set the font
ObjectSetString(0, name, OBJPROP_FONT, "Arial");
// set the size of the font
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, font_size);
// set the color of the font
ObjectSetInteger(0, name, OBJPROP_COLOR, clrLightGray);
// set the object into the background, behind price
ObjectSetInteger(0, name, OBJPROP_BACK, true);
// turn off the ability to select the object
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, name, OBJPROP_SELECTED, false);
// make it hidden from the object list
ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
return(true);
}

What font can you use?

MetaTrader only uses the fonts available on your Windows system. To see what fonts are available navigate to Control Panel / Appearance / Fonts.

Here you will see a list of font’s available and their names. Simply copy the name of the font you want to use and in the code above where you see this line:

// set the font
ObjectSetString(0, name, OBJPROP_FONT, "Arial");

Change the string "Arial" to whatever you would prefer, such as "Courier New".

Can You Have More Than 1 Watermark on a Chart?

You may want to display information vertically, or you may want to display a watermark in the upper-right corner and lower-left corner. If you’d like to display more than one piece of data on your chart you can do so with some slight modifications to the code.

If we modify the main inputs of our function and transform them as parameters then our chart can display an array of multiple watermarks:

//+------------------------------------------------------------------+
//| watermark-charts.mq4 |
//| Copyright 2020, Ryan Sheehy. |
//| https://robottradingforex.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Ryan Sheehy"
#property link "https://robottradingforex.com/watermark-mql4/"
#property version "1.00"
#property strict
extern int height_pixels_1=320;
extern int width_pixels_1=640;
extern int font_size_1=80;
extern int height_pixels_2=120;
extern int width_pixels_2=640;
extern int font_size_2=80;
int OnInit() {
ObjectsDeleteAll(0);
string txt = "";
// change the periods to a string label
if (Period() == PERIOD_MN1) txt = "MONTH";
if (Period() == PERIOD_W1) txt = "WEEK";
if (Period() == PERIOD_D1) txt = "DAY";
if (Period() == PERIOD_H4) txt = "4 HOUR";
if (Period() == PERIOD_H1) txt = "1 HOUR";
if (Period() == PERIOD_M30) txt = "30 MIN";
if (Period() == PERIOD_M15) txt = "15 MIN";
if (Period() == PERIOD_M5) txt = "5 MIN";
if (Period() == PERIOD_M1) txt = "1 MIN";
createWatermark(width_pixels_1, height_pixels_1, font_size_1, txt, "watermark1");
createWatermark(width_pixels_2, height_pixels_2, font_size_2, Symbol(), "watermark2");
return(INIT_SUCCEEDED);
}
/*
* @param width_pixels - width where watermark starts from anchor
* @param height_pixels - height where watermark starts from anchor
* @param font_size - size of font
* @param txt - text of the watermark
* @param name - unique identifier of the object
*/
bool createWatermark(int width_pixels, int height_pixels, int font_size, string txt, string name) {
// check if we already have it on the chart, if so delete it
ObjectDelete( 0, name );
// preparing for error
ResetLastError();
// create the object shell and report any errors
if (!ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0)) {
Print(__FUNCTION__, ": failed to create text label! Error code = ",GetLastError());
return(false);
}
// set the text
ObjectSetString(0, name, OBJPROP_TEXT, txt);
// set the corner reference of X & Y axis
ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
// set the anchor reference of the label
ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_CENTER);
// set the X & Y co-ordinates
ObjectSetInteger(0, name, OBJPROP_XDISTANCE, width_pixels);
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, height_pixels);
// set the font
ObjectSetString(0, name, OBJPROP_FONT, "Arial");
// set the size of the font
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, font_size);
// set the color of the font
ObjectSetInteger(0, name, OBJPROP_COLOR, clrLightGray);
// set the object into the background, behind price
ObjectSetInteger(0, name, OBJPROP_BACK, true);
// turn off the ability to select the object
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, name, OBJPROP_SELECTED, false);
// make it hidden from the object list
ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
return(true);
}

Here is the result:

Multiple watermarks
Create multiple vertical watermarks on your chart” alt=”1 hour EURUSD chart with multiple watermarks

By changing your code a little you can now call the watermark as many times as you wish:

createWatermark(width_pixels, height_pixels, font_size, txt, name);

Conclusion

In this post you discovered how to create a watermark feature on your charts to make it easier to read essential information about the chart. This feature is very handy when you are taking screenshots of your chart and are reviewing the performance of your trading system.

Depending on how you want your watermark to appear the free code above can help you position your watermark according to the dimensions you input into the Expert Advisor properties.

You also can add multiple watermarks if needed to your code and position them individually according to the parameter values you set.

A watermark is a great way to define the core information about your chart.

Leave a Reply