|
1 | | -# 📈 Crypto Chart Patterns Detection |
| 1 | +# 📈 Crypto Chart Pattern Detector |
2 | 2 |
|
3 | | -A Python-based tool for detecting and analyzing chart patterns in cryptocurrency markets using technical analysis. |
| 3 | +[](https://github.com/tysoncung/crypto-chart-patterns/actions/workflows/test.yml) |
4 | 4 |
|
5 | | -## 🎯 Overview |
| 5 | +Advanced technical analysis tool for detecting chart patterns in cryptocurrency markets. |
6 | 6 |
|
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 |
10 | 8 |
|
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. |
12 | 10 |
|
13 | 11 | ## 🚀 Features |
14 | 12 |
|
| 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 |
15 | 27 | - Real-time cryptocurrency data fetching from Binance |
16 | 28 | - Automatic pattern detection using mathematical algorithms |
17 | 29 | - Visual pattern highlighting on price charts |
18 | 30 | - 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 |
21 | 34 |
|
22 | 35 | ## 📋 Requirements |
23 | 36 |
|
24 | 37 | ```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 |
33 | 46 | ``` |
34 | 47 |
|
35 | 48 | ## 🛠️ Installation |
36 | 49 |
|
37 | | -1. Clone the repository: |
38 | 50 | ```bash |
39 | 51 | git clone https://github.com/tysoncung/crypto-chart-patterns.git |
40 | 52 | cd crypto-chart-patterns |
41 | | -``` |
42 | | - |
43 | | -2. Install dependencies: |
44 | | -```bash |
45 | 53 | pip install -r requirements.txt |
46 | 54 | ``` |
47 | 55 |
|
48 | | -3. Run the Jupyter notebook: |
49 | | -```bash |
50 | | -jupyter notebook chart-patterns.ipynb |
51 | | -``` |
52 | | - |
53 | | -## 📊 Usage |
| 56 | +## 📊 Quick Start |
54 | 57 |
|
55 | | -### Basic Usage |
| 58 | +### Command Line Usage |
56 | 59 |
|
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 |
60 | 63 |
|
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 |
64 | 66 |
|
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 |
68 | 69 |
|
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 |
71 | 72 | ``` |
72 | 73 |
|
73 | | -### Pattern Detection Parameters |
| 74 | +### Python API |
74 | 75 |
|
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 |
81 | 79 |
|
82 | | -Run a comprehensive pattern screen across multiple parameters: |
| 80 | +# Initialize detector |
| 81 | +detector = PatternDetector("BTCUSDT", "1h") |
83 | 82 |
|
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() |
89 | 85 |
|
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) |
91 | 91 |
|
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 | +``` |
97 | 96 |
|
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 |
103 | 98 |
|
104 | | -## 📉 Forward Returns Analysis |
| 99 | +Run the comprehensive test suite: |
105 | 100 |
|
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 |
111 | 104 |
|
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 |
113 | 107 |
|
114 | | -## 🎨 Visualization |
| 108 | +# Run specific test |
| 109 | +pytest test_patterns.py::TestPatternDetector::test_triangle_pattern_detection |
| 110 | +``` |
115 | 111 |
|
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 |
119 | 113 |
|
120 | | -## 🔧 Customization |
| 114 | +This project uses GitHub Actions for continuous integration: |
121 | 115 |
|
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 |
123 | 120 |
|
124 | | -To add a new pattern, modify the `find_patterns()` function: |
| 121 | +## 📈 Pattern Types |
125 | 122 |
|
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 |
140 | 128 |
|
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) |
142 | 133 |
|
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) |
144 | 137 |
|
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 |
153 | 141 |
|
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 |
155 | 146 |
|
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) |
161 | 149 |
|
162 | 150 | ## ⚠️ Disclaimer |
163 | 151 |
|
|
0 commit comments