MARGINPAD
Home / Blog / Python trading bot

Python Crypto Trading Bot Tutorial (2026): Build & Test One Free, No Deposit

Guides · 8 min read · Updated June 2026

Trading bots are everywhere in 2026, and Python is the easiest way to build one. The good news: you don't need a fancy framework or a funded exchange account to start. In this tutorial you'll build a working crypto trading bot in Python and test it against real live prices with fake money — free, no deposit. (Educational only — not financial advice.)

What you'll build

A simple momentum bot: it watches a coin, and when the short-term trend flips up it opens a small long; when the trend flips down it closes. Every order goes to a paper-trading API that fills at the real live price and simulates leverage, stop-loss, take-profit and liquidation — so a bug costs you nothing instead of your account.

Step 1 — get a free API key

Go to the MarginPad trading API, sign in with an email (no password, no KYC) and generate your key. That's the only setup. Price and candle endpoints are keyless, so you can try one right now in your browser:

# live price, no key needed
curl "https://marginpad.io/api/bot/v1/price?symbol=BTC"

Step 2 — the price feed in Python

Everything runs on the requests library. Pull a live price and some candles for your signal:

import requests

API = "https://marginpad.io/api/bot"
KEY = "YOUR_KEY"
H = {"X-API-Key": KEY}

def price(sym="BTC"):
    return requests.get(f"{API}/v1/price", params={"symbol": sym}).json()["price"]

def closes(sym="BTC", interval="60", n=50):
    k = requests.get(f"{API}/v1/klines", params={"symbol": sym, "interval": interval}).json()
    return [c["close"] for c in k[-n:]]

Step 3 — the strategy (EMA cross)

A classic momentum rule: when a fast average crosses above a slow one, momentum is up. No libraries needed:

def ema(vals, period):
    k = 2 / (period + 1)
    e = vals[0]
    for v in vals[1:]:
        e = v * k + e * (1 - k)
    return e

def signal(sym="BTC"):
    c = closes(sym)
    fast, slow = ema(c[-9:], 9), ema(c[-21:], 21)
    return "long" if fast > slow else "flat"

Step 4 — open and close positions

Now wire the signal to real (paper) orders. Open a leveraged position with an optional stop-loss and take-profit; close it when the trend flips:

def open_long(sym="BTC", usd=50, lev=10):
    p = price(sym)
    return requests.post(f"{API}/v1/open", headers=H, json={
        "symbol": sym, "side": "long", "margin_usd": usd, "leverage": lev,
        "sl": p * 0.97, "tp": p * 1.06,
    }).json()

def close_all():
    return requests.post(f"{API}/v1/close_all", headers=H).json()

def positions():
    return requests.get(f"{API}/v1/positions", headers=H).json()

Step 5 — the bot loop

Put it together: check the signal on a schedule, open when it turns long, close when it goes flat, and print your live P&L.

import time

state = "flat"
while True:
    sig = signal("BTC")
    if sig == "long" and state == "flat":
        print("opening", open_long("BTC"))
        state = "long"
    elif sig == "flat" and state == "long":
        print("closing", close_all())
        state = "flat"
    pos = positions().get("positions", [])
    pnl = sum(p.get("unrealized_pnl_usd", 0) for p in pos)
    print(f"signal={sig} open={len(pos)} uPnL=${pnl:.2f}")
    time.sleep(60)

That's a complete, running crypto trading bot — and it never touches real money. Positions liquidate exactly like a real exchange when price crosses the level, and your stop-loss/take-profit execute automatically.

Step 6 — measure, then decide

Let it run for days across different conditions. Check GET /v1/account for your win rate and realised P&L, and review the results in the Trading Journal. Compare it to a plain backtest — if the live-price forward test diverges from the backtest, that gap is exactly the bugs and overfitting a backtest hides. Only fund a real account once the paper bot has proven itself. The full workflow is in how to test a crypto trading bot, and for the AI angle see can AI trade crypto futures.

TEST YOUR BOT FOR FREE

A free paper-trading REST API with real live prices — leverage, SL/TP, partial closes and liquidation simulation. Get an API key in 30 seconds.

Get your free API key →

Comments