Skip to main content
Grid trading bots automate buy-low/sell-high strategies within a price range. Authentication is required for order placement and management. AI parameters and RSI back-testing are public endpoints.

Method reference

Order management

MethodHTTPAuthEndpointDescription
placeGridAlgoOrder(params)POSTYes/api/v5/tradingBot/grid/order-algoCreate a spot, futures, or contract grid
amendGridAlgoOrder(params)POSTYes/api/v5/tradingBot/grid/amend-order-algoAmend TP/SL on a running grid
stopGridAlgoOrder(orders)POSTYes/api/v5/tradingBot/grid/stop-order-algoStop one or more grids
instantTriggerGridAlgoOrder(params)POSTYes/api/v5/tradingBot/grid/order-instant-triggerManually trigger a grid
closeGridContractPosition(params)POSTYes/api/v5/tradingBot/grid/close-positionClose position of a contract grid
cancelGridContractCloseOrder(params)POSTYes/api/v5/tradingBot/grid/cancel-close-orderCancel a contract grid close order

Queries

MethodHTTPAuthEndpointDescription
getGridAlgoOrderList(params)GETYes/api/v5/tradingBot/grid/orders-algo-pendingActive grid orders
getGridAlgoOrderHistory(params)GETYes/api/v5/tradingBot/grid/orders-algo-historyHistorical grid orders
getGridAlgoOrderDetails(params)GETYes/api/v5/tradingBot/grid/orders-algo-detailsDetails of a specific grid
getGridAlgoSubOrders(params)GETYes/api/v5/tradingBot/grid/sub-ordersSub-orders within a grid
getGridAlgoOrderPositions(params)GETYes/api/v5/tradingBot/grid/positionsPositions held by a contract grid

Margin and investment

MethodHTTPAuthEndpointDescription
spotGridWithdrawIncome(params)POSTYes/api/v5/tradingBot/grid/withdraw-incomeWithdraw profit from a spot grid
computeGridMarginBalance(params)POSTYes/api/v5/tradingBot/grid/compute-margin-balanceCompute required margin adjustment
adjustGridMarginBalance(params)POSTYes/api/v5/tradingBot/grid/margin-balanceAdd or reduce grid margin
adjustGridInvestment(params)POSTYes/api/v5/tradingBot/grid/adjust-investmentAdjust investment amount

Public tools

MethodHTTPAuthEndpointDescription
getGridAIParameter(params)GETNo/api/v5/tradingBot/grid/ai-paramAI-recommended grid parameters
computeGridMinInvestment(params)POSTNo/api/v5/tradingBot/grid/min-investmentMinimum investment for a grid config
getRSIBackTesting(params)GETNo/api/v5/tradingBot/public/rsi-back-testingRSI back-testing results
getMaxGridQuantity(params)GETNo/api/v5/tradingBot/grid/grid-quantityMaximum number of grid levels

Examples

Place a spot grid

import { RestClient } from 'okx-api';

const client = new RestClient({
  apiKey: process.env.API_KEY!,
  apiSecret: process.env.API_SECRET!,
  apiPass: process.env.API_PASS!,
});

const [grid] = await client.placeGridAlgoOrder({
  instId: 'BTC-USDT',
  algoOrdType: 'grid',        // 'grid' = spot grid
  maxPx: '70000',             // upper price boundary
  minPx: '50000',             // lower price boundary
  gridNum: '10',              // number of grid levels
  runType: '1',               // '1' = arithmetic spacing
  investmentAmt: '1000',      // total investment in USDT
  investmentCcy: 'USDT',
});

console.log('Grid algo ID:', grid.algoId);
const client = new RestClient(); // no auth

const [params] = await client.getGridAIParameter({
  algoOrdType: 'grid',
  instId: 'BTC-USDT',
  direction: 'long',
  duration: '7D',  // look-back window: '7D', '30D', '180D'
});

console.log('Recommended max price:', params.maxPx);
console.log('Recommended min price:', params.minPx);
console.log('Recommended grid count:', params.gridNum);
console.log('Annualized return estimate:', params.annualizedRate);

Stop a grid

const result = await client.stopGridAlgoOrder([
  {
    algoId: 'your-algo-id',
    instId: 'BTC-USDT',
    algoOrdType: 'grid',
    stopType: '1',  // '1' = sell all positions, '2' = keep positions
  },
]);
console.log('Stopped:', result[0].algoId);

View active grids

const activeGrids = await client.getGridAlgoOrderList({
  algoOrdType: 'grid',
});

for (const g of activeGrids) {
  console.log(`Grid ${g.algoId}: ${g.instId} ${g.minPx}-${g.maxPx}`);
  console.log(`  PnL: ${g.pnlRatio}, Running since: ${g.cTime}`);
}

Adjust grid margin

// Compute how much to add
const [compute] = await client.computeGridMarginBalance({
  algoId: 'your-algo-id',
  type: 'add',
  amt: '200',
});
console.log('Fee to add margin:', compute.fee);

// Apply the adjustment
await client.adjustGridMarginBalance({
  algoId: 'your-algo-id',
  type: 'add',
  amt: '200',
});

RSI back-testing

const client = new RestClient();

const [result] = await client.getRSIBackTesting({
  instId: 'BTC-USDT',
  timeframe: '1H',
  thold: '30',      // RSI threshold
  timePeriod: '14', // RSI period
  triggerCond: 'cross_down', // trigger when RSI crosses below threshold
  duration: '1M',
});

console.log('Number of triggers in back-test:', result.triggerNum);

Build docs developers (and LLMs) love