SuperAPI AI Directory

aicoin-trading

General & other

View repository

Updated 10-15 12:43

About this skill

Run the script: run from the directory where SKILL.md is located node scripts/exchange.mjs <action>. The three engines (OpenClaw / Hermes / Claude Code) containers automatically load the skill, directly cd to the skill directory.

AiCoin Trading — Order Placement Only

⛔ Iron Rules (violating any one of them is a serious error)

  1. Writing code to place orders is prohibited. Not allowed to write import ccxt、new ccxt.okx()、fetch("https://...") or any custom code to place orders. All orders can only be executed via node scripts/exchange.mjs create_order .
  2. Automatic confirmation is prohibited. create_order / close_position The first call returns a preview (including risk warnings), you must display the preview in full to the user, and only after the user replies "confirm" or "yes" can you make the second call adding "confirmed":"true" to execute.
  3. Modifying user parameters is prohibited. If the balance is insufficient, tell the user; do not automatically adjust the quantity or leverage.
  4. Closing positions on your own initiative is prohibited. Unless the user explicitly requests it.
  5. Closing positions must use close_position。 Using create_order to build a closing order is prohibited (easy to open a reverse position).
  6. Changes to leverage / margin mode must be confirmed first. set_trading_params and set_leverage are not read-only operations — they change the exchange account's contract configuration, directly affecting the margin occupation, liquidation price, and liquidation distance of all subsequent orders. The liquidation distance at 100x leverage and 5x leverage differs by 20 times; do not silent set before the user explicitly says to change it.Before calling, you must: tell the user in natural language which exchange, which trading pair's leverage / margin_mode you are about to change from what to what, and what the impact is; only after the user replies "confirm" or "yes" can you execute.

Counterexample ❌: The user says "open 100x long BTC", and without asking back whether the leverage was written wrong you directly set_trading_params leverage=100 and then place the order — the user may have misspoken and meant 10x; silently setting 100x is extremely risky. Correct ✅: First reply "The liquidation distance at 100x leverage is only about 0.95% (excluding fees); a single 5-minute BTC candle can sweep it. Confirm whether it is 100x or you meant 10x?" and wait for the user's explicit answer before set.

Order placement process (two steps, cannot be skipped)

步骤1: node scripts/exchange.mjs create_order '{"exchange":"okx","symbol":"BTC/USDT:USDT","type":"market","side":"buy","amount":1,"market_type":"swap"}'
→ 返回预览(交易对、方向、数量、价格、杠杆、保证金、风险提示)
→ 你必须把所有字段展示给用户

步骤2: 用户确认后
node scripts/exchange.mjs create_order '{"exchange":"okx","symbol":"BTC/USDT:USDT","type":"market","side":"buy","amount":1,"market_type":"swap","confirmed":"true"}'
→ 实际下单

Position closing process (two steps, cannot be skipped)

Closing positions must use close_position, using create_order to manually build a closing order is prohibited (easy to open a reverse position).

步骤1: node scripts/exchange.mjs close_position '{"exchange":"okx","market_type":"swap"}'
→ 返回所有持仓预览(交易对、方向、张数、盈亏)
→ 展示给用户

步骤2: 用户确认后
node scripts/exchange.mjs close_position '{"exchange":"okx","market_type":"swap","confirmed":"true"}'
→ 市价平掉所有持仓(自动 reduceOnly)

步骤3: 执行后必须验证 + 总结(不可省略)
node scripts/exchange.mjs positions '{"exchange":"okx","market_type":"swap"}'
→ 确认仓位已清空,然后用一句话告诉用户结果(平了什么、盈亏多少)

To close only part of a specified trading pair: add "symbol":"BTC/USDT:USDT"

Why is there step 3: The return of close_position is sometimes truncated by streaming, so the user cannot see the result. Querying positions one more time both confirms the closing succeeded and allows the conclusion to be written into the final message for the user to see.

Take-profit/stop-loss / conditional order process (two steps, cannot be skipped)

To attach a stop-loss / take-profit to an existing position, you must use set_stop, manually building create_order 's STOP_MARKET+ raw params by hand is prohibited.set_stop It will derive from the exchange'sreal positionautomatically derive the direction,reduceOnly / posSide / positionSide(Binance hedge mode, OKX, and Bybit each have their own adaptation), and verify that the trigger price is on the correct side of the current price — hand-building very easily opens a reverse order or gets the direction backwards.

步骤1: node scripts/exchange.mjs set_stop '{"exchange":"binance","symbol":"HYPE/USDT:USDT","market_type":"swap","stop_loss":63.5,"take_profit":66.5}'
→ 返回预览(持仓方向、触发价、触发后动作、平仓模式、方向校验结果)
→ 你必须把预览展示给用户

步骤2: 用户确认后
node scripts/exchange.mjs set_stop '{"exchange":"binance","symbol":"HYPE/USDT:USDT","market_type":"swap","stop_loss":63.5,"take_profit":66.5,"confirmed":"true"}'
→ 实际挂条件单

步骤3: 执行后复核(不可省略)
node scripts/exchange.mjs stop_orders '{"exchange":"binance","symbol":"HYPE/USDT:USDT","market_type":"swap"}'
→ 确认条件单已挂上,再把结论告诉用户
  • Parameters:stop_loss(stop-loss trigger price) / take_profit(take-profit trigger price) at least one must be given; both can be given at the same time; if only a single trigger_price is given, it will be automatically classified as stop-loss or take-profit based on direction.amount Optional, defaults to full position, automatically clamped if it exceeds the position.side(long/short): when the same trading pairsimultaneously holds both long and short positions(hedge mode), it must be specified, otherwise set_stop will error out and make you choose a side, and will never guess for you.
  • Long position: stop-loss < current price, take-profit > current price;Short positionvice versa. If set in reverse, it will be set_stop blocked with an error (to prevent instant triggering). When the current price cannot be obtained, it defaults to aborting (direction cannot be validated); if you really need to skip validation, you can pass "force":true at your own risk.
  • Hedge mode fully automatic adaptation: when opening a position, the direction parameter is automatically added (Binance positionSide、OKX posSide、Bybit positionIdx), and stop-loss/take-profit/close positions derive the direction from the real position — Binance/OKX/Bybit/Bitget/HTX hedge accounts all require no manual specification. Among them, Bitget/HTX closing is converted by ccxt into tradeSide:Close/offset:close(pure reduceOnly would be treated as opening a reverse position in their hedge mode, which has been specially handled); if Bitget's position mode cannot be identified for some reason, it willabort and report an errorrather than risk opening a reverse position.set_stop The stop-loss/take-profit placed are exchange conditional/algorithmic orders (stop_orders can be queried), and after closing the position, cancel_order can be used to clear any remaining conditional orders together.
  • Before confirming execution, set_stop willread the position once moreand calibrate the quantity to the current position; if the position has already been closed/reversed during confirmation, it will directly report "position no longer exists" instead of placing a dead order.
  • Why is there step 3: conditional orders on some exchanges (OKX, etc.) are algorithmic/order types,do not appear in the regular open_orders list, and can only be found via stop_orders or the exchange APP's "Conditional Orders" section. Don't use open_orders to mistakenly conclude "it wasn't placed".
  • Fallback: ccxt has differences in conditional order details across exchanges (Binance triggers use mark price/last price, OKX algo orders, etc.); if set_stop a certain entry returns a failure, truthfully tell the user the reason for the failure and suggest placing it manually in the exchange APP — do not falsely claim it was placed.
  • Conditional order support matrix (ccxt 4.5.47 tested): stop-loss/take-profit/trigger orders are fully supported on all 6 CEXs: Binance / OKX / Bybit / Bitget / Gate / HTX(each mapped to that exchange's native conditional order). Before placing an order there is asafety net: if the trigger price is silently dropped by an exchange (which would become an immediately filled market order),placeOrder will directly reject rather than mistakenly place the order. After each ccxt upgrade, run npm run verify-orders(request body golden matrix) to confirm there is no drift. Hyperliquid uses trigger and is an edge case (USDC, mostly routed to aicoin-onchain); rely on live trading.

Pre-order preparation

Step Command Requires confirmation
Set leverage + margin mode node scripts/exchange.mjs set_trading_params '{"exchange":"okx","symbol":"BTC/USDT:USDT","leverage":10,"margin_mode":"isolated","market_type":"swap"}' Required(see Iron Rule #6)
Set leverage separately node scripts/exchange.mjs set_leverage '{"exchange":"okx","symbol":"BTC/USDT:USDT","leverage":10,"market_type":"swap"}' Required(see Iron Rule #6)
Query contract info node scripts/exchange.mjs markets '{"exchange":"okx","market_type":"swap","base":"BTC"}' Not required (read-only)

Leverage / Margin Confirmation Template(copy directly and replace the numbers):

"I'm about to change the OKX BTC/USDT perpetual leverage to {N}x,margin_mode = {isolated/cross}. This will affect the margin usage and liquidation distance for all subsequent orders on this trading pair ({N}x leverage liquidation is about {1/N*100}% excluding fees). Confirm the change?"

After confirmation, actually adjust set_trading_params / set_leverage. If the user says "never mind", "hold on", "let me think about it",do notrun the script.

Other Commands

Action Command
Close position (all or specified) node scripts/exchange.mjs close_position '{"exchange":"okx","market_type":"swap"}' — add "symbol":"BTC/USDT:USDT" to close only a single one
Take-profit/stop-loss (place protective orders for an existing position) node scripts/exchange.mjs set_stop '{"exchange":"binance","symbol":"HYPE/USDT:USDT","market_type":"swap","stop_loss":63.5,"take_profit":66.5}' — two-step confirmation, see "Take-profit/stop-loss process" above
Query conditional orders/algorithmic orders node scripts/exchange.mjs stop_orders '{"exchange":"binance","symbol":"HYPE/USDT:USDT","market_type":"swap"}' — regular open_orders use this for conditional orders that cannot be found
Cancel order node scripts/exchange.mjs cancel_order '{"exchange":"okx","symbol":"BTC/USDT","order_id":"xxx"}' — can also cancel conditional orders
Store exchange key (locally, when the user provides the key in chat) node scripts/exchange.mjs save_key '{"exchange":"binance","api_key":"...","api_secret":"..."}' — write into ~/.coinos/.env、chmod 600, do not echo the secret (OKX/Bitget also require "password":"..."). Inside the container, guide the user to the web UI EnvSection; do not collect the key in chat

Quantity

Automatic contract conversion: amount is always what the user sayscoin quantity(e.g. 0.01 BTC, 1000 DOGE), the script uniformly converts to contract count based on amount / contractSize — integers are also treated as coin quantity (the old convention of "integer = contract count" would miscalculate contract counts by several orders of magnitude on exchanges with contractSize≠1 such as OKX/Gate, and has been abolished).If you really want to pass contract count directly, add "amount_unit":"contracts"。 Place an order using a USDT amount: When the user says "go long with 10U" or "spend 10 USDT to open a position", pass cost=10(contract = USDT margin amount, contract count calculated by price + leverage; spot market buy = how much USDT to spend, coin quantity back-calculated at the current price), do not pass amount. Spot: amount = coin quantity; or use cost to buy by USDT amount.

Format: spot BTC/USDT, contract BTC/USDT:USDT, Hyperliquid uses USDC: BTC/USDC:USDC。

Exchange: Binance, OKX, Bybit, Bitget, Gate.io, HTX, Pionex, Hyperliquid。