Indicator disappears at the end of debugging

 

Hey,

I am trying to make a custom indicator but for some reason it does not show up on chart when added and data window shows 0 value anywhere i hover on the chart, as if indicator was not calculated at all. If i start debugging i can see indicator line while debugging is running, but when debugging is done indicator disappears. Any idea why this would be?

//+------------------------------------------------------------------+
//|                                                          HMA.mq5 |
//|                                                               rk |
//|                                                 rokups.github.io |
//+------------------------------------------------------------------+
#property copyright "rk"
#property link      "rokups.github.io"
#property version   "1.00"

#include <MovingAverages.mqh>

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot HMA
#property indicator_label1  "HMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDarkTurquoise
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      Period = 50;
//--- indicator buffers
double         HMABuffer[];
double         HMARaw[];

int hWMA1;
int hWMA2;
double wma1[];
double wma2[];
int SmoothingPeriod;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    IndicatorSetString(INDICATOR_SHORTNAME, "HMA");
    PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Period);

    SetIndexBuffer(0, HMABuffer, INDICATOR_DATA);
    PlotIndexSetString(0, PLOT_LABEL, "HMA(" + string(Period) + ")");

    SetIndexBuffer(1, HMARaw, INDICATOR_CALCULATIONS);

    hWMA1 = iMA(_Symbol, PERIOD_CURRENT, Period / 2, 0, MODE_LWMA, PRICE_CLOSE);
    hWMA2 = iMA(_Symbol, PERIOD_CURRENT, Period / 1, 0, MODE_LWMA, PRICE_CLOSE);

    ArraySetAsSeries(wma1, true);
    ArraySetAsSeries(wma2, true);
    SmoothingPeriod = (int)sqrt(Period);

    return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| 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 < Period)
        return prev_calculated;
    
    if (prev_calculated == rates_total)
        return prev_calculated;
    
    CopyBuffer(hWMA1, 0, 0, Period, wma1);
    CopyBuffer(hWMA2, 0, 0, Period, wma2);
    HMARaw[prev_calculated] = (2 * wma1[0]) - wma2[0];
    if (prev_calculated > SmoothingPeriod)
        HMABuffer[prev_calculated] = LinearWeightedMA(prev_calculated - 1, SmoothingPeriod, HMARaw);
    return rates_total;
}
//+------------------------------------------------------------------+
Step on New Rails: Custom Indicators in MQL5
Step on New Rails: Custom Indicators in MQL5
  • www.mql5.com
I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.
Reason: