This project is designed to detect stock market patterns (e.g., candlestick patterns) in financial time-series data using YOLOv8, a state-of-the-art object detection model. It processes historical stock or forex data, generates candlestick charts, and detects patterns using a pre-trained YOLOv8 model. Detected patterns are saved for further analysis.
- Candlestick Chart Generation: Converts time-series data into candlestick charts for visualization.
- Time Gap Filtering: Skips chart generation if there is a time gap greater than 10 minutes in the data.
- Pattern Detection: Uses YOLOv8 to detect stock market patterns in the generated charts.
- Automatic Saving: Saves charts and detected patterns into organized directories.
- Customizable Parameters: Allows users to define window sizes for chart generation and adjust detection confidence thresholds.
You can install the required libraries using the following command:
pip install -r requirements.txtThe project expects a CSV file containing historical stock or forex data with the following columns:
time: Timestamp of the data.open: Opening price.high: Highest price.low: Lowest price.close: Closing price.
Example dataset format:
| time | open | high | low | close |
|---|---|---|---|---|
| 2023-10-01 12:00:00 | 1800 | 1810 | 1795 | 1805 |
| 2023-10-01 12:05:00 | 1805 | 1815 | 1800 | 1810 |
-
Data Preprocessing:
- The script reads the CSV file and processes the data into a pandas DataFrame.
- It converts the
timecolumn to datetime format and sets it as the index.
-
Time Gap Filtering:
- The script checks for time gaps greater than 10 minutes in the data.
- If a time gap is found, the corresponding chart is skipped.
-
Candlestick Chart Generation:
- The script generates candlestick charts for specified window sizes (e.g., 2 hours, 4.1 hours, 6 hours).
- Charts are saved in the
imagesdirectory.
-
Pattern Detection:
- The script uses a pre-trained YOLOv8 model (
foduucom/stockmarket-pattern-detection-yolov8) to detect patterns in the generated charts. - Detected patterns are annotated and saved in the
detected_patternsdirectory.
- The script uses a pre-trained YOLOv8 model (
-
Output:
- Charts with detected patterns are displayed using OpenCV and saved.
- Logs are printed to indicate whether patterns were detected in each chart.
- Head and shoulders bottom
- Head and shoulders top
- M_Head
- StockLine
- Triangle
- W_Bottom
-
Clone the repository:
git clone https://github.com/abbasi0abolfazl/stock-market-pattern-detection.git cd stock-market-pattern-detection -
Place your dataset (CSV file) in the project directory.
-
Run the script or pattern_detection.ipynb:
python main.py
-
Check the output:
- Generated candlestick charts are saved in the
imagesdirectory. - Detected patterns are saved in the
detected_patternsdirectory.
- Generated candlestick charts are saved in the
You can also run this project directly on Google Colab:
- Open the Colab notebook using the link above.
- Upload your dataset (CSV file) to the Colab environment.
- Run all cells in the notebook.
- Check the output:
- Generated candlestick charts are saved in the
imagesdirectory. - Detected patterns are saved in the
detected_patternsdirectory.
- Generated candlestick charts are saved in the
- Window Sizes: Modify the
window_sizeslist in themainfunction to change the time intervals for chart generation. - Time Gap Threshold: Adjust the
max_time_gapvariable in thegenerate_candlestick_chartsfunction to change the maximum allowed time gap. - Model Parameters: Adjust the YOLOv8 model parameters (e.g., confidence threshold, IoU threshold) in the
mainfunction. - Input Data: Update the
file_pathvariable in theload_and_preprocess_datafunction to point to your dataset.
stock-market-pattern-detection/
├── main.py # Main script
├── pattern_detection.ipynb # ipython script
├── README.md # Project documentation
├── XAUUSD_M5.csv # Example dataset
├── images/ # Generated candlestick charts
└── detected_patterns/ # Saved detected patterns
Contributions are welcome! If you have suggestions or improvements, please open an issue or submit a pull request.

