Sparse Compress vs TimeFrameSet giving different results for ATR

Hi,

Need some help.
I have a scenario where I will be requiring to use SuperTrend from multiple timeframes.
So for instance, a 15min timeframe will take
10 - 10:15
10.05 - 10.20
10.10 - 10.25
etc
which is why a direct TimeFrameSet and expand will not work (Since it will work only on the 15,30,45 candles as far as i know)

The alternate option was to use Sparse Compress. To evaluate the same, I tried the following:

//This is on 5min candles
Min_check = IIf ( Minute() % 15 == 10,1,0);
tn = TimeNum();

lenATR = 10;
width = 3;

C_arr = SparseCompress( Ref(Min_check,0) OR tn == 153000, Ref(C,0) );
H_arr = SparseCompress( Ref(Min_check,0) OR tn == 153000, HHV(H,3) );
L_arr = SparseCompress( Ref(Min_check,0) OR tn == 153000, LLV(L,3) ); 

C_temp = C;
H_temp = H;
L_temp = L;

//Trying to reset values of C,H and L arrays for calculations of ATR
C = C_arr;
H = H_arr;
L = L_arr;

nATR = ATR(lenATR);

//Reset back to normal
C = C_temp;
H = H_temp;
L = L_temp;


TimeFrameSet(in15Minute);
SuperT_15_arr = SuperTrend(10,3);
C_15 = C;
H_15 = H;
L_15 = L;

nATR_15 = ATR(lenATR);
TimeFrameRestore();

I was hoping the ATR returned from both functions will be exactly the same. However, when I tried to debug the same, all values (Close / high / low compressed are the same, but ATR is different)

Would like to understand the difference in behavior.

Thanks
Siddhant Kankani

These periods are OVERLAPPING. The can't overlap.

Hi Tomasz

Thanks for the response.
I am not looking at overlapping the periods (These are going to be used for separate strategies).

If i set my database to start at 9.15 -> the timeframe set and expand method work perfectly for the 15 minute candle.
However since I will also need the 10.05 - 10.20 compressed bar (on the 5minute candle), I am looking to explore the sparsecompress method and hence the above.

In the example above, only one of the periods are used (not overlapping).

Min_check = IIf ( Minute() % 15 == 10,1,0);

Let me know if any additional info is required.

Thanks
Siddhant Kankani

Obviously they WILL NOT be the same if they are coming from different time periods.

TimeFrameSet uses 10:00/10:15/10:30 not anything else.
If your SparseCompress uses 10:05/10:20/10:35 they will OBVIOUSLY be different.

I understand.
For me to understand the difference, I used timeframe set on the same timeframe as sparsecompress.
That is why, as in the screenshot, C_arr (which is from sparsecompress) is the same as C_15 which is from Timeframeset.
With the same C, H and L that is being used to calculate ATR, im getting different results and hence the post.

If any specific output, details are required, please let me know.

No, your original code (from first post) used

Min_check = IIf ( Minute() % 15 == 10,1,0);

which uses 10:10 timestamp not 10:00 that TimeFrameSet would use.

The 10:10 timestamp is basically the 5min candle closing from 10:10 to 10:15.
Which behaves the same way as the 15min candle at 10:00 (Closing at 10:15).

I am using "Start time of Interval as per the recommended setting". Sorry, maybe had I mentioned this in the first post, it would have been clearer.

Thanks
Siddhant Kankani

And here I am calling the ATR function on both the compressed arrays (as in the screenshot)
(C_arr is the sparse compress array vs C_15 is the timeframecompressed array and so on)

I am sure there must be something small that Im missing, but unable to figure.

Thanks again
Siddhant Kankani

So using SparseCompress you would get High and Low of that single 10:10 candle, not High and Low of RANGE from 10:00 to 10:15. TimeFrameSet gives High and Low from ENTIRE RANGE, not just one bar. Note that in exporation data with incorrect timestamps. You need to use TimeFrameEXPAND to display actual values after expansion to base interval. Those values should REPEAT 3 times (since 15:5 = 3).

Please read the manual:
https://www.amibroker.com/guide/h_timeframe.html

Using TimeFrameEXPAND IS CRUCIAL. That's the only way to display data with correct timestamps.

1 Like

For the sparse compress, I have used HHV with 3 bars to get me the high of 3 bars and similarly for Low.

The compressed data is what I'm running the ATR function on, and the compressed data looks exactly same for C, H and L for timeframeset and sparsecompress both.
Hence confused why ATR would be different.

Change ATR length to 1 and compare.

Secondly, don't check a small sample of your compressed arrays with TimeframeSet.
In the image, both arrays at the end are indeed identical.

In exploration, look at the starting bars, where there is a slight difference in both price arrays.

ATR uses wilders smoothing, so somewhere at the start, you will have some difference which can explain why both ATR have a slight difference.

Also check for data holes, another possibility.

Float (precision) rounding wouldn't be likely issue in this case.
But in other cases, it has been noted that user AFL calculating something as opposed to AB built-in function may have slight difference as internally double precision is being used.

And when you mis-align intentionally, like 10:05 to 10:19:59, don't expect identical result. So ensure when you are comparing, the time is aligned in 15m bars starting at the hour.

Thank you, this is what was the difference.
The timeframeset based arrays had an additional price entry at the beginning and hence the difference in ATR calculation.

Another query : I understand the starting data points are different and hence the initiate ATR calculations are different. But shouldnt they converge later on (and end up having the same values) when "x" number of data points for ATR(x) are the same?

Appreciate all the help.

Thanks
Siddhant Kankani

1 Like

it will converge to something very close but maybe not exactly (practical number of bars). Just read about say IIR( infinite impulse response) filter vs a finite one.

It did not really converge even towards the end (with quite a few data points in place) and hence I did further digging to find out further.

ATR uses Ref(C,-1) as one of the parameters (As taken from another forum response : Raw ATR Formula - #2 by fxshrat)

As seen on the exploration window,
I have posted results of :
TimeFrameExpand values of C, RefC1
SparseExpand values of C, RefC1

The closing time of 15:30 is the one that causes issues since the TimeFrameExpanded C changes value while the sparseexpanded value does not change value (Obviously due to its inherent behaviors) and hence the Ref value changes in the first part of the next morning.

Screenshot details below:

In the bigger picture, the small change doesnt make too much of a difference and hence I think I will go ahead with taking the difference as is.

Thank you for all the help.

Regards
Siddhant Kankani

1 Like