Firstly, it calculates hedge ration using OLS linear regression. Then it calculates spread and z-score of spread. if spread in specific range (which it's possible to change in settings) it makes Long/Short orders.
@wa1one, thanks. I understand that when it is only one asset, but in the pairs trade case, if the signal indicates to "enterLong" the spread, how does pine script know which asset to buy and which one to short? The spread between the two assets would be what is being displayed on the main chart, right?
colinchng
⋅
Hi Why did I. get the symbol error, I'm using SPY and I've tried others as well.
wa1one
⋅
I can update the script if you are interested. Some sophisticated things may be applied to calculate rolling window (as half-life of mean reverting process, for instance)
trader241
⋅
Curious, where did you get your beta formula from? As far as I know, that's not the beta formula. Are you trying to get the formula for the beta (= how much a stock moves with the market) or something else?
wa1one
⋅
@trader241 I did double-check and beta is correct. At least when I had rewritten the formula I got the same spread... So, could you provide any additional information on why the formula is incorrect? OK, it's might be the basic LS and not OLS, I always think that OLS is the basic LS algorithm. But beta looks good.
wa1one
⋅
We need to hold a proportional to hedgeRatio(beta)amount of stock2 in a portfolio. This is very important.