一个指标出现了除以零的错误

 
 

从本周开始,终端开始愚蠢地挂起 - 我不知道该怎么办了。

在一个指标中,出现除以零的错误 - 存在一个简单的增量 - 请求初始日期并在其中添加秒数并减去初始日期。在此之前,该指标稳定运行了半年。

在哪里挖?如何决定?

代码 -默认情况下在 TimeFrames 以下的 TF 上运行,由 M1 推荐。  

 //+------------------------------------------------------------------+
//|                                                   IndDrafter.mq5 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link        ""
#property version    "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots    1
//----
input int                    IndId = 1 ;                 //номер индикатора
input ENUM_TIMEFRAMES   TimeFrames = PERIOD_H1 ;         //период работы
input bool           UseDeltaPrice = true ;             //разрешение использовать для расчета ширины канала индикатор DeltaPrice
input int                HighPoint = 1000 ;             //фиксированная ширина канала для верхней цены, в пунктах
input int                 LowPoint = 1000 ;             //фиксированная ширина канала для нижней цены, в пунктах
input int             VariantDraft = 6 ;                 //вариант построения графических фигур, 1-8
input int                 BarsUsed = 100 ;                 //количество отображаемых баров, 0-вся история
input int         PeriodDeltaPrice = 3 ;                 //период индикатора DeltaPrice
input bool            UsePriceInfo = true ;             //разрешение вывода показаний положения цены
input ENUM_APPLIED_PRICE     Price = PRICE_CLOSE ;       //тип цены
input bool         UseVerticalLine = true ;             //разрешение вывода вертикальной линии раздела времени
input ENUM_LINE_STYLE   StyleDraft = STYLE_DASH ;       //стиль линии графических фигур
input color            ColorDraft1 = clrLimeGreen ;     //первый цвет линии графических фигур
input color            ColorDraft2 = clrDarkOrchid ;     //второй цвет линии графических фигур
input color            ColorDraft3 = clrDarkTurquoise ; //третий цвет линии графических фигур
input ENUM_DRAW_TYPE StyleBuffer = DRAW_LINE ;         //стиль линии индикатора положения цены
input color            ColorBuffer = clrRed ;           //цвет линии индикатора положения цены
input int              WidhtBuffer = 1 ;                 //ширина линии индикатора положения цены
input bool             UseFiboGrid = true ;             //разрешение накладывать сеть на графические фигуры
input ENUM_LINE_STYLE StyleFiboUp = STYLE_DOT ;         //стиль линий сетки
input color            ColorFiboUp = clrWhite ;         //цвет верхней сетки
input int              WidhtFiboUp = 1 ;                 //ширина линий верхней Fibo сетки
input double             LevelUp01 = 23.6 ;             //значение первого уровня
input double             LevelUp02 = 38.2 ;             //значение второго уровня
input double             LevelUp03 = 50.0 ;             //значение третье уровня
input double             LevelUp04 = 61.8 ;             //значение четвертого уровня
input double             LevelUp05 = 76.4 ;             //значение пятого уровня
input double             LevelUp06 = 100.0 ;             //значение шестого уровня
input double             LevelUp07 = 123.6 ;             //значение седьмого уровня
input double             LevelUp08 = 138.2 ;             //значение восьмого уровня
input double             LevelUp09 = 150.0 ;             //значение девятого уровня
input double             LevelUp10 = 161.8 ;             //значение десятого уровня
input ENUM_LINE_STYLE StyleFiboDn = STYLE_DOT ;         //стиль линий сетки
input color            ColorFiboDn = clrAqua ;           //цвет нижней сетки
input int              WidhtFiboDn = 1 ;                 //ширина линий нижней Fibo сетки
input double             LevelDn01 = 23.6 ;             //значение первого уровня
input double             LevelDn02 = 38.2 ;             //значение второго уровня
input double             LevelDn03 = 50.0 ;             //значение третье уровня
input double             LevelDn04 = 61.8 ;             //значение четвертого уровня
input double             LevelDn05 = 76.4 ;             //значение пятого уровня
input double             LevelDn06 = 100.0 ;             //значение шестого уровня
input double             LevelDn07 = 123.6 ;             //значение седьмого уровня
input double             LevelDn08 = 138.2 ;             //значение восьмого уровня
input double             LevelDn09 = 150.0 ;             //значение девятого уровня
input double             LevelDn10 = 161.8 ;             //значение десятого уровня
                                                     //input uint              InpCenterTime=12;            //время центральной линии
//----
double ExtBuffer[];
//----
double level_up[ 10 ];
double level_dn[ 10 ];
//----
int counted_bars;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit ()
  {
   IndicatorSetInteger ( INDICATOR_DIGITS , _Digits );
   IndicatorSetDouble ( INDICATOR_MAXIMUM , 2.05 );
   IndicatorSetDouble ( INDICATOR_MINIMUM ,- 0.05 );
   IndicatorSetString ( INDICATOR_SHORTNAME , "Drafter " + IntegerToString (IndId)+ " " + IntegerToString (VariantDraft));
//----
   SetIndexBuffer ( 0 ,ExtBuffer);
   PlotIndexSetInteger ( 0 , PLOT_DRAW_TYPE ,StyleBuffer);
   PlotIndexSetInteger ( 0 , PLOT_LINE_WIDTH ,WidhtBuffer);
   PlotIndexSetInteger ( 0 , PLOT_LINE_COLOR ,ColorBuffer);
   PlotIndexSetDouble ( 0 , PLOT_EMPTY_VALUE , EMPTY_VALUE );
   PlotIndexSetString ( 0 , PLOT_LABEL , "var " + IntegerToString (VariantDraft));
//----
   counted_bars= 0 ;
   level_up[ 0 ]=LevelUp01;
   level_up[ 1 ]=LevelUp02;
   level_up[ 2 ]=LevelUp03;
   level_up[ 3 ]=LevelUp04;
   level_up[ 4 ]=LevelUp05;
   level_up[ 5 ]=LevelUp06;
   level_up[ 6 ]=LevelUp07;
   level_up[ 7 ]=LevelUp08;
   level_up[ 8 ]=LevelUp09;
   level_up[ 9 ]=LevelUp10;
   level_dn[ 0 ]=LevelDn01;
   level_dn[ 1 ]=LevelDn02;
   level_dn[ 2 ]=LevelDn03;
   level_dn[ 3 ]=LevelDn04;
   level_dn[ 4 ]=LevelDn05;
   level_dn[ 5 ]=LevelDn06;
   level_dn[ 6 ]=LevelDn07;
   level_dn[ 7 ]=LevelDn08;
   level_dn[ 8 ]=LevelDn09;
   level_dn[ 9 ]=LevelDn10;
//----
   ObjectsDeleteAll ( 0 , "IndDrafter tline " + IntegerToString (IndId), 0 );
   ObjectsDeleteAll ( 0 , "IndDrafter triangle " + IntegerToString (IndId), 0 );
   ObjectsDeleteAll ( 0 , "IndDrafter fibo line " + IntegerToString (IndId), 0 );
   ObjectsDeleteAll ( 0 , "IndDrafter fibo text " + IntegerToString (IndId), 0 );
//----
   return ( INIT_SUCCEEDED );
  }
//+------------------------------------------------------------------+
//| Custom deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit ( const int reason)
  {
   ObjectsDeleteAll ( 0 , "IndDrafter tline " + IntegerToString (IndId), 0 );
   ObjectsDeleteAll ( 0 , "IndDrafter triangle " + IntegerToString (IndId), 0 );
   ObjectsDeleteAll ( 0 , "IndDrafter fibo line " + IntegerToString (IndId), 0 );
   ObjectsDeleteAll ( 0 , "IndDrafter fibo text " + IntegerToString (IndId), 0 );
  }
//+------------------------------------------------------------------+
//| 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 ( _Period >=TimeFrames) return ( 0 );
   if (prev_calculated== 0 ) counted_bars= 0 ;
//---
   ArraySetAsSeries (time, true );
   ArraySetAsSeries (open, true );
   ArraySetAsSeries (high, true );
   ArraySetAsSeries (low, true );
   ArraySetAsSeries (close, true );
   ArraySetAsSeries (ExtBuffer, true );
//----
   int Bars = Bars ( _Symbol ,TimeFrames);
   int limit= 0 ;
   int pdprice=(UseDeltaPrice?PeriodDeltaPrice: 0 );
   MqlRates rates_array[];
   if (counted_bars== 0 )
     {
       if (BarsUsed> 0 )
         limit= CopyRates ( _Symbol ,TimeFrames,time[ 0 ],BarsUsed+pdprice,rates_array);
       else if (BarsUsed== 0 )
         limit= CopyRates ( _Symbol ,TimeFrames,time[ 0 ], Bars ( _Symbol ,TimeFrames,time[rates_total- 1 ],time[ 0 ])+pdprice,rates_array);
         //limit=CopyRates(_Symbol,TimeFrames,time[rates_total-1],time[0],rates_array);
       if (limit<=pdprice) return ( 0 );
       ArraySetAsSeries (rates_array, true );
       for ( int i=limit-pdprice- 1 ;i>= 0 ;i--)
         CreateFigure(i,time,open,high,low,close,rates_array);
     }
   else
     {
      limit= CopyRates ( _Symbol ,TimeFrames, 0 , Bars -counted_bars+pdprice+ 1 ,rates_array);
       if (limit<=pdprice) return ( 0 );
       ArraySetAsSeries (rates_array, true );
       for ( int i=limit-pdprice- 1 ;i>= 0 ;i--)
         CreateFigure(i,time,open,high,low,close,rates_array);
     }
//----
   counted_bars= Bars ;
   return (rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CreateFigure( int i, const datetime & Time [], const double & Open [], const double & High [], const double & Low [], const double & Close [], MqlRates &rates[])
  {
   datetime start_time=rates[i].time;
   datetime stop_time=(start_time+ PeriodSeconds (TimeFrames));
//---
   int start_index,stop_index,limit;
   datetime vertical_line_time;
//---
   start_index= ArrayBsearch ( Time ,start_time);
   if ( Time [start_index]<start_time) start_index=(start_index> 0 ?start_index- 1 :start_index);
   start_time= Time [start_index];
   if (i> 0 )
     {
      stop_index= ArrayBsearch ( Time ,stop_time);
       if ( Time [stop_index]>stop_time) stop_index++;
      stop_time= Time [stop_index];
      limit=start_index-stop_index+ 1 ;
      vertical_line_time= Time [start_index-(limit>> 1 )];
     }
   else
     {
       if (TimeFrames== PERIOD_MN1 )
        {
         MqlDateTime mql_time;
         TimeToStruct (start_time+ 2678400 ,mql_time);
         int days= 32 -mql_time.day;
         stop_time=start_time+days* 86400 ;
        }
       else if (TimeFrames== PERIOD_W1 ) stop_time-= 86400 ;
      vertical_line_time=start_time+(stop_time-start_time)/ 2 ;
      limit=( int )(stop_time-start_time)/ PeriodSeconds ( _Period );
     }
//---
   double center_line_point=rates[i].open;
   double delta_price_high= 0.0 ;
   double delta_price_low= 0.0 ;
//----
   if (UseDeltaPrice== true )
     {
      delta_price_high=center_line_point+HighPoint* _Point ;
      delta_price_low=center_line_point-LowPoint* _Point ;
     }
//----
   if (UseDeltaPrice== false )
     {
      delta_price_high=center_line_point+HighPoint* _Point ;
      delta_price_low=center_line_point-LowPoint* _Point ;
     }
//----
   if (UseVerticalLine){};
//----
   if (VariantDraft== 6 )
     {
       if (UsePriceInfo== true )
        {
         if (limit/ 2 == 0 )
         {
         //limit=2;
         Print ( "i=" ,i, " start_time - " , TimeToString (start_time, TIME_DATE | TIME_MINUTES ), " stop_time - " , TimeToString (stop_time, TIME_DATE | TIME_MINUTES ));
         Print ( "start_index=" ,start_index, " stop_index=" ,stop_index);
         Print ( "TimeFrames=" , PeriodSeconds (TimeFrames));
         //start_index=ArrayBsearch(Time,start_time);
         }
         double d1=(delta_price_high-center_line_point)/(( double )limit/ 2.0 );
         double d2=(center_line_point-delta_price_low)/(( double )limit/ 2.0 );
         //----
        }
       //----
       if (UseFiboGrid== true )
        {
        }
     }
//----

  }
 
Aleksey Vyazmikin:

从本周初开始,终端就陷入了僵局--我不知道该怎么做。

在一个指标中出现了除以0 的错误--有一个简单的delta--要求一个开始日期,然后把秒数加到它上面,再减去开始日期。在此之前的半年里,该指标一直工作良好。

我在哪里挖?如何解决这个问题?

通过调试器,看看?

很明显,这个除法并没有检查可能的空值。

 
Aleksey Vyazmikin:

从本周初开始,终端就陷入了僵局--我不知道该怎么做。

在一个指标中出现了除以0 的错误--有一个简单的delta--要求一个开始日期,然后把秒数加到它上面,再减去开始日期。在此之前的半年里,该指标一直工作良好。

在哪里挖?如何解决这个问题?

当这种错误发生时,你会看到其中的行号和位置。检查有什么值,并在除法前将其检查为0。

 
Georgiy Merts:

到调试器上看看?

很明显,在除法时--没有检查可能的零值。

阿列克谢-科齐岑

当这种错误发生时,你在其中写上行号和位置。看看有什么值,并在除法前检查它是否为0。

谢谢,这当然很清楚--检查除以0的情况!我更关心的是,当从指标中访问日期数组时,为什么开始出现这个零点。

 
Aleksey Vyazmikin:

谢谢,这当然很清楚--检查除以0的情况!我更关心的是,当从指标中访问日期数组时,为什么会出现这个零点。

你能给我看一个例子吗?最小代码可重复的问题?

 
Aleksey Vyazmikin:

谢谢,这当然很清楚--检查除以0的情况!我更关心的是,当从指标中访问日期数组时,为什么会出现这个零点。

这时你需要从代码中找出分母是什么!以及为什么它变成了零...如果它是一个日期参考,那么一些日期就被清零了...

 
Aleksey Vyazmikin:

自从更新后,我也有一些指标 "浮动"...需要弄清楚什么是错的...

 
Vladimir Karputov:

你能举个例子吗?最小代码可重复的问题?

该指标是按顺序写的--那里的一切都很棘手,我正试图自己弄清原因,但到目前为止我还不能...

这里极限值变为零。TimeFrames==PERIOD_H1

double d1=(delta_price_high-center_line_point)/(limit/2);
void CreateFigure(int i,const datetime &Time[],const double &Open[],const double &High[],const double &Low[],const double &Close[],MqlRates &rates[])
  {
   datetime start_time=rates[i].time;
   datetime stop_time=(start_time+PeriodSeconds(TimeFrames));

   int start_index,stop_index,limit;
   datetime vertical_line_time;

   start_index=ArrayBsearch(Time,start_time);
   if(Time[start_index]<start_time) start_index=(start_index>0?start_index-1:start_index);
   start_time=Time[start_index];
   if(i>0)
     {
      stop_index=ArrayBsearch(Time,stop_time);
      if(Time[stop_index]>stop_time) stop_index++;
      stop_time=Time[stop_index];
      limit=start_index-stop_index+1;
      vertical_line_time=Time[start_index-(limit>>1)];
     }
   else
     {
      if(TimeFrames==PERIOD_MN1)
        {
         MqlDateTime mql_time;
         TimeToStruct(start_time+2678400,mql_time);
         int days=32-mql_time.day;
         stop_time=start_time+days*86400;
        }
      else if(TimeFrames==PERIOD_W1) stop_time-=86400;
      vertical_line_time=start_time+(stop_time-start_time)/2;
      limit=(int)(stop_time-start_time)/PeriodSeconds(_Period);
     }
//--вырезано
}
 
Aleksey Vyazmikin:

该指标是按订单写的--这都是很棘手的,我正试图自己去找原因,但到目前为止我还不能...

这里极限值变为零。TimeFrames==PERIOD_H1

需要一个最小的代码可重复的问题。不是一段代码。

 
Alexey Kozitsyn:

这时你需要从代码中找出分母是什么!以及为什么它变成了零...如果它指的是日期,那么一些日期就被清零了...

是的,我需要弄清楚--昨天我打印了半天的日期--一切正常,然后我关闭了打印机,几乎立即就出现了错误--异常......

上面贴了代码,你能告诉我在哪里挖吗?


阿列克谢-科齐岑

我在更新后也得到了一些指标 "浮动"...需要弄清楚什么是错的...

是的,我可以想象,对于那些例如完全不理解代码的人来说,这一定是一种什么样的情况。

原因: