Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create multiple watermarks on your chart as detailed here: https://robottradingforex.com/watermark-mql4/
//+------------------------------------------------------------------+
//| 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment