Indicator Refresh After Connection Lost?

 

I have a custom indicator I have written. It appears to work - it draws as required in a separate window and displays Print debug information in the Experts tab to show that it is working. If I recompile the indicator it reloads and refreshes as expected. From time to time I suffer from momentary "connection to 'Broker' lost" and when the connection comes back it looks as though the indicator refreshes, but the indicator chart is not redrawn and the Print debug information  suggests the summary indicator data has been cleared and not rebuilt. I am hoping to use this indicator as part of an EA that I am writing, and this would be disastrous if I cannot get the indicator to automatically refresh properly after one of these dropped connections. Can anyone suggest a solution for this please?

 
Matt Jones:

I have a custom indicator I have written. It appears to work - it draws as required in a separate window and displays Print debug information in the Experts tab to show that it is working. If I recompile the indicator it reloads and refreshes as expected. From time to time I suffer from momentary "connection to 'Broker' lost" and when the connection comes back it looks as though the indicator refreshes, but the indicator chart is not redrawn and the Print debug information  suggests the summary indicator data has been cleared and not rebuilt. I am hoping to use this indicator as part of an EA that I am writing, and this would be disastrous if I cannot get the indicator to automatically refresh properly after one of these dropped connections. Can anyone suggest a solution for this please?

Hi,,

some information about the help from the compilator mql4 the same for mql5

Check the reason # 6 and get it from the reson const int in OnDeinit() function

after you know in the OnInit() check with a bool (false true) if reson 6 is checked from the OnDeinit() and with true it will refresh your EA

Bye

Laurent



Uninitialization Reason Codes

Uninitialization reason codes are returned by the UninitializeReason() function. The possible values are the following:

Constant

Value

Description

REASON_PROGRAM

0

Expert Advisor terminated its operation by calling the ExpertRemove() function

REASON_REMOVE

1

Program has been deleted from the chart

REASON_RECOMPILE

2

Program has been recompiled

REASON_CHARTCHANGE

3

Symbol or chart period has been changed

REASON_CHARTCLOSE

4

Chart has been closed

REASON_PARAMETERS

5

Input parameters have been changed by a user

REASON_ACCOUNT

6

Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings

REASON_TEMPLATE

7

A new template has been applied

REASON_INITFAILED

8

This value means that OnInit() handler has returned a nonzero value

REASON_CLOSE

9

Terminal has been closed

The uninitialization reason code is also passed as a parameter of the predetermined function OnDeinit(const int reason).

The codes 1(REASON_REMOVE) and 2(REASON_RECOMPILE) are implemented for the indicators.

Example:

//+------------------------------------------------------------------+
//| get text description                                             |
//+------------------------------------------------------------------+
string getUninitReasonText(int reasonCode)
  {
   string text="";
//---
   switch(reasonCode)
     {
      case REASON_ACCOUNT:
         text="Account was changed";break;
      case REASON_CHARTCHANGE:
         text="Symbol or timeframe was changed";break;
      case REASON_CHARTCLOSE:
         text="Chart was closed";break;
      case REASON_PARAMETERS:
         text="Input-parameter was changed";break;
      case REASON_RECOMPILE:
         text="Program "+__FILE__+" was recompiled";break;
      case REASON_REMOVE:
         text="Program "+__FILE__+" was removed from chart";break;
      case REASON_TEMPLATE:
         text="New template was applied to chart";break;
      default:text="Another reason";
     }
//---
   return text;
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- The first way to get the uninitialization reason code
   Print(__FUNCTION__,"_Uninitalization reason code = ",reason);
//--- The second way to get the uninitialization reason code
   Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason));
  }

 
boobyditbeber:

Hi,,

some information about the help from the compilator mql4 the same for mql5

Check the reason # 6 and get it from the reson const int in OnDeinit() function

after you know in the OnInit() check with a bool (false true) if reson 6 is checked from the OnDeinit() and with true it will refresh your EA

Bye

Laurent



Uninitialization Reason Codes

Uninitialization reason codes are returned by the UninitializeReason() function. The possible values are the following:

Constant

Value

Description

REASON_PROGRAM

0

Expert Advisor terminated its operation by calling the ExpertRemove() function

REASON_REMOVE

1

Program has been deleted from the chart

