2 indicator time frames on one chart

 

Couls someone kindly help me to be able to say on an M15 chart to have 2 say stochastics or MACD windows, one obviously M15 and the other for timeframe H1 or H4 etc. Thanks

 

You'll need to write code rather than using standard indicators. The following code displays ATR in bands for multiple timeframes. e.g. you can display H1 or H4 ATR info in M15 chart. Note that H1 would be AtrTimeFrame=60.

Note that the values display like a staircase, which shows that it's working.

I know that it isn't the indicator you want, but hopefully you'll see the principles for displaying indicators for longer timeframes.

(I've fudged it to work without my personal library functions needing to be included separately)

//+------------------------------------------+
//|                       BREW_ATR_Bands.mq4 |
//+------------------------------------------+
int BarIxOfStartOfPeriod(datetime pDateTime, int pPeriodLenInMins)
{
   int res;
   datetime barStartTime = StartOfPeriod(pDateTime, pPeriodLenInMins);
   res = iBarShift(NULL, pPeriodLenInMins, pDateTime, false);
   return(res);
}
datetime StartOfPeriod(datetime pDateTime, int pPeriodLenInMins)
{
   if(pPeriodLenInMins==0)
      pPeriodLenInMins=Period();
   int secs = pPeriodLenInMins * 60;
   switch(pPeriodLenInMins)
   {
      case PERIOD_W1:   // return Sunday Morning
         return (StartOfWeekSundayMorning(pDateTime));
      case PERIOD_MN1:  // return 1st of month
         return (StartOfDay(pDateTime) - (TimeDay(pDateTime)-1) * 86400);
      default:
         return ((pDateTime / secs) * secs);
   }
}
datetime StartOfDay(datetime pDateTime)
{
   return ((pDateTime / 86400) * 86400);
}
datetime StartOfWeekSundayMorning(datetime pDateTime)
{
   return (((pDateTime - 86400 * 3) / (86400*7)) * 86400 * 7 + 86400 * 3);
}

//#include <LibUtil.mqh>
#property  copyright "Copyright © 2010, Brewmanz"
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 DarkSlateBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_color2 SlateBlue
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
#property indicator_color3 MediumSlateBlue
#property indicator_style3 STYLE_DOT
#property indicator_width3 1
#property indicator_color4 CornflowerBlue
#property indicator_style4 STYLE_DOT
#property indicator_width4 1
#property indicator_color5 SlateBlue
#property indicator_style5 STYLE_DOT
#property indicator_width5 1
#property indicator_color6 MediumSlateBlue
#property indicator_style6 STYLE_DOT
#property indicator_width6 1
#property indicator_color7 CornflowerBlue
#property indicator_style7 STYLE_DOT
#property indicator_width7 1
//---- input parameters
extern int MaPeriod=18;
extern int StepSizePercent=50;
extern int Price_0COHLMedTypWgt6=PRICE_CLOSE;
extern int MaMode_0SmaEmaSmmaLwma3=MODE_EMA;
extern int AtrBars=100;
extern int AtrTimeFrame=0;
int AtrTimeFrameActual;
//---- display buffers
double MaBuff[];
double MaP1Buff[];
double MaP2Buff[];
double MaP3Buff[];
double MaM1Buff[];
double MaM2Buff[];
double MaM3Buff[];
double AtrValBuff[];
//---- work buffers
//double TempBuffer1[];
//double TempBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   string short_name;
   IndicatorBuffers(8);
   IndicatorDigits(Digits);
   AtrTimeFrameActual = AtrTimeFrame;
   if(AtrTimeFrameActual == 0)
      AtrTimeFrameActual = Period();
//---- indicator line
   SetIndexStyle( 0,DRAW_LINE, indicator_style1, indicator_width1, indicator_color1);
   SetIndexBuffer(0,MaBuff);
   SetIndexStyle( 1,DRAW_LINE, indicator_style2, indicator_width2, indicator_color2);
   SetIndexBuffer(1,MaP1Buff);
   SetIndexStyle( 2,DRAW_LINE, indicator_style3, indicator_width3, indicator_color3);
   SetIndexBuffer(2,MaP2Buff);
   SetIndexStyle( 3,DRAW_LINE, indicator_style4, indicator_width4, indicator_color4);
   SetIndexBuffer(3,MaP3Buff);
   SetIndexStyle( 4,DRAW_LINE, indicator_style5, indicator_width5, indicator_color5);
   SetIndexBuffer(4,MaM1Buff);
   SetIndexStyle( 5,DRAW_LINE, indicator_style6, indicator_width6, indicator_color6);
   SetIndexBuffer(5,MaM2Buff);
   SetIndexStyle( 6,DRAW_LINE, indicator_style7, indicator_width7, indicator_color7);
   SetIndexBuffer(6,MaM3Buff);
   SetIndexStyle( 7,DRAW_NONE);
   SetIndexBuffer(7,AtrValBuff);
//---- name for DataWindow and indicator subwindow label
   short_name="ATRBands(";//+AtrPeriod+","+MaPeriod+","+StepSizePercent+"%)";
   IndicatorShortName("BREW "+short_name);
   Print("WindowExpertName()=", WindowExpertName());
//   string tf = MinsToTimeFrame(AtrTimeFrameActual);
//   string maType = MaModeAsString(MaMode_0SmaEmaSmmaLwma3);
//   string priceType = PriceTypeAsShortString(Price_0COHLMedTypWgt6);
//   string maLabel = tf+maType+MaPeriod+"_"+priceType;
   string maLabel = MaPeriod;
   SetIndexLabel(0,maLabel);
   maLabel = "MA"+MaPeriod;
   SetIndexLabel(1,maLabel + "_P" + 1 * StepSizePercent + "%");
   SetIndexLabel(2,maLabel + "_P" + 2 * StepSizePercent + "%");
   SetIndexLabel(3,maLabel + "_P" + 3 * StepSizePercent + "%");
   SetIndexLabel(4,maLabel + "_M" + 1 * StepSizePercent + "%");
   SetIndexLabel(5,maLabel + "_M" + 2 * StepSizePercent + "%");
   SetIndexLabel(6,maLabel + "_M" + 3 * StepSizePercent + "%");
   //SetIndexLabel(7,tf+"ATR"+AtrBars);
   SetIndexLabel(7,"ATR"+AtrBars);
//----
   SetIndexDrawBegin(0,MaPeriod);
   SetIndexDrawBegin(1,MaPeriod);
   SetIndexDrawBegin(2,MaPeriod);
   SetIndexDrawBegin(3,MaPeriod);
   SetIndexDrawBegin(4,MaPeriod);
   SetIndexDrawBegin(5,MaPeriod);
   SetIndexDrawBegin(6,MaPeriod);
//----
   return(0);
}
//+------------------------------------------------------------------+
int start()
{
   int ix,ixLimit,counted_bars=IndicatorCounted();
//----
   if(counted_bars>0)counted_bars--;
   ixLimit = Bars-counted_bars-1;
//----
   for(ix=ixLimit;ix>=0;ix--)
   {
      static double atr, atrStep;
      static datetime NewBarTimeCheck = 0;
      bool IsNewBar = false;
      if(NewBarTimeCheck != Time[ix])
      {  
         IsNewBar = true;
         NewBarTimeCheck = Time[ix];
      }
      if(IsNewBar)
      {
         int ixTF = BarIxOfStartOfPeriod(Time[ix], AtrTimeFrameActual); 
         atr = iATR(NULL, AtrTimeFrameActual, AtrBars, ixTF);
         atrStep = atr * StepSizePercent / 100;
         if(atr==0)
            atr=EMPTY_VALUE;
      }
      double ma = iMA(NULL, AtrTimeFrameActual, MaPeriod, 0, MaMode_0SmaEmaSmmaLwma3, Price_0COHLMedTypWgt6, ixTF);
      // DEBUGdouble ma = iMA(NULL, AtrTimeFrameActual, MaPeriod, 0, MaMode_0SmaEmaSmmaLwma3, Price_0COHLMedTypWgt6, ixTF);
      MaBuff[ix] = ma;
      if(atr != EMPTY_VALUE && ma != EMPTY_VALUE)
      {
         MaP1Buff[ix] = ma + 1 * atrStep;
         MaP2Buff[ix] = ma + 2 * atrStep;
         MaP3Buff[ix] = ma + 3 * atrStep;
         MaM1Buff[ix] = ma - 1 * atrStep;
         MaM2Buff[ix] = ma - 2 * atrStep;
         MaM3Buff[ix] = ma - 3 * atrStep;
      }
      else
      {
         MaP1Buff[ix] = EMPTY_VALUE;
         MaP2Buff[ix] = EMPTY_VALUE;
         MaP3Buff[ix] = EMPTY_VALUE;
         MaM1Buff[ix] = EMPTY_VALUE;
         MaM2Buff[ix] = EMPTY_VALUE;
         MaM3Buff[ix] = EMPTY_VALUE;
      }
      AtrValBuff[ix] = atr;
   }
//----
   return(0);
}
//+------------------------------------------------------------------+
Reason: