Why is this technical indicator cannot be displayed?

 
//+------------------------------------------------------------------+
//|                                                            t.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1

#property indicator_type1    DRAW_LINE
#property indicator_color1   C'121,250,121'
#property indicator_style1   STYLE_SOLID
#property indicator_label1   "Line"
#property indicator_width1    3


input int fastPeriod = 5;
input int slowPeriod = 25;
input ENUM_TIMEFRAMES IndicatorTimeFrame = PERIOD_D1;

double indvalue[];
double fastma[],slowma[];

int handle_fast;
int handle_slow;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
  SetIndexBuffer(0,indvalue,INDICATOR_DATA);
  
  handle_fast = iMA(_Symbol,PERIOD_CURRENT,fastPeriod,0,MODE_EMA,PRICE_CLOSE);
  handle_slow = iMA(_Symbol,PERIOD_CURRENT,slowPeriod,0,MODE_EMA,PRICE_CLOSE);
  
  if((handle_fast || handle_slow) == INVALID_HANDLE)
  {
      Print("Get MA Handle failed!");
      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[])
  {
//---
   int i = 0;
   CopyBuffer(handle_fast,0,0,2,fastma);
   CopyBuffer(handle_slow,0,0,2,slowma);
   
   ArraySetAsSeries(fastma,true);
   ArraySetAsSeries(slowma,true);
   
   for(i = prev_calculated; i < rates_total; i++)
   {
      indvalue[i] = fastma[1] - slowma[1];    
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
{
   IndicatorRelease(handle_fast);
   IndicatorRelease(handle_slow);
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.07.20
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

because you copy only 2 bars in the copybuffer which means that the indicator would try to copy data from two bars and ignore any new bars that are available for calculation. this causes the plot to go missing

I fixed the script:

//+------------------------------------------------------------------+
//|                                                            t.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   1

#property indicator_type1    DRAW_LINE
#property indicator_color1   clrGreen
#property indicator_style1   STYLE_SOLID
#property indicator_label1   "Line"
#property indicator_width1   3

input int fastPeriod = 5;
input int slowPeriod = 25;

double indvalue[];
double fastma[];
double slowma[];

int handle_fast;
int handle_slow;

int max_bars;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    // Indicator buffers mapping
    SetIndexBuffer(0, indvalue, INDICATOR_DATA);

    handle_fast = iMA(_Symbol, PERIOD_CURRENT, fastPeriod, 0, MODE_EMA, PRICE_CLOSE);
    handle_slow = iMA(_Symbol, PERIOD_CURRENT, slowPeriod, 0, MODE_EMA, PRICE_CLOSE);

    if (handle_fast == INVALID_HANDLE || handle_slow == INVALID_HANDLE)
    {
        Print("Get MA Handle failed!");
        return INIT_FAILED;
    }

    max_bars = Bars(Symbol(), Period());

    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[])
{
    // Calculate the number of bars to copy
    int to_copy = rates_total - prev_calculated;
    if (to_copy <= 0)
        to_copy = max_bars;

    // Copy data from indicator buffers
    CopyBuffer(handle_fast, 0, 0, to_copy, fastma);
    CopyBuffer(handle_slow, 0, 0, to_copy, slowma);

    // Calculate the indicator values
    for (int i = prev_calculated; i < rates_total; i++)
    {
        indvalue[i] = fastma[i] - slowma[i];
    }

    // Return value of prev_calculated for the next call
    return rates_total;
}
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
{
    IndicatorRelease(handle_fast);
    IndicatorRelease(handle_slow);
}
 
phade #:

because you copy only 2 bars in the copybuffer which means that the indicator would try to copy data from two bars and ignore any new bars that are available for calculation. this causes the plot to go missing

I fixed the script:

thanks a lot.

Reason: