Problems with observating different timeframes

 

Hello coders,

I would like to program a very simple indicator which displays an alert when the last bar's low is above the SMA20 and the space between the bar's low and the SMA20 must be smaller then the bar's size. Until now that is no problem. But the indicator should check this not only for the current timeframe but for 2 more. I look at the EUR/USD in H1 chart and the indicator should check the M15 and M5 chart if these criteria are also met.

The iMA() can be used in other timeframes but is it somehow possible to check the bar's high or low in other timeframes? I have no idea if this is possible and would be very thankful if anybody can give me some advice.

Thank you!

 
Yes, with iHigh and iLow
 
Great, thanks! I will try!
 

Obviously it is much more difficult than I thought... The alert is only displayed when the chart is opened in the corresponding timeframe. Here I tried it with the M1 chart to get faster results. Whatever I try, it only works in the timeframe the chart is open.

Maybe anybody could have a look at the code and help me?

//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property indicator_chart_window

double alertTag, SMA10, Lo, Hi;
int TF;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   TF = 1;
   SMA10 = iMA(NULL, TF, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
   Lo = iLow(NULL, TF, 1);
   Hi = iHigh(NULL, TF, 1);
   if (alertTag!=Time[0] && Lo>SMA10)
   {
      Alert("Bar above SMA10 | ",Symbol()," M",TF);
      alertTag=Time[0];
   }
   if (alertTag!=Time[0] && Hi<SMA10)
   {
      Alert("Bar below SMA10 | ",Symbol()," M",TF);
      alertTag=Time[0];
   }
   
   TF = 5;
   SMA10 = iMA(NULL, TF, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
   Lo = iLow(NULL, TF, 1);
   Hi = iHigh(NULL, TF, 1);
   if (alertTag!=Time[0] && Lo>SMA10)
   {
      Alert("Bar above SMA10 | ",Symbol()," M",TF);
      alertTag=Time[0];
   }
   if (alertTag!=Time[0] && Hi<SMA10)
   {
      Alert("Bar below SMA10 | ",Symbol()," M",TF);
      alertTag=Time[0];
   }
   
   TF = 15;
   SMA10 = iMA(NULL, TF, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
   Lo = iLow(NULL, TF, 1);
   Hi = iHigh(NULL, TF, 1);
   if (alertTag!=Time[0] && Lo>SMA10)
   {
      Alert("Bar above SMA10 | ",Symbol()," M",TF);
      alertTag=Time[0];
   }
   if (alertTag!=Time[0] && Hi<SMA10)
   {
      Alert("Bar below SMA10 | ",Symbol()," M",TF);
      alertTag=Time[0];
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

When you check the 1 minute TF and send an alert you then

alertTag=Time[0];

Alerts in higher TF that follow in the code include the condition

if (alertTag!=Time[0] && ........)

and that cannot be true

 

PS: I noticed something: When the M5 is opened, the M1-Alert is only displayed once in a 5-minute-period. And the last M5-bar was the first one which low was above the SMA10. Unfortunately there was no alert. There was just an alert for the M1 chart. So I think if the M15 chart is opened, the M1-Alert would only displayed once every 15 minutes. M1-check is the first in my code and therefore I think M5 and M15 is not being controlled because I did not get an M5 Alert.

But why?? Help is sooo needed and appreciated!

 

@GumRai: Yes, I understand! But how can this be solved? I added this alertTag because I don't want to get a alert with every tick. How would you solve that problem? Just remove it?


And do you have an idea why there was no M5-Alert? Does the indicator works this way: when there is an M1-Alert, M5 and M15 are not checked?


EDIT: I changed Time[0] to iTime(NULL, TF, 0)

Maybe that works..

EDIT 2: the timeframes work perfect now. Even if I open the H4 chart, I get the alerts for M1,M5,M15. But the problem now is that the alerts are displayed with every tick. And I have no idea how to fix it.

Here is the current code:

//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property indicator_chart_window

double alertTag, SMA10, Lo, Hi;
int TF;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   TF = 1;
   SMA10 = iMA(NULL, TF, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
   Lo = iLow(NULL, TF, 1);
   Hi = iHigh(NULL, TF, 1);
   if (alertTag!=iTime(NULL, TF, 0) && Lo>SMA10)
   {
      Alert("Bar above SMA10 | ",Symbol()," M",TF);
      alertTag=iTime(NULL, TF, 0);
   }
   if (alertTag!=iTime(NULL, TF, 0) && Hi<SMA10)
   {
      Alert("Bar below SMA10 | ",Symbol()," M",TF);
      alertTag=iTime(NULL, TF, 0);
   }
   
   TF = 5;
   SMA10 = iMA(NULL, TF, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
   Lo = iLow(NULL, TF, 1);
   Hi = iHigh(NULL, TF, 1);
   if (alertTag!=iTime(NULL, TF, 0) && Lo>SMA10)
   {
      Alert("Bar above SMA10 | ",Symbol()," M",TF);
      alertTag=iTime(NULL, TF, 0);
   }
   if (alertTag!=iTime(NULL, TF, 0) && Hi<SMA10)
   {
      Alert("Bar below SMA10 | ",Symbol()," M",TF);
      alertTag=iTime(NULL, TF, 0);
   }
   
   TF = 15;
   SMA10 = iMA(NULL, TF, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
   Lo = iLow(NULL, TF, 1);
   Hi = iHigh(NULL, TF, 1);
   if (alertTag!=iTime(NULL, TF, 0) && Lo>SMA10)
   {
      Alert("Bar above SMA10 | ",Symbol()," M",TF);
      alertTag=iTime(NULL, TF, 0);
   }
   if (alertTag!=iTime(NULL, TF, 0) && Hi<SMA10)
   {
      Alert("Bar below SMA10 | ",Symbol()," M",TF);
      alertTag=iTime(NULL, TF, 0);
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
mar:

@GumRai: Yes, I understand! But how can this be solved? I added this alertTag because I don't want to get a alert with every tick. How would you solve that problem? Just remove it?


And do you have an idea why there was no M5-Alert? Does the indicator works this way: when there is an M1-Alert, M5 and M15 are not checked?


EDIT: I changed Time[0] to iTime(NULL, TF, 0)

Maybe that works..

No, it doesn't work :(


If you need an alert when conditions are met in x time-frames, then you check x timeframes and if all are true, THEN you send the alert
 

Ok, I changed this line

if (alertTag!=iTime(NULL, TF, 0) && Lo>SMA10)

to this:

if (alertTag!=iTime(NULL, 1, 0) && alertTag!=iTime(NULL, 5, 0) && alertTag!=iTime(NULL, 15, 0) && Lo>SMA10)
But unfortunately that also doesn't work. I understand your idea but I don't know what is the correct syntax.
Reason: