How to intercept applied_price?

 

I'm experimenting on indicators and I wrote a simple SMA using iMA function, it works well just for program fixed applied_price (PRICE_CLOSE).

//+------------------------------------------------------------------+
//|                                                       SMA_03.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//---- plot MA
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int                period=13;
input int                shift=0;
//--- indicator buffers
double                   MABuffer[];
//--- indicator handle
int                      ma_handle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
   PlotIndexSetString(0,PLOT_LABEL,"SMA("+(string)period+")");
//---
   ma_handle=iMA(Symbol(),0,period,shift,MODE_SMA,PRICE_CLOSE);    // <================================
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   if(BarsCalculated(ma_handle)<rates_total) return(0);
   int to_copy;
   if(prev_calculated==0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      //--- last value is always copied
      to_copy++;
     }
   if(CopyBuffer(ma_handle,0,0,to_copy,MABuffer)<=0) return(0);   
   return(rates_total);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(ma_handle);
  }
//+------------------------------------------------------------------+

I didn't found a way to intercept and use the starting program selection "Parameters - Apply to:" instead, is it possible?

Thanks.
 
Nicola Grippaldi:

I'm experimenting on indicators and I wrote a simple SMA using iMA function, it works well just for program fixed applied_price (PRICE_CLOSE).

I didn't found a way to intercept and use the starting program selection "Parameters - Apply to:" instead, is it possible?

Thanks.
Apply to what ?
 

I intend this kind of selection https://c.mql5.com/2/0/price_for_calculations.png

and set corresponding value as applied_price parameter in iMA.

ma_handle=iMA(Symbol(),0,period,shift,MODE_SMA,value);
 
Nicola Grippaldi:

I intend this kind of selection https://c.mql5.com/2/0/price_for_calculations.png

and set corresponding value as applied_price parameter in iMA.

I know what's your intention. But I asked you on what values you want to apply iMA.

Instead of price, you can use an handle of an other indicator.

 

Yes! this works very well indeed

   ema_handle=iMA(Symbol(),0,ema_period,0,MODE_EMA,PRICE_CLOSE);  
   sma_handle=iMA(Symbol(),0,sma_period,sma_shift,MODE_SMA,ema_handle);  

but now I would like to start the chain, I would like to apply my indicator to any other opened indicator on the graph, just like an usual indicator having  a single data buffer OnCalculate

 
Nicola Grippaldi:

Yes! this works very well indeed

but now I would like to start the chain, I would like to apply my indicator to any other opened indicator on the graph, just like an usual indicator having  a single data buffer OnCalculate

So what ?
 
"Previous Indicator's Data"
 
Nicola Grippaldi:
"Previous Indicator's Data"
Already answered, get an handle to the indicator you want and use it instead of price. Please do some efforts and read the documentation: ChartIndicatorGet().
 
Alain Verleyen:
Already answered, get an handle to the indicator you want and use it instead of price. Please do some efforts and read the documentation: ChartIndicatorGet().

oops

thanks

 
Very good, ChartIndicatorGet() works fine.
I've defined an enumeration and an input field to select "close", "open", "previous indicator's data" ecc... and build the handle for passing it to iMA.

I dislike only the duplicated input structure and the fake functionality for "Parameters - Apply to:", but no matter.

PS: Solved, I use multiple data buffer form OnCalculate

Reason: