Calculate technical indicator on custom array

 

Hello.

I'd like to calculate a technical indicator in MQL5 based on the custom values array, i.e. an analogue of the iRSIOnArray(), iBandsOnArray(),etc. I could not find any info about how to do it and the blocks in the main article about migration to MQL5  are blank regarding these functions in older MQL4. The only solution that I could find was the MovingAverages.mqh in the Include that has some "OnArray" functions that represent full copied calculation of the technical MA indicators which is quite an ineffective solution code\lengthwise if the needed technical indicator is not that simple as MA.

Are there any other ways to achieve the goal?

Thank you.

 

It's not clear if the indicator you want to calculate is yours and available in code and how the custom array is provided. Take a look at the function OnCalculate at its short form:

int OnCalculate(const int rates_total,      // size of the price[] array 
                const int prev_calculated,  // bars handled on a previous call 
                const int begin,            // where the significant data start from 
                const double& price[]       // array to calculate 
);

The parameter price can be specified in indicator's settings dialog as a custom array from other indicator. But I'm not sure this is what you're asking for.

 

May be I was not that clear in my goals. Some code and comments for clarification.

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1 "ClosePrice"
#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_color1 clrRed
#property indicator_width1 1
#property indicator_label2 "MA"
#property indicator_type2 DRAW_LINE
#property indicator_style2 STYLE_SOLID
#property indicator_color2 clrGreen
#property indicator_width2 1
double Variables[];
double MA[];
int OnInit()
  {
      IndicatorSetInteger(INDICATOR_DIGITS,5);
      SetIndexBuffer(0,Variables,INDICATOR_DATA);
      ArraySetAsSeries(Variables,true);
      //PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Bars(Symbol(),Period()));
      SetIndexBuffer(1,MA,INDICATOR_DATA);
      ArraySetAsSeries(MA,true);
      //PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,Bars(Symbol(),Period()));
   return(INIT_SUCCEEDED);
  }
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 total = rates_total - prev_calculated - 1;
      for(int i=total;i>=0;i--)
        {
            CopyClose(NULL,0,0,total,Variables);//Для примера напихаем в буффер цены закрытия любого инструмента(но в конечном варианте это будут произвольные величины)
        }
      //Теперь цель по значениям нового буффера построить, например, скользяющую среднюю
      //int handleMA = iMA(NULL,0,14,0,MODE_SMA,PRICE_CLOSE);
      //Мы пытаемся создать новый хендл, однакого среди выбранных значений у нас есть только жёстко указанные OHLC данные(PRICE_CLOSE,итд) текущего инструмента, а не созданного буффера Variables
      //Как использовать в iMA свои производные данные?
      //Как будет выглядеть конечный код, используя неизведанные мной возможности OnCalculate?
      return(rates_total);
  }
Thanks a lot.
 

If you know Russian, you could probably get more assitance on the Russian forum.

Currently, it's still hard for me to think off something helpful, because it seems that you want to calculate an abstract formula (which is obviously missing among standard ones) on an array, so why don't you do it right in the code?

 
Stanislav Korotky:

If you know Russian, you could probably get more assitance on the Russian forum.

Currently, it's still hard for me to think off something helpful, because it seems that you want to calculate an abstract formula (which is obviously missing among standard ones) on an array, so why don't you do it right in the code?

The basic idea is to calculate a STANDARD indicator based on NON STANDARD price data. But you're probably right, may be it's better to give a try on Russian forum.

 
Roman Zhitnik:

The basic idea is to calculate a STANDARD indicator based on NON STANDARD price data. But you're probably right, may be it's better to give a try on Russian forum.

What is the source of the non-standard data? If it's available as an indicator buffer, you can apply many of standard indicators to the data via UI - here is a bit ancient article which summarizes which indicators support "apply to" option, but probably there is a similar table for MT5 somewhere.

 
Stanislav Korotky:

What is the source of the non-standard data? If it's available as an indicator buffer, you can apply many of standard indicators to the data via UI - here is a bit ancient article which summarizes which indicators support "apply to" option, but probably there is a similar table for MT5 somewhere.

Yes, it's possible to make it as an indi buffer but the provided article is not quite related to the task because the goal is not to attach it in UI (it's easy to apply it to "Previous Indicator's Data" in the interface itself) but to put it into code. That's why I tried to attach it in one of my previous posts.
 
Roman Zhitnik:

The basic idea is to calculate a STANDARD indicator based on NON STANDARD price data. But you're probably right, may be it's better to give a try on Russian forum.

Most of standard indicators are available as open source in the codebase. So it looks like your last resort just to embed this code into yours. Of course this will not be exact copy of built-in indicators implemented in the terminal itself. There can be some little differences.

 
https://www.mql5.com/ru/forum/216384
Базовые индикаторы, применяемые к кастомному инструменту
Базовые индикаторы, применяемые к кастомному инструменту
  • 2017.09.27
  • www.mql5.com
Здравствуйте. Задавал этот вопрос на английском форуме...
Reason: