A grid bot earns in a range and loses in a trend
How a grid of orders is built, what its income is actually made of, why martingale does not reduce risk but changes its shape, and what keeps the loss bounded. The code of such a bot is open.
A grid is the most common kind of ready-made trading bot. It is the easiest one to explain and the easiest one to sell: the pitch is always a variation on “it earns on the swings”. The reputation of grids is considerably better than the results they have earned over a long horizon, and this post is about the gap between the two.
The conversation is concrete: I have published the code of a grid bot, so everything below can be read in the sources and run on your own machine.
How a grid is built
A grid is a ladder of limit orders stretching away from the current price. For the long side it is built downwards: the bot takes the price as its anchor, steps away from it and places a buy order, steps again and places another, as many times as configured.
The moment a level fills, a counter-order goes out one step above it. The price ticks up, the counter-order is bought back, the level is free again and ready to fill. That is the entire mechanism.
| Level | Price | Size | Take-profit |
|---|---|---|---|
| anchor | 100.00 | — | — |
| 1 | 99.00 | 1 | 100.00 |
| 2 | 98.00 | 1 | 99.00 |
| 3 | 97.00 | 1 | 98.00 |
A grid does not guess direction. It bets on exactly one thing: that the price will come back to where it left from. While there are swings, the grid works. That is worth keeping in mind, because grids are usually sold as bots that “earn in any market”.
What the income is made of
One completed round trip comes down to a single line:
grid step − two commissions, multiplied by the size of the level.
The step and the commission are known in advance. The third multiplier — how many times the price will travel there and back — is known to no one. The entire result of a grid rests on it, and it can only be estimated from the history of the specific instrument, never from the promises of whoever is selling the bot.
The same line sets a floor under the step. Say the commission is 0.02 % per side:
- a 1 % step gives the exchange 0.04 of 1 %, that is 4 % of the income;
- a 0.1 % step gives the exchange 0.04 of 0.1 %, that is 40 % of the income;
- if the orders are filled at market rather than as limit orders, the commission is almost three times higher — and a small step puts the grid underwater from the very first round trip.
Two rules follow straight from the arithmetic: a grid lives on limit orders, and the step cannot be comparable to the commission. A bot that reaches across the spread is feeding the exchange, not you.
What happens when the price does not come back
The price moves one way and stays there. Levels fill one after another, the counter-orders are never bought back, the position grows, and the average entry price trails the market down.
At that moment the grid stops being a grid. What is left is an ordinary position against the trend — accumulated automatically, without a human decision, and without any ceiling unless one was configured.
On perpetual futures funding is added on top: a position that hangs there for days is one you are also paying for, regularly, and the more of it you hold the more you pay.
Martingale: risk is not reduced, its shape is changed
To make the average price catch up with the market faster, the size of each next level is increased. That is the martingale. Here is what it does, on one and the same 10 % move down.
The numbers are illustrative; the mechanics are what matters. The step is 1 point from a price of 100, and there are ten levels.
A flat grid, every level sized 1:
| Metric | Value |
|---|---|
| Bought in total | 10 |
| Average price | 94.50 |
| Capital committed | 945 |
| Loss at a price of 90 | −45 |
| As a share of the capital | −4.8 % |
A doubling martingale, sizes 1, 2, 4, 8 … 512:
| Metric | Value |
|---|---|
| Bought in total | 1023 |
| Average price | 90.99 |
| Capital committed | 93,083 |
| Loss at a price of 90 | −1013 |
| As a share of the capital | −1.1 % |
This is exactly what a martingale is built for: the average price ended up one per cent away from the market instead of five. A one per cent bounce is enough to get out flat, where the flat grid would have waited for five. As a percentage the drawdown looks better too: 1.1 against 4.8.
Now for the part the advertising leaves out. On the very same market move, the martingale lost twenty-two times more money and required ninety-eight times more capital. The percentage got smaller; the sum did not.
A martingale does not reduce risk. It changes its shape: instead of “losing a little, often” you get “losing almost nothing for a long time, and then having no money for the next level”. A martingale grid does not bleed out gradually. It goes once.
And one less obvious thing. A 10 % move against the position is not a disaster for a perpetual future — it is an ordinary Tuesday. A grid designed for ten levels of one per cent hits its limit on a perfectly routine move.
What turns a grid from a casino into an instrument
A grid is a workable strategy. What makes it unworkable is default settings and missing limits. These are not optional:
- A cap on position size. The most important limit of the lot. It answers the question “how much am I prepared to lose”, and the answer should come from you, not from a size progression.
- A stop-loss across the whole position. A grid has no exit from a trend of its own: it has no state that means “I was wrong”. That state has to be imposed from outside.
- A limit on live orders. There is no point in holding the whole plan in the market: the price will not reach the far levels today, and the per-account order limit is not infinite.
- Choosing the instrument. A grid is a bet on a range. So the instrument has to be one that actually ranges, not the one with the largest volume.
- Knowing what you are trading. A grid is short volatility: you collect many small premiums and one day pay out a large one. Income is smooth and predictable, losses are rare and big. That is the shape of the strategy itself, not of a poor implementation.
How to check before committing money
- Run it over the history of the specific instrument — not over the pretty picture in the bot’s advertising, but over a period that contained both a range and a trend.
- Look at the drawdown rather than the profit, and at the maximum position size: how much capital the strategy actually needed at its worst moment.
- Use a demo account. The market is real, the money is not. A week on demo answers more questions than a month of reading reviews.
The code is open — read it
Everything described above is implemented in SimpleTradeBot — a grid bot for Bybit perpetuals that I pulled out of my own trading system into a separate open project: a ladder of levels, per-level take-profits, a position-wide stop and a trailing stop, a cap on position size, and a web interface. MIT licence, with setup instructions on the project page.
Reading someone else’s code teaches more than reading about a strategy: you see not only what the bot does, but what it does when something goes wrong.
If you need a grid built for your strategy and your instrument — with your own rules for building the position, exiting it and bounding the risk — have a look at what bot development involves, or simply write to me. The more precisely the rules are described, the sooner it becomes clear whether the thing is worth building at all: sometimes the honest answer is “this strategy will not pay for itself”, and hearing it before the work starts is cheaper than hearing it after.
This text is not investment advice. None of the approaches described guarantees a profit; past results do not predict future ones.