Subscription to OnBookEvent sometimes falls off - is there such a thing? - page 6

 
Sergey Savinkin:

I'd like a comment from the developers because there's nothing in the documentation about it.

Do you not see from the log above that there is a counter?

 
prostotrader:

Doesn't it look from the log above that there is a counter?

You can see from the log above that one EA does not unsubscribe the other. But how it is implemented - through counter or something else - is not clear. In addition, the programmer has had an indicator and an Expert Advisor. But the indicator has an unpleasant feature of mixing up the order of start of OnInit() and OnDeinit(). Although, it should not confuse the counter, if it really has one.

 
Sergey Savinkin:

You can see from the log above that one EA does not unsubscribe the other. But how it is implemented - through counter or something else - is not clear. In addition, the programmer has an indicator and an Expert Advisor. But the indicator has an annoying feature of mixing up the order of starting OnInit() and OnDeinit().

Therefore, we have implemented the is_book variable to avoid confusion.

The developers have made MarketBookAdd() a FUNCTION for a reason.

 
prostotrader:

Can't you see from the log above that there is a counter?

The log is nothing when using a different situation than the one in which the problem occurs.

Take 2 of your programs (Expert Advisor and an indicator, or 2 indulators) - put them on the same chart and then delete one of them. The subscription of the remaining instance will fall off.

Even when OnInit/OnDeinit call sequence is correct (it was always correct in these tests), there is a problem.

 
Doesn't that approach work?
class MARKETBOOK
{
private:  
  const string SymbName;
  int Count;

public:  
  MARKETBOOK( const string Symb ) : SymbName(Symb), Count(0) { this.On(); }
  ~MARKETBOOK( void ) { this.Off(); }

  bool isExist( void ) const { return(this.Count); }
    
  bool On( void ) { return (this.isExist() || (bool)(this.Count += ::MarketBookAdd(this.SymbName))); }

  bool Off( void )
  {
    while (this.isExist())
      this.Count -= ::MarketBookRelease(this.SymbName);
      
    return(!this.isExist());
  }  
};

MARKETBOOK MarketBook(_Symbol);
 
fxsaber:
This approach doesn't work?

I haven't run it in the terminal yet, but I have a question - how does moving the code to an object wrapper help against "cutting off the ends" by the terminal itself (as is happening now)? Well, an object will be created and subscription will start, and then if someone closes another program with subscription, the object will never know that there's no subscription anymore.

I did it the simple way: I compare timeouts on OnBookEvent and OnTick. If timeout on the first exceeds timeout on the second by more than N seconds - I re-subscribe.

 
Stanislav Korotky :

I have not run it in the terminal yet, but the question arose - how will transferring the code to an object wrapper help from "chopping off the ends" by the terminal itself inside itself (as it is happening now)? Well, an object will be created and the subscription will begin, and then if someone closes another program with a subscription, the object will never know that the subscription is no more.

I did it in a simple way: I compare timeouts for OnBookEvent and OnTick. If the first timeout exceeds the second timeout by more than N seconds, I resubscribe.

Well, as expected, you are wrong.

1st indicator

 //+------------------------------------------------------------------+
