0

Crypto Trading Bots: Design the System Before the Strategy

Crypto Trading Bots: Design the System Before the Strategy

A trading bot does more than check prices and place orders. It turns market data and strategy rules into actions, while tracking orders, balances, failures, and risk limits. Before connecting a bot to an exchange, design the system around those responsibilities. This guide covers the main components and shows how to keep decision-making separate from order execution.

Start by defining what the bot is allowed to do

Set the bot’s boundaries before you write a strategy. Decide which markets it can access, which order types it can use, how much capital it can expose, and what conditions should stop it. These are system requirements, not details to fill in after the strategy is running.

Keep three questions separate:

  • What does the strategy observe? For example, prices or other market data provided through the exchange connection.
  • What decision can it make? It might request an order, request no action, or report that the available data isn’t enough to decide.
  • What actions are permitted? A risk-control layer should check every proposed order against the bot’s configured limits before it reaches the exchange.

This separation lets you change a strategy without silently changing the rules that govern exposure. It also gives operators a clear way to see why an order was accepted or rejected.

Keep the components distinct

A useful first architecture has five parts:

  1. Market-data input receives and validates data from the exchange or another configured source.
  2. Strategy logic evaluates that data and produces an intended action.
  3. Risk checks decide whether the proposed action is within the bot’s limits.
  4. Execution turns an approved action into an exchange request and handles the response.
  5. State and monitoring record decisions, requests, responses, and errors so the system can be inspected and restarted safely.

These parts can start in one application. What matters is that each has a clear responsibility. When one function calculates a signal and sends an order, it’s harder to test each behavior on its own or tell whether a problem came from the strategy or the exchange connection.

Treat exchange integration as a boundary

An exchange connection is an external dependency. Requests can fail, responses can be delayed, and the application can lose its connection while an order is being processed. Plan for those cases; don’t assume every request gets a clear, immediate answer.

Keep a local record of each order’s intent and status. Use a unique client-side identifier when the exchange interface supports one. If a request times out, don’t submit the same order again without checking first. Reconcile local state with the exchange’s order information; otherwise, a retry could create a second order even though the first one succeeded.

Keep API credentials out of source code and logs. Give them only the permissions the bot needs, and use separate credentials for testing and live trading. A trading component that only needs to place and inspect orders shouldn’t get broader permissions by default.

Put risk checks between the strategy and execution

The strategy should propose an action. A separate control should decide whether that action is allowed. Checks might include maximum order size, maximum open exposure, permitted trading pairs, and whether the bot is enabled. The right limits depend on the system’s requirements, so document them and make them configurable rather than hiding them in strategy code.

A simplified flow might look like this:

def handle_signal(signal, account_state, limits, exchange):
    if not signal.should_trade:
        return "no action"

    order = signal.to_order()

    if order.symbol not in limits.allowed_symbols:
        return "rejected: symbol not allowed"

    if order.quantity > limits.max_order_quantity:
        return "rejected: order exceeds limit"

    if account_state.exposure + order.estimated_value > limits.max_exposure:
        return "rejected: exposure limit"

    return exchange.submit(order)

This is illustrative pseudocode, not an exchange-specific implementation. A real system also needs to run checks against current, validated state. If that state is missing or stale, decide whether the safe response is to pause and request reconciliation rather than proceed.

Make state and recovery part of the design

A process restart shouldn’t make the bot forget its outstanding orders. Persist enough information to reconstruct what the bot intended, what it requested, and what response it received. On startup, reconcile that record with the exchange before allowing new actions.

Use clear states such as pending, accepted, partially_filled, filled, cancelled, and unknown, adapting them to the exchange interface. “Unknown” is useful: it marks an unresolved outcome without claiming that a request definitely succeeded or failed. Define how the system moves from that state to a confirmed one.

Logs should make decisions auditable without exposing secrets. Record timestamps, the market or account context needed to understand a decision, the checks applied, and the exchange response. That helps distinguish a strategy decision from a transport error or an unexpected exchange response.

Test behavior, not just strategy math

A backtest can show how a strategy would have behaved with historical data, but it doesn’t test every part of a live system. Test integration and operational paths separately:

  • Does the bot reject an order outside its configured limits?
  • What happens when market data is missing or malformed?
  • How does it handle a timeout after submitting an order?
  • Can it restart and reconcile outstanding orders?
  • Does a disabled bot remain unable to submit orders?

Use a test environment or simulated exchange interface where available before enabling live credentials. Keep strategy tests deterministic: supply fixed inputs and check the decisions they produce. Test error handling explicitly. A bot that works only when every dependency responds normally isn’t ready for unattended operation.

Build in stages

Start with a narrow scope: one strategy, a limited set of markets, explicit risk limits, and observable order state. Add complexity only when there’s a clear requirement and a way to test it. Keep the strategy, exchange adapter, and risk controls understandable on their own, even if you deploy them together at first.

For a deeper discussion of performance and implementation choices, see this guide to building high-performance crypto trading bots.

A dependable bot rests on defined boundaries, careful exchange handling, explicit controls, and recoverable state—not strategy code alone. Design those foundations first, then evaluate whether the strategy behaves as intended within them.

Written by Amitesh Misra, Founder at Trident Analytical Solutions Pvt Ltd (TAS), a technology company working in AI, blockchain, and software development.


All Rights Reserved

Viblo
Let's register a Viblo Account to get more interesting posts.