Development6 min read2026-11-17

Three Ways a Position Size Limit in Freqtrade Fails Silently

A size cap written in a Freqtrade callback can stop applying without any error: on an exception, on position increases, and with leverage. What we found and how to close each gap.

The limit you think you have

Most Freqtrade strategies that limit position size do it in one place: the custom_stake_amount callback, sometimes with a second check in confirm_trade_entry. It looks complete. The strategy proposes a stake, your code cuts it, the order goes out smaller.

We built a guard that enforces an external size limit inside Freqtrade, and tested it in dry-run on spot and on 5x futures. Along the way we found three cases where a limit written this way stops applying, and in none of them does the bot stop or the order fail. This post describes each one. They apply to any size limit, not only ours. Everything here was checked against the source and in live dry-run sessions of Freqtrade 2026.7.

1. An exception in your callback is read as approval

Freqtrade wraps every strategy callback so that a bug in user code does not bring the bot down. When a callback raises, the wrapper logs the error and returns a default value. That is a sensible design for a bot that has to keep running.

The defaults are what matter. For confirm_trade_entry the default is True: the entry is confirmed. For custom_stake_amount the default is the stake Freqtrade proposed in the first place, without your cut.

So if your limit depends on anything that can fail, such as a network call, a file, a division by a balance that happens to be zero, then the moment it fails is the moment the limit disappears. The error is in the log, but the order has already gone out at full size.

The fix is to catch every exception inside the callback and return the restrictive answer yourself: False from confirm_trade_entry, zero from custom_stake_amount. A size limit must never let an exception escape.

2. Position increases skip the entry callbacks

If your strategy uses position adjustment to add to a trade, those additional orders take a different path. Freqtrade calls adjust_trade_position, takes the stake it returns, and places the order. It does not call custom_stake_amount for that order, because that callback only runs when a new trade is opened. It does not call confirm_trade_entry either, for the same reason.

We saw this directly. In our first futures run the opening entry was capped as expected, and then three position increases of five times the stake went through untouched. The limit covered the first order and nothing after it.

If you add to positions, the limit has to be applied in adjust_trade_position as well, and it has to count what the trade already holds.

3. With leverage, stake is not exposure

custom_stake_amount works in stake, which is the collateral you put up. On futures the exposure is stake times leverage. A rule that says no more than 10% of the account per trade, written against stake, allows 50% of the account in exposure at 5x.

This one is not a Freqtrade quirk, it is a units mistake, but it is easy to make because the callback hands you the stake and the leverage as separate arguments and nothing forces you to multiply them. If your limit is meant as exposure, compute stake times leverage, compare that with the limit, and divide back by leverage before returning.

One more, if you share limits as a mixin

We packaged our guard as a mixin class, the usual way to share behaviour between strategies. A mixin that defines custom_stake_amount works until the strategy defines its own custom_stake_amount and does not call super. Then the strategy's version wins and the mixin's never runs. Nothing warns you.

Our test strategy did exactly this, by accident, and that is how we found it. We changed the mixin to wrap whatever callbacks the strategy defines when the class is created, so the limit is applied to their result whether or not they call super.

What a size guard has to do

Putting the cases together, a size limit inside Freqtrade is complete only if it does five things. It never raises. It covers new entries and position increases. It measures exposure, not stake. It still applies when the strategy overrides the callbacks. And it has a defined answer for the case where it cannot decide, because its data source is down or stale.

That last point is a choice, not a detail. If the limit comes from outside, you have to decide what happens when the outside is unreachable: refuse the entry, or fall back to a fixed ceiling of your own. Either is defensible. Trading at full size because a request timed out is not.

riskstate-guard

We published the result as riskstate-guard, an MIT-licensed Python package with no dependencies beyond the standard library. It is a mixin for Freqtrade and a plain class for any other bot. It reads the position limit for BTC and ETH from the RiskState API and applies it to new entries and position increases, on exposure per asset, long or short. Without a valid policy it refuses the entry by default.

It has a shadow mode that logs what it would have done and leaves your orders alone, which is the way to start. In our dry-run on a 100,000 account, a strategy asking for 50,000 of ETH exposure at 5x was cut to 15,880, the limit at the time, and later increases were dropped because no room was left.

What it is not: it does not generate signals, it does not manage stops or exits, it is inactive in backtesting because the API serves today's policy and not a past one, and we make no claim that it improves a strategy's returns. It limits how much you can have on. Install it with pip install riskstate-guard; the documentation is at riskstate.ai/docs/freqtrade.

Try it in shadow mode

One import and one base class. In shadow mode the guard logs every decision and leaves your orders untouched.