MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Automated Trading and Strategy Testing Forum

Forget Outdated Indicators and Buy Latest Ones in MetaTrader Market
To add comments, please log in or register
How to buy a product in MetaTrader AppStore? Read the article!
Rodrigo Malacarne
Moderator
4617
Malacarne 2013.12.16 15:28

I'm trying to create a simple panel/indicator that retrieves the best ask volume and the best bid volume in real time... However, after reading the MQL5 docs I found the information that OnBookEvent( ) works only for expert advisors, but not for indicators. Even setting OnTimer( ) to 10 miliseconds I'm not getting the information refreshed inside my panel.

In this case I cannot use OnCalculate( ), because the best ask volume and the best bid volume do change even when no trade occurs. Let me show the code I'm using:

#property version       "1.000"
#property description   "SIMPLE PANEL"
//+------------------------------------------------------------------+
//| INCLUDES                                                                       |
//+------------------------------------------------------------------+
#include <Controls\Dialog.mqh>
#include <Controls\Label.mqh>
#include <Controls\Panel.mqh>
//+------------------------------------------------------------------+
//| GLOBAL PARAMETERS AND VARIABLES                                                |
//+------------------------------------------------------------------+
MqlBookInfo       BookInfo[];
CAppDialog myPanel;
CLabel BidVolume;
CLabel AskVolume;
//+------------------------------------------------------------------+
//| ON INIT                                                                        |
//+------------------------------------------------------------------+
int OnInit() {
//--- Adding the DOM for the Symbol
   MarketBookAdd(_Symbol);
//--- Setting OnTimer interval
   EventSetMillisecondTimer(10);
//--- Creating panel
   myPanel.Create(0, "SIMPLE PANEL", 0, 20, 20, 200, 200);
//--- Creating ask volume label
   AskVolume.Create(0,"Ask volume",0,0,0,0,0);
   myPanel.Add(AskVolume);
//--- Creating bid volume label
   BidVolume.Create(0,"Bid volume",0,0,15,0,0);
   myPanel.Add(BidVolume);
//--- Setting initial values for bid and ask volumes
   MarketBookGet(NULL,BookInfo);
   for(int i=0;i<ArraySize(BookInfo)-1;i++)
     {
      if(BookInfo[i].type != BookInfo[i+1].type)
        {
         AskVolume.Text("Ask volume = "+BookInfo[i].volume);
         BidVolume.Text("Bid volume = "+BookInfo[i+1].volume);
        }
     }
//--- Run panel
   myPanel.Run();   
//--- Return
   return(0);
}
//+------------------------------------------------------------------+
//| ON DEINIT                                                                      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
//--- Destroy panel
   myPanel.Destroy(reason);
//--- Libera o DOM do ativo
   MarketBookRelease(_Symbol);
//--- Apaga todos os objetos
   ObjectsDeleteAll(0,0);
//--- Remove timer
   EventKillTimer();
//---   
}
//+------------------------------------------------------------------+
//| ON TIMER                                                                       |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
   MarketBookGet(NULL,BookInfo);
//---
   for(int i=0;i<ArraySize(BookInfo)-1;i++)
     {
      if(BookInfo[i].type != BookInfo[i+1].type)
        {
         AskVolume.Text("Ask volume = "+BookInfo[i].volume);
         BidVolume.Text("Bid volume = "+BookInfo[i+1].volume);
        }
     }

}
//+------------------------------------------------------------------+
//| ON CHART EVENT                                                                 |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
//---
   myPanel.ChartEvent(id, lparam, dparam, sparam);
//--- 
}

Although I'm able to correctly refresh the information using other methods, like Print( ) or Comment( ), I'm not able to refresh the information (with the desired frequency) inside the panel. It seems that the information is refreshed only when a new tick occurs..

Could you please tell me if what I'm trying to achieve is actually possible and, if possible, help me to identify where is the problem?

Thanks in advance! 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Symbol Properties - Documentation on MQL5
Interview with Mariusz Zarnowski (ATC 2012)
As December 28 is approaching, the list of leaders of the Automated Trading Championship 2012 is becoming clearer. With only two weeks to go until the end of the Championship, Mariusz Zarnowski (zrn) from Poland stands a good chance to be in the top three. His EA has already demonstrated how it can triple the initial deposit in just a couple of weeks.
Alain Verleyen
Moderator
14133
angevoyageur 2013.12.16 15:53
You probably need a timer resolution lesser than 10 milliseconds.
Rodrigo Malacarne
Moderator
4617
Malacarne 2013.12.16 16:14

Hello Alain,

thanks for the comment. I've tried a smaller timer frequency (1 milisecond) and it doesn't work either. As said, commands like Print( ) and Comment( ) do return the correct information when a new event occurs on the DOM. However I'm not able to properly refresh the information inside the panel. I believe I'm doing something wrong... any idea?

Alain Verleyen
Moderator
14133
angevoyageur 2013.12.16 16:19
Malacarne:

Hello Alain,

thanks for the comment. I've tried a smaller timer frequency (1 milisecond) and it doesn't work either. As said, commands like Print( ) and Comment( ) do return the correct information when a new event occurs on the DOM. However I'm not able to properly refresh the information inside the panel. I believe I'm doing something wrong... any idea?

Ah sorry, I missed the point.

Try to add the following line :

      if(BookInfo[i].type != BookInfo[i+1].type)
        {
         AskVolume.Text("Ask volume = "+BookInfo[i].volume);
         BidVolume.Text("Bid volume = "+BookInfo[i+1].volume);
         myPanel.Run();
        }
Rodrigo Malacarne
Moderator
4617
Malacarne 2013.12.16 16:45

Thanks again Alain!

It works perfectly !!!

/
To add comments, please log in or register