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

You forgot to expand. Please read AmiBroker user guide.

myADX = ADX(14);								// daily ADX
wADX_compressed = TimeFrameCompress(myADX, inWeekly);		// compressed weekly ADX
wADX_expanded = TimeFrameExpand(wADX_compressed , inWeekly); // expanded to shorter interval

ADXcond = myADX > 25 AND wADX_expanded > 30;

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

// weekly ADX
tmfrm = inWeekly;
TimeFrameSet( tmfrm );
    wADX_compressed = ADX(14);
TimeFrameRestore();

expandmode = expandLast;
wADX_expanded = TimeFrameExpand( wADX_compressed, tmfrm, expandmode );

ADXcond = myADX > 25 AND wADX_expanded > 30;
4 Likes

it worked well, thank you all :ok_hand:
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].