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
wADX = TimeFrameCompress(myADX, inWeekly); // weekly ADX
for (i = 1; i < (BarCount-1); i++)
if ( (myADX[i] > 25) AND (wADX[i] > 30) )
// 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.
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.
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.
it worked well, thank you all
Maybe to describe it in descriptive words: you have to switch to weekly timeframe first, generate the weekly array, go back to daily timeframe, expand the weekly array and so create a new array where every daily bar is filled with a value from the weekly bar. Then reference to this array inside the loop using [i].