全局初始化失败!!!!!!! - 页 2

 

没问题,angevogeur

这段代码最初是这样的。

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);
 }

我只是把它修改成这样。

//+------------------------------------------------------------------+
//| 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()));
  }

正如你所看到的,我并没有改变函数的 "肉"。我只是做了一些必要的修改以符合新的功能。有一件事我还有点摸不着头脑,就是从start()到OnStart()或OnTick()的变化,不管它是什么(我现在忘了,但我仍在使用start(),没有问题。我应该提到,这是一个自定义指标。我对另一件事感到不满的是,根据你是否在编写EA、指标或脚本,你可以使用什么的限制。有些东西是有意义的(比如除非你在编写EA,否则不允许实施交易),有些则是没有意义的(比如禁止在指标中使用MessageBox)。另一件让我很不爽的事情是,像交易的开仓和平仓没有事件可用。这对我现在正在做的事情很有帮助。

我的最后一句话的一个例子是这样的。我想在满足某些条件的情况下显示一个买入信号,在满足某些条件的情况下显示一个平仓买入交易信号,但我也想让我的用户能够取消买入信号,如果他们愿意,可以等待另一个信号,平仓信号也是一样。如果他们选择关闭交易,我希望买入和关闭买入信号消失,让指标观察另一个交易的进入标准。取消的部分工作得很好,但我无法检测到开仓和平仓事件,因为MQL4没有提供相应的信息传递。

如果你看了我的资料,你会发现我绝不是一个新手程序员。这种语言根本无法提供一个专业人士所期望和需要的现代开发平台的所有功能。另一个问题是,我无法使调试工作正常进行,这是一个主要的不满。我按照文档操作,但当我试图设置调试器时,我得到了一个图表,它弹出了初始化指标的属性对话框,但当我点击 "好 "来完成指标设置时,图表消失了,就这样。也许事情在从俄文到英文的翻译中丢失了,我只是错过了一些东西,或者是 "蛋糕 "还没有完全烤好。作为一个专业人士,我知道创建这样的开发语言和环境是一项多么艰巨的任务。我的意见更多的是为开发人员提供参考,而不是抱怨。

教授

 
ProfessorMetal:

没问题,angevogeur

该代码最初是这样的。


教授

对不起,你发布的代码没有编译。我要求你提供代码以尝试重现你的问题。
 

如果你没有声明我所设置的变量,也没有声明和充实我所调用的方法,它就不会被编译。这是不可能的。我希望你能知道这一点。我贴出了我认为你所要求的东西--解决我的问题的方案。无论如何,如果你是用MetaQuotes,并试图找出MetaTrader的问题并加以解决,我在下面发布了更多内容。注释掉init()中的if块,全局 声明MA_Display_Time_Frame为一个整数,并添加这些方法

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;
  }

由于这打算成为一个商业产品,这绝不是所有的指标代码,但这应该可以编译,并有可能导致使用原来的init()和deinit()方法出现问题。这足以创建几个标签和按钮。正如其中一位发帖者提到的,故障是间歇性的。然而,它与任何会导致指标去初始化的行为有关,如改变指标属性、改变时间框架或停止和重新启动终端。如果你想测试改变属性,请将这些外部元素添加到globals中。

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

如果你添加了外挂,就不需要对引用它们的if块进行注释。这对你来说应该足够了,可以让它编译并尝试重现这个问题。自从我改变了旧的初始化和去初始化函数 并转到新的版本后,这个问题就没有发生。如果你还需要什么,请告诉我。我将继续关注这个话题。

 

我有一个类似的情况,在图表上的indi工作正常。

改变参数 后工作正常,Tf转换后工作正常。

关闭MT4并重新启动后,indi没有显示出来。

它在图表的指标列表中,但它没有工作。

打开参数窗口并点击确定按钮后,indi立即从列表中消失了。

我尝试了所有的技巧,包括上述帖子中描述的技巧,都没有用。

结果发现是除以0的问题!

简单的:if(x!=0)条件解决了这个问题。

 

是的,我刚刚在另一个indi上遇到了同样的问题。

如果没有每次启动平台时的 "if",indi就会出现除以0的问题。

新的MT4似乎没有存储信息,直到它被勾选。

另一个解决方案是使用OnCalculate()而不是start()或OnStart(),我想?

但是,这是要向MetaQuotes投诉的事情。

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

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

你必须使用返回(0) 的技巧。

如果有兴趣,有人会详细介绍一下。

 
deysmacro:

你必须使用返回(0) 的技巧。

如果有兴趣,有人会详细说明一下。


那么,你在互联网上随处可见的旧指标不会自动修改它们的代码。

如果有人能调整代码并理解问题,他们可以自己做。

其余的人,每次打开MT4都看不到自己的indi,必然会感到不舒服。

如果他们有一个模板,他们可以用它来恢复indi,但这意味着每次都这样做。

模板是另一个故事,在新的Built 625中。

 

伙计们。

你们都不对头。你所描述的是一个时间问题,这一直是一个问题。你可以用这个非常简单地解决这个问题。

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

你可能得到的任何除以0 的错误是由于你试图在服务器稳定下来之前进行计算。如果你在初始化函数中进行计算--不要!把上述代码放在你的start()或OnStart()中,然后再做你需要做的事情。达达斯,试图让它像你一样简单,会让你吃大亏的。你的indi很幸运。

当一个indi消失的时候,你的初始化就失败了。如果你看一下你的日志,你会看到 "全局初始化失败"。你需要弄清楚你为什么会出现这种情况,并把它纠正过来。如果你不这样做,你所做的只是在一个需要缝合的伤口上贴了一张创可贴,它将会回到你身上。

 

在我看来,这个错误与OnInit()无关,错误描述有误导性。

只要有一行代码

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);
  }

它将给出数组超出范围 的错误。

改变时间框架,你会得到全局初始化失败,并且指标会从图表中删除。

 
可能是,GumRai,听起来这里有不同的问题。一个是deinit()在它应该被调用的时候没有被调用,而使一些东西被挂在那里。你所看到的OnCalculate()的情况听起来像是MQL的函数 实现中的一个故障,如果我理解它应该如何工作的话。另一种情况是在服务器稳定下来并 "初始化 "服务器端信息之前试图做一些事情。我只是在这里猜测,但最后一个可能也是导致你在OnCalculate()中看到的情况。听起来,MQL的开发者有一些调试工作要做。公平地说,他们所做的尝试并不是小事。小故障是可以预期的。