https://github.com/pakagronglb/python-correlations-trading-strategy
A correlation-based trading strategy using Python, analyzing the relationship between two assets to generate trading signals. The strategy is based on the concept of mean reversion in correlation between assets.
https://github.com/pakagronglb/python-correlations-trading-strategy
matplotlib pandas pyplot python trading visualisation yfinance
Last synced: 27 days ago
JSON representation
A correlation-based trading strategy using Python, analyzing the relationship between two assets to generate trading signals. The strategy is based on the concept of mean reversion in correlation between assets.
- Host: GitHub
- URL: https://github.com/pakagronglb/python-correlations-trading-strategy
- Owner: pakagronglb
- Created: 2025-05-02T05:07:44.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-02T05:08:57.000Z (about 1 year ago)
- Last Synced: 2025-05-02T06:20:53.514Z (about 1 year ago)
- Topics: matplotlib, pandas, pyplot, python, trading, visualisation, yfinance
- Language: Python
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 📈 Python Correlations Trading Strategy





## 🎯 Overview
A correlation-based trading strategy using Python, analyzing the relationship between two assets to generate trading signals. The strategy is based on the concept of mean reversion in correlation between assets.
## ✨ Features
- 📊 Real-time stock data fetching with retry logic
- 🔄 Correlation analysis between two assets
- 📈 Rolling correlation calculations
- 🎯 Signal generation based on correlation thresholds
- 📉 Visualization of correlation patterns and trading signals
- ⚡ Rate limiting and error handling
## 🛠️ Installation
1. Clone the repository:
```bash
git clone https://github.com/pakagronglb/python-correlations-trading-strategy.git
cd python-correlations-trading-strategy
```
2. Create and activate a virtual environment:
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
3. Install dependencies:
```bash
pip install -r requirements.txt
```
## 📋 Requirements
- Python 3.13+
- pandas
- yfinance
- matplotlib
- numpy
## 🚀 Usage
1. Import the necessary functions:
```python
from stock_data import fetch_stock_data
from correlation_strategy import calculate_correlation_signals, plot_correlation_signals
```
2. Fetch stock data:
```python
asset1_prices = fetch_stock_data('AAPL')
asset2_prices = fetch_stock_data('MSFT')
```
3. Calculate correlation signals:
```python
result = calculate_correlation_signals(data, 'AAPL', 'MSFT', window=50, wide_window=100, std_factor=1)
```
4. Visualize the results:
```python
plot_correlation_signals(result)
```
## 📊 Strategy Details
The strategy works by:
1. Calculating a rolling correlation between two assets
2. Computing a longer-term average correlation
3. Identifying significant deviations from the average
4. Generating signals when correlations move outside normal ranges
## 🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## 📝 License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## 🙏 Credits
This project was inspired by the tutorial from [CodeTrading](https://www.youtube.com/watch?v=O13aUVSs5pQ&ab_channel=CodeTrading).
Special thanks to:
- [CodeTrading](https://www.youtube.com/@CodeTrading) for the original concept and tutorial
- The yfinance team for providing the stock data API
- The pandas and matplotlib teams for their excellent data analysis tools
## 📞 Support
For support, please open an issue in the GitHub repository or contact the maintainers.
## 📈 Disclaimer
This project is for educational purposes only. Trading stocks involves risk, and past performance is not indicative of future results. Always do your own research and consult with a financial advisor before making investment decisions.