Download MetaTrader 5

Alert and Comment for External Indicators

11 May 2009, 13:33
Vladimir
1
2 411

Introduction

Practice shows that developers of indicators not always include the possibility of displaying alerts or comments in an indicator code which results in requests to add the possibility of showing signals in an indicator.

MQL4 Possibilities for Showing Information of Indicators

Possibilities of the MQL4 programming language do not include the extended usage of indicator buffers, for example extending them by adding comment or alert showing, which could be an advantage simplifying the creation of a program code. On the other hand, MQL4 allows reading the location of any objects and getting their full characteristics.

In this article I would like to show the method of getting data on a signal of an indicator attached to a chart and show its comment, alert or creating a global variable for further use for informational purposes, for example for passing it to an Expert Advisor.

All marks drawn on a chart use Wingdings and arrow codes. That is why it is not difficult to get information about a mark drawn on a chart. By the way, if an indicator author includes quite an informative description of the OBJ_ARROW object, a user can easily identify a signal. An example is the description of marks given by creators of classic Japanese candlesticks. In the standard performance of his indicator a user needs only to put the mouse cursor over the mark for getting a pop-up help with a corresponding text.



Standard showing of a signal


In MQL4 you can use some built-in options for more informative way of data presentation.

Among language commands the function

double ObjectGet (string name, int prop_id)

returns the value of a specified object property. To get an error data call the GetLastError() function.
See also ObjectSet().

Parameters:
name - object name.
prop_id - Identifier of object properties. Can be any value from the list of object properties.

Identifiers of object properties are used in functions ObjectGet() and ObjectSet(). Can be any of below values:

Constant Value Type Description
OBJPROP_TIME10datetimeReceives/sets the first coordinate of time
OBJPROP_PRICE11doubleReceives/sets the first coordinate of price
OBJPROP_TIME22datetimeReceives/sets the second coordinate of time
OBJPROP_PRICE23doubleReceives/sets the second coordinate of price
OBJPROP_TIME34datetimeReceives/sets the third coordinate of time
OBJPROP_PRICE35doubleReceives/sets the third coordinate of price
OBJPROP_COLOR6colorReceives/sets the color of an object
OBJPROP_STYLE7intReceives/sets the line style of an object
OBJPROP_WIDTH8intReceives/sets the line width of an object
OBJPROP_BACK9boolReceives/sets the background representation flag of an object
OBJPROP_RAY10boolReceives/sets the flag of ray properties for objects of OBJ_TREND type and the like
OBJPROP_ELLIPSE11boolReceives/sets the flag of displaying the full ellipse for OBJ_FIBOARC
OBJPROP_SCALE12doubleReceives/sets the value of an object scale
OBJPROP_ANGLE13doubleReceives/sets the angle value in degrees for OBJ_TRENDBYANGLE

OBJPROP_ARROWCODE

14

int

Receives/sets the arrow code of OBJ_ARROW. Can be one of wingdings symbols or one of predefined arrow codes

OBJPROP_TIMEFRAMES15intReceives/sets object representation on various periods. Can be one of or combination of several object visibility constants
OBJPROP_DEVIATION16doubleReceives/sets deviation value for OBJ_STDDEVCHANNEL
OBJPROP_FONTSIZE100intReceives/sets font size for OBJ_TEXT and OBJ_LABEL
OBJPROP_CORNER101intReceives/sets number of binding angle for OBJ_LABEL. Gets values 0-3
OBJPROP_XDISTANCE102intReceives/sets distance of X coordinate in pixels relative to the binding angle for OBJ_LABEL
OBJPROP_YDISTANCE103intReceives/sets distance of Y coordinate in pixels relative to the binding angle for OBJ_LABEL
OBJPROP_FIBOLEVELS200intReceives/sets number of levels of the Fibonacci object. Can be from 1 to 32
OBJPROP_LEVELCOLOR201colorReceives/sets line color of the object level
OBJPROP_LEVELSTYLE202intReceives/sets line style of the object level
OBJPROP_LEVELWIDTH203intReceives/sets the line width of the object level
OBJPROP_FIRSTLEVEL+n210+nintReceives/sets objet level number, where n is index of installed/received level. Can be from 0 to 31

That is why let's use parameter OBJPROP_ARROWCODE in order to find a necessary object and we will receive the list of all objects. After that let's select necessary objects in the array and show the information on them in the required form.

Then let's create a code for receiving information about names of candlesticks for indicator “japan”. We get the following code:

