dYdX Chain / Trade a range

Grid bot.

Place buy and sell orders across a bounded price grid. Set your spacing, quantities and the number of orders above and below the latest filled level.

UNDERSTAND THE BOT

The strategy
in detail.

How the grid moves

Grid turns a bounded price range into evenly spaced trading levels. It is useful when your strategy calls for repeated buys and sells at fixed distances, rather than a prediction generated by the bot. You choose the range and how much exposure each fill adds or removes.

The bot first places the configured starting order. A starting price of zero uses the order-book midpoint and snaps it to a grid level. Once that order fills, the bot maintains the specified number of buys below and sells above the latest completed level. Further fills move the working grid.

A configuration example

Suppose the bounds are 50,000 to 80,000 with a step of 1,000, one order on each side and an order size of 0.001 BTC. With a completed level at 60,000, the adjacent working levels are 59,000 and 61,000. These are configuration values, not current market prices. Bounds limit the available levels; they do not automatically close a position when price leaves the range.

What to monitor

Watch filled quantities, outstanding orders, net position and collateral together. A sequence of buys or sells can leave directional exposure. Matched profit pairs opposite fills one level apart and allocates actual trading fees by quantity; it is not total account profit and excludes funding, network fees and unrealised positions.

Unfilled cancelled orders can be recreated with new client IDs. A terminated partially filled order stops the bot so the account can be reconciled. Do not delete the saved state to force another start. This implementation does not provide an automatic stop-loss.

Get it running

You need Git, Docker with Compose, and a funded dYdX Chain testnet wallet to use the supplied example. The bot needs the wallet's mnemonic and a matching Chain address. Old v3 API keys, STARK keys and v3 state do not work here.

  1. Clone the repository and create your environment file:
git clone https://github.com/akilibots/akili-grid.git
cd akili-grid
cp .env.example .env
  1. Edit .env. Set DYDX_MNEMONIC to your Chain wallet's mnemonic, in quotes. Leave the Telegram values empty if you do not want notifications.

  2. Edit data/strategy.json. Set exchange.address to that wallet's address, check its subaccount and review the strategy quantities and prices. The sample endpoints select testnet.

  3. Build and start the bot:

docker compose up --build -d
docker compose logs -f

To stop it:

docker compose down

Stopping the process leaves its exchange orders in place. Check and cancel those orders separately when needed. Do not run two instances from the same data folder.

How it works

bounds.low, high and step define an evenly spaced grid. start.order selects buy or sell, start.size sets the initial quantity, and start.price = 0 uses the current order-book midpoint. The starting price must be within the bounds. The initial order is snapped to a grid level. Once filled, the bot maintains orders.below buys and orders.above sells around the latest completed level, each of orders.size. Multiple observed fills are processed before rebuilding.

Cancelled unfilled tracked orders are recreated with new client IDs. A terminated partially filled order stops execution for position reconciliation. The bot does not guess the missing quantity.

Matched profit pairs opposite fills one grid level apart and allocates actual fees by quantity. It is not total account P&L. stop.price must be zero; a stop-loss strategy is not implemented.

Configuration notes

Setting What to check
main.market The perpetual market, such as BTC-USD
exchange.address The Chain address matching DYDX_MNEMONIC
exchange.subaccount A cross-margin subaccount from 0 to 127, with collateral
exchange.network testnet or mainnet
exchange.indexer, websocket, node Endpoints for the same selected network
exchange.poll_seconds Seconds between status checks for the strategy loops

The node value is a TLS gRPC host and port, without http:// or https://. Review market sizes and prices before running any sample. Quantities are in the base asset, not a dollar budget. To use mainnet, change the network and all its endpoints together and configure the corresponding wallet and collateral.

You can supply JSON5 through the strategy environment variable instead of the strategy file. Settings are tied to saved strategy state; reconcile existing orders before changing an active strategy. Telegram notifications use TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID in .env.

Restarting and recovery

Compose mounts ./data, so state survives a container replacement. data/state.json records progress and data/exchange.json records client IDs before order submission. Keep them together and keep them out of Git.

An uncertain submission is checked using its existing client ID. The bot does not turn a timeout into a second trade. If it cannot reconcile an order, it stops so you can inspect the exchange and saved state. Do not delete state to bypass an error. Partial-fill and cancellation handling is described above.

For a v3 migration, reconcile old orders and positions first, archive the old state, then start with a fresh data folder. Use a separate data folder and subaccount for each strategy. Never commit wallet mnemonics or .env.

Limit orders use Chain long-term orders with a default 24-hour expiry. Prices follow market ticks and sizes must be exact multiples of stepSize. Market orders use short-term IOC orders with a slippage bound. Profit figures, where reported, include actual fill prices and trading fees, but exclude funding, network transaction fees and unrealised positions.

Test it yourself

docker build --target test -t bot-test .
docker run --rm --network none bot-test

The suite uses simulated exchange responses with networking disabled. It covers order handling and recovery; SDK checks also build and sign transactions locally. Passing tests is not a promise of live execution or profitable trading.

Running directly with Python

Use Python 3.15.2 and install the native C/C++ and Rust build tools required by the wallet dependencies. Docker includes these prerequisites in its build stage.

python3 -m venv .venv
. .venv/bin/activate
PIP_BUILD_CONSTRAINT="$PWD/build-constraints.txt" python3 -m pip install -r requirements-dev.txt
python3 -m pip check

Export the values from .env into your shell, then run python3 run.py. The direct Python entrypoint does not automatically read .env. The dYdX SDK constrains some signing packages to older compatible versions; build-constraints.txt supplies the build tools those packages require.

Use Grid in your workspace

Manage your Grid strategy through the Akilibots app. Keep the bot configuration, exchange connection and execution history together.

Set up your account and exchange

  1. Create your account and sign in to your workspace.
  2. Open exchange connections and add your dYdX Chain account and select the correct network and subaccount.
  3. Check that the connection uses the intended account and has the funds needed for the strategy. Use testnet to check the configuration first.
  4. Create a bot and select Grid.

Configure the strategy

  1. Choose the perpetual market and subaccount.
  2. Set the lower and upper price bounds and grid step.
  3. Choose the starting side, price and quantity.
  4. Set the quantity and number of orders above and below the active level.

Review and run

  1. Review the market, network, order quantities and maximum exposure. Save the configuration.
  2. Start the bot, then check the first action against the exchange account.
  3. Use the bot detail view to monitor orders, executions, logs and errors. A queued request or submitted order is not a confirmed fill.
  4. Pause the bot before changing its strategy. Check the exchange for open orders and positions; stopping a bot does not itself close a position.
  5. If execution stops with a reconciliation error, compare the saved bot state with exchange orders and fills before resuming.

Commercial access

For account access and deployment enquiries, contact Akilibots. Never include wallet mnemonics, private keys or API secrets in an email.

Commercial service guide