Spaces:
Running
Running
File size: 17,670 Bytes
9607690 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | # ๐โก CoinAPI Integration - Cross-Exchange Anomaly Detection
**Status**: โ
**INTEGRATED** | November 30, 2025
**Author**: Gary Leckey & GitHub Copilot
---
## ๐ฏ Overview
The CoinAPI integration adds **cross-exchange anomaly detection** to our trading system, enabling us to:
1. **Detect Market Manipulation** - Price discrepancies across exchanges
2. **Identify Wash Trading** - Circular trades inflating volume
3. **Spot Orderbook Spoofing** - Fake liquidity manipulation
4. **Find Arbitrage Opportunities** - Real price differences to exploit
5. **Refine Algorithms Automatically** - Adapt thresholds based on anomalies
### The Core Insight
> **"The Truth is in the Anomalies"**
When exchange data disagrees, the discrepancy reveals the *real story*:
- **Price manipulation** โ Avoid the asset temporarily
- **Wash trading** โ Blacklist completely
- **Orderbook spoofing** โ Require higher confidence
- **Cross-exchange spreads** โ Arbitrage opportunity!
---
## ๐๏ธ Architecture
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ COINAPI.IO (300+ Exchanges) โ
โ Professional aggregated data: OHLCV, quotes, orderbooks โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CoinAPIClient โ
โ - Rate-limited REST API calls โ
โ - Caching for efficiency โ
โ - Multi-exchange quote fetching โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AnomalyDetector โ
โ Detects 9 types of anomalies: โ
โ โข Price Manipulation (cross-exchange spread > 2%) โ
โ โข Wash Trading (repeated identical prices) โ
โ โข Orderbook Spoofing (70/30 bid/ask imbalance) โ
โ โข Volume Inflation (3x normal volume) โ
โ โข Latency Arbitrage (>500ms delays) โ
โ โข Frontrunning (100ms windows) โ
โ โข Liquidity Drain (sudden orderbook collapse) โ
โ โข Exchange Outage (missing data) โ
โ โข Cross-Exchange Spread (arbitrage opportunities) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Algorithm Refinement Engine โ
โ Applies automatic adjustments: โ
โ โข Blacklists symbols (wash trading) โ
โ โข Adjusts coherence thresholds (spoofing) โ
โ โข Uses multi-exchange mean prices (arbitrage) โ
โ โข Logs all refinements for analysis โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AurisEngine Integration โ
โ โข scan_for_anomalies() - Periodic scanning โ
โ โข is_symbol_blacklisted() - Filter opportunities โ
โ โข get_coherence_adjustment() - Dynamic thresholds โ
โ โข _apply_anomaly_refinement() - Auto-adjust rules โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ find_opportunities() Pipeline โ
โ 1. Scan for anomalies every 5 minutes โ
โ 2. Filter blacklisted symbols โ
โ 3. Apply coherence adjustments โ
โ 4. Continue with HNC + Probability analysis โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
---
## ๐ง Configuration
### Environment Variables
```bash
# Required for live anomaly detection
export COINAPI_KEY=your-api-key-here
export ENABLE_COINAPI=1
```
### CONFIG Parameters
```python
CONFIG = {
# CoinAPI Anomaly Detection
'ENABLE_COINAPI': False, # Requires API key
'COINAPI_SCAN_INTERVAL': 300, # Scan every 5 minutes
'COINAPI_MIN_SEVERITY': 0.40, # Act on anomalies โฅ40% severity
'COINAPI_BLACKLIST_DURATION': 3600, # Blacklist for 1 hour
'COINAPI_ADJUST_COHERENCE': True, # Auto-adjust thresholds
'COINAPI_PRICE_SOURCE': 'multi_exchange', # Use aggregated prices
}
```
---
## ๐ Anomaly Types & Refinements
| Anomaly Type | Detection Method | Automatic Refinement |
|--------------|------------------|----------------------|
| ๐ฐ **Price Manipulation** | Price >2% from cross-exchange mean | Increase coherence threshold +0.1<br>Reduce position size ร0.5 |
| ๐ **Wash Trading** | >15% trades at identical prices | **Blacklist for 1 hour**<br>Position size ร0.0 |
| ๐ **Orderbook Spoofing** | Bid/ask ratio >70/30 | Delay entry +60s<br>Reduce position size ร0.7 |
| ๐ **Cross-Exchange Spread** | >2% arbitrage opportunity | Use multi-exchange mean price<br>**Increase position size ร1.2** |
| โก **Latency Arbitrage** | >500ms delays | Compensate for latency<br>Reduce position size ร0.8 |
| ๐ **Volume Inflation** | Volume >3ร normal | Require higher volume confirmation |
| ๐ฏ **Frontrunning** | Suspicious <100ms patterns | Delay execution slightly |
| ๐ง **Liquidity Drain** | Orderbook collapse | Avoid until liquidity returns |
| ๐จ **Exchange Outage** | Missing data feeds | Skip exchange temporarily |
---
## ๐ Usage
### Basic Integration (Disabled by Default)
```python
from aureon_unified_ecosystem import AureonKrakenEcosystem
# Without CoinAPI (default)
eco = AureonKrakenEcosystem(initial_balance=200.0, dry_run=True)
# Anomaly detection is disabled but methods are available
```
### With CoinAPI Key
```python
import os
os.environ['COINAPI_KEY'] = 'your-key-here'
os.environ['ENABLE_COINAPI'] = '1'
from aureon_unified_ecosystem import AureonKrakenEcosystem
eco = AureonKrakenEcosystem(initial_balance=200.0, dry_run=True)
# CoinAPI anomaly detection is now ACTIVE
```
### Manual Anomaly Scanning
```python
# Scan specific symbols for anomalies
symbols = ['BTC/USD', 'ETH/USD', 'BNB/USD']
anomalies = eco.auris.scan_for_anomalies(symbols)
for anom in anomalies:
print(f"Detected: {anom['type']}")
print(f"Severity: {anom['severity']:.0%}")
print(f"Recommendation: {anom['recommendation']}")
```
### Check Blacklist Status
```python
# Check if symbol is blacklisted
if eco.auris.is_symbol_blacklisted('BTC/USD'):
print("BTC/USD is blacklisted due to anomaly")
# Get coherence adjustment
adjustment = eco.auris.get_coherence_adjustment('ETH/USD')
print(f"ETH/USD coherence threshold: ร{adjustment:.2f}")
```
---
## ๐ Integration with HNC + Probability Matrix
The CoinAPI layer works seamlessly with existing systems:
```
1. CoinAPI Anomaly Detection
โ (Filters & Adjusts)
2. Opportunity Filtering
โ (Blacklist check)
3. HNC Frequency Analysis
โ (Harmonic bonus/penalty)
4. Probability Matrix
โ (2-hour forecast)
5. Final Score & Position Sizing
โ
6. Trade Execution
```
### Example Flow
```python
# Symbol goes through complete pipeline:
# 1. CoinAPI detects orderbook spoofing on XYZ/USD
# โ Coherence threshold increased to 0.75 (from 0.65)
# 2. Opportunity filtering
# โ XYZ/USD requires 0.75 coherence instead of 0.65
# 3. HNC analysis shows 528Hz harmonic
# โ +15 score bonus, ร1.15 position size
# 4. Probability matrix shows 88% probability
# โ +20 score bonus
# 5. Final score: 145
# โ Passes thresholds despite higher coherence requirement
# โ Position opened with adjusted size
```
---
## ๐งช Testing
### Run Comprehensive Test Suite
```bash
python test_coinapi_integration.py
```
Tests include:
- โ
Blacklist functionality
- โ
Coherence adjustments
- โ
Anomaly refinement logic
- โ
Opportunity filtering
- โ
Full system integration
### Standalone CoinAPI Demo
```bash
python coinapi_anomaly_detector.py
```
Demonstrates:
- Cross-exchange price analysis
- Anomaly detection with simulated data
- Algorithm refinement recommendations
---
## ๐ฐ CoinAPI Free Tier
| Feature | Limit |
|---------|-------|
| **Requests/Day** | 100 |
| **Exchanges** | 300+ |
| **Data Types** | OHLCV, Quotes, Orderbooks, Trades |
| **Historical** | Limited |
| **WebSocket** | No (REST only) |
### Optimization Strategy
With 100 requests/day and 5-minute scans:
- **288 potential scans/day** (1 request per scan)
- **Scan 3-5 symbols per scan** (15-25 symbols covered)
- **Rotate through top opportunities** (not all symbols)
- **Cache results aggressively** (minimize duplicate calls)
---
## ๐ Real-World Example
### Detected Anomaly Report
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐โก COINAPI ANOMALY REPORT: BTC/USD โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ Exchanges Analyzed: 5 โ
โ Mean Price: $69420.50 โ
โ Price StdDev: $350.25 โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ DETECTED ANOMALIES: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ 1. ๐ฐ Price Manipulation โ
โ Severity: โโโโโโโโโโ 75% โ
โ Price 4.2% away from cross-exchange mean โ
โ โ AVOID โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ 2. ๐ Cross-Exchange Spread โ
โ Severity: โโโโโโโโโโ 45% โ
โ 2.8% arbitrage spread between exchanges โ
โ โ ARBITRAGE OPPORTUNITY โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ ALGORITHM REFINEMENTS: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ 1. Price manipulation detected - require higher confidence โ
โ โข coherence_threshold: +0.1 โ
โ โข position_size: ร0.5 โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ 2. Arbitrage opportunity - use aggregated price โ
โ โข price_source: multi_exchange_mean โ
โ โข position_size: ร1.2 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
---
## ๐ฏ Key Benefits
### 1. **Data Quality Validation**
- Cross-validates exchange data
- Detects feed manipulation
- Uses professional-grade aggregated data
### 2. **Adaptive Algorithm**
- Automatically adjusts to market conditions
- Learns from anomalies
- Refines thresholds in real-time
### 3. **Risk Reduction**
- Avoids manipulated markets
- Identifies wash trading
- Detects spoofed liquidity
### 4. **Opportunity Enhancement**
- Finds arbitrage opportunities
- Uses better pricing data
- Exploits market inefficiencies
---
## ๐ฎ Future Enhancements
1. **Historical Anomaly Analysis**
- Track anomaly patterns over time
- Machine learning on refinement effectiveness
2. **Exchange Reputation Scoring**
- Rate exchanges by data quality
- Prefer reliable exchanges
3. **Real-Time WebSocket Integration**
- Upgrade to paid tier for WebSocket
- Sub-second anomaly detection
4. **Cross-Asset Correlation**
- Detect manipulation across asset pairs
- Systemic risk indicators
---
## ๐ Files
| File | Purpose |
|------|---------|
| `coinapi_anomaly_detector.py` | Core anomaly detection engine |
| `test_coinapi_integration.py` | Comprehensive test suite |
| `aureon_unified_ecosystem.py` | Integration into main system |
| `COINAPI_INTEGRATION.md` | This documentation |
---
## ๐ Success Metrics
Track these metrics to measure CoinAPI value:
- **Anomalies Detected** - Total count by type
- **Symbols Blacklisted** - Wash trading avoidance
- **Refinements Applied** - Algorithm adjustments
- **Arbitrage Opportunities** - Cross-exchange spreads found
- **False Positive Rate** - Good trades filtered incorrectly
- **Performance Impact** - Win rate before/after CoinAPI
---
## ๐ Conclusion
CoinAPI integration adds a **professional data validation layer** to our trading system. By detecting anomalies in cross-exchange data, we:
โ
Avoid manipulated markets
โ
Identify real arbitrage opportunities
โ
Automatically refine our algorithms
โ
Use higher-quality aggregated pricing
โ
Reduce false signals and bad trades
The system now combines:
1. **HNC Frequency Analysis** (Solfeggio harmonics)
2. **Probability Matrix** (2-hour temporal forecasting)
3. **CoinAPI Anomaly Detection** (cross-exchange validation)
**Result**: A self-improving trading algorithm that learns from market anomalies and adapts in real-time.
---
*"The Truth is in the Anomalies"*
**Gary Leckey | November 30, 2025**
|