OpenBB-finance / OpenBB

Investment Research for Everyone, Everywhere.
https://openbb.co
Other
33.55k stars 3.06k forks source link

[FR] New stock comparison analysis - Cointegration Pairs Trading Model #5074

Closed viniciusparede closed 5 months ago

viniciusparede commented 1 year ago

What's the problem of not having this feature?

The residual cointegration model of a linear regression serves as the foundation for pair trading operations (Long & Short). The concept is to display a list of pairs whose residuals are both cointegrated and stationary within a specific period of the sample space. This enables investors to identify assets that are likely suitable for buying or selling operations.

For example, let's suppose the user wants to perform a comparative analysis involving a selected asset, such as NVDA. This company belongs to the S&P500. Therefore, the user would like to verify if there are any cointegrated pairs in that index and determine on which side (Long Spread or Short Spread) they lie. Let's say that NVDA predominantly exhibits a Short Spread pattern during the sample period. This suggests a potential buying pressure for this asset by hedge funds, which can serve as additional information when making buying or selling decisions.

Describe the solution you would like

Create the following files:

Input:

Output:

Additional information

START ├── Load target asset │ ├── Was the target asset loaded successfully? │ │ ├── Yes │ │ │ ├── Find cointegrated stocks from the S&P500 │ │ │ │ ├── Are there cointegrated stocks from the S&P500? │ │ │ │ │ ├── Yes │ │ │ │ │ │ ├── Identify pairs of assets that are both cointegrated, have stationary residuals, and positive beta │ │ │ │ │ │ │ ├── Are there pairs of assets with these characteristics? │ │ │ │ │ │ │ │ ├── Yes │ │ │ │ │ │ │ │ │ ├── Proceed with the analysis of these pairs of assets │ │ │ │ │ │ │ │ ├── No │ │ │ │ │ │ │ │ │ ├── There are no pairs of assets with these characteristics │ │ │ │ ├── No │ │ │ │ │ ├── There are no cointegrated stocks from the S&P500 │ ├── No │ │ ├── Failed to load the target asset ├── END

In this decision tree, the first point is to load the target asset. Then, it checks if the target asset was loaded successfully. If it was, the process proceeds to find cointegrated stocks from the S&P500. Next, it checks if there are any cointegrated stocks from the S&P500. If there are, the next step is to identify pairs of assets that are both cointegrated, have stationary residuals, and positive beta. It then checks if there are pairs of assets with these characteristics. If there are, the process proceeds with the analysis of these pairs of assets. Otherwise, there are no pairs of assets with these characteristics. If the target asset fails to load, there was a failure in loading the target asset.

References Performance Analysis of Statistical Arbitrage Portfolio Test for no-cointegration Calculate the autocorrelation function Augmented Dickey-Fuller unit root test Linear Regression

joshuabuildsthings commented 1 year ago

@viniciusparede - Thoughts on starting smaller & scaling out? Maybe the initial implementation can be a Report that takes two tickers are the input & runs a cointegrated pair test on that pair?

viniciusparede commented 1 year ago

Thank you for the comment, @joshuabuildsthings ! I believe this interaction with more experienced developers is crucial before starting to implement a potential new feature.

I agree with your idea. Do you think it's valid to start the development?

viniciusparede commented 1 year ago

I started developing a feature

Preview: image image

For the initial tests, I implemented the idea described by @joshuabuildsthings. After the model runs, the result will graphically show the spread for a specific pair, half-life time (stop by time), and the proportional quantity of shares to execute the operation.

I still have several adjustments to make, but I believe that soon this feature can contribute to the project and future user analyses.

joshuabuildsthings commented 1 year ago

@viniciusparede - That looks awesome! Congratulations.

viniciusparede commented 1 year ago

@viniciusparede - That looks awesome! Congratulations.

Thanks bro!