Download MetaTrader 5

2 instances of an indicator, 2 different periods, one chart...?

To add comments, please log in or register
Do you know that MQL5 has structures?
mcai4sh3
37
mcai4sh3 2010.12.09 18:03 

Hi,

 

 

I have a custom indicator, which currently plots onto a chart by using _Period.

 What I want to do is have one instance of the indicator calculated on PERIOD_H1 and another instance of the indicator plotted on PERIOD_D1. 

 

I want both indicators to be shown on a 1hour chart. As you would expect the PERIOD_H1 indicators works on the 1hour chart but the PERIOD_D1 indicator does not work on the hour chart.

 

The idea is that I want the PERIOD_D1 indicator to be a trend filter on a hour chart...?

 

Any help.

 

thanks 

Step on New Rails: Custom Indicators in MQL5
  • 2009.11.23
  • Андрей
  • www.mql5.com
I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.
mcai4sh3
37
mcai4sh3 2010.12.09 18:44  

So my question is:

 

IS there a dependecy between the chart\chart period and the onCalculate method... i.e. is it possible to get PERIOD_D1 bar values from within the onCalculate method even though our curent chart is set to PERIOD_H1 ?

 

Thanks 

Pawel Wojnarowski
685
Pawel Wojnarowski 2010.12.09 21:24  

Have you used iCustom?

 

iCustom

The function returns the handle of a specified custom indicator.

int  iCustom(
   string           symbol,     // symbol name
   ENUM_TIMEFRAMES  period,     // period
   string           name        // folder/custom indicator_name
   ...                          // list of indicator input parameters
   );

 Here's an example use of iCustom  from MQL5 reference:

 

Example:

#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  Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int MA_Period=21;
input int MA_Shift=0;
input ENUM_MA_METHOD MA_Method=MODE_SMA;
//--- indicator buffers
double         Label1Buffer[];
//--- Handle of the Custom Moving Average.mq5 custom indicator
int MA_handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   ResetLastError();
   MA_handle=iCustom(NULL,0,"Custom Moving Average",
                     MA_Period,
                     MA_Shift,
                     MA_Method,
                     PRICE_CLOSE // using the close prices
                     );
   Print("MA_handle = ",MA_handle,"  error = ",GetLastError());
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//--- Copy the values of the indicator Custom Moving Average to our indicator buffer
   int copy=CopyBuffer(MA_handle,0,0,rates_total,Label1Buffer);
   Print("copy = ",copy,"    rates_total = ",rates_total);
//--- If our attempt has failed - Report this
   if(copy<=0)
      Print("An attempt to get the values if Custom Moving Average has failed");
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

 

mcai4sh3
37
mcai4sh3 2010.12.13 15:44  

HI,

 

 

Yes I have used iCustom. I dont think I have explained the problem well....

If I use iCustom(_Symbol, PERIOD_D1, "INDICATOR");  then this will return a handle on the indicator values for DAYS i.e. one indicator value for one day i.e. one bar will represent one day which will return one indicator value - OK. 

Therefore on these terms the last 4 bars will be equal to the last four days - OK.

 

Now what I want to do is plot these DAY-values on an HOURLY chart.

This is where the problem comes in....

24bars on a hourly chart is equal to one day, so I expect MetaTrader to understand that the indicator value of one DAY bar should be equal to 24bars on an hourly chart.

This is not what happens, rather MetaTrader is unable to understand this and plots for example, 1st DAY bar indicator value onto 1st HOUR bar, 2nd DAY bar indicator value onto 2nd HOUR BAR, 3rd DAY bar indicator value onto 3rd HOUR BAR. This is incorrect.  

 

What I expect to happen is:

0-24 HOURLY BARS should have the 1st DAY indicator Value.

the next...

24-48 HOURLY BARS should have the 2nd DAY indicator Value. 

 

Hope you understand this.  

Documentation on MQL5: Technical Indicators / iCustom
  • www.mql5.com
Technical Indicators / iCustom - Documentation on MQL5
Abraham
89
Abraham 2010.12.16 03:27  
I have the same problem in another context, it's a problem with metatrader. If no-one solves the problem here for you, put into google 'Multi timeframe metatrader". It seems to me that some programming has been done in version 4, but I can't find any in version 5.
investeo
4098
investeo 2010.12.16 09:18  
mcai4sh3:

HI,

 

 

Yes I have used iCustom. I dont think I have explained the problem well....

If I use iCustom(_Symbol, PERIOD_D1, "INDICATOR");  then this will return a handle on the indicator values for DAYS i.e. one indicator value for one day i.e. one bar will represent one day which will return one indicator value - OK. 

Therefore on these terms the last 4 bars will be equal to the last four days - OK.

 

Now what I want to do is plot these DAY-values on an HOURLY chart.

This is where the problem comes in....

24bars on a hourly chart is equal to one day, so I expect MetaTrader to understand that the indicator value of one DAY bar should be equal to 24bars on an hourly chart.

This is not what happens, rather MetaTrader is unable to understand this and plots for example, 1st DAY bar indicator value onto 1st HOUR bar, 2nd DAY bar indicator value onto 2nd HOUR BAR, 3rd DAY bar indicator value onto 3rd HOUR BAR. This is incorrect.  

 

What I expect to happen is:

0-24 HOURLY BARS should have the 1st DAY indicator Value.

the next...

24-48 HOURLY BARS should have the 2nd DAY indicator Value. 

 

Hope you understand this.  

 

 

 

 

 


This is already implemented, https://www.mql5.com/en/code/177

Stochastic multi-timeframe [v04]
  • votes: 12
  • 2010.08.25
  • Armand Kilian | English Russian Spanish Portuguese
  • www.mql5.com
Stochastic indicator, can be applied to any timeframe (higher or lower than the current chart's timeframe).
To add comments, please log in or register