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

 
mila.com:

Thank you, but it returns zero for me. What could be the reason?

It can't be any other reason. No computer knows a year less than 1970. Start with the year that appears in the broker's quotes.

 
Alexey Viktorov:

It couldn't be any other year. No computer knows a year less than 1970. Start with the year that appears in the broker's quotes.

It's a good job, the first year of our era.)

 
Vitaly Muzichenko:

What's up, it's good, the first year of our era)

And -1 would be the first year BC.
 
Artyom Trishkin:
Use CopyXXX()

Thank you.


In MT5 you can shift the chart this way:

PlotIndexSetInteger(0,PLOT_SHIFT,InpChannelPeriod);

If compiled in MT4 it does not give errors but nothing works, is there an MT4 counterpart?
 
Aleksey Vyazmikin:

Thank you.


In MT5 it is possible to shift the chart this way:

PlotIndexSetInteger(0,PLOT_SHIFT,InpChannelPeriod);

If you compile in MT4 it does not give errors, however nothing works, is there an MT4 analogue?
Пользовательские индикаторы - Справочник MQL4
Пользовательские индикаторы - Справочник MQL4
  • docs.mql4.com
Пользовательские индикаторы - Справочник MQL4
 
Alexey Viktorov:

I chose there.

SetIndexShift(0,InpChannelPeriod); 

but the effect is completely different, i.e. the code doesn't work, the logic is different - I don't know...
 

Maybe someone can help. The essence of the indicator is to draw the Doncian channel as usual and then shift the lines of the last channel value behind the minus bar.

In MT5 everything seems to work, but in MT4 I do not understand what is wrong - I have redrawn it here and there, but it still draws nonsense - it shifts the channel itself, although I separately make calculation for values that will be shifted....

//+------------------------------------------------------------------+
//|                                             Donchian_Channel.mq5 |
//+------------------------------------------------------------------+
#property copyright "Vyazmikin Aleksey Vyacheslavovich"
#property link      "https://www.mql5.com/ru/users/-aleks-"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

//--- plot Label1
#property  indicator_label1  "High_Prognoz";
#property  indicator_type1   DRAW_LINE;
#property  indicator_color1  clrAquamarine;
#property  indicator_style1  STYLE_DOT;
#property  indicator_width1  1;
//--- plot Label2
#property  indicator_label2  "Low_Prognoz";
#property  indicator_type2   DRAW_LINE;
#property  indicator_color2  clrAquamarine;
#property  indicator_style2  STYLE_DOT;
#property  indicator_width2  1;


//--- input parameters
input int InpChannelPeriod=48; // Period

//--- indicator buffers
double ExtHighBufferPrognoz[];
double ExtLowBufferPrognoz[];
//---
int i,limit,start;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtHighBufferPrognoz,INDICATOR_DATA);
   SetIndexBuffer(1,ExtLowBufferPrognoz,INDICATOR_DATA);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- set first bar from what index will be drawn
   SetIndexDrawBegin(0,InpChannelPeriod);
   SetIndexDrawBegin(1,InpChannelPeriod);

   SetIndexShift(0,InpChannelPeriod);
   SetIndexShift(1,InpChannelPeriod);

//---
   return(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[])
  {

//--- check for rates
   if(rates_total<InpChannelPeriod*2) return(0);
//--- preliminary calculations
   if(prev_calculated==0) limit=InpChannelPeriod;
   else limit=prev_calculated;
//--- the main loop of calculations

   for(i=limit;i<rates_total && !IsStopped();i++)
     {
      start=i-InpChannelPeriod;
      ExtHighBufferPrognoz[i-InpChannelPeriod]=high[ArrayMaximum(high,InpChannelPeriod,start)];
      ExtLowBufferPrognoz[i-InpChannelPeriod] =low[ArrayMinimum(low,InpChannelPeriod,start)];

     }

   for(int x=rates_total-InpChannelPeriod;x<rates_total && !IsStopped();x++)
     {
      ExtHighBufferPrognoz[x]=ExtHighBufferPrognoz[rates_total-InpChannelPeriod];
      ExtLowBufferPrognoz[x]=ExtLowBufferPrognoz[rates_total-InpChannelPeriod];
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Aleksey Vyazmikin:

Maybe someone can help. The essence of the indicator is to draw the Doncian channel as usual and then shift the lines of the last channel value behind the minus bar.

In MT5 everything seems to work, but in MT4 I do not understand what is wrong - I have redrawn it here and there, but it still draws nonsense - it shifts the channel itself, although I separately make calculation for values that will go to shift....

Well look at the alligator code, the shift works there. Although, maybe the logic is different.

 
Alexey Viktorov:

Well, look at the alligator code, that's where the shift works. The logic may be different, though.


Yes the shift works for me too.

I fill the array with a shift, but it appears as if it is filled without a shift, but the shift itself happens visually.

The first part of the code leaves the buffer unfilled to the depth ofInpChannelPeriod from the last bar:

   for(i=limit;i<rates_total && !IsStopped();i++)
     {
      start=i-InpChannelPeriod;
      ExtHighBufferPrognoz[i-InpChannelPeriod]=high[ArrayMaximum(high,InpChannelPeriod,start)];
      ExtLowBufferPrognoz[i-InpChannelPeriod] =low[ArrayMinimum(low,InpChannelPeriod,start)];

     }

The second part should fill this area:

   for(int x=rates_total-InpChannelPeriod;x<rates_total && !IsStopped();x++)
     {
      ExtHighBufferPrognoz[x]=ExtHighBufferPrognoz[rates_total-InpChannelPeriod];
      ExtLowBufferPrognoz[x]=ExtLowBufferPrognoz[rates_total-InpChannelPeriod];
     }

But in reality it turns out like this:


 

Code in MT5

#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

//--- plot Label1
#property indicator_label1  "Predicted_high_price";
#property indicator_type1   DRAW_LINE;
#property indicator_color1  clrAquamarine;
#property indicator_style1  STYLE_DOT;
#property indicator_width1  1;
//--- plot Label2
#property indicator_label2  "Predicted_low_price";
#property indicator_type2   DRAW_LINE;
#property indicator_color2  clrAquamarine;
#property indicator_style2  STYLE_DOT;
#property indicator_width2  1;


//--- input parameters
input int InpChannelPeriod=48; // Period

//--- indicator buffers
double ExtHighBufferPrognoz[];
double ExtLowBufferPrognoz[];
//---
int i,limit,start;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtHighBufferPrognoz,INDICATOR_DATA);
   SetIndexBuffer(1,ExtLowBufferPrognoz,INDICATOR_DATA);   
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- set first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpChannelPeriod);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpChannelPeriod);   

   PlotIndexSetInteger(0,PLOT_SHIFT,InpChannelPeriod);
   PlotIndexSetInteger(1,PLOT_SHIFT,InpChannelPeriod);   


//---
   return(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[])
  {

//--- check for rates
   if(rates_total<InpChannelPeriod) return(0);
//--- preliminary calculations
   if(prev_calculated==0) limit=InpChannelPeriod;
   else limit=prev_calculated;
//--- the main loop of calculations
   for(i=limit;i<rates_total && !IsStopped();i++)
     {
      start=i-InpChannelPeriod;          
      ExtHighBufferPrognoz[i-InpChannelPeriod]=high[ArrayMaximum(high,start,InpChannelPeriod)];
      ExtLowBufferPrognoz[i-InpChannelPeriod]=low[ArrayMinimum(low,start,InpChannelPeriod)];

     }

   for(int x=rates_total-InpChannelPeriod;x<rates_total && !IsStopped();x++)
     {
      //int calc=x--;
      ExtHighBufferPrognoz[x]=ExtHighBufferPrognoz[rates_total-InpChannelPeriod-1];
      ExtLowBufferPrognoz[x]=ExtLowBufferPrognoz[rates_total-InpChannelPeriod-1];             
     }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Result:


ZS: Changed the code - the wrong ME was.