Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1306

 

Please help, I added one more buffer to the indicator (double ma_buffer_stop[];), but it doesn't draw or give errors(

//+------------------------------------------------------------------+
//|                                                   MTF_Moving.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                             https://www.mql5.com/ru/users/melnik |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com/ru/users/melnik"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Black
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_LINE
#property indicator_type3 DRAW_LINE

double ma_buffer_slow[];
double ma_buffer_fast[];
double ma_buffer_stop[];

//--- input parameters
input int                     PeriodMaSlow=21;  //Period slow Ma
input int                     PeriodMaFast=13;  //Pertiod fast Ma
input ENUM_APPLIED_PRICE      PriceMa=0;        //Applied price
input ENUM_MA_METHOD          MethodMa=0;       //Method Ma
input ENUM_TIMEFRAMES         Timeframe=60;     //Timeframe for calculate

ENUM_TIMEFRAMES prd;

int index=-1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, ma_buffer_slow, INDICATOR_DATA);
   SetIndexBuffer(1, ma_buffer_fast, INDICATOR_DATA);
   SetIndexBuffer(2, ma_buffer_stop, INDICATOR_DATA);
//---
   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((rates_total-prev_calculated-PeriodMaSlow)<=0)return(0);
  
   if(Period()>Timeframe) prd=PERIOD_CURRENT;
   if(Period()<=Timeframe) prd=prd=Timeframe;
  
   for(int i=rates_total-prev_calculated-PeriodMaSlow-1;i>=0;i--)
   {
      if(TimeMinute(time[i])==0)index=iBarShift(Symbol(), prd, time[i], false);
      
      ma_buffer_fast[i]=iMA(Symbol(), prd, PeriodMaFast, 0, MethodMa, PriceMa, index);
      ma_buffer_slow[i]=iMA(Symbol(), prd, PeriodMaSlow, 0, MethodMa, PriceMa, index);
      if(ma_buffer_fast[i+1]>=ma_buffer_slow[i+1]&&ma_buffer_fast[i]<ma_buffer_slow[i])
      {ma_buffer_stop[i]=ma_buffer_slow[i]+50*Point;}
      if(ma_buffer_fast[i+1]<=ma_buffer_slow[i+1]&&ma_buffer_fast[i]>ma_buffer_slow[i])
      {ma_buffer_stop[i]=ma_buffer_slow[i]-50*Point;}
   }
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Check, are we even getting in here?

if(ma_buffer_fast[i+1]>=ma_buffer_slow[i+1]&&ma_buffer_fast[i]<ma_buffer_slow[i]) printf(ma_buffer_slow[i]);
that's just the first thing that comes to mind.
 

Sometimes I catch the division at zero. (Anything can happen in the dynamics).

I look for it by commenting on division operations and throwing the EA on the chart.

Is there any way to get the string with the error using more normal methods?

The problem is that the error occurs only in a certain state. I.e., the error may disappear at a certain timeframe and with a new candle. It is good when you notice it at 4 o'clock. There is time to find it.

 
Valeriy Yastremskiy:

Is there any way to get the line with the error using more conventional methods?

You should have an entry in the expert log with the file and line number where division by zero took place

 
Igor Makanu:

You should have a record in your Expert Advisor log with the file and line number where division by zero took place

2020.11.27 11:55:29.795 qstr7_52_1 EURUSD,H4: zero divide in 'qstr7_52_1.mq4' (962,43)

Only this.

962.43 though.

There you go))) Thanks!!!!!


 
Aleksei Stepanenko:

Are we even getting in here?

just off the top of my head.

Going to

If I do this, it draws

   double prev01,prev02;

   if((rates_total-prev_calculated-PeriodMaSlow)<=0)return(0);
  
   if(Period()>Timeframe) prd=PERIOD_CURRENT;
   if(Period()<=Timeframe) prd=prd=Timeframe;
  
   for(int i=rates_total-prev_calculated-PeriodMaSlow-1;i>=0;i--)
   {
      if(TimeMinute(time[i])==0)index=iBarShift(Symbol(), prd, time[i], false);
      
      ma_buffer_fast[i]=iMA(Symbol(), prd, PeriodMaFast, 0, MethodMa, PriceMa, index);
      ma_buffer_slow[i]=iMA(Symbol(), prd, PeriodMaSlow, 0, MethodMa, PriceMa, index);
      prev01=iMA(Symbol(), prd, PeriodMaFast, 0, MethodMa, PriceMa, index+1);
      prev02=iMA(Symbol(), prd, PeriodMaSlow, 0, MethodMa, PriceMa, index+1);
      if(prev01>=prev02&&ma_buffer_fast[i]<ma_buffer_slow[i])
      {ma_buffer_stop[i]=ma_buffer_slow[i]+50*Point;}
      if(prev01<=prev02&&ma_buffer_fast[i]>ma_buffer_slow[i])
      {ma_buffer_stop[i]=ma_buffer_slow[i]-50*Point;}
   }
  
 

This array only has the right values at the intersection points, elsewhere its value is EMPTY_VALUE. Therefore, nothing is visible. You need to enter a global variable and assign values to it at the moment of intersection. And it will already give the current value at the moment of intersection, or the past value when there is no intersection.

double Stop=0;
int OnCalculate(....

if(ma_buffer_fast[i+1]>=ma_buffer_slow[i+1]&&ma_buffer_fast[i]<ma_buffer_slow[i]) Stop=ma_buffer_slow[i]+50*Point;
if(ma_buffer_fast[i+1]<=ma_buffer_slow[i+1]&&ma_buffer_fast[i]>ma_buffer_slow[i]) Stop=ma_buffer_slow[i]-50*Point;

ma_buffer_stop[i]=Stop;
 
Aleksei Stepanenko:

This array only has the right values at the intersection points, elsewhere its value is EMPTY_VALUE. Therefore, nothing is visible. You need to enter a global variable and assign values to it at the moment of intersection. And it will already give the current value at the moment of intersection, or the past value when there is no intersection.

Thank you, it worked)

 
Is it possible to forcibly cancel the timeframe change if the indicator function is still running?
 
Aleksei Stepanenko:

This array only has the right values at the intersection points, elsewhere its value is EMPTY_VALUE. Therefore, nothing is visible. You need to enter a global variable and assign values to it at the moment of intersection. And it will already give the current value at the moment of intersection, or the past value when there is no intersection.

I multiplied PeriodMaSlow by 2

   if((rates_total-prev_calculated-PeriodMaSlow)<=0)return(0);
  
   if(Period()>Timeframe) prd=PERIOD_CURRENT;
   if(Period()<=Timeframe) prd=prd=Timeframe;
  
   for(int i=rates_total-prev_calculated-PeriodMaSlow-1;i>=0;i--)

and the initial code works without errors

Reason: