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

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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:
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: