Skip to content

syip1/Pairs-Trading

Repository files navigation

Pairs-Trading

This is a program that backtests the trading strategy of pairs trading. It takes two similar stocks and varies the proportion of assets put in each stock based on the relative price of the two stocks.
Currently the following is done:

  1. Scrape data (through Yahoo Finance) of most of the stocks on the Hong Kong Stock Exchange, with trading volume above a certain threshold. This is put in the HK_stocks_from_2011.csv file. (scrape_stock_data.py)
  2. Filter the stocks and choose pairs with high correlation throughout the period. This is put in the possibilities.csv file. (correlation.py)
  3. Look at these pairs and select ones whose ratio are stationary. This is put in the final_choices.csv file.(check_relationship.py)
  4. Implement the basic types of pairs trading strategy, based on continuous asset allocation (adjusting it frequently based on the price ratio) as well as discrete asset allocation (only long one stock and short the other when the price ratio reaches certain thresholds). This is on top of the baseline strategy of buy-and-hold two stocks in equal amounts. (backtest_functions.py)
  5. Run the pairs trading strategy on the pairs of stocks selected for the first 70% of the time period, in order to select parameters. These parameters include number of days to look at for the reference price ratio, leverage when carrying out the long/short strategy, thresholds for buying and selling the stock for the discrete strategy, etc. The results of the training, and the best parameters selected, are recorded in the train_results.csv file. (pairs_trading_train.py)
  6. Using the parameters above, run the pairs trading strategy on the later 30% of the time period. The test results are recorded in the test_results.csv file. (pairs_trading_test.py)
I measured the effectiveness of a strategy by its Sharpe ratio. This is done by taking the 250-day returns and calculating their mean and standard deviation. The Sharpe ratio is $\frac{mean - risk_free_rate}{standard_deviation}$. The training yielded Sharpe ratios that were much higher than the baseline ones, but this was not reflected in the test file. This may be because of the short time period in the test file and the higher volatility of the pairs trading strategy, or it could also be that my calculation method of the Sharpe ratio is flawed. (I am considering 250-day-annualised returns for each possible 250-day period, but perhaps the periods should be shorter and not overlapping.) Moving forward, I have noted that increasing volatility often leads to increasing Sharpe ratio. I may try exploring the relationship between mean returns and standard deviation in more detail, in order to show how this strategy works for different risk profiles.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages