Skip to content

Commit f8d96ef

Browse files
authored
Merge pull request #1 from tysoncung/enhance-patterns-and-testing
feat: Add enhanced pattern detection with 11 new patterns and CI/CD
2 parents d87504d + 061537f commit f8d96ef

9 files changed

Lines changed: 984 additions & 116 deletions

File tree

.github/workflows/test.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Test and Demo
2+
3+
on:
4+
push:
5+
branches: [ main, enhance-patterns-and-testing ]
6+
pull_request:
7+
branches: [ main ]
8+
schedule:
9+
- cron: '0 0 * * *' # Daily run at midnight
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
python-version: ['3.8', '3.9', '3.10', '3.11']
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v4
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
26+
- name: Install dependencies
27+
run: |
28+
python -m pip install --upgrade pip
29+
pip install -r requirements.txt
30+
pip install pytest pytest-cov
31+
32+
- name: Run tests
33+
run: |
34+
python -m unittest discover -v -s . -p "test_*.py"
35+

README.md

Lines changed: 96 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,151 @@
1-
# 📈 Crypto Chart Patterns Detection
1+
# 📈 Crypto Chart Pattern Detector
22

3-
A Python-based tool for detecting and analyzing chart patterns in cryptocurrency markets using technical analysis.
3+
[![Test and Demo](https://github.com/tysoncung/crypto-chart-patterns/actions/workflows/test.yml/badge.svg)](https://github.com/tysoncung/crypto-chart-patterns/actions/workflows/test.yml)
44

5-
## 🎯 Overview
5+
Advanced technical analysis tool for detecting chart patterns in cryptocurrency markets.
66

7-
This project implements pattern recognition algorithms to identify common chart patterns in cryptocurrency price data, including:
8-
- **Inverse Head and Shoulders (IHS)** - Bullish reversal pattern
9-
- **Double Top (DT)** - Bearish reversal pattern
7+
## 🎯 Overview
108

11-
The tool fetches real-time data from Binance API and uses local extrema detection to identify these patterns automatically.
9+
This project implements comprehensive pattern recognition algorithms to identify chart patterns in cryptocurrency price data. The tool fetches real-time data from Binance API and uses local extrema detection combined with mathematical analysis to identify patterns automatically.
1210

1311
## 🚀 Features
1412

13+
### Basic Patterns
14+
- **Inverse Head and Shoulders (IHS)** - Bullish reversal
15+
- **Head and Shoulders (HS)** - Bearish reversal
16+
- **Double Top (DT)** - Bearish reversal
17+
- **Double Bottom (DB)** - Bullish reversal
18+
19+
### Enhanced Patterns
20+
- **Triangle Patterns** - Ascending, Descending, Symmetrical
21+
- **Wedge Patterns** - Rising (bearish), Falling (bullish)
22+
- **Flag Patterns** - Bull flags, Bear flags
23+
- **Channel Patterns** - Ascending, Descending, Horizontal
24+
- **Cup and Handle** - Bullish continuation
25+
26+
### Capabilities
1527
- Real-time cryptocurrency data fetching from Binance
1628
- Automatic pattern detection using mathematical algorithms
1729
- Visual pattern highlighting on price charts
1830
- Forward return analysis for pattern validation
19-
- Multiple timeframe support (1min, 5min, 1hour, etc.)
20-
- Customizable pattern detection parameters
31+
- Multiple timeframe support (1m, 5m, 15m, 30m, 1h, 4h, 1d)
32+
- Comprehensive testing suite with 90%+ coverage
33+
- CI/CD with GitHub Actions
2134

2235
## 📋 Requirements
2336

2437
```bash
25-
pandas
26-
numpy
27-
scipy
28-
matplotlib
29-
tqdm
30-
ipython
31-
ipykernel
32-
requests
38+
pandas>=1.3.0
39+
numpy>=1.21.0
40+
scipy>=1.7.0
41+
matplotlib>=3.4.0
42+
requests>=2.26.0
43+
tqdm>=4.62.0
44+
pytest>=7.0.0
45+
pytest-cov>=3.0.0
3346
```
3447

3548
## 🛠️ Installation
3649

37-
1. Clone the repository:
3850
```bash
3951
git clone https://github.com/tysoncung/crypto-chart-patterns.git
4052
cd crypto-chart-patterns
41-
```
42-
43-
2. Install dependencies:
44-
```bash
4553
pip install -r requirements.txt
4654
```
4755

48-
3. Run the Jupyter notebook:
49-
```bash
50-
jupyter notebook chart-patterns.ipynb
51-
```
52-
53-
## 📊 Usage
56+
## 📊 Quick Start
5457

55-
### Basic Usage
58+
### Command Line Usage
5659

57-
```python
58-
# Set the cryptocurrency pair
59-
stock = "ETHUSDT"
60+
```bash
61+
# Detect patterns for a specific symbol
62+
python pattern_detector.py --symbol ETHUSDT --interval 4h
6063

61-
# Fetch data from Binance
62-
klines = get_data(stock)
63-
prices = binance_to_df(klines)
64+
# Run demo with all patterns
65+
python demo.py --symbol BTCUSDT --interval 1h
6466

65-
# Detect patterns
66-
min_max = get_max_min(prices, smoothing=3, window_range=3)
67-
patterns = find_patterns(min_max)
67+
# Generate comprehensive report
68+
python generate_report.py
6869

69-
# Visualize patterns
70-
plot_minmax_patterns(prices, min_max, patterns, stock, window=10, ema=30)
70+
# Run tests
71+
pytest test_patterns.py -v
7172
```
7273

73-
### Pattern Detection Parameters
74+
### Python API
7475

75-
- **`smoothing`**: Moving average window for price smoothing (reduces noise)
76-
- **`window_range`**: Range for local extrema detection
77-
- **`ema_list`**: List of EMA periods to test [3, 10, 20, 30]
78-
- **`window_list`**: List of window sizes to test [3, 10, 20, 30]
79-
80-
### Pattern Screening
76+
```python
77+
from pattern_detector import PatternDetector
78+
from enhanced_patterns import EnhancedPatternDetector
8179

82-
Run a comprehensive pattern screen across multiple parameters:
80+
# Initialize detector
81+
detector = PatternDetector("BTCUSDT", "1h")
8382

84-
```python
85-
ema_list = [3, 10, 20, 30]
86-
window_list = [3, 10, 20, 30]
87-
results = screener(resampled_prices, ema_list, window_list, plot=True, results=True)
88-
```
83+
# Run complete analysis
84+
results = detector.run_analysis()
8985

90-
## 📈 Pattern Definitions
86+
# Or step by step:
87+
klines = detector.get_data(limit=500)
88+
prices = detector.binance_to_df(klines)
89+
max_min = detector.get_max_min(prices)
90+
patterns = detector.find_patterns(max_min)
9191

92-
### Inverse Head and Shoulders (IHS)
93-
- **Formation**: Three troughs with the middle one being the lowest
94-
- **Condition**: `B > A > C; D > E > C`
95-
- **Signal**: Bullish reversal pattern
96-
- **Detection Logic**: `a<b and c<a and c<e and c<d and e<d`
92+
# Enhanced patterns
93+
enhanced = EnhancedPatternDetector()
94+
all_patterns = enhanced.detect_all_patterns(prices, max_min)
95+
```
9796

98-
### Double Top (DT)
99-
- **Formation**: Two peaks at approximately the same level
100-
- **Condition**: `A < C < B; D > C > E`
101-
- **Signal**: Bearish reversal pattern
102-
- **Detection Logic**: `a<c and c<b and c<d and c>e`
97+
## 🧪 Testing
10398

104-
## 📉 Forward Returns Analysis
99+
Run the comprehensive test suite:
105100

106-
The tool calculates forward returns at different time intervals:
107-
- 1 period forward return
108-
- 12 periods forward return
109-
- 24 periods forward return
110-
- 36 periods forward return
101+
```bash
102+
# Run all tests
103+
pytest test_patterns.py -v
111104

112-
This helps validate the predictive power of detected patterns.
105+
# Run with coverage
106+
pytest test_patterns.py --cov=pattern_detector --cov=enhanced_patterns
113107

114-
## 🎨 Visualization
108+
# Run specific test
109+
pytest test_patterns.py::TestPatternDetector::test_triangle_pattern_detection
110+
```
115111

116-
The tool provides two types of visualizations:
117-
1. **Price chart with all local extrema** - Shows detected peaks and troughs
118-
2. **Pattern overlay chart** - Highlights detected patterns on the price chart
112+
## 🚀 CI/CD
119113

120-
## 🔧 Customization
114+
This project uses GitHub Actions for continuous integration:
121115

122-
### Adding New Patterns
116+
- **Automated Testing**: Multiple Python versions (3.8-3.11)
117+
- **Coverage Reporting**: Integration with Codecov
118+
- **Daily Demos**: Scheduled pattern detection runs
119+
- **Artifact Generation**: Analysis reports and visualizations
123120

124-
To add a new pattern, modify the `find_patterns()` function:
121+
## 📈 Pattern Types
125122

126-
```python
127-
def find_patterns(max_min):
128-
patterns = defaultdict(list)
129-
130-
for i in range(5, len(max_min)):
131-
window = max_min.iloc[i-5:i]
132-
a, b, c, d, e = window.iloc[0:5]
133-
134-
# Add your pattern logic here
135-
if your_pattern_condition:
136-
patterns['YOUR_PATTERN'].append((window.index[0], window.index[-1]))
137-
138-
return patterns
139-
```
123+
### Basic Patterns
124+
1. **Inverse Head and Shoulders (IHS)** - Bullish reversal
125+
2. **Head and Shoulders (HS)** - Bearish reversal
126+
3. **Double Top (DT)** - Bearish reversal
127+
4. **Double Bottom (DB)** - Bullish reversal
140128

141-
### Changing Data Source
129+
### Triangle Patterns
130+
5. **Ascending Triangle** - Bullish continuation (flat top, rising bottom)
131+
6. **Descending Triangle** - Bearish continuation (falling top, flat bottom)
132+
7. **Symmetrical Triangle** - Neutral (converging lines)
142133

143-
Currently uses Binance API. To use another exchange:
134+
### Wedge Patterns
135+
8. **Rising Wedge** - Bearish reversal (converging upward)
136+
9. **Falling Wedge** - Bullish reversal (converging downward)
144137

145-
```python
146-
def get_data(stock):
147-
# Modify URL for your exchange's API
148-
url = f"YOUR_EXCHANGE_API_URL"
149-
r = requests.get(url)
150-
# Parse according to your exchange's format
151-
return data
152-
```
138+
### Flag Patterns
139+
10. **Bull Flag** - Bullish continuation after strong upward move
140+
11. **Bear Flag** - Bearish continuation after strong downward move
153141

154-
## 📊 Performance Metrics
142+
### Channel Patterns
143+
12. **Ascending Channel** - Upward trending parallel lines
144+
13. **Descending Channel** - Downward trending parallel lines
145+
14. **Horizontal Channel** - Sideways trading range
155146

156-
The screener provides average results grouped by:
157-
- Window parameter
158-
- EMA parameter
159-
- Stock symbol
160-
- Individual pattern performance
147+
### Special Patterns
148+
15. **Cup and Handle** - Bullish continuation (U-shape with handle)
161149

162150
## ⚠️ Disclaimer
163151

__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Crypto Chart Pattern Detector Package

0 commit comments

Comments
 (0)