Download MetaTrader 5

Discussion of article "Applying One Indicator to Another"

To add comments, please log in or register
You can test an application before buying it. Find out how to do that!
MetaQuotes Software Corp.
Moderator
181266
MetaQuotes Software Corp. 2010.02.08 09:34 

New article Applying One Indicator to Another is published:

Do you want to improve an indicator for its correct application to the other indicator's data? In this article we'll review all the steps required for such modification.

Author: MetaQuotes

OldZ
53
OldZ 2010.05.12 19:06  

This article contains the detailed knowledge.

Thanks.

piptrash
4
piptrash 2010.12.16 17:44  

Hi,

This article is really interesting and open new opportunities to manage indicators.

Since I'm a beginner in MQL5, I've tried to implement the possibility to apply an indicator to another. I've written a little code which only duplicate

an adaptative moving average hoping to use it on any indicator of the chart.

Unfortunately, in the list of parameters which appears when you launch the indicator I don't have the case "apply to : previuos indicator's data".

How the code should be organised to have the possibility to apply it to another indicator ?

Here is my code:

#property indicator_separate_window
#property indicator_minimum             1.3
#property indicator_maximum             1.35
//#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot dMA
#property indicator_label1  "dAMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      AMAPeriod=9;
input int      FastEMA=2;
input int      SlowEMA=20;
//--- indicator buffers
double         AMABuffer[];
double         dAMABuffer[];

int            h_dAMA;
int            h_AMA;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,dAMABuffer,INDICATOR_DATA);
   SetIndexBuffer(1,AMABuffer,INDICATOR_CALCULATIONS);
   
   
   
   //PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,MAPeriod+1);
   
   h_AMA=iAMA(_Symbol,PERIOD_CURRENT,AMAPeriod,FastEMA,SlowEMA,0,PRICE_CLOSE);
   if(h_AMA == INVALID_HANDLE)
    {
      Print("AMA indicator initialization error, Code = ", GetLastError());
      return(-1);
    }
   ArraySetAsSeries(AMABuffer,true);
   ArraySetAsSeries(dAMABuffer,true);
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,MathMax(AMAPeriod,SlowEMA));
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    // size of the price[] array
                const int prev_calculated,// number of bars processed at the previous call
                const int begin,          // where the significant data start from
                const double &price[]     // array for calculation
                )
  {
   
   if(BarsCalculated(h_AMA)<rates_total) return(0);
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<=0) 
     {
      to_copy=rates_total;
      if(CopyBuffer(h_AMA,0,0,to_copy,AMABuffer)<=0) return(0);
      for (int i=0;i<to_copy-MathMax(AMAPeriod,SlowEMA);i++)
          {
            dAMABuffer[i]=AMABuffer[i];
          }
     }
   else
     {
      to_copy=rates_total-prev_calculated+MathMax(AMAPeriod,SlowEMA)-1;
      //--- last value is always copied
      to_copy++;
      if(CopyBuffer(h_AMA,0,0,to_copy,AMABuffer)<=0) return(0); 
      for (int i=0;i<to_copy;i++)
         {
            dAMABuffer[i]=AMABuffer[i];
         }
     }
//--- try to copy
   
   Print(dAMABuffer[0]); //to see if we go till the end
//--- return value of prev_calculated for next call
//---
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
piptrash
4
piptrash 2010.12.18 17:51  

Hi,

I've understood the problem: you are only able to apply one indicator to another which is in the same window.

Do you have an idea on how to apply one indicator (and to display it in a separate window) to another which is in the main window?

thanks

Bests


Paul
50
Paul 2012.01.07 15:54  

Hi,

This is a very good article but I have some questions. 

1.  Only the indicators that uses the short form of the OnCalculate() function call, can be applaied to another indicator or the indicators that use the long form of the OnCalculate() function call can be applied too? If so , haw can be applied one indicator with long form of the OnCalculate() function call, to another indicator???

2. Applying one indicator to another (TSI over RSI) generates some display problems (see attached image). While RSI is plotted from 0 to 100 the second indicator (TSI) have values below 0 too , values wich are not visibile on the chart. Wich is the problem and why the chart don't properly adjust to display correctly both indicators. Can we solve this problem or it's a MetaTrader bug???

Files:
RSIcTSI.png 32 kb
Alain Verleyen
Moderator
30742
Alain Verleyen 2013.07.04 17:02  
paul.necsoiu:

Hi,

This is a very good article but I have some questions. 

1.  Only the indicators that uses the short form of the OnCalculate() function call, can be applaied to another indicator or the indicators that use the long form of the OnCalculate() function call can be applied too? If so , haw can be applied one indicator with long form of the OnCalculate() function call, to another indicator???

2. Applying one indicator to another (TSI over RSI) generates some display problems (see attached image). While RSI is plotted from 0 to 100 the second indicator (TSI) have values below 0 too , values wich are not visibile on the chart. Wich is the problem and why the chart don't properly adjust to display correctly both indicators. Can we solve this problem or it's a MetaTrader bug???

1. Yes only indicator that uses the short form of OnCalculate can be applied to another indicator data.

2. You have to edit to properties of RSI indicator and change "Fixed minimum" on the Scale tab to -50 instead of 0.

churays
7
churays 2015.07.13 19:22  
I am somewhat a newbie in mql programming...my question is how do i make an indicator that checks the upper bollinger band value at a specific bar,whether at that point the bollinger band value is between the open and close of that bar after which it plots a down arrow above the current bar.. if close[1]>upperbollingvalue[1]>open[1] .. I have tried searchng around but no luck..
To add comments, please log in or register