What You’ll Build
A Python-based DCA (Dollar Cost Averaging) bot that:
- Buys a fixed amount of BTC at regular intervals
- Logs all trades to a CSV file
- Sends you a summary via the console
- Can run 24/7 on a VPS or your local machine
A $50/week BTC DCA started in January 2022 — right before the crash — would be worth over $25,000 today on a $10,500 investment. That’s +140% returns through the worst crypto winter in history. This bot automates that discipline, removing the emotional hesitation that makes most people stop buying when prices drop.
Prerequisites
- Python 3.10+ with a virtual environment
- Binance API keys (see our setup guide)
- Claude Code installed
Step 1: Describe the Bot to Claude Code
Open Claude Code in your project directory and give it this prompt:
Build a DCA trading bot with these specs:
- Buy $10 of BTC/USDT every 4 hours on Binance
- Use ccxt for exchange connection
- Load API keys from .env
- Log each trade to trades.csv with timestamp, price, and amount
- Add error handling for network issues
- Add a dry-run mode that simulates trades without placing real orders
Step 2: Review the Generated Code
Claude Code will produce a complete script using the ccxt library to handle exchange connectivity. Here’s the core structure you should see:
import ccxt
import csv
import os
import time
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
DRY_RUN = os.getenv('DRY_RUN', 'true').lower() == 'true'
BUY_AMOUNT_USD = 10
INTERVAL_SECONDS = 4 * 60 * 60 # 4 hours
SYMBOL = 'BTC/USDT'
def create_exchange():
return ccxt.binance({
'apiKey': os.getenv('BINANCE_API_KEY'),
'secret': os.getenv('BINANCE_API_SECRET'),
})
def execute_buy(exchange):
ticker = exchange.fetch_ticker(SYMBOL)
price = ticker['last']
amount = BUY_AMOUNT_USD / price
if DRY_RUN:
print(f"[DRY RUN] Would buy {amount:.6f} BTC at ${price:,.2f}")
else:
order = exchange.create_market_buy_order(SYMBOL, amount)
print(f"[LIVE] Bought {amount:.6f} BTC at ${price:,.2f}")
log_trade(price, amount)
return price, amount
def log_trade(price, amount):
with open('trades.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow([datetime.now().isoformat(), price, amount, DRY_RUN])
def main():
exchange = create_exchange()
print(f"DCA Bot started ({'DRY RUN' if DRY_RUN else 'LIVE'})")
while True:
try:
execute_buy(exchange)
except Exception as e:
print(f"Error: {e}")
time.sleep(INTERVAL_SECONDS)
if __name__ == '__main__':
main()
Step 3: Test in Dry Run Mode
Run the bot with DRY_RUN=true (default):
python dca_bot.py
You should see simulated buy orders logged to your terminal and trades.csv.
Step 4: Go Live
When you’re ready to trade with real money:
DRY_RUN=false python dca_bot.py
Warning: Start with the minimum possible amount. Monitor the bot closely for the first few cycles.
Key Improvements to Ask Claude Code For
Once the basic bot works, ask Claude Code to add:
- Telegram notifications on each trade
- A maximum daily spend limit
- Automatic stop if price drops more than 20% in 24h
Next Steps
- Compare: no-code DCA — See how OpenClaw handles the same strategy without writing code in our OpenClaw BTC DCA tutorial
- Prerequisite: API setup — If you skipped it, follow the full Claude Code + Binance API setup guide first