Stock Pair Trading Strategy - Minimum Distance Method

Strategy

Pairs Trading (Pairs Trading) provides a risk-avoiding and profitable strategy for this predicament. It is also called spread trading or statistical arbitrage trading. It is a market-neutral strategy with small risks and relatively stable returns. . The general approach is to look for two stocks with a hedging effect in the historical price trend in the market to form a pair, so that the price difference (Spreads) of the stock pair roughly fluctuates within a range.

One possible way of doing this is to short stocks with strong price moves while long stocks with weaker price moves when stock pair spreads deviate positively, in anticipation of a recovery in the future. When the price difference converges to the long-term normal level, that is, the stock price with a stronger trend falls back, or the stock price with a weaker trend becomes stronger, close the position to earn the income when the price difference converges; when the stock pair price difference deviates negatively, open a position in reverse, When the price difference increases back to the normal range and then close the position, you can also earn profits.


stock pair selection

1. Intra-industry matching, select stocks with similar scales in the same industry for matching, such as pairing stocks of listed companies in the banking industry

Two pairs.

2. Industry chain matching. According to the industry chain, the stocks of listed companies in the same industry chain are matched, for example, a mobile phone

The production company is paired with its upstream camera production company.

3. Financial management matching, starting from the perspective of fundamental analysis, selecting listed companies' price-earnings ratios, debt ratios, product types, etc.

Matching with recent stocks, thereby reducing some unnecessary search costs.

4. Index pairing: The constituent stocks of the Shanghai and Shenzhen 300 Index, the Shanghai Stock Exchange 50 Index, and the Growth Enterprise Market Index are often paired by the industry and scholars

5. Stock pool matching: pairing different types of stocks issued by the same company or stocks issued in different markets is also a matching option.

Minimum distance pairing

principle

One selection criterion for pair trading is to look for stock pairs with historically stable spreads. In order to objectively measure the distance between two stock prices, it is first necessary to standardize the stock prices.

Given stock X and stock Y, we can calculate the sum of squared standardized price deviations SSDx,y between the two:

e68584254d17f27178430600035f3ac8.png

Among them, p^Xt is the cumulative rate of return within t days, and the calculation formula is (the rate of return of the day + 1).cumprod(), where cumprod is a cumulative multiplication function.

the code

#Minimum distance method stock pair trading

def SSD(priceX,priceY):
    if priceX is None or priceY is None:
        print('缺少价格序列.')
        
    returnX=(priceX-priceX.shift(1))/priceX.shift(1)[1:] # 计算 X 收益率
    returnY=(priceY-priceY.shift(1))/priceY.shift(1)[1:] # 计算 Y 收益率
    
    standardX=(returnX+1).cumprod() # 使用cumprod()函数累计求乘
    standardY=(returnY+1).cumprod()
    
    SSD=np.sum((standardX-standardY)**2) # 计算累计收益率偏差
    return(SSD) 
 
dis=SSD(df_s1['close'],df_s2['close'])  注:df_s1['close']是股票1的收盘价序列;df_s2同理。
print(dis)

Cointegration Model Principle

Another commonly used method for selecting stock pairs for pair trading is to select stock pairs that have a cointegration relationship between two stock price series. want

To judge whether the historical prices of two stocks have a cointegration relationship, it is necessary to check whether the logarithmic price series of the two stocks are consistent

order integration sequence, or first test whether the return sequence {r} of two stocks is a stationary time series.

The ADF() function of the arch package can use the ADF unit root method to test the stationarity of the sequence. The null hypothesis of the ADF unit root test is "there is a unit root in the sequence". If we cannot reject the null hypothesis, it means that the sequence we are checking may There is a unit root, and the sequence is non-stationary; if we reject the null hypothesis, the sequence does not have a unit root, that is, the sequence is a stationary time series.

Pairs Trading Strategy Implementation Steps

There are roughly 4 steps to implement the pair trading strategy in Python as follows.

(1) During the formation period, carry out cointegration test on the logarithmic prices of two stocks A and B

(2) Find out the pairing ratio beta and the pairing price, and calculate the mean and standard deviation of the price difference.

(3) During the trading period, u±1.5σ and u±0.2σ are set as the thresholds for opening and closing positions, and u±2σ can be regarded as the cointegration relationship.

The threshold that can break the forced liquidation, the specific trading rules are as follows:

● When the price difference crosses u+1.5σ, short the paired stocks and build a position in the opposite direction (sell B stock, buy A stock at the same time, A and B

The equity-to-funds ratio is beta;

● When the price difference falls between u+0.2σ, go long on the paired stock and close the position in the opposite direction;

Pairs trading strategy thinking

Low risk: Statistical principles are used for arbitrage, which has nothing to do with the rise and fall of the market. There is basically no risk when the external environment does not change

Therefore, it is also called risk-free arbitrage.

Few trading opportunities: The stock deviation of paired stocks is often rare, and the opportunities are fleeting, and quantitative investment is becoming more and more common

In some cases, it is difficult to capture manually.

The impact of the news is great: after major news events, the pairing relationship is often reversed or terminated. At this time, the pairing trading strategy must be adjusted immediately according to the situation, otherwise serious losses will occur.

Guess you like

Origin blog.csdn.net/oSuiYing12/article/details/124511136