Oil Company Pairs Trading Strategy
In this project, I set out to create a trading strategy that capitalises on the relationships between different oil company stocks. The goal was to develop a system that could potentially make money regardless of whether the overall market was going up or down. This approach, known as pairs trading, is like betting on the relative performance of two related stocks rather than on the direction of the market as a whole.

Step 1: Choosing the Stocks
​
I decided to focus on major oil companies for this project. Why oil companies? Well, these companies tend to be affected by similar factors (like oil prices), which makes them good candidates for the strategy I wanted to use. I selected five big players in the oil industry:
​
-
ExxonMobil (XOM)
-
Chevron (CVX)
-
BP
-
Shell (SHEL)
-
TotalEnergies (TTE)
​
​
Step 2: Getting the Data
​
To analyse these stocks, I needed their historical price data. Instead of manually downloading this information, which would be time-consuming and prone to errors, I used a Python library called yfinance. This library allows me to automatically download stock data from Yahoo Finance.I chose to look at data from January 1, 2020, to September 12, 2024. Why this time period? It includes recent market conditions and gives me enough data to work with without going too far back in time.
​
​
Step 3: Finding Related Pairs
​
Next, I wanted to find pairs of stocks that tend to move together over time. This relationship is called "cointegration" in statistical terms. Think of it like two friends who might wander off in different directions for a bit but always end up walking side by side again.To find these cointegrated pairs, I wrote a function called find_cointegrated_pairs(). This function does the following:
​
-
It looks at every possible pair of stocks in our list.
-
For each pair, it runs a statistical test (called the Engle-Granger test) to see if they're cointegrated.
-
If the test result (p-value) is less than 0.05 (a common threshold in statistics), we consider that pair to be cointegrated.
​
After running this analysis, I found that BP and ExxonMobil (XOM) formed a cointegrated pair. This means that while BP and XOM might move differently day-to-day, they tend to follow each other in the long run.
​
​
Step 4: Calculating the Spread
​
Now that I had my pair (BP and XOM), I needed to figure out how to measure the difference between them. This difference is called the "spread."First, I calculated something called the "hedge ratio." This tells me how much XOM stock I should use to balance out the BP stock. I used a technique called linear regression to find this ratio.Then, I calculated the spread by subtracting the price of XOM (multiplied by the hedge ratio) from the price of BP. This spread becomes the basis of our trading decisions. To make sense of whether the spread is unusually large or small, I converted it into what's called a "z-score." A z-score tells us how many standard deviations away from the average the spread is. This is like measuring how unusual the current relationship between BP and XOM is compared to their typical relationship.
​
​
Step 6: Setting Up the Trading Rules
​
Now comes the exciting part – deciding when to buy and sell. I set up these rules:
-
If the z-score goes above 2, it means BP is unusually expensive compared to XOM. So, I sell BP and buy XOM.
-
If the z-score goes below -2, it means BP is unusually cheap compared to XOM. So, I buy BP and sell XOM.
-
When the z-score returns to 0, it means the relationship has normalised, so I close out the trade.
These rules are based on the idea that the relationship between BP and XOM will always tend to return to normal over time.
​
​
Step 7: Simulating the Trading Strategy
​
With all these pieces in place, I simulated how this strategy would have performed over our chosen time period. For each day:
-
I checked the z-score to see if I should open or close a trade.
-
If I opened a trade, I calculated how much money I would make (or lose) based on the price movements of BP and XOM.
-
I kept track of the total value of the portfolio over time.
​
​
Step 8: Evaluating the Performance
​
After running the simulation, I calculated several important metrics to see how well the strategy performed:
​
-
Total Return: This tells me how much money the strategy made overall. In this case, it was 120.26%, which means if I started with $100, I'd end up with $220.26.
-
Annual Return: This is the return converted to a yearly rate, making it easier to compare with other investments. The strategy showed an 18.35% annual return.
-
Sharpe Ratio: This measures how much return I'm getting for the amount of risk I'm taking. A higher number is better. The strategy had a Sharpe ratio of 1.1108, which is pretty good.
-
Maximum Drawdown: This shows the biggest loss the strategy experienced at any point. It was -13.38%, meaning at its worst point, the strategy had lost 13.38% of its value.
​
Step 9: Visualising the Results
​
To better understand how the strategy performed, I created two important graphs:
​
1. A line graph showing how the value of the portfolio changed over time.
​
​
​
​
​
​
​
​
​
​​
​
​
​
​
​
​
​
​2. A graph of the z-score over time, with lines showing when trades would be opened and closed. When the blue line moves above +2, it indicates potential overvaluation of BP relative to XOM. This is a key moment for considering a short trade on BP. On the other hand, when the blue line dips below -2, it suggests potential undervaluation of BP relative to XOM, signalling a possible long trade on BP. This strategy relies on the assumption that any significant deviation from the mean (z-score = 0) will eventually correct itself. This reversion provides opportunities for entering and exiting trades profitably.
I decided to use this z-score graph because it provides a clear visual representation of when the spread between BP and XOM deviates from its norm. By setting specific entry and exit thresholds, I can systematically identify trading signals based on statistical evidence.
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​​
​
Conclusion
​
This project demonstrates how we can use statistical relationships between stocks to create a trading strategy. While the results look promising, it's important to remember that past performance doesn't guarantee future results. The strategy involves complex calculations and carries risks, but it showcases how mathematical and statistical concepts can be applied to financial markets. Working on this pairs trading project has been a rewarding experience for me. I enjoyed the process of developing this strategy, from the initial data analysis to the final performance evaluation.The challenges I encountered, such as understanding cointegration and implementing the trading logic, pushed me to expand my knowledge and skills. Each obstacle overcome felt like a significant achievement, and seeing the strategy come to life through code and visualisations was exciting. I'm now eager to explore more advanced concepts and develop even more sophisticated strategies.
​
You can find my Python code here.​

