A sophisticated Dollar-Cost Averaging (DCA) bot for cryptocurrency markets with backtesting, parameter optimization, and risk management features. Designed for Binance exchange with testnet support.
-
Historical Data Management
- Automated OHLCV data fetching
- SQLite storage with deduplication
- Multiple timeframe support
-
Strategy Backtesting
- Parameter grid search optimization
- Advanced metrics calculation (Sharpe Ratio, Max Drawdown)
- Vectorized backtesting engine
-
Live Trading
- Testnet (paper trading) support
- Real-time order execution
- Partial fill handling
- Multi-threaded architecture
-
Risk Management
- Portfolio drawdown monitoring
- Emergency liquidation triggers
- Position size optimization
-
Adaptive Logic
- Daily parameter re-optimization
- Shadow trading validation
- Strategy health checks
- Python 3.8+
- Binance API keys (testnet supported)
- CCXT library
- Pandas/Numpy for quantitative analysis
- SQLite for local storage
-
Clone repository:
git clone https://github.com/aaurelions/DCABot cd DCABot
-
Install dependencies:
pip install ccxt pandas numpy sqlite3
-
Create configuration file (
config.json
):{ "apiKey": "your_api_key", "secret": "your_api_secret", "testnet": true }
-
Initialize database:
from dca_bot import DCABot bot = DCABot() bot.insert_test_config() # Creates sample configuration
Parameter | Description | Default |
---|---|---|
base_order | Initial position size (USD) | 100 |
dca_order | Follow-up DCA size (USD) | 50 |
price_deviation | Required drop for DCA trigger (%) | 2.0 |
take_profit | Profit target (%) | 5.0 |
max_dca_orders | Maximum DCA levels | 5 |
dca_multiplier | DCA spacing multiplier | 1.2 |
cooldown | Minimum time between rounds (sec) | 3600 |
erDiagram
tokens_config ||--o{ dca_rounds : symbol
dca_rounds ||--o{ dca_trades : round_id
historical_data {
string symbol
datetime timestamp
float open
float high
float low
float close
float volume
}
backtest_results {
string symbol
string params_hash
float total_profit
float win_rate
float max_drawdown
float sharpe_ratio
}
# Initialize bot
bot = DCABot(config_path='config.json')
# Fetch historical data (1-minute resolution, 100 days)
bot.fetch_historical_data('BTC/USDT', '1m', days=100)
# Run backtest with parameter grid
param_ranges = {
'price_deviation': [1.5, 2.0, 2.5],
'take_profit': [3.0, 4.0, 5.0],
'dca_multiplier': [1.0, 1.2, 1.5]
}
best_params = bot.backtest_strategy('BTC/USDT', param_ranges)
# Start live trading if profitable
bot.execute_dca_strategy_if_profitable('BTC/USDT', param_ranges)
Daily Optimization Loop
# Runs parameter optimization every 24h
bot.optimize_parameters()
Portfolio Protection
# Force close all positions if 10% drawdown
bot._check_portfolio_drawdown(threshold=0.1)
Shadow Trading
# Validate strategy with fresh market data
bot.run_backtest_validation('ETH/USDT')
-
Data Preparation
- Clean OHLCV data with outlier detection
- Slippage modeling (0.02% default)
- Exchange fee simulation (0.1% taker fee)
-
Strategy Logic
graph TD A[Base Order] -->|Price < Avg-Avg*Deviation| B[DCA Order] A -->|Price > Avg+Avg*TP| C[Take Profit] B --> D{Max DCA Reached?} D -->|Yes| E[Emergency Close] D -->|No| A
-
Metrics Calculation
- Sharpe Ratio: Risk-adjusted returns
- Max Drawdown: Worst peak-to-trough decline
- Win Rate: Profitable trade percentage
- Position sizing based on volatility
- Circuit breakers for extreme market moves
- Time-based order expiration
- Exchange connectivity monitoring
This software is for educational purposes only. Cryptocurrency trading involves significant risk of loss and is not suitable for all investors. Past performance is not indicative of future results.
MIT License