O

okx-cex-bot

by @okxv
4.4(20)

专注于OKX中心化交易所(CEX)的交易机器人,当用户希望启动网格交易或其他自动化策略时提供支持,帮助用户在Web3市场中实现自动化交易。

okx-exchangetrading-botsalgorithmic-tradingcryptocurrency-tradingapi-integrationGitHub
安装方式
npx skills add okx/agent-skills --skill okx-cex-bot
compare_arrows

Before / After 效果对比

1
使用前

手动进行网格交易或DCA(平均成本法)投资,需要长时间盯盘、手动下单,容易受情绪影响,且难以精确执行复杂的交易策略,导致错过最佳交易时机。

使用后

通过CLI命令创建、启动、停止和监控OKX上的网格交易机器人和DCA机器人,实现了交易策略的自动化执行。机器人能够24/7不间断运行,精确捕捉市场波动,有效管理风险,并提升潜在的交易收益。

SKILL.md

OKX CEX Bot Trading CLI

Grid and DCA (Contract Martingale) trading bot management on OKX exchange. All grid and DCA bots in this skill are native OKX platform bots — they run server-side on OKX and do not require a locally running process. Requires API credentials.

Prerequisites

  1. Install okx CLI:

    npm install -g @okx_ai/okx-trade-cli
    
  2. Configure credentials — check first, then set up if missing:

    okx config show   # shows configured profiles; api_key shows last 4 chars if set
    

    If credentials are already configured → proceed to step 3.

    If not configured:

    okx config init          # interactive wizard — demo mode (opens browser → OKX demo API page)
    okx config init          # run again for live mode (creates a second profile)
    

    Creates okx-demo (demo) and/or okx-prod (live) profiles. Each run safely merges into the existing config file without overwriting other profiles.

    Or set environment variables:

    export OKX_API_KEY=your_key
    export OKX_SECRET_KEY=your_secret
    export OKX_PASSPHRASE=your_passphrase
    

    Security: NEVER accept API credentials directly in chat. If the user pastes credentials, warn them and ask them to run okx config init or edit ~/.okx/config.toml manually with their local editor instead. Credentials must never appear in conversation history.

    Switch between profiles:

    okx config set default_profile okx-demo   # switch to demo
    okx config set default_profile okx-prod   # switch to live
    okx --profile okx-prod bot grid orders --algoOrdType grid  # one-off override
    
  3. Test with demo mode (simulated trading, no real funds):

    okx --profile demo bot grid orders --algoOrdType grid
    

Credential & Profile Check

Run this check before any authenticated command.

Step A — Verify credentials

okx config show       # verify configuration status (output is masked)
  • If the command returns an error or shows no configuration: stop all operations, guide the user to run okx config init, and wait for setup to complete before retrying.
  • If credentials are configured: proceed to Step B.

Step B — Confirm profile (required)

--profile is required for all authenticated commands. Never add a profile implicitly.

ValueModeFunds
live实盘Real funds — bot operates with real money
demo模拟盘Simulated — safe for testing bot configurations

Resolution rules:

  1. Current message intent is clear (e.g. "real" / "实盘" / "live" → live; "test" / "模拟" / "demo" → demo) → use it and inform the user: "Using --profile live (实盘)" or "Using --profile demo (模拟盘)"
  2. Current message has no explicit declaration → check conversation context for a previous profile:
    • Found → use it, inform user: "Continuing with --profile live (实盘) from earlier"
    • Not found → ask: "Live (实盘) or Demo (模拟盘)?" — wait for answer before proceeding

Handling 401 Authentication Errors

If any command returns a 401 / authentication error:

  1. Stop immediately — do not retry the same command
  2. Inform the user: "Authentication failed (401). Your API credentials may be invalid or expired."
  3. Guide the user to update credentials by editing the file directly with their local editor:
    ~/.okx/config.toml
    
    Update the fields api_key, secret_key, passphrase under the relevant profile. Do NOT paste the new credentials into chat.
  4. After the user confirms the file is updated, run okx config show to verify (output is masked)
  5. Only then retry the original operation

Demo vs Live Mode

Profile is the single control for 实盘/模拟盘 switching — exactly two options:

--profileModeFunds
live实盘Real funds — bot operates with real money
demo模拟盘Simulated — no real funds
okx --profile live  bot grid create ...    # 实盘 — real funds
okx --profile demo  bot grid create ...    # 模拟盘 — simulated funds

Rules:

  1. --profile is required on every authenticated command — determined in "Credential & Profile Check" Step B
  2. Every response after a command must append: [profile: live] or [profile: demo]
  3. Do not use the --demo flag for mode switching — use --profile instead
  4. For bot create/stop operations (write commands), profile must be explicitly confirmed before execution

Example

User: "Start a BTC grid bot"
Agent: "Live (实盘) or Demo (模拟盘)?"
User: "Demo"
Agent runs: okx --profile demo bot grid create --instId BTC-USDT --algoOrdType grid ...
Agent replies: "Grid bot created: 12345678 (OK) — simulated, no real funds used. [profile: demo]"

Skill Routing

  • For market data (prices, charts, depth, funding rates) → use okx-cex-market
  • For account balance, P&L, positions history, fees, transfers → use okx-cex-portfolio
  • For regular spot/swap/futures/algo orders → use okx-cex-trade
  • For grid and DCA trading strategies (native OKX platform bots) → use okx-cex-bot (this skill)

Quickstart

# Create a spot grid bot on BTC ($90k–$100k, 10 grids, invest 1000 USDT)
okx bot grid create --instId BTC-USDT --algoOrdType grid \
  --minPx 90000 --maxPx 100000 --gridNum 10 --quoteSz 1000

# Create a contract grid bot on BTC perp (neutral, 5x leverage, 100 USDT margin)
# basePos defaults to true for long/short (opens base position); neutral ignores it
okx bot grid create --instId BTC-USDT-SWAP --algoOrdType contract_grid \
  --minPx 90000 --maxPx 100000 --gridNum 10 \
  --direction neutral --lever 5 --sz 100

# List all active grid bots
okx bot grid orders --algoOrdType grid

# Get grid bot details and P&L
okx bot grid details --algoOrdType grid --algoId <algoId>

# Stop a grid bot (keep assets as-is)
okx bot grid stop --algoId <algoId> --algoOrdType grid --instId BTC-USDT --stopType 2

# Create a contract DCA bot on BTC perp (long, 3x leverage, 3% TP)
okx bot dca create --instId BTC-USDT-SWAP --lever 3 --direction long \
  --initOrdAmt 100 --safetyOrdAmt 50 --maxSafetyOrds 3 \
  --pxSteps 0.03 --pxStepsMult 1 --volMult 1 --tpPct 0.03

# List all active contract DCA bots
okx bot dca orders

# Get DCA bot details
okx bot dca details --algoId <algoId>

# Stop a DCA bot
okx bot dca stop --algoId <algoId>

Command Index

Grid Bot

#CommandTypeDescription
1okx bot grid createWRITECreate a grid bot
2okx bot grid stopWRITEStop a grid bot
3okx bot grid ordersREADList active or history grid bots
4okx bot grid detailsREADSingle grid bot details + PnL
5okx bot grid sub-ordersREADIndividual grid fills or live orders

DCA Bot (Contract Only)

#CommandTypeDescription
6okx bot dca createWRITECreate a Contract DCA bot
7okx bot dca stopWRITEStop a Contract DCA bot
8okx bot dca ordersREADList active or history Contract DCA bots
9okx bot dca detailsREADSingle Contract DCA bot details + PnL
10okx bot dca sub-ordersREADContract DCA cycles and orders within a cycle

Cross-Skill Workflows

Spot Grid Bot

User: "Start a BTC grid bot between $90k and $100k with 10 grids, invest 1000 USDT"

1. okx-cex-market    okx market ticker BTC-USDT                     → confirm price is in range
2. okx-cex-portfolio okx account balance USDT                       → confirm available funds
        ↓ user approves
3. okx-cex-bot       okx bot grid create --instId BTC-USDT --algoOrdType grid \
                       --minPx 90000 --maxPx 100000 --gridNum 10 --quoteSz 1000
4. okx-cex-bot       okx bot grid orders --algoOrdType grid          → confirm bot is active
5. okx-cex-bot       okx bot grid details --algoOrdType grid --algoId <id> → monitor PnL

Contract Grid Bot (Long / Short / Neutral)

User: "Create a long grid bot on BTC perp from $90k to $100k, 10x leverage"

1. okx-cex-market    okx market ticker BTC-USDT-SWAP                → confirm current price
2. okx-cex-portfolio okx account balance USDT                       → confirm margin
        ↓ user approves
3. okx-cex-bot       okx bot grid create --instId BTC-USDT-SWAP --algoOrdType contract_grid \
                       --minPx 90000 --maxPx 100000 --gridNum 10 \
                       --direction long --lever 10 --sz 100
                       # basePos defaults to true (opens base position for long/short)
                       # For short: --direction short; for neutral: --direction neutral
4. okx-cex-bot       okx bot grid orders --algoOrdType contract_grid → confirm active

Contract DCA Bot (Long / Short)

User: "Start a long DCA bot on BTC perp, 3x leverage, $200 initial, 3% TP"

1. okx-cex-market    okx market ticker BTC-USDT-SWAP               → confirm current price
2. okx-cex-portfolio okx account balance USDT                       → confirm margin
        ↓ user approves
3. okx-cex-bot       okx bot dca create --instId BTC-USDT-SWAP \
                       --lever 3 --direction long \
                       --initOrdAmt 200 --safetyOrdAmt 100 --maxSafetyOrds 3 \
                       --pxSteps 0.03 --pxStepsMult 1 --volMult 1 --tpPct 0.03
                       # For short: --direction short
4. okx-cex-bot       okx bot dca orders                             → confirm active
5. okx-cex-bot       okx bot dca details --algoId <id>              → monitor PnL

Monitor and Stop All Bots

User: "Show all my active bots and stop the ones losing money"

1. okx-cex-bot       okx bot grid orders --algoOrdType grid         → list spot grid bots
2. okx-cex-bot       okx bot grid orders --algoOrdType contract_grid → list contract grid bots
3. okx-cex-bot       okx bot dca orders                             → list DCA bots
4. okx-cex-bot       okx bot grid details --algoOrdType grid --algoId <id>  → check PnL
5. okx-cex-bot       okx bot grid stop --algoId <id> --algoOrdType grid --instId BTC-USDT --stopType 2

Operation Flow

Step 0 — Credential & Profile Check

Before any authenticated command:

Determine profile (required):

  • Options: live (实盘) or demo (模拟盘) — exactly these two values
  1. Current message intent clear (e.g. "real"/"实盘"/"live" → live; "test"/"模拟"/"demo" → demo) → use it, inform user: "Using --profile live (实盘)"
  2. Current message has no explicit declaration → check conversation context for previous profile:
    • Found → use it, inform user: "Continuing with --profile live (实盘) from earlier"
    • Not found → ask: "Live (实盘) or Demo (模拟盘)?" — wait for answer

If no credentials configured: guide user to run okx config init, stop all trading actions

After every command result: append [profile: live] or [profile: demo] to the response

Critical: algoId and algoOrdType Rules

  • algoId is the bot's algo order ID (returned by bot grid create / bot dca create or listed by bot grid orders / bot dca orders). It is NOT a normal trade order ID (ordId). Never fabricate or guess an algoId — always obtain it from a prior create or list command.
  • algoOrdType for grid commands must match the bot's actual type. Always use the algoOrdType value from the bot grid orders response — do not infer from user description alone. Passing a mismatched type (e.g., contract_grid for a spot grid bot) causes error 50016.
  • When operating on an existing bot (details, sub-orders, stop), always retrieve the bot list first to get the correct algoId and algoOrdType pair, unless the user provides them explicitly.

Step 1: Identify Bot Type and Action

  • Grid bot create → okx bot grid create
  • Grid bot stop → okx bot grid stop
  • Grid bot status/P&L → okx bot grid orders + okx bot grid details
  • Grid individual fills → okx bot grid sub-orders
  • DCA bot create → okx bot dca create (requires --lever and --direction)
  • DCA bot stop → okx bot dca stop (only --algoId needed)
  • DCA bot status/P&L → okx bot dca orders + okx bot dca details
  • DCA individual cycles/orders → okx bot dca sub-orders

Step 2: Run Read Commands Immediately — Confirm Profile (Step 0) then Writes

Read commands (orders, details, sub-orders): run immediately.

  • --algoOrdType for grid → infer from context (grid for spot, contract_grid for perp)
  • --history → use default (active); only query history if explicitly requested

Write commands (create, stop): confirm once before executing.

  • Grid create: confirm --minPx, --maxPx, --gridNum; verify --minPx < current price < --maxPx; confirm investment size
    • Spot grid: --quoteSz (USDT) or --baseSz (base currency)
    • Contract grid: --direction (long/short/neutral), --lever, --sz (investment margin in USDT); --basePos defaults to true (open base position for long/short)
  • DCA create: confirm --instId, --lever, --direction, --initOrdAmt, --safetyOrdAmt, --maxSafetyOrds, --pxSteps, --pxStepsMult, --volMult, --tpPct; optional: --slPct, --slMode, --allowReinvest, --triggerStrategy, --triggerPx
  • Grid stop: confirm --stopType (default omitted → keep assets; 1=sell all to quote)
  • DCA stop: only --algoId needed
  • Demo dry-run: suggest okx --profile demo bot grid create ... when user is unsure

⚠ Insufficient balance — NEVER auto-transfer funds. If the trading account balance is insufficient to create a bot, do NOT automatically initiate a fund transfer (account_transfer). Instead, inform the user of the shortfall (current available vs. required amount) and ask how they want to proceed. Suggested options to present:

  1. Transfer funds from funding account to trading account (specify amount)
  2. Reduce the investment size to fit available balance
  3. Cancel the bot creation

Step 3: Verify After Writes

  • After grid create: run okx bot grid orders to confirm bot is active; then okx bot grid details to monitor PnL
  • After DCA create: run okx bot dca orders to confirm bot is active; then okx bot dca details for details
  • After stop: run okx bot grid orders --history / okx bot dca orders --history to confirm bot is stopped

CLI Command Reference

Grid Bot — Create

okx bot grid create --instId <id> --algoOrdType <type> \
  --maxPx <px> --minPx <px> --gridNum <n> \
  [--runType <1|2>] \
  [--quoteSz <n>] [--baseSz <n>] \
  [--direction <long|short|neutral>] [--lever <n>] [--sz <n>] \
  [--basePos] [--no-basePos] \
  [--json]
ParamRequiredDefaultDescription
--instIdYes-Instrument (e.g., BTC-USDT for spot grid, BTC-USDT-SWAP for contract grid)
--algoOrdTypeYes-grid (spot grid) or contract_grid (contract grid)
--maxPxYes-Upper price boundary
--minPx

...

用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价

统计数据

安装量6.0K
评分4.4 / 5.0
版本
更新日期2026年5月23日
对比案例1 组

用户评分

4.4(20)
5
25%
4
50%
3
25%
2
0%
1
0%

为此 Skill 评分

0.0

兼容平台

🔧Claude Code
🔧OpenClaw
🔧OpenCode
🔧Codex
🔧Gemini CLI
🔧GitHub Copilot
🔧Amp
🔧Kimi CLI

时间线

创建2026年3月16日
最后更新2026年5月23日