REASON_RECOMPILE

2

Program has been recompiled

REASON_CHARTCHANGE

3

Symbol or chart period has been changed

REASON_CHARTCLOSE

4

Chart has been closed

REASON_PARAMETERS

5

Input parameters have been changed by a user

REASON_ACCOUNT

6

Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings

REASON_TEMPLATE

7

A new template has been applied

REASON_INITFAILED

8

This value means that OnInit() handler has returned a nonzero value

REASON_CLOSE

9

Terminal has been closed

The uninitialization reason code is also passed as a parameter of the predetermined function OnDeinit(const int reason).

The codes 1(REASON_REMOVE) and 2(REASON_RECOMPILE) are implemented for the indicators.

Example:

//+------------------------------------------------------------------+
//| get text description                                             |
//+------------------------------------------------------------------+
string getUninitReasonText(int reasonCode)
  {
   string text="";
//---
   switch(reasonCode)
     {
      case REASON_ACCOUNT:
         text="Account was changed";break;
      case REASON_CHARTCHANGE:
         text="Symbol or timeframe was changed";break;
      case REASON_CHARTCLOSE:
         text="Chart was closed";break;
      case REASON_PARAMETERS:
         text="Input-parameter was changed";break;
      case REASON_RECOMPILE:
         text="Program "+__FILE__+" was recompiled";break;
      case REASON_REMOVE:
         text="Program "+__FILE__+" was removed from chart";break;
      case REASON_TEMPLATE:
         text="New template was applied to chart";break;
      default:text="Another reason";
     }
//---
   return text;
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- The first way to get the uninitialization reason code
   Print(__FUNCTION__,"_Uninitalization reason code = ",reason);
//--- The second way to get the uninitialization reason code
   Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason));
  }

Hello, and thank you so much for your reply - I did not know about the Uninitialization Codes.

As I worked through this, I found when the connection drops and resets, the indicator OnDeinit and OnInit functions do not actually get called.

This lead me to a deeper trace, and in the end I found that I was initializing my indicator buffers (hence the data was reset) but there were a couple of variables I was not re-initialising - they were only initialised in the OnInit function.

The result was that the buffers were re-initialised but then not recalculated as the other variables hadn't been reset.

So thank you again - you caused me to dig deeper (and in the right place) and to find the solution.

Regards,

Matt.

 
Matt Jones:

Hello, and thank you so much for your reply - I did not know about the Uninitialization Codes.

As I worked through this, I found when the connection drops and resets, the indicator OnDeinit and OnInit functions do not actually get called.

This lead me to a deeper trace, and in the end I found that I was initializing my indicator buffers (hence the data was reset) but there were a couple of variables I was not re-initialising - they were only initialised in the OnInit function.

The result was that the buffers were re-initialised but then not recalculated as the other variables hadn't been reset.

So thank you again - you caused me to dig deeper (and in the right place) and to find the solution.

Regards,

Matt.


that is a loop : OnInit->OnCalculate->OnDeinit->....... those are in the loop and can't be modified. it is like you make your own function, but here all is ready to go.

It is well and not so well. One thread only and before the loop go to the end some function will have to wait for.

You have to try how really the OnInit and OnDeinit work , I got some problems with that in my prog ccanvas clock. there are very strange thing about how to the OnDeinit and OnInit work......

I give you my code if you want to read it may be some parts should be interesting for you.

your welcome, have a nice day :))


Laurent

Files:
 

I have an indicator, which I wish to refresh at a particular time every day.

Is there a way to do it?


Thanks

Rinku

 
  1. Deinit is called only for the reasons listed. Connection lost is not one of them. On reconnect, history is updated, then indicator is called with prev_calculated being zero.
  2. Indicator shouldn't need to be refreshed, ever. You are not properly initializing it when prev_calculated is zero.
  3. Changing the chart symbol or TF or indicators parameters will result in a deinit/indicator reload/init cycle.
    Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.
    2. They don't update unless you assign to them.
    3. In C/C++ you can only initialize them with constants, and they default to zero.
    4. In MTx you should only initialize them with constants. There is no default in MT5 or (MT4 with strict which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    5. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - Inflation - MQL4 programming forum
  4. Right click → Refresh will not. History updates and prev_calculated is zero.
Reason: