cd /news/developer-tools/the-botfather-building-your-first-cr… · home topics developer-tools article
[ARTICLE · art-35145] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

The Botfather: Building Your First Crypto Trading Bot

A developer built a simple crypto trading bot using CCXT and asyncio to automate repetitive manual trading tasks. The bot watches market conditions and executes trades based on a moving average strategy, handling rate limits and errors asynchronously.

read4 min views1 publishedJun 20, 2026

Honestly, I was tired of staring at charts at 2 a.m., trying to catch that perfect entry while my coffee went cold. I’d set a manual alert, jump onto the exchange, click “buy”, and then second‑guess myself as the price slipped away. It felt like I was playing a never‑ending game of Whac‑A‑Mole, and I kept losing the mole.

One night, after yet another missed opportunity, I thought: What if I could offload the repetitive bits to a script? Not a fancy AI that predicts the future—just a simple bot that watches the market, checks a condition, and places an order when the condition is met. If I could automate the boring part, I could focus on strategy, learning, and maybe even get some sleep. That was the dragon I wanted to slay: the exhaustion of manual trading.

The big “aha!” moment came when I realized I didn’t need to build a high‑frequency trading engine from scratch. There are solid, well‑tested libraries that handle the messy bits—authentication, rate limits, WebSocket connections—so I could concentrate on the logic.

Using CCXT (a unified crypto exchange library) and a touch of asyncio, I could write a bot that:

It felt like Neo dodging bullets in The Matrix when the bot finally executed a trade without crashing or getting rate‑limited. The relief was genuine: I could now let the code do the watching while I worked on the next idea.

My first attempt was a blocking while True

loop with time.sleep

. It looked harmless, but it had two nasty traps:

import time
import ccxt

exchange = ccxt.binance({
    'apiKey': 'YOUR_TESTNET_KEY',
    'secret': 'YOUR_TESTNET_SECRET',
    'enableRateLimit': True,  # we set it but never respected it properly
    'options': {'defaultType': 'future'}
})

symbol = 'BTC/USDT'

while True:
    ticker = exchange.fetch_ticker(symbol)
    price = ticker['last']
    if price > 20000:
        order = exchange.create_market_buy_order(symbol, 0.001)
        print('Bought!', order)
    time.sleep(5)  # sleeping 5 s still blows past rate limits if we fetch other endpoints

Here’s the version that survived my late‑night tests. I wrapped the exchange in an async helper, added proper exception handling, and respected the built‑in rate limiter by awaiting exchange.sleep(exchange.rateLimit)

after each call.

import asyncio
import ccxt.async_support as ccxt  # note the async version
from datetime import datetime

async def main():
    exchange = ccxt.binance({
        'apiKey': 'YOUR_TESTNET_KEY',
        'secret': 'YOUR_TESTNET_SECRET',
        'enableRateLimit': True,
        'options': {'defaultType': 'future'},
    })

    symbol = 'BTC/USDT'
    qty = 0.001   # adjust to your testnet balance

    while True:
        try:
            ticker = await exchange.fetch_ticker(symbol)
            price = ticker['last']
            print(f"[{datetime.now().isoformat()}] {symbol} @ {price}")

            ohlcv = await exchange.fetch_ohlcv(symbol, timeframe='1m', limit=20)
            close_prices = [c[4] for c in ohlcv]   # close price is index 4
            sma = sum(close_prices) / len(close_prices)

            if price > sma * 1.02:   # 2 % above SMA → buy signal
                print("🚀 BUY signal! Placing market order...")
                order = await exchange.create_market_buy_order(symbol, qty)
                print(f"✅ Order placed: {order['id']}")
                await asyncio.sleep(10)
            else:
                print("🔎 No signal yet.")
        except ccxt.NetworkError as e:
            print(f"⚠️ Network issue: {e}. Retrying in 15 s...")
            await asyncio.sleep(15)
        except ccxt.ExchangeError as e:
            print(f"❌ Exchange error: {e}. Skipping this cycle.")
            await asyncio.sleep(5)
        except Exception as e:
            print(f"💥 Unexpected error: {e}. Waiting 20 s...")
            await asyncio.sleep(20)

        await asyncio.sleep(exchange.rateLimit / 1000)

    await exchange.close()

if __name__ == '__main__':
    asyncio.run(main())

What changed?

exchange.rateLimit

) ensures we stay well within the exchange’s limits, preventing those dreaded 429 responses. fetch_order

or listen to user‑data websockets to know when the trade is actually executed. Now that you’ve got a working skeleton, the real fun begins. You can swap the naïve SMA‑based condition for anything you like: RSI thresholds, Bollinger Band breaks, even a simple sentiment score pulled from Twitter. Because the exchange handling is abstracted away, you spend your energy on strategy, not on plumbing.

Imagine waking up to find your bot has captured a few profitable moves while you were asleep, or watching it react instantly to a sudden spike—something you’d never catch manually. That feeling of “the machine is working for me” is addictive in the best way.

Plus, the skills you’ve just practiced—async programming, API interaction, error resilience—translate directly to other domains: web scraping, IoT device management, or even building microservices for a startup.

Grab the code above, run it on Binance’s testnet, and add one extra rule: only allow a buy if the 24‑hour volume is above a certain threshold (say, 100 BTC). Hint: exchange.fetch_ticker(symbol)

already returns a quoteVolume

field you can use.

When you get it working, tweak the condition, experiment with different timeframes, or try deploying it on a VPS with a simple cron job.

What’s the first strategy you’ll automate? Drop a comment or tweet your results—I can’t wait to see what you build!

Happy coding, and may your spreads be tight and your slippage low!

── more in #developer-tools 4 stories · sorted by recency
── more on @ccxt 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/the-botfather-buildi…] indexed:0 read:4min 2026-06-20 ·