|
1 | | -# crypto-chart-patterns |
| 1 | +# 📈 Crypto Chart Patterns Detection |
| 2 | + |
| 3 | +A Python-based tool for detecting and analyzing chart patterns in cryptocurrency markets using technical analysis. |
| 4 | + |
| 5 | +## 🎯 Overview |
| 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 |
| 10 | + |
| 11 | +The tool fetches real-time data from Binance API and uses local extrema detection to identify these patterns automatically. |
| 12 | + |
| 13 | +## 🚀 Features |
| 14 | + |
| 15 | +- Real-time cryptocurrency data fetching from Binance |
| 16 | +- Automatic pattern detection using mathematical algorithms |
| 17 | +- Visual pattern highlighting on price charts |
| 18 | +- Forward return analysis for pattern validation |
| 19 | +- Multiple timeframe support (1min, 5min, 1hour, etc.) |
| 20 | +- Customizable pattern detection parameters |
| 21 | + |
| 22 | +## 📋 Requirements |
| 23 | + |
| 24 | +```bash |
| 25 | +pandas |
| 26 | +numpy |
| 27 | +scipy |
| 28 | +matplotlib |
| 29 | +tqdm |
| 30 | +ipython |
| 31 | +ipykernel |
| 32 | +requests |
| 33 | +``` |
| 34 | + |
| 35 | +## 🛠️ Installation |
| 36 | + |
| 37 | +1. Clone the repository: |
| 38 | +```bash |
| 39 | +git clone https://github.com/tysoncung/crypto-chart-patterns.git |
| 40 | +cd crypto-chart-patterns |
| 41 | +``` |
| 42 | + |
| 43 | +2. Install dependencies: |
| 44 | +```bash |
| 45 | +pip install -r requirements.txt |
| 46 | +``` |
| 47 | + |
| 48 | +3. Run the Jupyter notebook: |
| 49 | +```bash |
| 50 | +jupyter notebook chart-patterns.ipynb |
| 51 | +``` |
| 52 | + |
| 53 | +## 📊 Usage |
| 54 | + |
| 55 | +### Basic Usage |
| 56 | + |
| 57 | +```python |
| 58 | +# Set the cryptocurrency pair |
| 59 | +stock = "ETHUSDT" |
| 60 | + |
| 61 | +# Fetch data from Binance |
| 62 | +klines = get_data(stock) |
| 63 | +prices = binance_to_df(klines) |
| 64 | + |
| 65 | +# Detect patterns |
| 66 | +min_max = get_max_min(prices, smoothing=3, window_range=3) |
| 67 | +patterns = find_patterns(min_max) |
| 68 | + |
| 69 | +# Visualize patterns |
| 70 | +plot_minmax_patterns(prices, min_max, patterns, stock, window=10, ema=30) |
| 71 | +``` |
| 72 | + |
| 73 | +### Pattern Detection Parameters |
| 74 | + |
| 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 |
| 81 | + |
| 82 | +Run a comprehensive pattern screen across multiple parameters: |
| 83 | + |
| 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 | +``` |
| 89 | + |
| 90 | +## 📈 Pattern Definitions |
| 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` |
| 97 | + |
| 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` |
| 103 | + |
| 104 | +## 📉 Forward Returns Analysis |
| 105 | + |
| 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 |
| 111 | + |
| 112 | +This helps validate the predictive power of detected patterns. |
| 113 | + |
| 114 | +## 🎨 Visualization |
| 115 | + |
| 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 |
| 119 | + |
| 120 | +## 🔧 Customization |
| 121 | + |
| 122 | +### Adding New Patterns |
| 123 | + |
| 124 | +To add a new pattern, modify the `find_patterns()` function: |
| 125 | + |
| 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 | +``` |
| 140 | + |
| 141 | +### Changing Data Source |
| 142 | + |
| 143 | +Currently uses Binance API. To use another exchange: |
| 144 | + |
| 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 | +``` |
| 153 | + |
| 154 | +## 📊 Performance Metrics |
| 155 | + |
| 156 | +The screener provides average results grouped by: |
| 157 | +- Window parameter |
| 158 | +- EMA parameter |
| 159 | +- Stock symbol |
| 160 | +- Individual pattern performance |
| 161 | + |
| 162 | +## ⚠️ Disclaimer |
| 163 | + |
| 164 | +This tool is for educational and research purposes only. Cryptocurrency trading involves substantial risk of loss. Past pattern performance does not guarantee future results. Always do your own research and consider consulting with a financial advisor. |
| 165 | + |
| 166 | +## 🤝 Contributing |
| 167 | + |
| 168 | +Contributions are welcome! Please feel free to submit a Pull Request. Areas for improvement: |
| 169 | +- Add more chart patterns (Triangle, Flag, Wedge, etc.) |
| 170 | +- Implement machine learning for pattern validation |
| 171 | +- Add backtesting capabilities |
| 172 | +- Support for more exchanges |
| 173 | +- Real-time pattern alerts |
| 174 | + |
| 175 | +## 📝 License |
| 176 | + |
| 177 | +MIT License - see LICENSE file for details |
| 178 | + |
| 179 | +## 🔗 Resources |
| 180 | + |
| 181 | +- [Binance API Documentation](https://binance-docs.github.io/apidocs/) |
| 182 | +- [Technical Analysis Patterns](https://www.investopedia.com/articles/technical/112601.asp) |
| 183 | +- [SciPy Signal Processing](https://docs.scipy.org/doc/scipy/reference/signal.html) |
| 184 | + |
| 185 | +## 👤 Author |
| 186 | + |
| 187 | +**Tyson Cung** |
| 188 | +- GitHub: [@tysoncung](https://github.com/tysoncung) |
| 189 | + |
| 190 | +## 🙏 Acknowledgments |
| 191 | + |
| 192 | +- Binance for providing free API access |
| 193 | +- The Python scientific computing community |
| 194 | +- Technical analysis researchers and practitioners |
0 commit comments