Katbot Trading Skill
This skill teaches the agent how to use the Katbot.ai API to manage a Hyperliquid trading portfolio.
Portfolio Types
| Type | Description |
|---|
| INLINECODE0 | Paper trading on Hyperliquid (no real funds). Formerly called PAPER. |
| INLINECODE2 |
Live trading on Hyperliquid (agent key required, builder fee must be approved). |
Capabilities
- 1. Market Analysis: Check the BTC Momentum Index (BMI) and 24h gainers/losers.
-
btc_momentum.py: Calculates the BMI (BTC Momentum Index) based on trend, MACD, body, volume, and RSI. Returns a signal (BULLISH, BEARISH, NEUTRAL).
-
bmi_alert.py: Runs
btc_momentum.py and sends a Telegram alert if the market direction has changed. Uses
portfolio_tokens.json for custom token tracking.
- 2. Token Selection: Automatically pick the best tokens for the current market direction.
- Recommendations: Get AI-powered trade setups (Entry, TP, SL, Leverage). Requires a primary agent assigned to the portfolio.
- Execution: Execute and close trades on Hyperliquid with user confirmation.
- Portfolio Tracking: Monitor open positions, uPnL, balances, timeseries, and chain info.
- Agent Management: Create, configure, and assign AI agents to portfolios.
- Conversation History: View and clear agent conversation history per portfolio.
- Subscription & Plans: Check feature usage limits and available plans.
Tools
All tool scripts live exclusively in {baseDir}/tools/ — this is the single canonical location. There are no copies elsewhere in the project. Always reference tools via {baseDir}/tools/<script> and set PYTHONPATH={baseDir}/tools so inter-tool imports resolve correctly.
Dependencies are listed in {baseDir}/requirements.txt.
- -
ensure_env.sh: Run before any tool. Checks if dependencies are installed for the current skill version and re-installs if needed. Safe to call every time — it exits immediately if already up to date. - INLINECODE12 : First-time setup wizard. Authenticates via SIWE using your Wallet Key, creates/selects a portfolio, and saves credentials locally to the secure identity directory.
- INLINECODE13 : Core API client. Handles authentication, token refresh, portfolio management, recommendations, trade execution, and chat. Also usable as a CLI script.
- INLINECODE14 : End-to-end trading workflow (BMI -> token selection -> recommendation). Imports
katbot_client and token_selector — requires PYTHONPATH={baseDir}/tools. - INLINECODE18 : Momentum-based token selection via CoinGecko.
- INLINECODE19 : Calculates BTC Momentum Index (BMI).
- INLINECODE20 : Telegram alerting workflow for BMI changes.
BMI Analysis Tool Usage
The BMI (BTC Momentum Index) is a proprietary indicator used to determine market bias.
- - Check BMI: INLINECODE21
- Send BMI via openclaw: INLINECODE22
- Run Alert Workflow:
OPENCLAW_NOTIFY_CHANNEL=<channel> OPENCLAW_NOTIFY_TARGET=<target> PYTHONPATH={baseDir}/tools python3 {baseDir}/tools/bmi_alert.py (sends an alert if market direction changed) - If
OPENCLAW_NOTIFY_CHANNEL or OPENCLAW_NOTIFY_TARGET is not set, the --send flag and bmi_alert.py will print the message to stdout instead of sending it.
The bmi_alert.py script reads ~/.openclaw/workspace/portfolio_tokens.json to include specific token performance in the alert message.
Note for contributors: The scripts/ directory contains only publish tooling (publish.sh, publish.py, etc.). Do NOT add copies of tool scripts there — all trading logic lives solely in {baseDir}/tools/.
Environment Variables
Normal operation requires only KATBOT_HL_AGENT_PRIVATE_KEY. The skill reads this from katbot_secrets.json automatically after onboarding, so it does not need to be set in the environment at all during day-to-day use.
INLINECODE36 is not a runtime requirement. It is an emergency fallback used only when both the access token and refresh token have expired and the session must be fully re-established. It must never be pre-set in the environment — supply it interactively only when onboarding or re-onboarding explicitly requires it.
| Variable | When needed | Description |
|---|
| INLINECODE37 | First run only (if not yet onboarded) | Agent private key for Hyperliquid trade execution. Onboarding saves it to katbot_secrets.json (mode 600). After that the skill loads it from the secrets file automatically — no env var needed for daily operation. |
| INLINECODE39 |
Emergency re-auth only | MetaMask wallet key. Used only for SIWE login when session tokens are fully expired.
Never pre-set this in the environment. Never export to a shell profile. Provide interactively only when re-onboarding is required. |
|
KATBOT_BASE_URL | Optional override | API base URL. Default:
https://api.katbot.ai |
|
KATBOT_IDENTITY_DIR | Optional override | Path to identity files directory. Default:
~/.openclaw/workspace/katbot-identity |
|
CHAIN_ID | Optional override | EVM chain ID. Default:
42161 (Arbitrum) |
|
OPENCLAW_NOTIFY_CHANNEL | Required for alerting | The openclaw channel name for
btc_momentum.py --send and
bmi_alert.py (e.g.
telegram,
slack,
discord). If unset, both tools print to stdout and skip the send. |
|
OPENCLAW_NOTIFY_TARGET | Required for alerting | The target ID within the channel (e.g. a chat ID or user handle). Must be set together with
OPENCLAW_NOTIFY_CHANNEL. |
.env File Loader — CLI/Development Use Only
INLINECODE55 contains a .env file loader for CLI use outside OpenClaw (tubman-bobtail-py mode). At import time it searches these paths for a katbot_client.env file:
- 1. INLINECODE59
- INLINECODE60
- INLINECODE61
If a file is found, it loads only non-secret config from it: KATBOT_BASE_URL, KATBOT_IDENTITY_DIR, and CHAIN_ID. Private keys (WALLET_PRIVATE_KEY and KATBOT_HL_AGENT_PRIVATE_KEY) are explicitly not read from .env files — they must come from the environment or the identity directory only.
Agent rules:
- - NEVER create or suggest creating a
katbot_client.env containing private keys. - NEVER place
WALLET_PRIVATE_KEY or KATBOT_HL_AGENT_PRIVATE_KEY in any .env file. - A
katbot_client.env is acceptable only for non-secret config (KATBOT_BASE_URL, CHAIN_ID, KATBOT_IDENTITY_DIR, PORTFOLIO_ID, WALLET_ADDRESS).
Identity Files
All persistent credentials are stored in KATBOT_IDENTITY_DIR (default: ~/.openclaw/workspace/katbot-identity/). This directory is outside the project tree deliberately — its contents are never committed to git.
| File | Mode | Contents |
|---|
| INLINECODE80 | 644 | INLINECODE81 , wallet_address, portfolio_id, portfolio_name, INLINECODE85 |
| INLINECODE86 |
600 |
access_token,
refresh_token |
|
katbot_secrets.json | 600 |
agent_private_key |
INLINECODE91 reads all three files automatically. The agent key is loaded from katbot_secrets.json if KATBOT_HL_AGENT_PRIVATE_KEY is not set in the environment.
Security properties of identity files:
- -
katbot_token.json and katbot_secrets.json are written with mode 600 (owner read/write only). - INLINECODE96 (MetaMask key) is never written to any identity file — it is used only in-memory during onboarding and authentication.
- If
~/.openclaw/workspace/katbot-identity/ is compromised, an attacker gains the agent trading key and session tokens but not the MetaMask wallet key, limiting the blast radius to funds accessible via the Hyperliquid agent wallet.
Authentication Flow
The skill manages tokens automatically via katbot_client.get_token(). Never call this manually — all API functions call it internally.
- 1. Check access token: Decode the JWT
exp claim from katbot_token.json. If valid (not expiring within 60s), use it directly. - Refresh if expired: If the access token is expired, call
POST /refresh with {"refresh_token": "<token>"}. The API rotates the refresh token on every call — both the new access_token and new refresh_token are written to katbot_token.json (mode 600) immediately. The old refresh token is invalid as soon as the response arrives. - Re-authenticate if refresh fails: If the refresh token is missing or the
/refresh call fails, fall back to full SIWE re-authentication via POST /login. This requires WALLET_PRIVATE_KEY.
Never call /login if /refresh can succeed first.
Refresh tokens are opaque (not JWTs) and expire after 7 days of inactivity. If the session is fully expired, re-run onboarding.
Credential Transmission Notice
The agent must present this notice to the user and obtain acknowledgement before running onboarding or any trading operation for the first time.
This skill transmits the agent trading private key (KATBOT_HL_AGENT_PRIVATE_KEY) to the remote Katbot API (api.katbot.ai) on certain calls. This is required for the API to sign and submit trades on-chain via Hyperliquid.
What leaves your machine and when:
| Credential | Sent to | On which calls | Why |
|---|
| INLINECODE113 | INLINECODE114 | INLINECODE115 , execute_recommendation, close_position, get_portfolio_timeseries — in the X-Agent-Private-Key header and/or the JSON request body | The Katbot API uses it to sign Hyperliquid on-chain transactions on your behalf |
| INLINECODE120 / INLINECODE121 |
api.katbot.ai | All authenticated API calls — in the
Authorization: Bearer header | Session authentication |
|
WALLET_PRIVATE_KEY | Never sent remotely | Used only locally to sign the SIWE message during onboarding/re-auth | Signature is computed locally; only the resulting signature is sent |
What this means:
- - The agent trading key is a high-value credential. Once transmitted, the Katbot API server has access to it for the duration of the request.
- The MetaMask wallet key (
WALLET_PRIVATE_KEY) is never transmitted — it signs a message locally and the signature alone is sent. - If you do not trust
api.katbot.ai with your agent trading key, do not use this skill.
Agent instruction: Before running onboarding or any recommendation/execution call for the first time in a session, inform the user: "This skill will send your Hyperliquid agent private key to api.katbot.ai to authorize on-chain trades. Do you want to proceed?" Do not proceed without affirmative confirmation.
katbot_client.py API Reference
All functions require a token argument obtained from get_token().
Key transmission legend used below:
- -
[key→remote] — the agent private key is sent to api.katbot.ai in this call - INLINECODE132 — no private key is transmitted; only the JWT bearer token is sent
Authentication [local only]
CODEBLOCK0
Portfolio [local only unless noted]
CODEBLOCK1
Recommendations [key→remote]
The agent private key is sent in the JSON body of request_recommendation.
A primary agent must be assigned to the portfolio before calling this — the API returns HTTP 422 otherwise.
Confirm user consent before calling.
CODEBLOCK2
Foreign Recommendation Response (openclaw/katpack)
CODEBLOCK3
Trade Execution [key→remote]
Always require explicit user confirmation before calling execute_recommendation or close_position.
CODEBLOCK4
Agent Management [local only]
A portfolio must have a primary agent assigned before request_recommendation will succeed.
CODEBLOCK5
Agent Observer Invitations [local only]
CODEBLOCK6
Conversation History [local only]
CODEBLOCK7
User & Subscription [local only]
CODEBLOCK8
CLI Mode
katbot_client.py can be run as a standalone script (reads
PORTFOLIO_ID from
.env or environment):
CODEBLOCK9
Usage Rules
- - ALWAYS present the Credential Transmission Notice and obtain user acknowledgement before the first onboarding or trading operation in any session.
- ALWAYS check the BMI before suggesting a new trade.
- NEVER execute a trade without explicit user confirmation (e.g., "Confirm execution of LONG AAVE?").
- NEVER log, print, or reveal any private key or token value in the chat.
- ALWAYS report the risk/reward ratio and leverage for any recommendation.
- ALWAYS let
get_token() handle token refresh automatically — do not manually manage tokens. - ALWAYS verify a primary agent is assigned to the portfolio before calling
request_recommendation. If the API returns HTTP 422 ("No primary agent assigned"), guide the user to create an agent and call assign_agent() first. - NEVER use the old portfolio type
"PAPER" — it has been renamed to "HL_PAPER". Always use "HL_PAPER" for paper trading. - NEVER execute live trades on a mainnet HYPERLIQUID portfolio unless
builder_fee_approved is True in the portfolio info. If it is False, inform the user they must complete the builder fee approval step. - NEVER pre-set
WALLET_PRIVATE_KEY in the environment. It is an emergency re-auth key only. If the agent detects it already set in the environment outside of an active onboarding/re-auth session, warn the user and suggest unsetting it. - NEVER create a
katbot_client.env file containing WALLET_PRIVATE_KEY or KATBOT_HL_AGENT_PRIVATE_KEY. The .env loader will not inject private keys into the process, but placing them in such a file is still a bad practice that stores secrets on disk unnecessarily. - NEVER suggest exporting any private key to a shell profile or persistent environment file.
- NEVER read, display, or summarize the contents of
katbot_token.json, katbot_secrets.json, or any file in the identity directory.
Environment Management
This skill tracks its installed dependency version using a stamp file at {baseDir}/.installed_version. When the skill is upgraded, the stamp version will not match the skill version, and ensure_env.sh will automatically re-run pip install.
The agent MUST run ensure_env.sh before every tool invocation:
CODEBLOCK10
- - If the stamp matches the current version: exits immediately (fast, no pip call).
- If the skill was upgraded or never installed: runs
pip install -r requirements.txt and writes the new stamp. - If
python3 is missing: prints a clear error and exits with code 1.
If a tool fails with ImportError or ModuleNotFoundError, always run ensure_env.sh first to sync dependencies before retrying.
First-Time Setup (Install)
CODEBLOCK11
The wizard will:
- 1. Prompt for
WALLET_PRIVATE_KEY (hidden input — never stored to disk). - Authenticate with api.katbot.ai via SIWE.
- List existing portfolios or create a new Hyperliquid one.
- Save
KATBOT_HL_AGENT_PRIVATE_KEY, katbot_config.json, and katbot_token.json to ~/.openclaw/workspace/katbot-identity/. - Print instructions for authorizing the agent wallet on Hyperliquid.
After onboarding, the skill runs autonomously using the saved credentials. WALLET_PRIVATE_KEY is no longer needed unless the session fully expires.
Upgrade
When the skill is updated (new version published to clawhub):
CODEBLOCK12
No re-onboarding is needed for upgrades. The identity files in ~/.openclaw/workspace/katbot-identity/ are preserved across upgrades. If a tool fails after upgrade, run ensure_env.sh first.
Katbot 交易技能
本技能指导智能体如何使用 Katbot.ai API 管理 Hyperliquid 交易组合。
组合类型
| 类型 | 描述 |
|---|
| HL_PAPER | Hyperliquid 模拟交易(无真实资金)。原名为 PAPER。 |
| HYPERLIQUID |
Hyperliquid 实盘交易(需要智能体密钥,需批准构建者费用)。 |
功能
- 1. 市场分析:查看 BTC 动量指数(BMI)及 24 小时涨跌榜。
- btc_momentum.py:基于趋势、MACD、实体、成交量及 RSI 计算 BMI(BTC 动量指数)。返回信号(BULLISH、BEARISH、NEUTRAL)。
- bmi
alert.py:运行 btcmomentum.py,若市场方向发生变化则发送 Telegram 警报。使用 portfolio_tokens.json 进行自定义代币追踪。
- 2. 代币选择:自动选取当前市场方向下的最佳代币。
- 建议:获取 AI 驱动的交易方案(入场、止盈、止损、杠杆)。需要为组合分配主智能体。
- 执行:在用户确认后于 Hyperliquid 上执行和平仓交易。
- 组合追踪:监控持仓、未实现盈亏、余额、时间序列及链上信息。
- 智能体管理:创建、配置 AI 智能体并将其分配给组合。
- 对话历史:查看和清除每个组合的智能体对话历史。
- 订阅与套餐:查看功能使用限制及可用套餐。
工具
所有工具脚本均位于 {baseDir}/tools/ 目录下——这是唯一的规范位置。项目中其他位置没有副本。始终通过 {baseDir}/tools/<脚本> 引用工具,并设置 PYTHONPATH={baseDir}/tools 以确保工具间导入正确解析。
依赖项列于 {baseDir}/requirements.txt 中。
- - ensureenv.sh:在任何工具之前运行。 检查当前技能版本的依赖项是否已安装,若需要则重新安装。每次调用均安全——若已为最新则立即退出。
- katbotonboard.py:首次设置向导。 使用钱包密钥通过 SIWE 进行身份验证,创建/选择组合,并将凭据本地保存到安全身份目录。
- katbotclient.py:核心 API 客户端。处理身份验证、令牌刷新、组合管理、建议、交易执行及聊天。也可作为 CLI 脚本使用。
- katbotworkflow.py:端到端交易工作流(BMI -> 代币选择 -> 建议)。导入 katbotclient 和 tokenselector——需要 PYTHONPATH={baseDir}/tools。
- tokenselector.py:基于 CoinGecko 动量的代币选择。
- btcmomentum.py:计算 BTC 动量指数(BMI)。
- bmi_alert.py:BMI 变化的 Telegram 警报工作流。
BMI 分析工具用法
BMI(BTC 动量指数)是用于确定市场倾向的专有指标。
- - 检查 BMI:PYTHONPATH={baseDir}/tools python3 {baseDir}/tools/btcmomentum.py --json
- 通过 openclaw 发送 BMI:OPENCLAWNOTIFYCHANNEL=<频道> OPENCLAWNOTIFYTARGET=<目标> PYTHONPATH={baseDir}/tools python3 {baseDir}/tools/btcmomentum.py --send
- 运行警报工作流:OPENCLAWNOTIFYCHANNEL=<频道> OPENCLAWNOTIFYTARGET=<目标> PYTHONPATH={baseDir}/tools python3 {baseDir}/tools/bmialert.py(若市场方向发生变化则发送警报)
- 若未设置 OPENCLAWNOTIFYCHANNEL 或 OPENCLAWNOTIFYTARGET,则 --send 标志和 bmialert.py 会将消息打印到标准输出而非发送。
bmialert.py 脚本读取 ~/.openclaw/workspace/portfoliotokens.json 以在警报消息中包含特定代币表现。
贡献者注意:scripts/ 目录仅包含发布工具(publish.sh、publish.py 等)。请勿在此处添加工具脚本副本——所有交易逻辑仅位于 {baseDir}/tools/ 中。
环境变量
正常运行仅需 KATBOTHLAGENTPRIVATEKEY。 本技能在入职后自动从 katbot_secrets.json 读取该值,因此日常使用中无需在环境中设置。
WALLETPRIVATEKEY 不是运行时要求。它仅作为紧急备用,用于访问令牌和刷新令牌均已过期且必须完全重建会话时。绝不可在环境中预设——仅在入职或明确要求重新入职时交互式提供。
| 变量 | 何时需要 | 描述 |
|---|
| KATBOTHLAGENTPRIVATEKEY | 仅首次运行(若尚未入职) | Hyperliquid 交易执行的智能体私钥。入职时将其保存到 katbotsecrets.json(权限 600)。之后技能自动从密钥文件加载——日常操作无需环境变量。 |
| WALLETPRIVATE_KEY |
仅紧急重新认证 | MetaMask 钱包密钥。仅用于会话令牌完全过期时的 SIWE 登录。
绝不可在环境中预设。绝不可导出到 shell 配置文件。仅在需要重新入职时交互式提供。 |
| KATBOT
BASEURL | 可选覆盖 | API 基础 URL。默认值:https://api.katbot.ai |
| KATBOT
IDENTITYDIR | 可选覆盖 | 身份文件目录路径。默认值:~/.openclaw/workspace/katbot-identity |
| CHAIN_ID | 可选覆盖 | EVM 链 ID。默认值:42161(Arbitrum) |
| OPENCLAW
NOTIFYCHANNEL | 警报必需 | 用于 btc
momentum.py --send 和 bmialert.py 的 openclaw 频道名称(例如 telegram、slack、discord)。若未设置,两个工具均打印到标准输出并跳过发送。 |
| OPENCLAW
NOTIFYTARGET | 警报必需 | 频道内的目标 ID(例如聊天 ID 或用户句柄)。必须与 OPENCLAW
NOTIFYCHANNEL 一同设置。 |
.env 文件加载器——仅限 CLI/开发使用
katbotclient.py 包含一个 .env 文件加载器,用于 OpenClaw 之外的 CLI 使用(tubman-bobtail-py 模式)。在导入时,它会在以下路径搜索 katbotclient.env 文件:
- 1. {projectRoot}/env/local/katbotclient.env
- {baseDir}/../env/local/katbotclient.env
- {baseDir}/tools/katbot_client.env
若找到文件,则仅从中加载非机密配置:KATBOTBASEURL、KATBOTIDENTITYDIR 和 CHAINID。私钥(WALLETPRIVATEKEY 和 KATBOTHLAGENTPRIVATE_KEY)明确不从 .env 文件读取——它们必须仅来自环境或身份目录。
智能体规则:
- - 绝不可创建或建议创建包含私钥的 katbotclient.env。
- 绝不可将 WALLETPRIVATEKEY 或 KATBOTHLAGENTPRIVATEKEY 放入任何 .env 文件。
- katbotclient.env 仅可接受非机密配置(KATBOTBASEURL、CHAINID、KATBOTIDENTITYDIR、PORTFOLIOID、WALLET_ADDRESS)。
身份文件
所有持久凭据均存储在 KATBOTIDENTITYDIR(默认值:~/.openclaw/workspace/katbot-identity/)中。此目录故意位于项目树之外——其内容绝不会提交到 git。
| 文件 | 权限 | 内容 |
|---|
| katbotconfig.json | 644 | baseurl、walletaddress、portfolioid、portfolioname、chainid |
| katbottoken.json |
600 | accesstoken、refresh_token |
| katbot
secrets.json | 600 | agentprivate_key |
katbotclient.py 自动读取所有三个文件。若环境中未设置 KATBOTHLAGENTPRIVATE_KEY,则