Русский 中文 Español Deutsch 日本語 Português
Alert and Comment for External Indicators

Alert and Comment for External Indicators

MetaTrader 4Examples | 11 May 2009, 13:33
8 084 1
Vladimir
Vladimir

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 Ltd.
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
Using Neural Networks In MetaTrader Using Neural Networks In MetaTrader
This article shows you how to easily use Neural Networks in your MQL4 code taking advantage of best freely available artificial neural network library (FANN) employing multiple neural networks in your code.
Channels. Advanced Models. Wolfe Waves Channels. Advanced Models. Wolfe Waves
The article describes rules of marking patterns of Wolfe Waves. You will find here details of constructing and rules of accurate marking, which help to find correct formations of waves quickly and correctly.
On the Long Way to Be a Successful Trader - The Two Very First Steps On the Long Way to Be a Successful Trader - The Two Very First Steps
The main point of this article is to show a practical way to implement an effective MM. This can be achieved only by using a certain kind of strategies that we need to identify and describe first. In the following we’ll cover the basic concepts of how to build such a strategy and we’ll point out the common mistakes which always end up in draining a trader’s account.
Effective Averaging Algorithms with Minimal Lag: Use in Indicators and Expert Advisors Effective Averaging Algorithms with Minimal Lag: Use in Indicators and Expert Advisors
The article describes custom averaging functions of higher quality developed by the author: JJMASeries(), JurXSeries(), JLiteSeries(), ParMASeries(), LRMASeries(), T3Series() and MASeries(). The author considers the hot substitution of these functions in indicators using the call of the SmoothXSeries() function.