Use one custom indicator output in another custom indicator

 

I have two custom indicators. I wish to use output of one custom indicator in  another custom indicator. However I am unable to do so. I wish to achieve this functionality in code it self.

What changes I need to make in the code below:

1st Custom Indicator:

SummationAverage:

//+------------------------------------------------------------------+

//|                                             SummationAverage.mq5 |

//|                                  Copyright 2021, MetaQuotes Ltd. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2021, MetaQuotes Ltd."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_plots   1

//--- plot Label1

#property indicator_label1  "Label1"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrYellow

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input int      Inp_Number_Bars   = 15;  // Number of bars


//--- indicator buffers

double         ForceBuffer[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ForceBuffer,INDICATOR_DATA);

   

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

//---

    if(rates_total<Inp_Number_Bars-1)

      return(0);// not enough bars for calculation

//--- main loop

   int i=0,limit=0;

//--- first calculation or number of bars was changed

   if(prev_calculated==0)// first calculation

     {

      ArrayInitialize(ForceBuffer,0.0);

      limit=Inp_Number_Bars;

      //---

      double firstValue=0.0;

      for(i=0; i<limit; i++)

         ForceBuffer[i]=0.0;

     }

   else

      limit=prev_calculated-1;

       for(i=limit; i<rates_total && !IsStopped(); i++)

     {

      double firstValue=0.0;

      for(int j=i-Inp_Number_Bars+1; j<=i; j++)

         firstValue+=price[j];



      ForceBuffer[i]=firstValue;

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+
2nd indicator which gets the output of first

EMA Trend:

//+------------------------------------------------------------------+

//|                                                    EMA_Trend.mq5 |

//|                                  Copyright 2021, MetaQuotes Ltd. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#include <MovingAverages.mqh>

#property copyright "Copyright 2021, MetaQuotes Ltd."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots   1

//--- plot Trend

#property indicator_label1  "Trend"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input int      n=300;

input int      trend=250;

input ENUM_MA_METHOD InpMethod = MODE_EMA;

//--- indicator buffers

double         TrendBuffer[];

double         EMATempBuffer[];

double         BooleanBuffer[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,TrendBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,EMATempBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(2,BooleanBuffer,INDICATOR_CALCULATIONS);

//---

   //--- bar, starting from which the indicator is drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,trend);

   string shortname;

   StringConcatenate(shortname,"EMA_Trend(",n,",",trend,")");

//--- set a label do display in DataWindow

   PlotIndexSetString(0,PLOT_LABEL,shortname);   

//--- set a name to show in a separate sub-window or a pop-up help

   IndicatorSetString(INDICATOR_SHORTNAME,shortname);

//--- set accuracy of displaying the indicator values

   IndicatorSetInteger(INDICATOR_DIGITS,5);

   //handle = iCustom(NULL,0,"SummationAverage",trend,2,0);

      return(0);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

//---

   int limit = rates_total - prev_calculated;

   int period=0;

   if (prev_calculated>0) limit++;

   else {EMATempBuffer[0]=0.0;BooleanBuffer[0]=0.0;} 

   ExponentialMAOnBuffer(rates_total,prev_calculated,

                         1,n,price,EMATempBuffer);

   for (int i = limit-1; i>=0;i--){

      if (price[i]>EMATempBuffer[i])

         BooleanBuffer[i]=-1;

      else

         BooleanBuffer[i]=1;

   }

   int to_copy;

   if(prev_calculated>rates_total || prev_calculated<=0) to_copy=rates_total;

   else

     {

      to_copy=rates_total-prev_calculated;

      //--- last value is always copied

      to_copy++;

     }

   ArraySetAsSeries(TrendBuffer, true);

   //CopyBuffer(handle,0,0,to_copy,TrendBuffer);

   return(rates_total);

  }

//+------------------------------------------------------------------+

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2021.05.08
  • 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
 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 
  1. code button.
  2. input ENUM_INDICATOR InpMAMethod;
    ⋮
             firstValue+=InpMAMethod[j];
    Do not post code that will not even compile.
 

William Roeder:

  1. code button.
  2. Do not post code that will not even compile.

I have fixed the mistake apologies for the error. I was trying many things, forgot to paste the one which compiles.
 
Never mind i found the answer
Switch the order of indicators instead of using output of 1 in 2, I used output of 2 in 1
Reason: