[HEINZ JUERGEN THIEME] Need help with Custom Indicator code — the separate window is empty

 
Comments that do not relate to the "Learning the code syntax", have been moved into this topic.
 
hello,

I try to learn the language for mt5 and now I have started to code an indicator which shows the difference of two other indicators in an separate window, for example MA10 and MA20.

The code for this indicator was compiled without problems but when I run this indicator the separate window is empty.  Another indicator I have coded which shows the difference of RVI main and signal line is working well. Where is my mistake, I suppose it is in the last part "  Custom indicator iteration function " . I would be happy if some one is helping. Thank you.

 The code of the not working indicator is below. It is also attached as file.

//+------------------------------------------------------------------+
//|                                      Indicator_Differenz(MA).mq5 |
//|                                     Copyright 2023, hatee        |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023 hatee"
#property version   "1.0"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "MAfast-MAslow Difference;"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrSkyBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//---- input parameters
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;
input int    MAfast= 30;      //Period
input int    MAslow= 50;      //Period
input ENUM_MA_METHOD   MA_fast_Method = MODE_EMA;             // MA fast Method
input ENUM_MA_METHOD   MA_slow_Method = MODE_EMA;             // MA slow Method
input ENUM_APPLIED_PRICE MA_Price_close  = PRICE_CLOSE;       // MA Price
input ENUM_APPLIED_PRICE MA_Price_open  = PRICE_OPEN;        // MA Price


//--- indicator buffers
double         MAfastBuffer[];
double         MAslowBuffer[];
double         DiffBuffer[];
int    MAfasthandle;
int    MAslowhandle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,DiffBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,MAfastBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,MAslowBuffer ,INDICATOR_CALCULATIONS);

   ArraySetAsSeries(DiffBuffer,true);
   ArraySetAsSeries(MAfastBuffer,true);
   ArraySetAsSeries(MAslowBuffer,true);
  
    if(Bars(_Symbol,_Period)<60)
  {
  Alert("We have less than 60 bars for Indicator exited now!!");
  return (-1);
  
  } 
MAfasthandle = iMA(NULL, TimeFrame, MAfast,0,MA_fast_Method, MA_Price_close);
MAslowhandle = iMA(NULL, TimeFrame, MAslow,0,MA_fast_Method, MA_Price_close);
  
    if(MAfasthandle<0)
    {
  Alert("Can not create handle ",GetLastError(),"!!");
  return (-1);
     }
    if(MAslowhandle<0)
  {
  Alert("Can not create handle ",GetLastError(),"!!");
   return (-1);
  }
 //---
   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[])
  {
//---
   ArraySetAsSeries(close,true);
   if(BarsCalculated(MAfasthandle)<rates_total) return(0);
   if(BarsCalculated(MAslowhandle)<rates_total) return(0);
   if(CopyBuffer(MAfasthandle,0,0,rates_total,MAfastBuffer)<=0) return (0);
   if(CopyBuffer(MAslowhandle,1,0,rates_total,MAslowBuffer)<=0) return (0);
  
   
   int pos=prev_calculated-1;
   if(pos<0) pos=0;
   for(int i=pos; i<rates_total-(MAfast+ MAslow +1); i++)
  
  
  
     {
    
      DiffBuffer[i]= (MAfastBuffer[i]-MAslowBuffer[i]);  
    

     }
   return(rates_total);
   }
//+------------------------------------------------------------------+

 
HEINZ JUERGEN THIEME #:
hello,

I try to learn the language for mt5 and now I have started to code an indicator which shows the difference of two other indicators in an separate window, for example MA10 and MA20.

The code for this indicator was compiled without problems but when I run this indicator the separate window is empty.  Another indicator I have coded which shows the difference of RVI main and signal line is working well. Where is my mistake, I suppose it is in the last part "  Custom indicator iteration function " . I would be happy if some one is helping. Thank you.

 The code of the not working indicator is below. It is also attached as file.


Please post your code properly, use code button, edit your post, don't repost
 
Dominik Christian Egert #:
Please post your code properly, use code button, edit your post, don't repost
Have edited my post 
 
In most cases the indicator functions like iMA only use buffer 0 for their main data. So you must alter the copybuffer lines like so to make everything work correctly:

if(CopyBuffer(MAfasthandle,0,0,rates_total,MAfastBuffer)<=0) return (0);
if(CopyBuffer(MAslowhandle,0,0,rates_total,MAslowBuffer)<=0) return (0);

I believe this is the only change you need to make.
 
phade #:
In most cases the indicator functions like iMA only use buffer 0 for their main data. So you must alter the copybuffer lines like so to make everything work correctly:


I believe this is the only change you need to make.

hello phade,

thank you for helping. I have changed  the copybuffer line and the indicator is now working well.

Reason: