Development7 min read2026-11-23

Build Your Own Risk Rules or Use a Risk API? A Comparison for Crypto Bots and Agents

When a few lines of your own code are the right position limit for a crypto bot or agent, when an external risk API is worth the dependency, and what each option costs you.

By Alvaro Villacorta, Founder, RiskState

Short answer

If your limit is one rule, such as a fixed maximum size, a daily loss stop or a moving-average filter, write it yourself: it is a few lines and you understand all of it. An external risk API earns its place in three cases: the limit depends on data you would have to collect and keep working (derivatives positioning, on-chain, macro, a DeFi loan), several bots or agents must obey the same limit, or someone else has to verify afterwards which limit applied. Many setups use both: a fixed local ceiling plus an external limit, whichever is lower.

The short answer

If your limit is one rule, such as a fixed maximum size, a daily loss stop or a moving-average filter, write it yourself: it is a few lines and you understand all of it. An external risk API earns its place in three cases: the limit depends on data you would have to collect and keep working (derivatives positioning, on-chain, macro, a DeFi loan), several bots or agents must obey the same limit, or someone else has to verify afterwards which limit applied. Many setups use both: a fixed local ceiling plus an external limit, whichever is lower.

The rest of this article goes through what each option involves. We build a risk API, so read it with that in mind; we have tried to state the case for writing your own rules as we would if we did not.

What you can build in an afternoon

Four rules cover most of what a single bot needs, and none of them requires outside data beyond the price history you already have.

• A fixed ceiling. No position above a set fraction of the account, measured as exposure (stake times leverage), not as collateral. • A volatility scale. Measure 30-day realized volatility and cut the ceiling when it rises above its usual range. • A trend filter. Halve the ceiling, or stop opening positions, while price is below a moving average such as the 50-day. • A loss stop. No new entries after a set daily or weekly loss.

These rules are a legitimate choice and not a beginner's shortcut. They are transparent, they have no dependency that can go down, and you can test them against your own strategy in an hour. If your question is only how much to hold in a quiet market versus a falling one, they may be all you need. We do not sell RiskState as a way to earn more than a simple rule would. We measured it: over 2018–2026 at equal exposure, our size series ties a bare 50-day filter on BTC and trails it on ETH. The figures are in score_v4 against simple rules.

Where your own rules get expensive

The rules are cheap. Three things around them are not.

Inputs. As soon as a rule depends on something other than price, such as funding rates, open interest, on-chain valuation, macro series or the health factor of a DeFi loan, you are running a data pipeline. Sources change their response format, close free tiers and block cloud IP ranges. The failure that matters is the quiet one: a fetch fails, the code falls back to a default, and the rule keeps running on a constant. We found exactly this in our own recorder, where several inputs had been defaults for months, and wrote about it in why crypto signals fail out of sample.

Enforcement. A limit is only as good as the place it is applied. In Freqtrade, a size cap written in the usual callback does not apply to position increases and is skipped when the callback throws; we describe the cases in three ways a position size limit fails silently. Every framework has its own version of this.

The undecided case. Your rule needs an answer for the moment it cannot compute: data is stale, the source is down, a number is missing. Refusing the entry and falling back to a fixed ceiling are both defensible. Having no answer means the bot trades at full size exactly when it knows least.

What an external risk API gives you

A risk API moves the limit, and the data behind it, out of your process. Your bot sends the asset and reads back a maximum size, a list of blocked actions and the reason that is binding. With RiskState that is one call to the RiskState API, which returns max_size_fraction and blocked_actions for BTC and ETH.

• Maintained inputs. Derivatives positioning, on-chain valuation, macro and, if you pass a wallet, the health factor of a Spark or Aave loan, with fallbacks for each source and a field that reports which source answered. • One limit for everything. Several bots, an agent and a manual desk can read the same limit, so two of them cannot each take the full allowance. • A record someone else can check. Every response carries a policy_hash over its inputs. Six months later you can show which limit applied to a trade, and a third party can recompute it. • A defined answer when data is missing. If price, RSI or funding cannot be read, the API returns an error instead of a limit computed on defaults, and the client is expected to refuse the entry.

What it costs you

• A dependency. A network call sits in front of your entries. You have to decide what the bot does when the call fails, and test that path. • Less control. You cannot tune the logic. RiskState's scoring is frozen for six months at a time, which protects the record and also means a rule you disagree with stays until the next version. • Narrow coverage. BTC and ETH only. A bot that trades forty altcoins needs its own rules for thirty-eight of them. • Someone else's judgement. The limit reflects our reading of what makes conditions dangerous. You can read how it is built, but it is not derived from your strategy's own results. • Price. The API is free during the beta; the paid tiers that follow are on the pricing page.

How to decide

Write your own rules when you run one bot, the limit depends only on price and your own profit and loss, and nobody but you needs to trust the record.

Use an external limit when the conditions you want to react to need data you do not want to maintain, when more than one system trades the same capital, when the trader is an AI agent whose prompt you cannot fully trust (see guardrails for why the limit has to live outside the model), or when an investor, a risk officer or a client will ask what limit was in force.

Use both when you can. Keep a fixed ceiling in your own code that never depends on anyone, and apply the external limit on top, taking the lower of the two. Our Freqtrade connector, riskstate-guard, works this way: a local ceiling, the API limit, and a refusal by default when no valid limit is available.

Trying it without changing your orders

Whichever external limit you evaluate, run it in shadow first: log what it would have done next to what your bot did, for a few weeks, and read the differences. riskstate-guard has a shadow mode for this, and the keyless connector at /connect lets you ask Claude or ChatGPT for the current BTC and ETH limits without writing any code. If the differences are not ones you would have wanted, keep your own rules.

Compare it with your own rules

Shadow mode logs every decision next to your bot's own and leaves your orders untouched.