int    obj_total=ObjectsTotal(); // Define the total number of objects on a chart
  for(int i=0;i<obj_total;i++) // Loop of going through all objects with the purpose of finding necessary ones
    {
            if (ObjectGet(ObjectName(i),OBJPROP_ARROWCODE) // reading the code

First we should define how we will deal with Wingdings and correspondingly set up parameters in the program code. In this example we will handle the part of Wingdings table that starts with 100.

if (ObjectGet(ObjectName(i),OBJPROP_ARROWCODE)>100&&
                     ObjectGet(ObjectName(i),OBJPROP_TIME1)>Time[ExtBars])

The ExtBars variable is used for defining the number of last bars for finding object drawn on a chart.

Now we will show Alerts and (or) Comments.

       if (ExtAlert==true){
                        Alert(Symbol(),Period()," ",
                              TimeYear(ObjectGet(ObjectName(i),OBJPROP_TIME1)),".",
                              TimeDay(ObjectGet(ObjectName(i),OBJPROP_TIME1)),".",
                              TimeMonth(ObjectGet(ObjectName(i),OBJPROP_TIME1)),"; ",
                              TimeHour(ObjectGet(ObjectName(i),OBJPROP_TIME1)),":",
                              TimeMinute(ObjectGet(ObjectName(i),OBJPROP_TIME1))," ",
                              ObjectName(i));}
       if (ExtComment==true){
                        Comment("\n","\n","\n",
                              Symbol(),Period()," ",
                              TimeYear(ObjectGet(ObjectName(i),OBJPROP_TIME1)),".",
                              TimeDay(ObjectGet(ObjectName(i),OBJPROP_TIME1)),".",
                              TimeMonth(ObjectGet(ObjectName(i),OBJPROP_TIME1)),"; ",
                               TimeHour(ObjectGet(ObjectName(i),OBJPROP_TIME1)),":",
                                TimeMinute(ObjectGet(ObjectName(i),OBJPROP_TIME1))," ",
                                ObjectName(i));}

In the comment line

("\n","\n","\n",

is used for showing information onto the third line of a display.

If necessary, you can include sending the information to global variables for using them in an Expert Advisor. Thus eliminating the breach of copyright of indicator authors we get the possibility to use information on alerts for our own purposes and do not have to ask authors to modify their codes.

Using the described approach we get the following result:


Operating result of the indicator showing external information


In the analogous way the code works with the indicator ExCandles2. This is the second indicator published in MQL4 base; it was tried together with the published code.

Conclusion

Here is the source code of the indicator for displaying information about appearing signals of indicators:

//+------------------------------------------------------------------+
//|                                                  AlertSignal.mq4 |
//|                                  Copyright © 2009, WWW.FIBOOK.RU |
//|                                                 http://fibook.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, FIBOOK.RU"
#property link      "http://fibook.ru"

#property indicator_chart_window
extern int ExtBars=10;        // Number of last bars for calculation
extern int ExtArrow=100;      // Code number in the "wingdings" table for the count start
extern bool ExtAlert=true;    // Switch of Alerts displaying
extern bool ExtComment=true;  // Switch of Comments displaying
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if (per == Time[0]) return; // Correct once per a period
      per = Time[0];

   int cod=0;
//----

int    obj_total=ObjectsTotal();
  for(int i=0;i<obj_total;i++)
    {
            if (ObjectGet(ObjectName(i),OBJPROP_ARROWCODE)>ExtArrow&&
                     ObjectGet(ObjectName(i),OBJPROP_TIME1)>Time[ExtBars])
                {
                  if (ExtAlert==true){
                        Alert(Symbol(),Period()," ",
                              TimeYear(ObjectGet(ObjectName(i),OBJPROP_TIME1)),".",
                              TimeDay(ObjectGet(ObjectName(i),OBJPROP_TIME1)),".",
                              TimeMonth(ObjectGet(ObjectName(i),OBJPROP_TIME1)),"; ",
                              TimeHour(ObjectGet(ObjectName(i),OBJPROP_TIME1)),":",
                              TimeMinute(ObjectGet(ObjectName(i),OBJPROP_TIME1))," ",
                              ObjectName(i));}
                  if (ExtComment==true){
                        Comment("\n","\n","\n",
                              Symbol(),Period()," ",
                              TimeYear(ObjectGet(ObjectName(i),OBJPROP_TIME1)),".",
                              TimeDay(ObjectGet(ObjectName(i),OBJPROP_TIME1)),".",
                              TimeMonth(ObjectGet(ObjectName(i),OBJPROP_TIME1)),"; ",
                               TimeHour(ObjectGet(ObjectName(i),OBJPROP_TIME1)),":",
                                TimeMinute(ObjectGet(ObjectName(i),OBJPROP_TIME1))," ",
                                ObjectName(i));}
                }
     }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/1568

Attached files |
alertsignal.mq4 (6.31 KB)
Last comments | Go to discussion (1)
ginogee
ginogee | 20 Jul 2016 at 13:27
Hi

Thank you for your excellent article on Alert and Comment for External Indicators.

I have made use of this code many times and with great reliability until  recently.

I found that the code i was using did not use graphical objects for the arrow signals on charts, but instead used the DRAW ARROW command as part of a line index style attached to a buffer.

The arrow on my charts does not appear in the list of objects so i am at a loss as to how to make use of your alert code.

is there a way around this problem.

Apologies in advance if i have not explained myself clearly, I am not a coder but have managed to fumble through on other peoples knowledge so far.

Many thanks

Ginogee
Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

False trigger protection for Trading Robot False trigger protection for Trading Robot

Profitability of trading systems is defined not only by logic and precision of analyzing the financial instrument dynamics, but also by the quality of the performance algorithm of this logic. False trigger is typical for low quality performance of the main logic of a trading robot. Ways of solving the specified problem are considered in this article.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.