displaying higher TF data properly on lower TF

 

Hello all,

I am new to mql4 but have had some programming experience elsewhere.

I am using these calculations to describe trend:

price above BOTH ema200 and ema50 = uptrend.

price below BOTH emas = downtrend.

I am displaying a bar at the bottom of the screen to indicate if the above conditions are happening, like so:

sample

While I thought I would be able to calculate this by simply using an array for both emas, I found I had to assign both to index buffers for this to work, I sadly don't know why this is so!

What I would like to do is display the bars of a higher timeframe, such as daily or 4H, on a lower TF chart like the 1H. Meaning, I would like to be able to see if my conditions for trend are being met on the DAILY, while displaying a 1H chart.

While I may change the second parameter of iMA to 1440 (DAILY) to calculate the emas properly, all I can do is simply map them to the lower TF data one-to-one, ie NOT displaying properly (naturally!).

I was hoping someone could point to a solution as I seem to be unable to find it! I am guessing iBarShift may have something to do with it.

thank you in advance,

Aristides.

here's my code so far:

#property description "c>ema(50) and c>ema(200) is an uptrend"
#property description "c<ema(50) and c<ema(200) is a downtrend"
#property indicator_separate_window
#property indicator_minimum 0.0
#property indicator_maximum 0.1
#property indicator_buffers 4       // Number of buffers
#property indicator_color1 Green     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line

extern int per=PERIOD_CURRENT;

double Buf_0[],Buf_1[];             // Declaring arrays (for indicator buffers)
double h50[],h200[];
//--------------------------------------------------------------------
int init()                          // Special function init()
  {
   SetIndexBuffer(0,Buf_0);       
   SetIndexStyle (0,DRAW_HISTOGRAM, STYLE_SOLID, 4);
   SetIndexBuffer(1,Buf_1);       
   SetIndexStyle (1,DRAW_HISTOGRAM, STYLE_SOLID, 4);
   
   //forced to use buffers for the EMAs, cant seee any other way!
   

   SetIndexBuffer(2, h50);
   SetIndexStyle(2, 0, STYLE_SOLID, 4);   
   SetIndexBuffer(3, h200);
   SetIndexStyle(3, 0, STYLE_SOLID, 4);   


   return(0);                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int i,                           // Bar index
       Counted_bars,                // Number of counted bars
       newbar;
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
     
   h50[i]=iMA(NULL,per,50,0,MODE_EMA,PRICE_CLOSE,i);
   h200[i]=iMA(NULL,per,200,0,MODE_EMA,PRICE_CLOSE,i);

     
   //now bring forth the appropriate bar 
   
   //no trend for now 
   Buf_0[i] = EMPTY_VALUE;
   Buf_1[i] = EMPTY_VALUE;
     
     newbar=i;
     //newbar=iBarShift(NULL,per,Time[i],FALSE) ;
     
      if (iClose(NULL,per,newbar)<h50[newbar] && iClose(NULL,per,newbar)<h200[newbar])
      {
         //down
         Buf_0[i] = EMPTY_VALUE;
         Buf_1[i] = 1;
         }   
   
        
      if (iClose(NULL,per,newbar)>h50[newbar] && iClose(NULL,per,newbar)>h200[newbar]) 
      {
         //up
         Buf_1[i] = EMPTY_VALUE;
         Buf_0[i] = 1;
         }      
      
      i--;                          // Calculating index of the next bar
   }
//--------------------------------------------------------------------
   return(0);                          // Exit the special funct. start()
  }
 
aristid:

While I thought I would be able to calculate this by simply using an array for both emas, I found I had to assign both to index buffers for this to work, I sadly don't know why this is so!

What I would like to do is display the bars of a higher timeframe, such as daily or 4H, on a lower TF chart like the 1H. Meaning, I would like to be able to see if my conditions for trend are being met on the DAILY, while displaying a 1H chart.

  1. You had to make them buffers, so they would auto resize and were set as series. There was no need to use arrays at all, you could have just gotten the values and stored the result in your two buffers.
  2.    while(i>=0)                      // Loop for uncounted bars
         {
         
       h50[i]=iMA(NULL,per,50,0,MODE_EMA,PRICE_CLOSE,i);
    
    i is the index to your chart. If you are on the M1, a value of 1 is one minute ago. But if per is D1, a value of 1 is yesterday. Apples and oranges. You MUST convert your chart's index to the corresponding per chart index (iBarShift) before trying to get per chart values.
  3.    Counted_bars=IndicatorCounted(); // Number of counted bars
       i=Bars-Counted_bars-1;           // Index of the first uncounted
    
    IndicatorCounted doesn't know anything about per. So initially (assuming you fixed #2) you will get a stair step values from the higher timeframe. But after that, you will be processing just chart bar zero and per bar zero, no stair steps.
  4.    i=Bars-Counted_bars-1;           // Index of the first uncounted
       while(i>=0)                      // Loop for uncounted bars
         {
         
       h50[i]=iMA(NULL,per,50,0,MODE_EMA,PRICE_CLOSE,i);
       h200[i]=iMA(NULL,per,200,0,MODE_EMA,PRICE_CLOSE,i);
    
    When per is current chart (or zero) iMA has to look back 200 bars to calculate your h200, but you start drawing at Bars-1. Handle look back.
  5. iClose(NULL,per,newbar)
    Don't you want your charts price compared to per MAs?
   Counted_bars=IndicatorCounted(); // Number of counted bars
   
   // Handle lookback of 200 of per
   if(Counted_bars < 200 * per / _Period) Counted_bars = 200 * per / _Period;
   i=Bars-Counted_bars-1;           // Index of the first uncounted

   // Recalculate all bars using per's bar zero.
   datetime Time0Per = iTime(NULL, per, 0);
   if(Time[i] > Time0Per)  i = iBarShift(NULL,0, Time0Per);

   while(i>=0)                      // Loop for uncounted bars
     {
      Buf_0[i] = EMPTY_VALUE;
      Buf_1[i] = EMPTY_VALUE;

      int iPer = iBarShift(NULL, per, Time[i]);
      double   h50=iMA(NULL,per,50,0,MODE_EMA,PRICE_CLOSE,iPer),
               h200=iMA(NULL,per,200,0,MODE_EMA,PRICE_CLOSE,iPer);

      if (Close[i]< MathMin(h50,h200)) Buf_1[i] = 1;
      if (Close[i]> MathMax(h50,h200)) Buf_0[i] = 1;
      
      i--;                          // Calculating index of the next bar
   }
 
Dear WHRoeder, I wholeheartedly thank you for you very kind, precise, valuable response. Your points were very helpful. It seems there is a bit of trouble accessing daily from 15min or even from 1h at some points. (all bars appear white). Maybe they are too far apart and I need to force some history downloading? I am not sure how this goes. Accessing the closest timeframes works beautifully. (daily data from a 4h chart for example). Thanks once again, Aristides.