StructureX

Home › Guides › Trading bots & code

Grid trading bot explained: how it works — and exactly how it loses

A grid bot places a ladder of buy and sell orders and earns small amounts as price bounces between them. In a sideways market it looks smooth. In a trend it can quietly pile up a big loss. Here's both sides, with numbers.

Beginner guide · 9-minute read · Updated 2026-10-21

The idea in one picture

Imagine a ladder drawn across a price chart.

Every bounce from one rung to the next earns one small step. The bot never predicts direction. It just repeats: buy lower, sell a bit higher.

That's a grid. The rungs are the grid levels, and the gap between them is the grid step.

A worked example

Price is at 100. The grid has 5 buy levels, 2 apart: 98, 96, 94, 92, 90. Each buy is 1 unit, and each one sells 2 higher.

Example code for learning — test it on paper first; it is not a ready-made trading system.

start, step, levels = 100.0, 2.0, 5
buys = [start - step * (i + 1) for i in range(levels)]      # 98, 96, 94, 92, 90
print("Buy orders at:", buys)
def open_result(price_now):
    return sum(price_now - b for b in buys if b >= price_now)  # filled buys only
for price in (96.0, 90.0, 85.0, 80.0):
    filled = [b for b in buys if b >= price]
    print(f"Price {price}: holding {len(filled)} units, open result {open_result(price):+.0f}")

In plain words:

It prints:

Buy orders at: [98.0, 96.0, 94.0, 92.0, 90.0]
Price 96.0: holding 2 units, open result -2
Price 90.0: holding 5 units, open result -20
Price 85.0: holding 5 units, open result -45
Price 80.0: holding 5 units, open result -70

All results are in price points per unit. A math example, not a promise.

When it works: a range

Say price wobbles between 100 and 96 for a week.

Each full bounce earns +2 per unit, before fees. If price ranges sideways long enough, those small steps add up. That's why grid backtests on sideways markets look so smooth.

Fees matter a lot here. A grid makes many small trades, so commission and the Spread take a slice of every step. If the step is too small, fees can eat most of it.

How it loses: a trend down

Now say price doesn't bounce. It falls from 100 to 80.

Every buy fills. No sell ever fills, because price never climbs back a rung. Look at the output above:

Each range bounce earned +2. So −70 equals 35 successful bounces wiped out by one move. That's the core problem: a grid wins small and often, and loses big and rarely.

And by default, nothing stops it. A grid has no stop loss built into the idea. It just waits for price to come back — and price doesn't have to.

How it loses: a trend up

Say price rises from 100 to 130 instead.

The grid bought nothing, because all its buys were below 100. Or, if it held units, it sold them at the first rungs above and then sat in cash while the move kept going.

This isn't a loss of money, but it's a cost: a grid is built to sell rallies. In a strong uptrend it steps aside early. A trend-follower would do the opposite. See trend following vs mean reversion.

The hidden risks

A grid isn't a range strategy with no risk. It's a range strategy with no stop. It's fine while the range holds. The question to answer before running one is: what happens when it doesn't?

If you test one, set these limits first

Treat a grid like any other trading rule. Write down:

  1. The range you expect and why — for example, clear turning points above and below on the bigger chart. See range trading explained.
  2. A hard stop below the range. If price closes below the lowest level (a candle body, not just a wick), the bot sells everything and stops.
  3. Total risk. Add up the loss if every buy fills and the stop is hit. In the example: 5 units bought from 98 down to 90 (average 94), stop at 86 = 8 × 5 = −40 points. Size the grid so that number is a small slice of your account. Our position size calculator helps.
  4. A daily loss limit and a kill switch you can use without code.
  5. Error handling and logs. A grid sends many orders. Log every one, and check open orders after any disconnect before placing new ones.

More: trading bot risk controls.

Then backtest on several kinds of markets — ranges and trends — watching for look-ahead bias (using prices before they happened). Then paper trade it for weeks. See paper trading a bot.

Grid vs DCA bot

They're cousins. A grid buys at fixed levels and sells each one a step higher. A DCA bot adds bigger and bigger buys as price falls and sells everything at one target. Both add to a losing position. Both need a hard limit. See DCA bot explained.

Common mistakes

Questions

What is a grid trading bot?

A bot that places buy orders at set levels below the price and a sell order one level above each buy. It aims to collect small gains as price moves up and down inside a range.

Do grid trading bots work?

They can collect small gains in sideways markets, but they lose when price trends away from the grid, especially with no stop loss. Many small gains can be erased by one large move, and fees reduce every gain.

What is the biggest risk of grid trading?

A strong trend down. Every buy fills, no sells fill, and the open loss grows with no built-in exit. With leverage, that can lead to liquidation.

Should a grid bot have a stop loss?

We'd say yes. A hard stop below the range, sized so the total loss is a small slice of the account, turns an open-ended risk into a planned one.

Want the next step? We'll walk you through it.

Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.

Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytime

Or see the whole method, step by step:

See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)

One chart idea a day, in plain English. Follow along:

Follow on XFollow on TradingView