首页/金融与投资/okx-cex-trade
O

okx-cex-trade

by @okxv
4.4(20)

专注于OKX中心化交易所的交易操作,支持用户进行加密货币买卖等交易活动。

okx-exchangecentralized-exchange-(cex)cryptocurrency-tradingspot-tradingapi-tradingGitHub
安装方式
npx skills add okx/agent-skills --skill okx-cex-trade
compare_arrows

Before / After 效果对比

1
使用前

在OKX中心化交易所进行买卖操作时,需要手动输入指令,过程繁琐,容易错过最佳交易时机。

使用后

能够直接执行买入比特币、卖出以太坊等交易指令,简化操作流程,帮助用户快速响应市场变化,抓住交易机会。

SKILL.md

OKX CEX Trading CLI

Spot, perpetual swap, delivery futures, and options order management on OKX exchange. Place, cancel, amend, and monitor orders; query option chains and Greeks; set take-profit/stop-loss and trailing stops; manage leverage and positions. Requires API credentials.

Prerequisites

  1. Install okx CLI:
    npm install -g @okx_ai/okx-trade-cli
    
  2. Configure credentials:
    okx config init
    
    Or set environment variables:
    export OKX_API_KEY=your_key
    export OKX_SECRET_KEY=your_secret
    export OKX_PASSPHRASE=your_passphrase
    
  3. Test with demo mode (simulated trading, no real funds):
    okx --profile demo spot orders
    

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
demo模拟盘Simulated funds

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 money — irreversible
demo模拟盘Simulated — no real funds
okx --profile live  spot place ...    # 实盘 — real funds
okx --profile demo  spot place ...    # 模拟盘 — 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

Example

User: "Buy 0.01 BTC"
Agent: "Live (实盘) or Demo (模拟盘)?"
User: "Demo"
Agent runs: okx --profile demo spot place --instId BTC-USDT --side buy --ordType market --sz 0.01
Agent replies: "Order placed: 7890123456 (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, fees, transfers → use okx-cex-portfolio
  • For regular spot/swap/futures/options/algo orders → use okx-cex-trade (this skill)
  • For grid and DCA trading bots → use okx-cex-bot

Sz Conversion Rules for Derivatives

Applies to all swap, futures, and option orders. --sz always means number of contracts, never a currency amount. Follow this flow before every derivative place order.

Step 1 — Identify contract type from instId

instId patternContract typeSettlementctVal unit
*-USDT-SWAPLinear perpetual swapUSDTBase currency (BTC/ETH/…)
*-USDT-YYMMDDLinear delivery futuresUSDTBase currency
*-USD-SWAPInverse perpetual swapBase coinUSD (fixed, e.g. 100 USD)
*-USD-YYMMDDInverse delivery futuresBase coinUSD (fixed, e.g. 100 USD)
*-USD-YYMMDD-strike-C/PInverse optionBase coinBase currency (e.g. 0.1 BTC)

Step 2 — Determine user's sz intent

User mentions a quantity:

1. Includes currency unit (USDT / USD / $ / ¥ / 元)
   → User is specifying a quote-currency amount → must convert to contracts (see Step 3)

2. Explicitly says "X contracts" / "X 张" / "X 手"
   → Use directly as sz, no conversion needed

3. Plain number with no unit (for swap/futures/option orders)
   → Ambiguous — ask before proceeding:
     "您输入的 X 是合约张数还是 USDT 金额?"
     (Is X the number of contracts or a USDT amount?)
     Wait for the user's answer before continuing.

Step 3 — Apply the correct conversion formula

Before running any formula, fetch the required data:

# Get ctVal, minSz, lotSz for the instrument
okx market instruments --instType <SWAP|FUTURES|OPTION> --instId <instId> --json

# Get current mark price (for linear contracts and options)
okx market mark-price --instType <SWAP|FUTURES|OPTION> --instId <instId> --json

A. Linear contracts (*-USDT-SWAP / *-USDT-YYMMDD)

ctVal unit: base currency (e.g. ETH)
markPx unit: USDT

Formula:  sz = floor(usdtAmt / (markPx × ctVal))

Example — ETH-USDT-SWAP (ctVal=0.1 ETH, markPx=2000 USDT):
  200 USDT → floor(200 / (2000 × 0.1)) = floor(1.0) = 1 contract  ✓
   50 USDT → floor(50  / (2000 × 0.1)) = floor(0.25) = 0          ✗ insufficient

B. Inverse contracts (*-USD-SWAP / *-USD-YYMMDD)

ctVal unit: USD (fixed face value, e.g. 100 USD per contract)
markPx is NOT needed for sz calculation

Formula:  sz = floor(usdtAmt / ctVal)   (1 USDT ≈ 1 USD)

Example — BTC-USD-SWAP (ctVal=100 USD):
  500 USDT → floor(500 / 100) = 5 contracts  ✓

⚠ Settlement warning (always show):
  "This is an inverse contract. Margin and P&L are settled in BTC, not USDT.
   Your USDT must be converted to BTC to meet margin requirements."

C. Inverse options (*-USD-YYMMDD-strike-C/P)

markPx unit: base currency (e.g. BTC per contract)
ctVal unit: base currency (e.g. 0.1 BTC)
Need BTC spot price to convert USDT → contracts

Required data:
  okx option greeks --uly <BTC-USD> --expTime <YYMMDD> --json  → markPx (BTC)
  okx market ticker BTC-USDT --json                            → last price (btcPx)

Formula (buyer cost):
  sz = floor(usdtAmt / (markPx_BTC × btcPx × ctVal))

Example — BTC-USD-250328-95000-C (markPx=0.005 BTC, btcPx=95000, ctVal=0.1 BTC):
  200 USDT → floor(200 / (0.005 × 95000 × 0.1))
           = floor(200 / 47.5) = floor(4.21) = 4 contracts
  Total premium ≈ 4 × 0.005 × 0.1 = 0.002 BTC ≈ 190 USDT

⚠ Always show both BTC and USDT premium cost to the buyer.
⚠ Seller margin is also in BTC — remind user of liquidation risk.

Step 4 — Validate and confirm before placing

After computing sz:

1. sz == 0 or sz < minSz
   → Reject. Inform user:
     "Amount too small: minimum order is {minSz} contract(s),
      equivalent to ~{minSz × markPx × ctVal} USDT."

2. sz not a multiple of lotSz
   → Round down to the nearest valid multiple:
     sz = floor(sz / lotSz) × lotSz

3. sz ≥ minSz
   → Show conversion summary and wait for user confirmation before placing:

     Conversion summary:
       Input:    {usdtAmt} USDT
       markPx:   {markPx}  |  ctVal: {ctVal}
       Raw:      {usdtAmt} / ({markPx} × {ctVal}) = {rawResult}
       Rounded:  {sz} contracts  (~{sz × markPx × ctVal} USDT actual value)
     Confirm order with sz={sz}?

Quickstart

# Market buy 0.01 BTC (spot)
okx spot place --instId BTC-USDT --side buy --ordType market --sz 0.01

# Limit sell 0.01 BTC at $100,000 (spot)
okx spot place --instId BTC-USDT --side sell --ordType limit --sz 0.01 --px 100000

# Long 1 contract BTC perp (cross margin)
okx swap place --instId BTC-USDT-SWAP --side buy --ordType market --sz 1 \
  --tdMode cross --posSide long

# Long 1 contract with attached TP/SL (one step)
okx swap place --instId BTC-USDT-SWAP --side buy --ordType market --sz 1 \
  --tdMode cross --posSide long \
  --tpTriggerPx 105000 --tpOrdPx -1 --slTriggerPx 88000 --slOrdPx -1

# Close BTC perp long position entirely at market
okx swap close --instId BTC-USDT-SWAP --mgnMode cross --posSide long

# Set 10x leverage on BTC perp (cross)
okx swap leverage --instId BTC-USDT-SWAP --lever 10 --mgnMode cross

# --- Stock Token (TSLA, NVDA, etc.) ---
# Step 1: set leverage ≤ 5x (stock tokens max leverage is 5x)
okx swap leverage --instId TSLA-USDT-SWAP --lever 5 --mgnMode cross

# Step 2: open long on TSLA (--posSide required for stock tokens)
okx swap place --instId TSLA-USDT-SWAP --side buy --ordType market --sz 1 \
  --tdMode cross --posSide long

# Open short on NVDA
okx swap leverage --instId NVDA-USDT-SWAP --lever 3 --mgnMode cross
okx swap place --instId NVDA-USDT-SWAP --side sell --ordType market --sz 1 \
  --tdMode cross --posSide short

# Close TSLA long entirely at market
okx swap close --instId TSLA-USDT-SWAP --mgnMode cross --posSide long

# Set TP/SL on a spot BTC position (sell when price hits $105k, SL at $88k)
okx spot algo place --instId BTC-USDT --side sell --ordType oco --sz 0.01 \
  --tpTriggerPx 105000 --tpOrdPx -1 \
  --slTriggerPx 88000 --slOrdPx -1

# Place trailing stop on BTC perp long (callback 2%)
okx swap algo trail --instId BTC-USDT-SWAP --side sell --sz 1 \
  --tdMode cross --posSide long --callbackRatio 0.02

# Place trailing stop on spot BTC position (callback 2%)
okx spot algo trail --instId BTC-USDT --side sell --sz 0.01 --callbackRatio 0.02

# View open spot orders
okx spot orders

# View open swap positions
okx swap positions

# Cancel a spot order
okx spot cancel --instId BTC-USDT --ordId <ordId>

Command Index

Spot Orders

#CommandTypeDescription
1okx spot placeWRITEPlace spot order (market/limit/post_only/fok/ioc)
2okx spot cancelWRITECancel spot order
3okx spot amendWRITEAmend spot order price or size
4okx spot algo placeWRITEPlace spot TP/SL algo order
5okx spot algo amendWRITEAmend spot TP/SL levels
6okx spot algo cancelWRITECancel spot algo order
7okx spot algo trailWRITEPlace spot trailing stop order
8okx spot ordersREADList open or historical spot orders
9okx spot getREADSingle spot order details
10okx spot fillsREADSpot trade fill history
11okx spot algo ordersREADList spot TP/SL algo orders

Swap / Perpetual Orders

#CommandTypeDescription
12okx swap placeWRITEPlace perpetual swap order
13okx swap cancelWRITECancel swap order
14okx swap amendWRITEAmend swap order price or size
15okx swap closeWRITEClose entire position at market
16okx swap leverageWRITESet leverage for an instrument
17okx swap algo placeWRITEPlace swap TP/SL algo order
18okx swap algo trailWRITEPlace swap trailing stop order
19okx swap algo amendWRITEAmend swap algo order
20okx swap algo cancelWRITECancel swap algo order
21okx swap positionsREADOpen perpetual swap positions
22okx swap ordersREADList open or historical swap orders
23okx swap getREADSingle swap order details
24okx swap fillsREADSwap trade fill history
25okx swap get-leverageREADCurrent leverage settings
26okx swap algo ordersREADList swap algo orders

Futures / Delivery Orders

#CommandTypeDescription
27okx futures placeWRITEPlace delivery futures order
28okx futures cancelWRITECancel delivery futures order
29okx futures amendWRITEAmend delivery futures order price or size
30okx futures closeWRITEClose entire futures position at market
31okx futures leverageWRITESet leverage for a futures instrument
32okx futures algo placeWRITEPlace futures TP/SL algo order
33okx futures algo trailWRITEPlace futures trailing stop order
34okx futures algo amendWRITEAmend futures algo order
35okx futures algo cancelWRITECancel futures algo order
36okx futures ordersREADList delivery futures orders
37okx futures positionsREADOpen delivery futures positions
38okx futures fillsREADDelivery futures fill history
39okx futures getREADSingle delivery futures order details
40okx futures get-leverageREADCurrent futures leverage settings
41okx futures algo ordersREADList futures algo orders

Options Orders

#CommandTypeDescription
42okx option instrumentsREADOption chain: list available contracts for an underlying
43okx option greeksREADImplied volatility + Greeks (delta/gamma/theta/vega) by underlying
44okx option placeWRITEPlace option order (call or put, buyer or seller)
45okx option cancelWRITECancel unfilled option order
46okx option amendWRITEAmend option order price or size
47okx option batch-cancelWRITEBatch cancel up to 20 option orders
48okx option ordersREADList option orders (live / history / archive)
49okx option getREADSingle option order details
50okx option positionsREADOpen option positions with live Greeks
51okx option fillsREADOption trade fill history

Cross-Skill Workflows

Spot market buy

User: "Buy $500 worth of ETH at market"

1. okx-cex-market    okx market ticker ETH-USDT             → get current price to estimate sz
2. okx-cex-portfolio okx account balance USDT               → confirm available funds ≥ $500
        ↓ user approves
3. okx-cex-trade     okx spot place --instId ETH-USDT --side buy --ordType market --sz <sz>
4. okx-cex-trade     okx spot fills --instId ETH-USDT        → confirm fill price and size

Open long BTC perp with TP/SL

User: "Long 5 contracts BTC perp at market, TP at $105k, SL at $88k"

1. okx-cex-portfolio okx account balance USDT               → confirm margin available
2. okx-cex-portfolio okx account max-size --instId BTC-USDT-SWAP --tdMode cross → confirm size ok
        ↓ user approves
3. okx-cex-trade     okx swap place --instId BTC-USDT-SWAP --side buy \
                     

...

用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价

统计数据

安装量6.2K
评分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日