Selecting Timeframe based on array

is it possible to select time frame at runtime based on array values. We generally hardcode the timeframe such as

timeframeset(in5Minute); //hardcoded value for timeframe
...calculate....
timeframerestore()

but can we do something like below. The below syntax may not be correct.

timeframeMultiplier=50/barsWithinPattern; // this is array calculated at runtime
timeframeset(in1Minute * timeframeMultiplier); 
...calculate....
timeframerestore()

As per the help for TimeframeSet, the input interval is a number, or a constant that translates to a number. You cannot use array as an input.
image

Yes. Is there a workaround to this? Is there a way, some clever coding perhaps, to select timeframe interval at runtime based on values stored in array.

Hello AlgoEnthusiast,

https://www.amibroker.com/guide/afl/timeframeset.html

You should to use the value at an index exposure ( scalar/single value ) not the array.

// request for example endvalue of the array

bi = BarIndex();
evbi = EndValue( bi );

barsWithinPattern = 10; // array of 10s ...
timeframeMultiplier = 50 / barsWithinPattern[evbi]; // or whatever index postion ...

TimeFrameSet( 60 * timeframeMultiplier ); 
//...calculate....
TimeFrameRestore();

Best regards,
Peter

But wouldn't barsWithinPattern[evbi] return the value of the last bar of the array everytime? So far example if there are 1000 bars then for bar 100 it will return the value stored in bar 1000 and again for bar 500 it will return the value stored in bar 1000.

Hello AlgoEnthusiast,

Please read first

https://www.amibroker.com/guide/h_understandafl.html

As written it is a example, if you want to request the value at index 100 position please use

timeframeMultiplier = 50 / barsWithinPattern[100];

or build a loop to run through that array and extract what you are looking for ...

Best regards,
Peter

Yes it can be done if i run 0 to barcount-1 loop which will completely crash my code. I was looking for a clever way.

As a general rule, clever solutions require detailed knowledge of the problem to be solved. Your question is extremely vague, so it's likely the responses you receive will be equally vague.

1 Like

Imagine you have to validate a pattern within a pattern. Lets suppose you need 50 bars to validate the pattern because the said pattern attains its character and vigour in that size. Now lets assume there are 3 legs to this pattern. Each of this leg is a pattern in itself and need 50 bars to validate and must be validated individually.
So now to validate leg-A (which has 25 bars) has to be expanded and viewed in a timeframe where it made up of 50 bars. That means i need to go down to 1/2 interval i.e. if the larger patter was in 10 minutes i need to go to 5 minutes. Similarly leg-b may have 15 bars at the highest degree and to validate i have to switch to roughly 1/3rd interval.
Then how will this be solved without switching to a lower timeframe determined at runtime?

One idea would be to fix the two timeframes, so that you always use the faster timeframe (say 3-min) to evaluate the legs and the slower timeframe (say 10-min) to evaluate the larger pattern. This would be much more computationally efficient, but it doesn't sound like it would achieve your goal.

To do what you've described will definitely involve looping, as each leg will need to be evaluated independently using its own timeframe. However, you don't need to do a TimeframeSet / Leg Evaluation for ALL bars, but rather just the bars where a leg ends, so that at least should reduce the computational load. Also, if the evaluation of Pattern 1, Leg A fails, then I'm guessing you could be "clever" about skipping the evaluation of Legs B & C and just move on to Pattern 2.

It does seem a bit odd to me that you might evaluate P1 Leg A on 5-min bars and P2 Leg A on 3-min bars, but only you can tell if that will give you a tradeable edge or not. Everyone has their own secret sauce. :slight_smile:

For now i have hardcode 3 timeframe 1/2, 1/4th and 1/6th of larger timeframe to do the sub-pattern evaluation. This should suffice.

It does seem a bit odd to me that you might evaluate P1 Leg A on 5-min bars and P2 Leg A on 3-min bars

it depends on how many bars the said leg has in the largest degree. Evaluation is possible when it is expanded to 50 bars. So if the said leg has 10 bars in the highest timeframe then switch to1/5th timeframe to get 50 bars, if it has 25 bars in the highest degree then switch to 1/2 timeframe so on so forth. It is difficult to predict exactly how many bars a leg will have at the highest degree but one can roughly assume that it will be anywhere between 10-30 bars. So based on this assumption i have chosen the three subdivisions of the highest timeframe to do the sub-pattern evaluation.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.