Help with indicator return wrong value when change time frame

 

Hello Everyone,

I try to compare MA15 today and MA15 yesterday; MA15 yesterday vs MA15 the day before yesterday. Then I record that 2 values in 2 buffers.

I try two approaches:

The first one: compare, assign value, make buffer equal to that value. This needs 2 comparisons. the 2 buffers are: D1trend and D1trend_1

The second one: compare, assign value directly to a buffer. Then I get the 2nd day buffer equal to lag value of today buffer. This just needs 1 comparison. The 2 buffers are: new_indi and new_indi_1

For the first approach, the value shows up correctly for both H4, D1 windows for both buffer.

For the second approach, the lag buffer (new_indi_1) show up correctly ONLY on D1 and wrong by 5 bar "forward" on H4 chart. The current buffer (new_indi) is correct on both chart.

I think the reason is that (I+1) when on H4 chart means 1 bar on H4 and so when I change to (I+6), H4 chart is right but of course, D1 chart is clearly wrong now.


I am not sure how to correct it so correct information shows up on any chart displayed.

Could you please help me with that?

Thank you,

SCFX

//+------------------------------------------------------------------+
//|                                                     Eng_loss.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_separate_window
#property indicator_minimum -5
#property indicator_maximum 5
#property indicator_buffers 4
#property indicator_color1 White 
#property indicator_color2 White 
#property indicator_color3 Red 
#property indicator_color4 Red 

//--- buffers

double D1trend[], D1trend_1[];
double new_indi[], new_indi_1[];


extern int D1= PERIOD_D1;
extern int H4= PERIOD_H4;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

/* SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);   SetIndexBuffer(0, D1trend);   SetIndexLabel(0,"D1trend");
   SetIndexStyle(1,DRAW_NONE);   SetIndexBuffer(1,D1trend_1);   SetIndexLabel(1,"D1trend_1");
   
   SetIndexStyle(2,DRAW_ARROW);   SetIndexBuffer(2, new_indi);   SetIndexLabel(2,"new_indi");
   SetIndexStyle(3,DRAW_NONE);   SetIndexBuffer(3,new_indi_1);   SetIndexLabel(3,"new_indi_1");
 */  
   SetIndexStyle(0,DRAW_NONE);   SetIndexBuffer(0, D1trend);   SetIndexLabel(0,"D1trend");
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);   SetIndexBuffer(1,D1trend_1);   SetIndexLabel(1,"D1trend_1");
   
   SetIndexStyle(2,DRAW_NONE);   SetIndexBuffer(2, new_indi);   SetIndexLabel(2,"new_indi");
   SetIndexStyle(3,DRAW_ARROW);   SetIndexBuffer(3,new_indi_1);   SetIndexLabel(3,"new_indi_1");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
      int    counted_bars=IndicatorCounted();
      int    i;
      int    numberbars=Bars - counted_bars;
      double   bar_0,bar_1,bar_2,D1trend_value,D1trend_value_1;
      if(counted_bars<5) counted_bars=5;

//----

for(i=numberbars-1; i>=1;i--)
   {     bar_0 = iBarShift(Symbol(), D1, Time[i]);          
         bar_1 = iBarShift(Symbol(), D1, Time[i]) + 1;      
         bar_2 = iBarShift(Symbol(), D1, Time[i]) + 2;     

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//APPROACH 1
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

   if(iMA(NULL,D1,15,0,MODE_EMA,0,bar_0)>=iMA(NULL,D1,15,0,MODE_EMA,0,bar_1)) {D1trend_value=1;} else {D1trend_value=-1;}         
   if(iMA(NULL,D1,15,0,MODE_EMA,0,bar_1)>=iMA(NULL,D1,15,0,MODE_EMA,0,bar_2)) {D1trend_value_1=1;} else {D1trend_value_1=-1;}                  

   D1trend[i]=D1trend_value;
   D1trend_1[i]=D1trend_value_1;


//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//APPROACH 2
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   if(iMA(NULL,D1,15,0,MODE_EMA,0,bar_0)>=iMA(NULL,D1,15,0,MODE_EMA,0,bar_1)) {new_indi[i]=1;} else {new_indi[i]=-1;}      
   //lag operation
   new_indi_1[i]=new_indi[i+1];   

}
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

Not quite sure what you are expecting from this indi

if(iMA(NULL,D1,15,0,MODE_EMA,0,bar_1)>=iMA(NULL,D1,15,0,MODE_EMA,0,bar_2)) {D1trend_value_1=1;} else {D1trend_value_1=-1;}
D1trend_1[i]=D1trend_value_1; 

is the histogram on your chart and compares Daily barshift 1 with barshift 2

if(iMA(NULL,D1,15,0,MODE_EMA,0,bar_0)>=iMA(NULL,D1,15,0,MODE_EMA,0,bar_1)) {new_indi[i]=1;} else {new_indi[i]=-1;}      
   //lag operation
   new_indi_1[i]=new_indi[i+1];   

is the arrow on your chart and compares Daily barshift 0 with barshift 1

So on the H4 chart, new_indi will change 6 bars before D1Trend and so new_indi_1 will change 5 bars before

 

Thank you, GumRai.

So I wonder how can I fix the LAG approach so that it show correct value on both H4 and D1 as the first approach?

SCFX

 
scfx: I try two approaches:
The first one: compare, assign value, make buffer equal to that value. This needs 2 comparisons. the 2 buffers are: D1trend and D1trend_1
The second one: compare, assign value directly to a buffer. Then I get the 2nd day buffer equal to lag value of today buffer. This just needs 1 comparison.

The first, d1trend_1 is the value from a day ago. The second is the current D1trend from one bar ago. Not the same except on the D1 chart.

For the second approach you'd need

#define HR2400 86400       // 24 * 3600 = PERIOD_D1 * 60
int      TimeOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when % HR2400 );            }
datetime DateOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when - TimeOfDay(when) );   }
datetime Yesterday(datetime when=0){      if(when == 0)  when = TimeCurrent();
   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
                                       return( iTime(NULL, PERIOD_D1, iD1) ); }
///////////////////
// new_indi_1[i]=new_indi[i+1];  
   datetime  now = Time[i],
             yest = Yesterday(now);
   int      iYest = iBarShift(NULL,0, yest);
   new_indi_1[i]=new_indi[iYest];  
 
scfx:

Thank you, GumRai.

So I wonder how can I fix the LAG approach so that it show correct value on both H4 and D1 as the first approach?

SCFX


I don't know what you mean by "correct value". I'm not being facetious, I just don't know what you expect from this indi.

D1Trend will lag new_indi by 1 full day and new_indi_1 is offset from new_indi by 1 bar in whatever timefame the indi is attached to.

Explain exactly what you want the indi to do and I or others may be able to offer advice.

 
WHRoeder:

The first, d1trend_1 is the value from a day ago. The second is the current D1trend from one bar ago. Not the same except on the D1 chart.

For the second approach you'd need



Thank you, WHRoeder,

Your code is perfect. I am trying to understand it.


Sorry for my poor explanation, GumRai. All I want is to preserve the D1 value even when I change chart to say H4.


SCFX

 
scfx:



Sorry for my poor explanation, GumRai. All I want is to preserve the D1 value even when I change chart to say H4.




You do preserve the value when changing TF.

It's just that the same value will be spread over 6 bars on H4

 
GumRai:


You do preserve the value when changing TF.

It's just that the same value will be spread over 6 bars on H4


Hi GumRai,

Actually my second approach will not "preserve and spread" as I expect because the (I+1) on H4 chart is 1 bar of H4.

So WHRoeder code helps to tell MT4 that on H4 chart, on non-D1 chart, the number 1 in (I+1) is 1 D1 bar. So now it "preserve and spread"

SCFX