我的指标在我换到新的时间框架图时就消失了 - 页 2

 

谢谢你的解释。

我现在明白了。

SCFX

 

你好。

我对这个疯狂的错误抓耳挠腮。

当我改变时间框架时,这个简单的4行代码仍然消失。

疯狂的是,我已经应用了这里的建议,但仍然失败。

我感觉很糟糕...

请帮助我。

非常感谢。

SCFX

//+------------------------------------------------------------------+
//|                                                        H_roc.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//#property indicator_minimum -3.5
//#property indicator_maximum 3.5
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "boring"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
//--- indicator buffers
double         boring[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,boring);
   
          
//---
   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[])
  {

//----

   int limit=rates_total-prev_calculated;
   if(limit<=0) limit=300;
//---
 
 for(int i=1;i<=limit  ;i++)
{  if(   MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50
     ) 
      boring[i]=Close[i];
    
}  
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
scfx:

你好。

我对这个疯狂的错误抓耳挠腮。

当我改变时间框架时,这个简单的4行代码仍然消失。

疯狂的是,我已经应用了这里的建议,但仍然失败。

我感觉很糟糕...

请帮助我。

非常感谢。

SCFX



2014.06.15 11:26:39.908 'test.mq4'中的零除法(59,44)
 
angevoyageur:
2014.06.15 11:26:39.908 在'test.mq4'(59,44)中除以零。


谢谢你的回复。

在我的日志中,没有这样的通知,但当我改变TF时,这个指标被删除了。

我仍然无法解决这个问题。

SCFX

2014.05.18 08:41:31.080自定义指标 H_889_boring GBPUSD,H1:删除
2014.05.18 08:41:25.441 自定义指标H_889_boring GBPUSD,H4:加载成功

 
scfx:

我仍然无法修复它。

SCFX

那是因为你没有很努力地尝试。

如果你让别人帮你做,你就学不到任何东西,唯一的办法是自己做,这样你就能理解它,自己做的唯一办法是检查每一行代码,把它分解,弄清它到底在做什么,有几种方法可以做到这一点。

for(int i=1;i<=limit  ;i++)
{  if(   MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50
     ) // what is this parentheses doing here ? How can you expect to write code that works if you are this sloppy about it ?
      boring[i]=Close[i];
    
}  

这些都是你可以做的事情,可以帮助你找到问题的原因

  • 你说你检查了日志标签,你有没有查看旁边的专家标签?
  • 我上周已经告诉过你,你的代码会导致数组超出范围,你是否查过这个问题,找出它的含义?
  • 在metaeditor中有一个调试器,你可以试着学习如何使用它来调试你的代码。
  • 你可以使用Print(),这样你就可以检查代码运行时你的变量的值是多少。

如果你点击了专家标签,它会告诉你你的指标在这一行做了零除法,改变时间框架与此无关。

if(   MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50

因此,这必须意味着这个High[i]-Low[i]由于某种原因而为零。如果你看一下图表上的指标,你可以看到指标画了一些值,然后在只有一个刻度的柱子上停止。 如果只有一个刻度,high[i]==low[i],这将导致零除法。

现在确保high[i]-low[i]不被使用,如果它是零的话。

   int limit=rates_total-prev_calculated;
   if(limit<=0) limit=300;
//---
   for(int i=1;i<=limit  ;i++)
   {
    if(High[i] - Low[i] == 0) continue; // --------------- skip this bar
    
    if( MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50 ) 
    {boring[i]=Close[i];
   }}
//--- return value of prev_calculated for next call
   return(rates_total);
  }

现在你会看到专家标签告诉你,代码没有再进行零除,但它在这一行产生了数组超出范围的错误。

if(high[i] - low[i] == 0)  

那么,如果你去掉所有的计算和条件来测试这个循环会发生什么?

   int limit=rates_total-prev_calculated;
   if(limit<=0) limit=300;
//---

   for(int i=1;i<=limit  ;i++)
   { 
    boring[i]=Close[i];
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

现在,专家选项卡再次报告数组超出范围,这次是在这一行。

boring[i]=Close[i];

所以现在你知道你的代码在循环中每次使用价格数组都会导致数组超出范围。所以,请找出循环的问题,以及为什么Close[i], Low[i], High[i]会超出范围。你可以看到指标一直画到图表的末端,所以错误一定是在末端,即最高的数组索引。

   int limit=rates_total-prev_calculated; // what is the value of limit in this calculation when prev_calulated == 0 ?
   if(limit<=0) limit=300;
//---

   for(int i=1;i<=limit;i++) // how does the value of limit compare with the highest available Close[] array index ?

看来你是想做一个指标,而没有先学习如何编写一个能正确工作的循环。有很多例子可以说明如何做到这一点。看看所包含的指标的代码并进行实验,直到你完全理解它们是如何工作的,然后试着做一个指标来画一条简单的单线,在你试图让它进行计算之前,它能正常工作。
 

@ scfx

唯一能修复的人是你。或者去https://www.mql5.com/en/job

并在那里发布工作。

 
SDC:
...

请SDC,没必要这么苛刻,即使你是对的。
 
angevoyageur:

请SDC,没必要这么苛刻,即使你是对的。

lol 我把我的帖子重新措辞了一下;)

 

对不起,我误导了你,我道歉,指标可以这样编码。

int limit = -1;
   if( prev_calculated == 0 ) limit =  rates_total - 3000;// will calculate 3000 Bars
  if( prev_calculated > 0 )   limit = rates_total-prev_calculated;

 for(int i=limit;  i>=0; i--)
   {
    if(High[i] - Low[i] == 0) continue; // --------------- skip this bar
    

// Please test it, check if it's OK;
// Put the indicator in a backtest EA, say MACDSample, to check it
 

如果图表中的条数少于3000条,它仍然会超出范围