//|                                                   Test_ind_1.mq5 |
//|                                      Copyright 2018 prostotrader |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018 prostotrader"
#property link        "https://www.mql5.com"
#property version    "1.00"
#define on_call - 111
#property indicator_separate_window
bool is_book = false ;
double Buff[];
int event_cnt = 0 ;
#property indicator_buffers 1
#property indicator_plots    1
//--- plot Label1
#property indicator_label1    "Test_1"
#property indicator_type1    DRAW_LINE
#property indicator_color1    clrAqua
#property indicator_style1    STYLE_SOLID
#property indicator_width1    1
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit ()
  {
   //--- Set buffers 
   IndicatorSetInteger ( INDICATOR_DIGITS , 0 );
   IndicatorSetString ( INDICATOR_SHORTNAME , "Test_ind_1" );
//---Set buffers
   SetIndexBuffer ( 0 ,Buff, INDICATOR_DATA );
   PlotIndexSetDouble ( 0 , PLOT_EMPTY_VALUE , EMPTY_VALUE );
   ArraySetAsSeries (Buff, true ); 
   is_book = MarketBookAdd ( Symbol ());
   if (is_book == true )
    { 
       Print ( __FUNCTION__ , ": Подписка на стакан добавлена. Символ " , Symbol ());
    }
//---
   return ( INIT_SUCCEEDED );
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit ( const int reason)
  {
     if (is_book == true )
    { 
       MarketBookRelease ( Symbol ());
       Print ( __FUNCTION__ , ": Подписка на стакан удалена. Символ " , Symbol ());
    }  
  }  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate ( const int rates_total,
                 const int prev_calculated,
                 const int begin,
                 const double &price[])
  {
     if (prev_calculated == 0 )
    {
       ArrayInitialize (Buff, EMPTY_VALUE );
    }
   Buff[ 0 ] = 2 ;
//--- return value of prev_calculated for next call
   event_cnt = rates_total;
   return (rates_total);
  }
//+------------------------------------------------------------------+
//| BookEvent function                                               |
//+------------------------------------------------------------------+
void OnBookEvent ( const string &symbol)
  {
   if (symbol == Symbol ())
   {
     Print ( __FUNCTION__ , ": Подписка работает. Символ " , symbol);
       double price[];
       OnCalculate (event_cnt,event_cnt,on_call,price);
   }
   
  }  
//+------------------------------------------------------------------+

Second indicator

 //+------------------------------------------------------------------+
//|                                                   Test_ind_1.mq5 |
//|                                      Copyright 2018 prostotrader |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018 prostotrader"
#property link        "https://www.mql5.com"
#property version    "1.00"
#define on_call - 111
#property indicator_separate_window
bool is_book = false ;
double Buff[];
int event_cnt = 0 ;
#property indicator_buffers 1
#property indicator_plots    1
//--- plot Label1
#property indicator_label1    "Test_2"
#property indicator_type1    DRAW_LINE
#property indicator_color1    clrLime
#property indicator_style1    STYLE_SOLID
#property indicator_width1    1
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit ()
  {
   //--- Set buffers 
   IndicatorSetInteger ( INDICATOR_DIGITS , 0 );
   IndicatorSetString ( INDICATOR_SHORTNAME , "Test_ind_2" );
//---Set buffers
   SetIndexBuffer ( 0 ,Buff, INDICATOR_DATA );
   PlotIndexSetDouble ( 0 , PLOT_EMPTY_VALUE , EMPTY_VALUE );
   ArraySetAsSeries (Buff, true ); 
   is_book = MarketBookAdd ( Symbol ());
   if (is_book == true )
    { 
       Print ( __FUNCTION__ , ": Подписка 2 на стакан добавлена. Символ " , Symbol ());
    }
//---
   return ( INIT_SUCCEEDED );
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit ( const int reason)
  {
     if (is_book == true )
    { 
       MarketBookRelease ( Symbol ());
       Print ( __FUNCTION__ , ": Подписка 2 на стакан удалена. Символ " , Symbol ());
    }  
  }  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate ( const int rates_total,
                 const int prev_calculated,
                 const int begin,
                 const double &price[])
  {
     if (prev_calculated == 0 )
    {
       ArrayInitialize (Buff, EMPTY_VALUE );
    }
   Buff[ 0 ] = 2 ;
//--- return value of prev_calculated for next call
   event_cnt = rates_total;
   return (rates_total);
  }
//+------------------------------------------------------------------+
//| BookEvent function                                               |
//+------------------------------------------------------------------+
void OnBookEvent ( const string &symbol)
  {
   if (symbol == Symbol ())
   {
     Print ( __FUNCTION__ , ": Подписка 2 работает. Символ " , symbol);
       double price[];
       OnCalculate (event_cnt,event_cnt,on_call,price);
   }
   
  }  
//+------------------------------------------------------------------+


Result

 2018.07 . 24 22 : 20 : 26.992 Test_ind_1 (Si- 9.18 ,M1) OnInit : Подписка на стакан добавлена. Символ Si- 9.18
2018.07 . 24 22 : 20 : 26.998 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 27.214 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 27.226 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 27.528 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 28.250 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 28.374 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 28.388 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 28.974 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 29.014 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 29.114 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 29.238 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 29.296 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 29.304 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 29.397 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 29.405 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 30.321 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 30.335 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 30.593 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 30.607 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 30.915 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 31.407 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 31.491 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 31.505 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 31.611 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 31.707 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 31.815 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 33.395 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 33.577 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 33.777 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 33.785 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 33.923 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 33.943 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 34.693 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 35.725 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 36.059 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 36.251 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 36.265 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 20 : 36.418 Test_ind_1 (Si- 9.18 ,M1) OnDeinit : Подписка на стакан удалена. Символ Si- 9.18
2018.07 . 24 22 : 21 : 41.846 Test_ind_1 (Si- 9.18 ,M1) OnInit : Подписка на стакан добавлена. Символ Si- 9.18
2018.07 . 24 22 : 21 : 41.852 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 41.884 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 42.658 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 42.926 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 44.540 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 44.632 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 45.396 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 45.722 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 46.132 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 46.514 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 46.860 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 47.012 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 47.064 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 47.254 Test_ind_2 (Si- 9.18 ,M1) OnInit : Подписка 2 на стакан добавлена. Символ Si- 9.18
2018.07 . 24 22 : 21 : 52.020 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 52.020 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 52.026 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 52.026 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 52.056 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 21 : 52.056 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18

2018.07 . 24 22 : 22 : 07.882 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 07.886 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 07.886 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 07.946 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 07.946 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 07.961 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 07.961 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 07.963 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 07.963 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 08.039 Test_ind_1 (Si- 9.18 ,M1) OnBookEvent : Подписка работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 08.039 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 08.433 Test_ind_1 (Si- 9.18 ,M1) OnDeinit : Подписка на стакан удалена. Символ Si- 9.18
2018.07 . 24 22 : 22 : 08.947 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 09.067 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 09.143 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 09.162 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 09.164 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 09.910 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 12.446 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 16.896 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 16.908 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 17.144 Test_ind_2 (Si- 9.18 ,M1) OnBookEvent : Подписка 2 работает. Символ Si- 9.18
2018.07 . 24 22 : 22 : 17.216 Test_ind_2 (Si- 9.18 ,M1) OnDeinit : Подписка 2 на стакан удалена. Символ Si- 9.18
 
Stanislav Korotky:

I haven't run it in the terminal yet, but I have a question - how does moving the code to an object wrapper help against "cutting off the ends" by the terminal itself (as is happening now)?

Init and Deinit don't participate in the subscription. Perhaps this circumstance would help.

 

I have run these indicators in different windows of the same symbol and it works fine,

but when I ran both indicators in the same symbol window, it really

subscription "fell off" when removing one indicator.

Write to the SD (maybe they will have time to make a fix in the new build)

Reason: