The Botfather: Building Your First Crypto Trading Bot
The Botfather: Building Your First Crypto Trading Bot
教父级指南:构建你的第一个加密货币交易机器人
The Quest Begins (The “Why”)
探索的起点(“为什么”要这样做)
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 Revelation (The Insight)
启示(洞察力)
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: Connects to an exchange (I used Binance’s testnet so I wouldn’t lose real money). Polls the ticker for a symbol at a reasonable interval. Checks a simple condition—like “price > 20 % above the 20‑period moving average”. Places a market order if the condition holds, then waits for the next cycle. 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.
当意识到我不需要从零开始构建高频交易引擎时,我迎来了“顿悟”时刻。市面上已经有稳健且经过充分测试的库来处理那些繁琐的部分——身份验证、速率限制、WebSocket 连接——所以我可以将精力集中在逻辑上。利用 CCXT(一个统一的加密货币交易所库)和一点点 asyncio,我可以编写一个机器人:连接到交易所(我使用了币安的测试网,这样就不会亏损真金白银);以合理的间隔轮询某个交易对的行情;检查一个简单的条件——比如“价格高于 20 期移动平均线 20%”;如果条件成立则下市价单,然后等待下一个周期。当机器人最终在没有崩溃或触发速率限制的情况下执行交易时,那种感觉就像《黑客帝国》里的尼奥躲避子弹一样。这种解脱感是真实的:现在我可以让代码负责盯盘,而我则可以去构思下一个点子。
Wielding the Power (Code & Examples)
掌握力量(代码与示例)
The Struggle – A Naïve Loop
挣扎——一个天真的循环
My first attempt was a blocking while True loop with time.sleep. It looked harmless, but it had two nasty traps:
Trap #1 – No error handling. A network hiccup would raise an exception and kill the whole script.
Trap #2 – Ignoring rate limits. Binance lets you make only a certain number of requests per minute; hammering the API got me banned from the testnet quickly.
我的第一次尝试是一个带有 time.sleep 的阻塞式 while True 循环。它看起来无害,但有两个讨厌的陷阱:
陷阱 1:没有错误处理。网络波动会引发异常并导致整个脚本终止。
陷阱 2:忽略速率限制。币安限制了每分钟的请求次数;频繁调用 API 很快就让我被测试网封禁了。
# 🚫 Naïve version – don't use this in production
# 🚫 天真版本——请勿在生产环境中使用
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']
# super‑naive condition: buy if price > 20000
# 超级天真的条件:如果价格 > 20000 则买入
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
The Victory – A Clean, Async Bot
胜利——一个整洁的异步机器人
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.
这是在我的深夜测试中幸存下来的版本。我将交易所封装在一个异步助手函数中,添加了适当的异常处理,并通过在每次调用后 await exchange.sleep(exchange.rateLimit) 来遵守内置的速率限制器。
# ✅ Async, resilient bot – feel free to copy & experiment
# ✅ 异步、稳健的机器人——欢迎复制并尝试
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}")
# Example condition: price above 20‑period simple moving average
# 示例条件:价格高于 20 期简单移动平均线
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?
- Async I/O lets the bot pause without blocking the whole thread, making it easy to add more logic later (e.g., multiple symbols, websocket streams).
- Granular error handling keeps the script alive when the internet blips or the exchange returns an error.
- Rate‑limit awareness (
exchange.rateLimit) ensures we stay well within the exchange’s limits, preventing those dreaded 429 responses. - Testnet first – always test on Binance’s testnet (or any exchange’s sandbox) before risking real funds.
发生了什么变化?
- 异步 I/O 让机器人可以在不阻塞整个线程的情况下暂停,这使得以后添加更多逻辑(例如多个交易对、WebSocket 流)变得容易。
- 细粒度的错误处理 在网络波动或交易所返回错误时,能让脚本保持运行。
- 速率限制意识 (
exchange.rateLimit) 确保我们保持在交易所的限制范围内,避免了可怕的 429 错误响应。 - 优先使用测试网 —— 在冒着真金白银的风险之前,务必先在币安测试网(或任何交易所的沙盒环境)进行测试。
Common Traps to Avoid
常见的避坑指南
-
Fetching too much data too often. If you pull klines for every loop iteration without caching, you’ll hammer the API. In the example, we request only the last 20 candles—enough for a simple SMA but light on bandwidth.
-
Ignoring order status. Placing an order doesn’t guarantee it filled immediately, especially in low‑liquidity markets. A production bot should poll
fetch_orderor listen to user‑data websockets to know when the trade is actually executed. -
过于频繁地获取过多数据。 如果你在每次循环迭代中都拉取 K 线而不进行缓存,你会把 API 搞垮。在示例中,我们只请求了最近的 20 根 K 线——这对于简单的 SMA 来说足够了,且带宽占用很小。
-
忽略订单状态。 下单并不保证立即成交,尤其是在流动性较低的市场中。生产环境的机器人应该轮询
fetch_order或监听用户数据 WebSocket,以确认交易何时真正执行。
Why This New Power Matters
为什么这种新能力很重要
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.
现在你已经拥有了一个可运行的框架,真正的乐趣才刚刚开始。你可以将天真的 SMA 条件替换为你喜欢的任何指标:RSI 阈值、布林带突破,甚至是来自 Twitter 的简单情绪得分。因为交易所的处理逻辑已经被抽象化了,你可以将精力花在策略上,而不是底层的“管道”搭建上。