Need help with an indicator (calculating the EMA for a LinearRegression formula)

 

Hi there,

I'm just curious as to how to proceed with calculating the EMA for a LinearRegression calculation.
So I have adopted a LinearRegression from mladen (credits to the owner). 
And this seems to be working, what i wish to do next is to calculate the EMA of the resulting LinearRegression. Below is the formula:

Add note: I am aware of iMA but I am not sure how to add calculating LinRegCalc, since the only option are PRICE_...
so i thought of perhaps adding a manual calculation for EMA, just like iLinr below

//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1 
#property indicator_label1  "LinearReg"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_width1  2

//--- indicator buffers
double hlc3Buffer[];
double linRegCalc[]; 
double linRegEMA[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, linRegCalc, INDICATOR_DATA); // Assign pricecomp to the first plot
// SetIndexBuffer(1, linRegEMA, INDICATOR_DATA);
   
//--- indicator short name assignment
   IndicatorSetString(INDICATOR_SHORTNAME, "PriceComp");


   return (INIT_SUCCEEDED);
  }

//
void OnDeinit(const int reason)
  {
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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 = (prev_calculated > 0 ? prev_calculated - 1 : 0);
   for(; i < rates_total && !_StopFlag; i++)
     {
      // Calculate HLC3 value
      double hlc3Buffer = (high[i] + low[i] + close[i]) / 3.0;

      // Calculate linear regression values
      linRegCalc[i] = iLinr(hlc3Buffer, 21, i, rates_total); // Linear regression with period 21
//    linRegEMA[i] = ??? //trying to find the EMA for the past 5 bars of linRegCalc[i]

   return(rates_total);
  }
  

//------------------------------------------------------------------
// Custom function(s)
//------------------------------------------------------------------

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double iLinr(double price, int period, int i, int bars, int lookback = 0, int instance = 0)
  {
#define ¤ instance
#define _functionInstances 10
   struct sLinrArrayStruct
     {
      double         price;
      double         wsumm;
      double         psumm;
      double         weight;
     };
   static sLinrArrayStruct m_array[][_functionInstances];
   static double m_linregValues[]; // Array to store historical Linear Regression values
   static int m_arraySize = 0;

// Resize arrays if necessary
   if(m_arraySize < bars)
     {
      int _res = ArrayResize(m_array, bars + 500);
      if(_res <= bars)
         return(0);
      m_arraySize = _res;

      // Resize the historical values array
      ArrayResize(m_linregValues, bars + 500);
     }

//
//--- Linear Regression Calculation
//

   m_array[i][¤].price = price;
   if(i > period)
     {
      m_array[i][¤].weight = m_array[i-1][¤].weight;
      m_array[i][¤].wsumm  = m_array[i-1][¤].wsumm + price * period - m_array[i-1][¤].psumm;
      m_array[i][¤].psumm  = m_array[i-1][¤].psumm + price - m_array[i-period][¤].price;
     }
   else
     {
      m_array[i][¤].wsumm  = 0;
      m_array[i][¤].psumm  = 0;
      m_array[i][¤].weight = 0;
      for(int k = 0, w = period; k < period && i >= k; k++, w--)
        {
         m_array[i][¤].wsumm  += m_array[i-k][¤].price * w;
         m_array[i][¤].psumm  += m_array[i-k][¤].price;
         m_array[i][¤].weight += w;
        }
     }

// Calculate Linear Regression value for the current bar
   double linregValue = 3.0 * m_array[i][¤].wsumm / m_array[i][¤].weight - 2.0 * m_array[i][¤].psumm / (double)period;

// Store the Linear Regression value in the historical array
   m_linregValues[i] = linregValue;

// Return the Linear Regression value from the specified lookback period
   if(i >= lookback)
     {
      return m_linregValues[i - lookback];
     }
   else
     {
      return 0; // Not enough data for the lookback period
     }

#undef ¤ #undef _functionInstances
  }

//----------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------


Appreciate any help with regards to this. Thanks in advance. :)

 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

You may find this function useful:

double calc_ema(double &in[], double &out[], int period, int index)
{
   double alpha=2.0/(1+period);
   out[index]=out[index+1]*(1-alpha)+inp[index]*alpha;
   return out[index];
}
 

Forum on trading, automated trading systems and testing trading strategies

Moving Average of custom indicator

Fernando Carreiro, 2023.10.03 01:39

There are many example indicators in the CodeBase, many of which incorporate moving averages. Study them and apply the code to your own requirements.

Here are some examples of my own which incorporate EMA's.

Code Base

Exponential Commodity Channel Index

Fernando Carreiro, 2023.04.16 04:58

Commodity Channel Index using exponential moving averages.

Code Base

Wilder's Average True Range (ATR)

Fernando Carreiro, 2023.01.22 13:25

An implementation of the original Average True Range indicator by John Welles Wilder Jr. as described in his book—New Concepts in Technical Trading Systems [1978].

Code Base

Wilder's Relative Strength Index

Fernando Carreiro, 2023.01.22 18:12

An implementation of the Relative Strength Index indicator by John Welles Wilder Jr. as described in his book—New Concepts in Technical Trading Systems [1978].

Code Base

Exponential Range Average & Deviation Offset

Fernando Carreiro, 2022.06.25 16:02

An exponential moving average of the true range and the offset of its average deviation

Code Base

Time Segmented Volume (TSV)

Fernando Carreiro, 2023.01.01 16:40

Based on the original “Time Segmented Volume (TSV)” developed by Worden Brothers, Inc.

Code Base

Dōteki Heikin Ashi (Dynamic Average Foot/Bar)

Fernando Carreiro, 2018.10.15 17:33

A dynamic version of the standard Heikin Ashi indicator (code compatible with both MQL4 or MQL5).
 
The MovingAverages.mqh file can provide the EMA of any data. So, if I understood you correctly, your solution would involve using it.
 

many options, like what has been discussed...although I will say that once you start using buffers set as time series, the functions for MA need to be constructed differently, and that's where it starts getting confusing...but in this case buffers are not set as series so it will be straightforward