Grid Bots - Automated Grid Trading
TL;DR
What is a Grid Bot:
- Automated trading strategy that places buy/sell orders at regular price intervals
- Profits from price oscillation within a defined range
- Works on both Spot and Perpetual markets
Spot vs Perp Grid:
| Spot Grid | Perp Grid | |
|---|---|---|
| Asset ownership | You own the actual assets | You hold positions (contracts) |
| Funding requirement | Need base + quote assets | Margin only |
| Short selling | Requires borrowing | Built-in (can go short natively) |
| Complexity | Higher (deficit handling) | Simpler |
Spot Grid Funding:
- Deficit: How much base asset you're missing to cover sell orders
- Borrow Mode: Borrow the missing base asset from lending pool (margin trading)
- Buy Mode: Use quote currency to buy the missing base asset upfront
Table of Contents
- How Grid Bots Work
- Spot Grid vs Perp Grid
- Spot Grid Funding
- The Deficit Concept
- Borrow Mode vs Buy Mode
- Grid Parameters
1. How Grid Bots Work
The Basic Concept
A grid bot divides a price range into equal intervals (the "grid") and places orders at each level:
Price $110 ─────── Sell order (top of range)
Price $108 ─────── Sell order
Price $106 ─────── Sell order
Price $104 ─────── Sell order
Price $102 ─────── Sell order
────────────────── Current price: $100 ──────────────────
Price $98 ─────── Buy order
Price $96 ─────── Buy order
Price $94 ─────── Buy order
Price $92 ─────── Buy order
Price $90 ─────── Buy order (bottom of range)How Profits Are Made
- When price moves up: Sell orders get filled
- When price moves down: Buy orders get filled
- Each completed buy→sell cycle captures the grid spacing as profit
Example:
Grid spacing: $2
1. Buy at $98 → Sell at $100 = $2 profit
2. Buy at $96 → Sell at $98 = $2 profit
(Repeat as price oscillates)Key Parameters
| Parameter | Description |
|---|---|
priceLow | Bottom of the grid range |
priceHigh | Top of the grid range |
gridCount | Number of grid levels (orders) |
totalGridValue | Total capital allocated to the grid |
2. Spot Grid vs Perp Grid
Perpetual Grid (Simpler)
Perp grids trade perpetual futures contracts:
- You only need margin (collateral)
- Can go long or short natively
- No need to own the underlying asset
- Funding rates apply (see Funding Rate)
Spot Grid (More Complex)
Spot grids trade actual assets:
- Orders above current price are sell orders → require the base asset (e.g., BTC)
- Orders below current price are buy orders → require the quote asset (e.g., USDC)
The challenge: You need to have enough of both assets to place all orders.
Example: BTC_USDC grid, current price $100
Orders above $100 (sell orders):
- Need BTC to sell
Orders below $100 (buy orders):
- Need USDC to buy3. Spot Grid Funding
When creating a spot grid, the backend pre-calculates all orders based on your parameters (price range, grid count, total value). Orders are split by the current market price:
- Orders below current price → Buy orders (require quote currency, e.g., USDC)
- Orders above current price → Sell orders (require base currency, e.g., BTC)
Required Assets
- Quote Asset (e.g., USDC): Needed for buy orders below current price
- Base Asset (e.g., BTC): Needed for sell orders above current price
If you don't have enough base asset, you have a deficit that must be funded.
4. The Deficit Concept
The deficit represents how much base asset you're missing to cover the sell orders in your grid.
Calculation
deficit = max(0, requiredBaseAmount - baseAssetBalance)Where:
requiredBaseAmount= Sum of all sell order quantities from the pre-calculated gridbaseAssetBalance= User's available + locked + lent balance of the base asset
Example
Grid: BTC_USDC, range $90-$110, current price $100
Sell orders need: 0.5 BTC total
Your BTC balance: 0.2 BTC
Deficit = 0.5 - 0.2 = 0.3 BTCIf deficit = 0: You have enough base asset, no funding decision needed.
If deficit > 0: You must choose how to fund the gap.
5. Borrow Mode vs Buy Mode
When you have a deficit, you have two options:
Borrow Mode (autoBorrow: true)
How it works:
- Borrow the deficit amount from the lending pool
- The borrowed amount is used to cover your sell orders
- You pay interest on the borrowed amount (see Borrow & Lend)
When to use:
- You want to preserve your quote balance
- You expect to close the grid before interest costs become significant
- Lending rates are favorable
Requirements:
- Borrow/Lend must be enabled in your jurisdiction
- A lending market must exist for the base asset
- You must have sufficient margin
Buy Mode (autoBorrow: false)
How it works:
- Use your quote currency to buy the deficit amount upfront
- The purchase happens before the grid starts
- No ongoing interest costs
When to use:
- You want to avoid borrowing/margin
- Borrow/Lend is not available
- You have excess quote currency
Key parameter: maxEntryPrice
- The maximum price you're willing to pay when buying the deficit
- Defaults to ~1% above current price
- Protects against slippage during the initial buy
Auto-Selection Logic
The system auto-selects the funding mode based on availability:
- If user has sufficient base balance → Default to borrow (doesn't matter since no funding needed)
- If borrowing unavailable but can buy → Force buy mode
- If can't afford to buy but can borrow → Force borrow mode
- If both available → Default to borrow, user can override
Comparison Table
| Aspect | Borrow Mode | Buy Mode |
|---|---|---|
| Upfront cost | None | Buy deficit immediately |
| Ongoing cost | Interest on borrowed amount | None |
| Quote balance | Preserved | Reduced |
| Requirements | Borrow/Lend enabled + margin | Sufficient quote balance |
| Risk | Liquidation if margin drops | Slippage on entry buy |
6. Grid Parameters
Core Parameters (All Grid Types)
| Field | Type | Description |
|---|---|---|
priceLow | number | Lower bound of price range |
priceHigh | number | Upper bound of price range |
gridCount | number | Number of grid levels |
totalGridValue | number | Total allocation in quote currency |
Spot-Specific Parameters
| Field | Type | Description |
|---|---|---|
autoBorrow | boolean | true = borrow mode, false = buy mode |
maxEntryPrice | number | Max price for buying deficit (buy mode only) |
Optional Parameters
| Field | Type | Description |
|---|---|---|
closePositionsOnStop | boolean | Close all positions when bot stops |
gridTrigger | boolean | Enable conditional start |
gridTriggerAmount | number | Price trigger to start the grid |
trailingUpPrice | number | Trailing stop upper bound |
trailingDownPrice | number | Trailing stop lower bound |
Summary
Grid bots automate buy-low-sell-high strategies within a price range. The key complexity in spot grids is ensuring you have enough assets on both sides of the current price:
- Check for deficit - Do you have enough base asset for sell orders?
- If deficit > 0, choose funding:
- Borrow: Take a loan from lending pool, pay interest
- Buy: Spend quote currency upfront to acquire the base asset
- System auto-selects based on what's available to the user
- User can override if both options are available