global initialization failed!!!!!!! - page 2

To add comments, please log in or register
Jeffrey Irick
339
Jeffrey Irick  

No problem, angevogeur

The code was originally this:

int init()
  {
      // Check for input errors
      if (Use_LT_TimeFrame_Confirmation)
      {
         if (Number_Of_TimeFrames < 1 || Number_Of_TimeFrames > 4)
         {
            Alert("Initialization Error: Number of time frames for timeframe trend confirmation must be between 2 and 4, inclusively.");
         }
         
         if (Number_Of_Periods_For_Trend_Agreement < 2)
         {
            Alert("Initialization Error: Number of time frames for timeframe trend aggreement must be greater than 1.");
         }
      }
      
      IndicatorShortName("White Wolf Custom Software Moving Averages Indicator");
      
//---- indicators
      SetIndexStyle(0,DRAW_LINE);
      SetIndexBuffer(0,EMABuffer1);
      SetIndexStyle(1,DRAW_LINE);
      SetIndexBuffer(1,EMABuffer2);
      SetIndexStyle(2,DRAW_LINE);
      SetIndexBuffer(2,EMABuffer3);
      SetIndexStyle(3,DRAW_LINE);
      SetIndexBuffer(3,SMABuffer);
   
      SetIndexEmptyValue(0,0.0);
      SetIndexEmptyValue(1,0.0);
      SetIndexEmptyValue(2,0.0);
      SetIndexEmptyValue(3,0.0);
//----

      // MA Period Buttons
      MA_Display_Time_Frame = Period(); // Set the trade entry time frame to the current chart period - this ensures that we have a TF for the MA calculations
   
   // Show the timeframe buttons so the user can refine their entry strategy if they wish
 
      ResetLastError();
      Alert("In init() - Calling CreateMAPeriodButtons()");
      CreateMAPeriodButtons();
      if (GetLastError() != 0)
         Alert("GetLasteError() returned " + IntegerToString(GetLastError()));
         
      ResetLastError();
      Alert("In init() - Calling CreateDismissSignalButtons()");
      CreateDismissSignalButtons();
      if (GetLastError() != 0)
         Alert("GetLasteError() returned " + IntegerToString(GetLastError()));
      
      ResetLastError();
      Alert("In init() - Calling SetPeriodButtonState()");
      SetPeriodButtonState();
      if (GetLastError() != 0)
         Alert("GetLasteError() returned " + IntegerToString(GetLastError()));
   
   // Set normalization factor for current currency pair
   if ((Digits == 4) || (Digits == 5))
         NormalizationFactor = 0.0001;
      else
         NormalizationFactor = 0.01;
   
   return(0);
  }

void deinit()
  {
      Alert("In de-init() - getting ready to delete objects ");
      
      ResetLastError();
      ObjectsDeleteAll(0, OBJ_LABEL);
      Alert("In de-init() - attempting to delete labels - GetLastError() returns  " + IntegerToString(GetLastError()));
      
      ResetLastError();
      ObjectsDeleteAll(0, OBJ_BUTTON);
      Alert("In de-init() - attempting to delete buttons - GetLastError() returns  " + IntegerToString(GetLastError()));

return(0);
 }

I simply modified it to this:

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
  {
      // Check for input errors
      if (Use_LT_TimeFrame_Confirmation)
      {
         if (Number_Of_TimeFrames < 1 || Number_Of_TimeFrames > 4)
         {
            Alert("Initialization Error: Number of time frames for timeframe trend confirmation must be between 2 and 4, inclusively.");
         }
         
         if (Number_Of_Periods_For_Trend_Agreement < 2)
         {
            Alert("Initialization Error: Number of time frames for timeframe trend aggreement must be greater than 1.");
         }
      }
      
      IndicatorShortName("White Wolf Custom Software Moving Averages Indicator");
      
//---- indicators
      SetIndexStyle(0,DRAW_LINE);
      SetIndexBuffer(0,EMABuffer1);
      SetIndexStyle(1,DRAW_LINE);
      SetIndexBuffer(1,EMABuffer2);
      SetIndexStyle(2,DRAW_LINE);
      SetIndexBuffer(2,EMABuffer3);
      SetIndexStyle(3,DRAW_LINE);
      SetIndexBuffer(3,SMABuffer);
   
      SetIndexEmptyValue(0,0.0);
      SetIndexEmptyValue(1,0.0);
      SetIndexEmptyValue(2,0.0);
      SetIndexEmptyValue(3,0.0);
//----

      // MA Period Buttons
      MA_Display_Time_Frame = Period(); // Set the trade entry time frame to the current chart period - this ensures that we have a TF for the MA calculations
   
   // Show the timeframe buttons so the user can refine their entry strategy if they wish
 
      ResetLastError();
      Alert("In init() - Calling CreateMAPeriodButtons()");
      CreateMAPeriodButtons();
      if (GetLastError() != 0)
         Alert("GetLasteError() returned " + IntegerToString(GetLastError()));
         
      ResetLastError();
      Alert("In init() - Calling CreateDismissSignalButtons()");
      CreateDismissSignalButtons();
      if (GetLastError() != 0)
         Alert("GetLasteError() returned " + IntegerToString(GetLastError()));
      
      ResetLastError();
      Alert("In init() - Calling SetPeriodButtonState()");
      SetPeriodButtonState();
      if (GetLastError() != 0)
         Alert("GetLasteError() returned " + IntegerToString(GetLastError()));
   
   // Set normalization factor for current currency pair
   if ((Digits == 4) || (Digits == 5))
         NormalizationFactor = 0.0001;
      else
         NormalizationFactor = 0.01;
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
      Alert("In de-init() - getting ready to delete objects ");
      
      ResetLastError();
      ObjectsDeleteAll(0, OBJ_LABEL);
      Alert("In de-init() - attempting to delete labels - GetLastError() returns  " + IntegerToString(GetLastError()));
      
      ResetLastError();
      ObjectsDeleteAll(0, OBJ_BUTTON);
      Alert("In de-init() - attempting to delete buttons - GetLastError() returns  " + IntegerToString(GetLastError()));
  }

As you can see, I didn't change the "meat" of the functions. I simply made the mods necessary to conform to the new functionality. One thing I'm still a little foggy about is the change from start() to OnStart() or OnTick(), whichever it is(I've forgotten at the moment, but I'm still using start() with no problems. I should mention that this is a custom indicator. Another thing that I have a gripe about is the limitations on what you can use depending on whether you're coding an EA, indicator or script. There are some things that make sense(such as not allowing trades to be implemented unless you're coding an EA) and others that don't(such as the prohibition of using a MessageBox in and indicator). Another thing that really gripes me is not having events available for things like trade opens and closes. This would be really helpful for what I'm putting together right now.

An example of what I'm getting at with my last statement is this: I want to be able to display a Buy signal if certain criteria are met, display a Close Buy Trade signal for it if certain criteria are met but I also want to allow my user to be able to cancel the Buy signal and wait for another if they wish and ditto for the Close signal. If they choose to close the trade, I want the Buy and Close Buy signals to disappear and have the indicator watch for entry criteria for another trade. The cancellation parts work perfectly but I can't detect Open and Close events because MQL4 doesn't provide the messaging for it.

If you've looked at my profile, you'll have seen that I'm by no means a novice programmer. The language simply doesn't provide all the functionality a professional expects and needs from a modern development platform. On another note, I can't get the debugging to work which is a major gripe. I followed the documentation but when I tried to set the debugger in motion I got a chart which popped up the properties dialog for initializing my indicator but when I hit Okay to finish the indicator setup the chart disappeared and that was that. Perhaps things are getting lost in the translation from Russian to English and I'm just missing something or perhaps the "cake" isn't quite baked. As a professional I know how monumental a task it is to create a development language and environment like this. My remarks are meant more in the nature of FYI's for the development staff rather than complaints.

Prof

Alain Verleyen
37536
Alain Verleyen  
ProfessorMetal:

No problem, angevogeur

The code was originally this:


Prof

Sorry but the code you have posted didn't compile. I ask you the code to try to reproduce your problem.
Jeffrey Irick
339
Jeffrey Irick  

If you haven't declared the variables I'm setting and also don't have the methods I'm calling declared and fleshed out it won't compile. It can't. I would have expected you to know that. I posted what I thought you were asking for - the solution that fixed my problem. At any rate, if you're with MetaQuotes and are trying to figure out what's going on with MetaTrader and fix it, I've posted more below. Comment out the if block in init(), declare MA_Display_Time_Frame globally as an integer and add these methods:

void CreateMAPeriodButtons()
  {
//  Alert("In CreateMAPeriodButtons()");
      int X_Distance = 10;
      int Y_Distance = 20;
      // Create MA Period Label
      ObjectCreate("MAPeriodLabel", OBJ_LABEL, 0, 0, 0);
      ObjectSet("MAPeriodLabel", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
      ObjectSet("MAPeriodLabel", OBJPROP_XDISTANCE, X_Distance);
      ObjectSet("MAPeriodLabel", OBJPROP_YDISTANCE, Y_Distance);
      ObjectSetText("MAPeriodLabel", "MA Display Period", 12, "Arial", clrYellow);
      
      // Create Period Buttons
      CreateButton("M1", "M1", 1, 140, 50, 100, 20, "Arial", 12, clrYellow, clrGray);
}

void CreateDismissSignalButtons()
  {
      int X_Distance = 25;
      int Y_Distance = 100;
      
      // Create Dismiss Label
      ObjectCreate("DismissSignalsLabel", OBJ_LABEL, 0, 0, 0);
      ObjectSet("DismissSignalsLabel", OBJPROP_CORNER, CORNER_RIGHT_LOWER);
      ObjectSet("DismissSignalsLabel", OBJPROP_XDISTANCE, X_Distance);
      ObjectSet("DismissSignalsLabel", OBJPROP_YDISTANCE, Y_Distance);
      ObjectSetText("DismissSignalsLabel", "Dismiss Trade Signals", 12, "Arial", clrYellow);
      
      // Create Dismiss Buttons
      
      CreateButton("DismissBuySignal", "Dismiss Buy Signal", CORNER_RIGHT_LOWER, 200, 95, 190, 20, "Arial", 12, clrYellow, clrGray);
}

void CreateButton(string strButtonName, string strButtonText, const int nCorner, const int nXpos, const int nYpos, int nWidth, int nHeight, string strFont, 
                    int nFontSize, int nFontColor, int nBackColor, bool bSelected = false)
  {      
      ObjectCreate(0, strButtonName, OBJ_BUTTON, 0, 0, 0);

      //--- set button coordinates
      
      ObjectSetInteger(0, strButtonName, OBJPROP_CORNER, nCorner);

      ObjectSetInteger(0, strButtonName, OBJPROP_XDISTANCE, nXpos);

      ObjectSetInteger(0, strButtonName, OBJPROP_YDISTANCE, nYpos);
 
      //--- set button size
      ObjectSet(strButtonName, OBJPROP_XSIZE, nWidth);
     
      ObjectSet(strButtonName, OBJPROP_YSIZE, nHeight);

      //--- set the chart's corner, relative to which point coordinates are defined
      
      ObjectSet(strButtonName, OBJPROP_CORNER, nCorner);

      //--- set the text
      
      ObjectSetString(0, strButtonName, OBJPROP_TEXT, strButtonText);
      ObjectSetString(0, strButtonName, OBJPROP_FONT, strFont);
      ObjectSetInteger(0, strButtonName, OBJPROP_FONTSIZE, nFontSize);
      ObjectSetInteger(0, strButtonName, OBJPROP_COLOR, nFontColor);

      //--- set background color
      
      ObjectSetInteger(0, strButtonName, OBJPROP_BGCOLOR, nBackColor);
      
      return;
  }

Since this is intended to be a commercial product that's not all the indicator code by any means but this should compile and potentially cause the problem using the original init() and deinit() methods. It's enough to create a couple of labels and buttons. As one of the posters mentioned, the failure was intermittent. It was, however, related to any action that would caused the indicator to deinitialize, such as changing indicator properties, changing time frames or stopping and restarting the terminal. If you want to test changing the properties, add these externs to the globals:

extern int               Number_Of_TimeFrames = 2;
extern int               Number_Of_Periods_For_Trend_Agreement = 25;
extern bool             Allow_Modify_Entry_Timeframe = true;

If you add the externs, there's no need to comment out the if block that references them.That should be enough for you to get it to compile and try to reproduce the problem. The problem has not occurred since I changed the old initialization and deinitialization functions and went to the new versions. If you need anything else let me know. I'll keep an eye on the thread.

Dadas
260
Dadas  

I had a similar situation, the indi worked fine when dropped on the chart.

It worked fine after parameters change, it worked fine after Tf shift.

After closing the MT4 and restarting it, the indi didn't show up.

It was in the indicators list on the chart, but it didn't work.

After opening the parameters window and clicking the OK button, the indi immediately dissapeared from the list.

I tried all the tricks including the ones described in the above posts, nothing worked.

It turned out to be the division by 0 issue!

The simple: if(x!=0) condition solved the problem.

Dadas
260
Dadas  

Yup, I've just had the same issue with another indi.

Without the "if" every time the platform is started, the indi has the division by 0 issue,

the new MT4 seems to have no information stored until it gets ticked.

The other solution would be to use the OnCalculate() instead of start() or OnStart(), I guess?

But, that is something to complain about to MetaQuotes.

   double pipValue = MarketInfo(Symbol(),MODE_TICKVALUE); 

   if(pipValue!=0)
    {
   double lots   = AccountBalance()*(RiskPercent/100.0)/(StopLoss*pipValue);
    }
Syed Naufal Gaddafi
1896
Syed Naufal Gaddafi  

You have to use return (0) trick.

Someone will elaborate a bit if there is interest.

Dadas
260
Dadas  
deysmacro:

You have to use return (0) trick.

Someone will elaborate a bit if there is interest.


Well, old indicators that you can find everywhere on the internet don't get their code modified automatically.

If someone can tweak codes and understands the problem, they can do it on their own.

All the rest are just bound to feel the discomfort of not seeing their indi's every time they open the MT4.

If they have a template, they can use it to restore the indi, but that means doing this every time.

Templates are another story, in the new Built 625.

Jeffrey Irick
339
Jeffrey Irick  

Guys,

You're all off the mark. What you're describing is a timing issue that's always been a problem. You can take care of that very simply with this:

// Wait for the server "turmoil" to settle before doing anything
      string AcctCurrency = AccountCurrency();
       
      if (AcctCurrency == "")
          return(0);

Any division by zero error you may be getting is due to you trying to make calculations before the server has settled down. If you're doing calculations in the initialization function - DON'T! Put the above code in your start() or OnStart() and THEN do what you need to. Dadas, trying to make it as simple as you are is going to bite you big time. You're getting lucky on your indi.

When an indi disappears, you've got an initialization failure. If you look at your logs you will see "global initialization failure". You need to figure out why you're getting that and straighten it out. If you don't, all you've done is put a BandAid on a cut that needed stitches and it WILL come back on you.

Keith Watford
Moderator
16074
Keith Watford  

Seems to me that this error has nothing to do with OnInit() and the error description is misleading.

With just a single line of code

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[])
  {
//---
  Print(High[rates_total]);
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }

It will give the array out of range error.

Change timeframe and you get Global initialisation failed and the indicator is removed from the chart

Jeffrey Irick
339
Jeffrey Irick  
Could be, GumRai, Sounds like there are different issues here. One is deinit() not being called when it should be and leaving things hanging around. What you're seeing with OnCalculate() sounds like a glitch in MQL's implementation of the function, if I understand how it's supposed to work. Another is trying to do stuff before the server has settled down and "initialized" the server side info. I'm just guessing here, but the last may also be causing what you're seeing with OnCalculate(). Sounds like the MQL developers have a bit of debugging to do. To be fair, what they're attempting is not trivial. Glitches are only to be expected.
To add comments, please log in or register