Help on custom indicator need to calculate every tick

 

I am a novice programmer. I have cobbled together a MACD predictor line using a custom EMA algorithm to handle floating points, but it is not calculating every tick. I am guessing it's because of my loop. I should see the MACDP buffer changing per the tick as it increases or decreases and when I load it, sometimes it seems like the line is way off depending on the timeline. Pls Help!


//+------------------------------------------------------------------+
//|                                                        MACDP.mq5 |
//|                                       Copyright 2022, Josh Nixon |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Josh Nixon"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property strict
//--- indicator settings
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_width1  1
#property indicator_label1  "MACDP"

//--- indicator parameters
input double InpFastEMA=8.3897;   // Fast EMA Period
input double InpSlowEMA=17.5185;   // Slow EMA Period
input double InpSignalEMA=9.0503;  // Signal EMA Period
//--- indicator buffers
double    ExtMacdBuffer[];
double    ExtSignalBuffer[];
double    ExtFastBuffer[];
double    ExtSlowBuffer[];
double    ExtMACDPBuffer[];
//--- equations
double a = 2 / (1 + InpFastEMA);
double b = 1 - a;
double c = 2 / (1 + InpSlowEMA);
double d = 1 - c;
double e = 2 / (1 + InpSignalEMA);
double f = 1 - e;

//--- right input parameters flag
bool      ExtParameters=false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,(int)InpSignalEMA-1);
//--- indicator buffers mapping
     SetIndexBuffer(1,ExtMacdBuffer);
   ArraySetAsSeries(ExtMacdBuffer,false);
   SetIndexBuffer(2,ExtSignalBuffer);
   ArraySetAsSeries(ExtSignalBuffer,false);

//---
   SetIndexBuffer(3,ExtFastBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,ExtSlowBuffer,INDICATOR_CALCULATIONS);
//--- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME,StringFormat("MACDP(%.1f,%.1f,%.1f)",InpFastEMA,InpSlowEMA,InpSignalEMA));
//--- check for input parameters
   if(InpFastEMA<=1 || InpSlowEMA<=1 || InpSignalEMA<=1 || InpFastEMA>=InpSlowEMA)
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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<=InpSignalEMA || !ExtParameters)
      return(0);
//--- last counted bar will be recounted
   int begin=prev_calculated<1?1:prev_calculated-1;
//--- macd counted in the 1-st buffer
   for(int i=begin;i<rates_total && !_StopFlag;i++)
     {
      ExtFastBuffer[i]=ExponentialMA(i,InpFastEMA,ExtFastBuffer[i-1],close);
      ExtSlowBuffer[i]=ExponentialMA(i,InpSlowEMA,ExtSlowBuffer[i-1],close);
      ExtMacdBuffer[i]=ExtFastBuffer[i]-ExtSlowBuffer[i];
      ExtMACDPBuffer[i]= ((ExtSignalBuffer[i] * f) + (ExtFastBuffer[i] * b * e) - (ExtSlowBuffer[i] * d * e) - (ExtFastBuffer[i] * b) + (ExtSlowBuffer[i] * d)) / (a - c - (a * e) + (c * e));
     }
//--- signal line counted in the 2-nd buffer
   ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpSignalEMA,ExtMacdBuffer,ExtSignalBuffer);
//--- done

   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Exponential Moving Average                                       |
//+------------------------------------------------------------------+
double ExponentialMA(const int position,const double period,const double prev_value,const double &price[])
  {
//---
   double result=0.0;
//--- calculate value
   if(period>0)
     {
      double pr=2.0/(period+1.0);
      result=price[position]*pr+prev_value*(1-pr);
     }
//---
   return(result);
  }
//+------------------------------------------------------------------+
//|  Exponential moving average on price array                       |
//+------------------------------------------------------------------+
int ExponentialMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,
                          const double period,const double& price[],double& buffer[])
  {
   int    i,limit;
//--- check for data
   if(period<=1 || rates_total-begin<period) return(0);
   double dSmoothFactor=2.0/(1.0+period);
//--- save as_series flags
   bool as_series_price=ArrayGetAsSeries(price);
   bool as_series_buffer=ArrayGetAsSeries(buffer);
   if(as_series_price)  ArraySetAsSeries(price,false);
   if(as_series_buffer) ArraySetAsSeries(buffer,false);
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
     {
      limit=(int)period+begin;
      //--- set empty value for first bars
      for(i=0;i<begin;i++) buffer[i]=0.0;
      //--- calculate first visible value
      buffer[begin]=price[begin];
      for(i=begin+1;i<limit;i++)
         buffer[i]=(price[i]*dSmoothFactor)+(buffer[i-1]*(1.0-dSmoothFactor));
     }
   else limit=prev_calculated-1;
//--- main loop
   for(i=limit;i<rates_total;i++)
      buffer[i]=price[i]*dSmoothFactor+buffer[i-1]*(1.0-dSmoothFactor);
//--- restore as_series flags
   if(as_series_price)  ArraySetAsSeries(price,true);
   if(as_series_buffer) ArraySetAsSeries(buffer,true);
//---
    return(rates_total);
  }
//+------------------------------------------------------------------+
 
  1. double a = 2 / (1 + InpFastEMA);
    double b = 1 - a;
    double c = 2 / (1 + InpSlowEMA);
    double d = 1 - c;
    double e = 2 / (1 + InpSignalEMA);
    double f = 1 - e;

    Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  2. result=price[position]*pr+prev_value*(1-pr);

    That form increases rounding errors. Simplify:

    result=prev_value + (price[position]-prev_value)*pr;
  3. #property indicator_buffers 5
    #property indicator_plots   1
    
       PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,(int)InpSignalEMA-1);
    //--- indicator buffers mapping
       SetIndexBuffer(2,ExtSignalBuffer);

    You have one plot and five budffers. Their indexes are zero onwards.

 
William Roeder #:
  1. Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  2. That form increases rounding errors. Simplify:

  3. You have one plot and five budffers. Their indexes are zero onwards.

Thank you sir I might try to PM you after I make the suggested changes!