Initialization problem

 

Hi guys,


I found an issue:

There is a MarketInfo(Symbol(),MODE_TICKVALUE) query in my indicator's OnInit() part.

It works good, except one: if the indicator is already on the chart when I start the MT4, the tickvalue gives 0.

So, I think there is something wrong with MarketInfo in OnInit(), when you start the MT4.

Did anyone experience this?

 
  1. The only thing you should do in OnInit is what is necessary to start the indicator/EA. You are not connected to a chart yet. Broker values can be bogus. Wait for a tick.
  2. don't use TickValue by itself. Only as a ratio with TickSize. See DeltaValuePerLot()
 
WHRoeder:
  1. The only thing you should do in OnInit is what is necessary to start the indicator/EA. You are not connected to a chart yet. Broker values can be bogus. Wait for a tick.
  2. don't use TickValue by itself. Only as a ratio with TickSize. See DeltaValuePerLot()

Yes, I know the difference between TickValue and TickSize.

Because TickValue is a static thing, it was reasonable to query it in OnInit(). Also, it works good if I NEWLY load the indicator on a chart.

The only problem occurs when the indicator is ALREADY on the chart, when I start the MT4. So, I think it is an issue.

 
ggekko:

Yes, I know the difference between TickValue and TickSize.

Because TickValue is a static thing, it was reasonable to query it in OnInit(). Also, it works good if I NEWLY load the indicator on a chart.

The only problem occurs when the indicator is ALREADY on the chart, when I start the MT4. So, I think it is an issue.

No it is not an issue! You are just not contemplating what is happening. When you start MT4, with a Chart that already has your Indicator on it, it will run the OnInit() even before MT4 has had a chance to connect to the broker, hence the value of 0.

That is why, as WHRoeder stated, you should only check Market and Symbol Information, once the first tick comes in, because that is the sign that MT4 is connected to the broker.

Even then, you should check the Connection Status first, because there are 3rd party tools out there that cause a tick event to be generated even when you are not connected to the broker. So always check/verify all the necessary conditions before requesting Market or Symbol information.

 
Sure. It were great, if these sort of things would be described on the official documentation somewhere.
 

Guys, there is something weird.

My test-indicator (code below) is already on the chart, when MT4 is starting.

#property strict
#property indicator_chart_window

bool firstRun=true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   if(firstRun)
     {
      if(IsConnected()) Print("Connection OK!");
      else Print("Not connected.");
      double tickValue=MarketInfo(Symbol(),MODE_TICKVALUE);
      Print("TickValue: ",DoubleToStr(tickValue,Digits));
      firstRun=false;
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


I got "Not connected." and TickValue=0.00000.

There is no 3rd party stuff at all.

What did give the trigger for OnCalculate(..) without a tick?

 
ggekko: Guys, there is something weird. My test-indicator (code below) is already on the chart, when MT4 is starting.

I got "Not connected." and TickValue=0.00000. There is no 3rd party stuff at all.

What did give the trigger for OnCalculate(..) without a tick?

There is nothing weird. You're just not using your thinking cap!

When you add an Indicator (or open MT4 with a chart and indicator allready defined), don't you think that the Indicator has to process all previous bars first in order to get to the current Bar?

That is why it is called OnCalculate() and not OnTick() as in the EA's. On first call it is supposed to calculated all previous data in the bar history (i.e. prev_calculated == 0).

PS! In essence, if calculations depend on the Tick-value, don't do anything until it is connected! Plus, remember that the tick value is only valid for current prices, not for history data.

EDIT: All this may not be explicitly outlined in the documentation, but it is naturally deducible and I consider it common sense, after reading what is laid out in the documentation.

 
FMIC:

EDIT: All this may not be explicitly outlined in the documentation, but it is naturally deducible and I consider it common sense, after reading what is laid out in the documentation.

I personally don't think it's quite as clear as you are implying, and the documentation could be clearer. Particularly because MT4's behaviour has changed over time.

As an example of what I would not call "naturally deducible" or "common sense":

  • If MT4 is already running, but you have no broker connection, then OnInit() is called if you add a new EA to a chart.
  • But if you start up MT4 with an EA already attached to a chart, then OnInit() is not called in an EA until there is a broker connection. If automatic login fails, for example, then OnInit() is not called, and the EA is entirely dormant.

So, in the absence of a broker connection, you get calls to OnInit() in an EA under some circumstances but not others.

With an indicator, the documentation says that a call to OnCalculate() "usually happens when a new tick is received for the symbol" (my italics). What the word "usually" conceals is that you always get a call both to OnInit() and to OnCalculate() when an indicator starts up, regardless of whether there is a broker connection, and therefore very unlike the behaviour of an EA.

 
jjc:

As an example of what I would not call "naturally deducible" or "common sense": [...]

... partly because it's a marketing decision rather than a technological decision.

If you are downloading and installing MT4 for the first time, both the broker and MetaQuotes want you to see charts with candles and indicators on them immediately, even before you have created a new demo account or filled in existing account details. They don't want a bare and empty platform. Therefore, OnInit() and OnCalculate() must be called immediately in an indicator, regardless of the presence of a broker connection. EAs follow different rules. There are undeniably good reasons for those different rules in an EA, but I don't personally think that the marketing priorities of brokers are "naturally deducible" or "common sense" from a developer's point of view.

More generally, MT4 prioritises fast start-up whereas other platforms wait until connections are fully established and all data is coherent and consistent before kicking things off.

 
jjc:

  • If MT4 is already running, but you have no broker connection, then OnInit() is called if you add a new EA to a chart.
  • But if you start up MT4 with an EA already attached to a chart, then OnInit() is not called in an EA until there is a broker connection. If automatic login fails, for example, then OnInit() is not called, and the EA is entirely dormant.

I disagree! You are forgetting that the reason that EAs works differently from Indicators, is not because of fixed behavior, but because you have set it to be so in the "Options". The option in question is enabled by default, but you can disable it:

  • Disable Auto Trading when the account has been changed
  • Disable Auto Trading when the profile has been changed
  • Disable Auto Trading when the charts symbol or period has been changed
 
jjc:

... partly because it's a marketing decision rather than a technological decision.

If you are downloading and installing MT4 for the first time, both the broker and MetaQuotes want you to see charts with candles and indicators on them immediately, even before you have created a new demo account or filled in existing account details. They don't want a bare and empty platform. Therefore, OnInit() and OnCalculate() must be called immediately in an indicator, regardless of the presence of a broker connection. EAs follow different rules. There are undeniably good reasons for those different rules in an EA, but I don't personally think that the marketing priorities of brokers are "naturally deducible" or "common sense" from a developer's point of view.

More generally, MT4 prioritises fast start-up whereas other platforms wait until connections are fully established and all data is coherent and consistent before kicking things off.

Please note that this thread is about Indicators and the OnCalculate() and not about EA's and OnTick(). It is also not about other platforms, but about MT4.

I am not implying that MT4 or MQL4 itself is "naturally deducible" or "common sense" (because it is not). I am saying that AFTER reading the documentation, that the expected behavior is deducible and that it makes sense that it be so.

Obviously you disagree and that is totally valid as opinions go, but basically it is one opinion against another - so lets just agree to disagree, because neither of us is going to budge on it.

Reason: