Calculate High & Low of Spread

Hello,
I am trying to calculate spread using below code

{
SetForeign( Pair_Symbol_1 );
C1 = C;

RestorePriceArrays();

SetForeign( Pair_Symbol_2 );
C2 = C;
RestorePriceArrays();

Spread = C2/C1;
}

I am doing these Spread calculations in 30min TimeFrame .However I also want to calculate high & low of spread in 30 min. My data has the granularity of 1min so spread can be calculated in 1 min timeframe by using "TimeFrameSet( in1Minute )". However , I was wondering how to get OHLC data in 30min from 1min Spread calculation.

Thanks in advance !!
Regards,
Rahul Jain

You don't need SetForeign() calls,
Directly use this https://www.amibroker.com/guide/afl/timeframegetprice.html

TimeFrameGetPrice( pricefield, interval, shift = 0, mode = expandFirst )

It achieves everything in a single call and its faster too.

Thanks @travick for the optimization. I will replace SetForeign() with TimeFrameGetPrice(). But that still does not solve my purpose of getting OHLC values of spread i.e C2/C1. I need to compute C2/C1(single value,no OHLC) in 1 minute time frame & from that compute OHLC values for 30min time frame.

I'm sorry to say but You have clearly not understood the function and how you can use it at best.

Also, if you read your 2nd post, you are not sure about what OHLC means or not using it in right context.
OHLC will always be 4 values. If you want all or any of the 4, you specify each in a separate TimeFrameGetPrice() call.

In the formula you shared in 1st post, no where do you mention O,H,L. Just dividing the two Close Arraays.
Kindly clarify first.

Unable to comprehend the ambiguity:

getting OHLC values of spread i.e C2/C1. I need to compute C2/C1(single value,no OHLC) in 1 minute time frame & from that compute OHLC values for 30min time frame.

Why do "you" want to compute 30m TF, let AB handle it.

If you get O of both symbols in 30m TF, and do O2/O1, isn't that the Open spread value in 30m TF?
Do the same for the other 3 Price Arrays.

Thanks @travick for the reply.

I understand the usage of TimeFrameGetPrice() call & yes to get OHLC I need 4 such calls .Still that doesn't solve the problem. I understand O = O2/O1 is open of spread value in 30m TF. But what about High and Low of the spread in terms of price of two stocks = H2/H1 or H2/L1 or none of them?

Please note it's not necessary High of both equities will coincide , or High & lows.

What I need is 30 min high & close of C2(1min)/C1(1min). Hope I am able to explain myself this time.

Regards,
Rahul Jain

Then just use HHV for a lookback period or HighestSince().

x = 30; // Lookback bars, last 30 in 1 min TF will give 30min H/L
spreadH = HHV( symb2_High, x ) / HHV( symb1_High, x );

spreadL = LLV( symb2_Low, x ) / LLV( symb1_Low, x );

widestA = HHV( symb2_High, x ) / LLV( symb1_High, x );

//    and many other permutations

Same for Low Arrays of both symbols, LLV or LowestSince()

Edit: Code not optimized, there are repeated same function calls. Just for illustration.

If I understand what @rahuiitk is after, I would do this just a bit differently, like this:

lb = 30;
C1 = Foreign(sym1,"C");
C2 = Foreign(sym2,"C");
spread = C2/C1;

spreadHigh = HHV(spread,lb);
spreadLow = LLV(spread,lb);

This will find the highest and lowest value of the spread over the past 30 minutes, rather than finding the ratio of the highest high (for example) for each symbol, which may have occurred at different times.

Hehe, wish the explanation was crystal clear.
I interpreted this part the way I did

This would mean H2/H1 wouldn't work, hence, identifying each symbols High and then the ratio.

OP will clarify in the Nth post.

1 Like

Good point @travick - I missed that in the OP's post. And yes, clear explanations would save LOTS of time throughout the forum!

Thanks @mradtke & @travick for all the help. Will try to be more articulate next time.