Histogram bars not displaying in seperate window

 

Hi there,

I want to create an indicator for sigma spikes as explained here:

https://adamhgrimes.com/how-to-calculate-sigmaspikes/

To start, I'm just focusing on displaying the daily returns and haven't been able to do this as yet.  I've tried reviewing the help files I find them very confusing.  I'm trying to follow the instructions from here instead:

https://www.youtube.com/watch?v=ki-ShYKppXI&t=163s


Anyway, the code below is how I'm trying to display daily returns in a seperate window.

//+------------------------------------------------------------------+
//|                                                SigmaSpike_v1.mq5 |
//|                                                      |
//|                                                               NA |
//+------------------------------------------------------------------+
#property copyright ""
#property link      "NA"
#property version   "1.00"
#property indicator_separate_window

#property indicator_buffers   1                                      // How many buffers that store information
#property indicator_plots     1                                      // How many indicators to be drawn

#property indicator_type1 DRAW_HISTOGRAM
#property indicator_label1 "SigmaSpikes"
#property indicator_color1 clrGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 4

//--- Input parameters
// NA

double BufferDailyReturns[];

int IndicatorPeriod = 20;

int DailyReturnsHandle;

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

   DailyReturnsHandle = iClose(_Symbol, PERIOD_CURRENT, 1) / iClose(_Symbol, PERIOD_CURRENT, 2) - 1;

   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, IndicatorPeriod);
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(DailyReturnsHandle != INVALID_HANDLE)
      IndicatorRelease(DailyReturnsHandle);

  }
//+------------------------------------------------------------------+
//| 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(IsStopped())                                          // True if MT5 wants to stop the indicator.
      return(0);                                            // Avoids getting caught in to a loop when the flag has been set to true.

   if(rates_total < IndicatorPeriod)                        // If not enough bars are present to calculate the indicator, this will
      return(0);                                            // return 0 so that the next iteration can check if there is enough data

   if(BarsCalculated(DailyReturnsHandle) < rates_total) // If all the bars haven't been calculated prev_calculated will return 0 and the next
      return(0);                                        // iteration will proceed as if for the first time until all bars have been calculated

   int copy_bars = 0;
   if(prev_calculated > rates_total || prev_calculated <= 0)
      copy_bars = rates_total;
   else
     {
      copy_bars = rates_total - prev_calculated;
      if(prev_calculated > 0)
         copy_bars++;
     }

   if(IsStopped())                                          // True if MT5 wants to stop the indicator.
      return(0);                                            // Avoids getting caught in to a loop when the flag has been set to true.
   if(CopyBuffer(DailyReturnsHandle, 0, 0, copy_bars, BufferDailyReturns) <= 0)
      return(0);

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I've also tried replacing:

if(CopyBuffer(DailyReturnsHandle, 0, 0, copy_bars, BufferDailyReturns) <= 0)
      return(0);

with:

for(int i = copy_bars; i < rates_total && !IsStopped(); i++)
      BufferDailyReturns[i] = close[i] / close[i + 1] - 1;

In both cases I get the same result i.e. a blank seperate window.


Thanks for your clarification / advice.

 
   DailyReturnsHandle = iClose(_Symbol, PERIOD_CURRENT, 1) / iClose(_Symbol, PERIOD_CURRENT, 2) - 1;
  1. iClose does not return a handle.
  2. Variables do not update unless you assign to them in OnCalculate.