套利
配對交易策略的虛擬碼是什麼?
我正在嘗試了解配對交易策略。我知道我們必須同時做多和做空協整資產。但是我仍然對策略的運作方式有些困惑。我為我認為的配對交易策略編寫了虛擬碼?
x=price data of asset x y=price data of asset y if x and y are correlated and cointegrated calculate pair ratio(spread) x/y or y/x? calculate average of pair ratio(spread) if spread > mean sell asset ? buy asset ? else spread < mean sell asset ? buy asset ? close if else find new pair of assets x and y go to line 1 with new x and y close if
在這裡,我將配對比率(x/y 或 y/x)作為價差?我的第一個問題是我應該採用 x/y 或 y/x 哪個配對比率?
如果我認為
x/y
是分散的,那麼我應該做什麼buy
,sell
如果spread>mean
。如果我對配對交易虛擬碼的評估有誤,請隨時糾正我。
以下連結很好地總結了典型的配對交易策略:
它實際上也有完整的 python 程式碼。但它不包括協整檢查。
編輯:
if X and Y are cointegrated: calculate Beta between X and Y calculate spread as X - Beta * Y calculate z-score of spread # entering trade (spread is away from mean by two sigmas): if z-score > 2: sell spread (sell 1000 of X, buy 1000 * Beta of Y) if z-score < -2: buy spread (buy 1000 of X, sell 1000 * Beta of Y) # exiting trade (spread converged close to mean): if we're short spread and z-score < 1: close the trades if we're long spread and z-score > -1: close the trades # repeat above on each new bar, recalculating rolling Beta and spread etc.