Adding a New Exchange to Horizon
This guide will walk you through the process of adding support for a new cryptocurrency exchange to the Horizon system. The Horizon system uses CCXT for exchange integrations and follows a modular architecture for easy expansion.
Prerequisites
Before starting, ensure you have:
- Access to the exchange’s API documentation
- API credentials (key, secret, and password if required)
- Understanding of the exchange’s order structure and market types
Step 1: Add Exchange Constants
First, add your exchange to the supported exchanges list in watcher/constants/index.js:
Step 2: Create Exchange Module
Create a new file watcher/modules/exchanges/your-exchange.js
with the following structure:
Step 3: Register the Exchange Module
Add your exchange module to the registry in watcher/modules/exchanges/index.js
:
Step 4: Configure the Exchange
Add exchange configuration to your config/default.json
:
Step 5: Test the Integration
- Create test orders on the exchange (preferably in demo/testnet mode)
- Start the Horizon watcher with your exchange:
- Monitor the logs for order processing and signal generation
Common Challenges and Solutions
Symbol Format Differences
Many exchanges use different symbol formats. You might need to transform them:
Order Status Mapping
Exchanges often use different terms for order statuses. Update watcher/modules/ccxt/orderStatus.js
if needed:
Balance Structure
If the exchange has a unique balance structure, implement the transformation:
Testing Guidelines
- Test all order types (market, limit, etc.)
- Verify leverage calculation
- Check balance updates
- Confirm WebSocket updates
- Validate signal generation
- Test error handling
Best Practices
- Error Handling: Always include proper error handling in exchange-specific code
- Logging: Add meaningful logs for debugging
- Documentation: Document any exchange-specific behaviors or limitations
- Configuration: Make exchange-specific settings configurable
- Testing: Test with both live and demo accounts
Troubleshooting
Common issues and solutions:
-
Order not detected
- Check symbol format matching
- Verify order status mapping
- Enable debug logging
-
Invalid leverage calculation
- Verify balance structure
- Check position value calculation
- Log intermediate values
-
WebSocket disconnections
- Implement reconnection logic
- Add heartbeat checks
- Monitor connection state
Security Considerations
- Never commit API credentials
- Use environment variables for sensitive data
- Implement rate limiting
- Handle API errors gracefully
- Validate all exchange responses
Additional Resources
- CCXT Documentation: https://docs.ccxt.com/
- Exchange API Documentation Links
- Horizon Issue Tracker: https://github.com/taoshidev/horizon/issues