# Multi time frame and loops

hi All,

I'm writing an algorithm where I scan the market over time, therefore using loops. I would like the algorithm to check at bar [i] if e.g. daily ADX > 25 and weekly ADX > 30.

``````myADX = ADX(14);								// daily ADX

for (i = 1; i < (BarCount-1); i++)
{

{
// continue with some algorithm here
}
}
``````

The problem is that refering to [i] e.g. = 10 means daily bar number 10 and weekly bar number 10, which is shifted into the future. But it should compare the daily bar with its corresponding weekly bar.

Do I have to convert the daily index number into a weekly index number first, or is there an easier way to achieve the same result?

I'm a newbie here, read many articles, but none could guide me the right direction so far.
Your help is therefore much appreciated.

``````myADX = ADX(14);								// daily ADX

``````

Also ask yourself whether loop is necessary. I presume in your case it is most probably not.

e.g.

``````someVariable = IIf( ADXcond, /*then true case*/, /*else false case*/ );
``````
4 Likes

In addition to the comments from @fxshrat, it should be noted that your wADX variable does not hold an ADX value calculated from weekly bars, but rather the last daily ADX value from each week, i.e. the daily ADX calculated on Friday in most cases. Perhaps that's intentional on your part, but thought I'd point it out since you said you're a newbie.

2 Likes

Thank you for pointing this out. In order to calculate the correct ADX value on a weekly basis, I would have to switch using TimeFrameSet first, then calculate the value and finally expand it to the daily Time Frame again? I am aware that in general ways, you could code this without loops, but in my particular case, I really need to use loops.

Yes, if you want to have weekly ADX then TimeFrameSet is a proper way to do that.

e.g.

``````myADX = ADX(14);	 // daily ADX

tmfrm = inWeekly;
TimeFrameSet( tmfrm );
it worked well, thank you all 