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:
- 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)
- Filter the stocks and choose pairs with high correlation throughout the period. This is put in the possibilities.csv file. (correlation.py)
- Look at these pairs and select ones whose ratio are stationary. This is put in the final_choices.csv file.(check_relationship.py)
- 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)
- 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)
- 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)