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
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];
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.
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
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
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

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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