Portfolio Drift Monitor
Real-time monitoring of Kalshi portfolio positions with configurable alerts when positions move significantly. Ideal for active traders tracking exposure, P&L swings, and position concentration.
Overview
The Portfolio Drift Monitor tracks your Kalshi positions and alerts when any position moves beyond your configured threshold percentage since the last check. It:
- - Compares snapshots: Current positions vs. last recorded snapshot
- Detects drift: Flags positions that moved ≥ threshold (default 5%)
- Rate-limits checks: Prevents excessive API calls (configurable, default 60min interval)
- Formats alerts: Emoji indicators (📈/📉), directional arrows, price changes
- Persistent state: Stores snapshots locally at INLINECODE0
When to Use This Skill
- - You hold Kalshi positions overnight and want alerts if something moves sharply
- You want hourly drift checks between your morning and evening briefs
- You manage multiple positions and need to catch concentration risk early
- You want a lightweight risk monitoring layer that runs on a schedule without manual checking
Prerequisites
- - kalshi_python SDK: Install via INLINECODE1
- Kalshi API credentials:
KALSHI_KEY_ID and KALSHI_KEY_PATH environment variables - API key file: Located at path specified by
KALSHI_KEY_PATH (typically ~/.kalshi/key.pem)
Configuration
Set these environment variables or edit defaults in your environment:
| Variable | Default | Purpose |
|---|
| INLINECODE6 | (required) | Your Kalshi API key ID from dev.kalshi.com |
| INLINECODE7 |
(required) | Path to your Kalshi private key file (ES256 PEM) |
|
PORTFOLIO_DRIFT_THRESHOLD |
5.0 | Percentage change to trigger alert (e.g., 5 = 5%) |
|
PORTFOLIO_DRIFT_INTERVAL |
60 | Minutes between checks (rate limiting, prevents API spam) |
How to Trigger
Use any of these phrases in conversation:
- - "check portfolio drift"
- "portfolio alert"
- "position monitor"
- "drift check"
- "kalshi positions"
How It Works
Step 1: Fetch Current Portfolio
The skill calls Kalshi's API to get your current open positions:
CODEBLOCK0
This reads KALSHI_KEY_ID and KALSHI_KEY_PATH from environment, authenticates with Kalshi, and retrieves all open positions.
Step 2: Load Previous Snapshot
On first run, creates baseline snapshot. On subsequent runs, compares current positions with the last recorded snapshot stored at ~/.openclaw/state/portfolio_snapshot.json.
Step 3: Detect Drift
For each position, calculates percent change in:
- - Holdings (share count)
- Unrealized P&L (USD value)
- Price (avg entry price vs. current)
If any metric ≥ threshold, flags that position.
Step 4: Format and Output
Outputs drift alerts with:
- - Directional emoji: 📈 (up) or 📉 (down)
- Position symbol and side (YES/NO contract)
- Percentage change with sign (e.g., "+12.5%")
- Time since last check
Step 5: Update Snapshot
Saves current portfolio as new baseline for next check.
Output Format
Example output for a position that drifted:
CODEBLOCK1
Error Handling
- - First run: Creates baseline snapshot, no alerts
- API failure: Logs error, doesn't update snapshot
- Rate limit hit: Skips check if interval not elapsed
- No positions: Outputs "Portfolio is empty"
Command Examples
Check portfolio now (bypassing rate limit)
CODEBLOCK2
Check with custom threshold
CODEBLOCK3
Check and see full state file
CODEBLOCK4
Threshold Tuning
| Profile | Threshold | Interval | Use Case |
|---|
| Tight | 3% | 30 min | Active day trading, many positions |
| Standard |
5% | 60 min | Default — balanced signal-to-noise |
|
Loose | 10% | 120 min | Long-term positions, fewer alerts |
|
News-reactive | 2% | 15 min | Event-driven trading (FOMC, elections) |
OpenClaw Ecosystem Integration
Risk management layer for the Prediction Market Trading Stack. Schedule hourly checks to catch position swings between Morning Briefs.
Scheduling:
skills:
portfolio-drift-monitor:
enabled: true
schedule: "0 * * * *" # Every hour
timeout_seconds: 30
| Connected Skill | How It Connects |
|---|
| Market Morning Brief | Morning/evening briefs bookend your day; drift alerts fill the gaps |
| Kalshi Command Center |
Monitors positions opened through the command center |
|
Kalshalyst | Tracks exposure on positions flagged by contrarian analysis |
Install the complete stack:
CODEBLOCK6
Performance & Cost
- - Runtime: <5 seconds per check
- API calls: 1 (Kalshi portfolio read)
- Cost: $0 (Kalshi free tier)
- State file: ~2KB (
portfolio_snapshot.json)
Scaling
24 checks/day (hourly) = 24 Kalshi API calls. Well within free tier limits.
Debugging
Check logs and state:
CODEBLOCK7
Agent Bug-Fix Protocol
CRITICAL: When you (the agent) discover and fix a bug in any script during a conversation, you MUST write the fix to the source file on disk using file editing tools. Showing the fix in chat is NOT sufficient — the next scheduled run starts a fresh session and will not have your conversational fix.
Checklist after finding a bug:
- 1. Edit the actual
.py file on disk (e.g., ~/skills/portfolio-drift-monitor/scripts/portfolio_drift.py) - Also update the installed copy at INLINECODE18
- Confirm both files are written before telling the user the fix is done
- If the fix is significant, stage a git commit in INLINECODE19
Never say "fixed" if you only demonstrated the fix in conversation output.
Known Limitations
- - Kalshi-only: Currently monitors Kalshi positions only. Polymarket positions require separate tracking.
- Price-based drift: Drift is calculated on mid-market price changes, not on volume or open interest shifts.
- No alerting transport: Outputs to stdout. Your OpenClaw agent or a wrapper script handles delivery (iMessage, Slack, etc.).
Additional Resources
Feedback & Issues
Found a bug? Have a feature request? Want to share results?
Part of the OpenClaw Prediction Stack — the first prediction market skill suite on ClawHub.
投资组合偏移监控器
实时监控Kalshi投资组合头寸,当头寸发生显著变动时触发可配置警报。适用于追踪风险敞口、盈亏波动和头寸集中度的活跃交易者。
概述
投资组合偏移监控器追踪您的Kalshi头寸,并在任何头寸自上次检查以来变动超过配置阈值百分比时发出警报。它能够:
- - 比较快照:当前头寸与上次记录的快照进行对比
- 检测偏移:标记变动幅度≥阈值(默认5%)的头寸
- 限速检查:防止过多API调用(可配置,默认60分钟间隔)
- 格式化警报:表情符号指示器(📈/📉)、方向箭头、价格变动
- 持久化状态:在~/.openclaw/state/portfolio_snapshot.json本地存储快照
何时使用此技能
- - 您持有Kalshi隔夜头寸,希望在价格剧烈波动时收到警报
- 您希望在早晚简报之间进行每小时偏移检查
- 您管理多个头寸,需要及早发现集中度风险
- 您需要一个轻量级风险监控层,按计划自动运行,无需手动检查
前提条件
- - kalshipython SDK:通过pip install kalshi-python安装
- Kalshi API凭证:KALSHIKEYID和KALSHIKEYPATH环境变量
- API密钥文件:位于KALSHIKEY_PATH指定的路径(通常为~/.kalshi/key.pem)
配置
设置以下环境变量或在您的环境中编辑默认值:
| 变量 | 默认值 | 用途 |
|---|
| KALSHIKEYID | (必需) | 来自dev.kalshi.com的Kalshi API密钥ID |
| KALSHIKEYPATH |
(必需) | Kalshi私钥文件路径(ES256 PEM格式) |
| PORTFOLIO
DRIFTTHRESHOLD | 5.0 | 触发警报的百分比变动(例如,5 = 5%) |
| PORTFOLIO
DRIFTINTERVAL | 60 | 检查间隔分钟数(限速,防止API滥用) |
如何触发
在对话中使用以下任一短语:
- - 检查投资组合偏移
- 投资组合警报
- 头寸监控
- 偏移检查
- kalshi头寸
工作原理
步骤1:获取当前投资组合
该技能调用Kalshi的API获取您当前的未平仓头寸:
bash
python scripts/portfolio_drift.py
此脚本从环境中读取KALSHIKEYID和KALSHIKEYPATH,通过Kalshi进行身份验证,并检索所有未平仓头寸。
步骤2:加载上次快照
首次运行时,创建基准快照。后续运行时,将当前头寸与存储在~/.openclaw/state/portfolio_snapshot.json的上次记录快照进行比较。
步骤3:检测偏移
对于每个头寸,计算以下指标的百分比变化:
- - 持仓量(股份数量)
- 未实现盈亏(美元价值)
- 价格(平均入场价与当前价格)
如果任何指标≥阈值,则标记该头寸。
步骤4:格式化并输出
输出偏移警报,包含:
- - 方向表情符号:📈(上涨)或📉(下跌)
- 头寸代码和方向(YES/NO合约)
- 带符号的百分比变化(例如+12.5%)
- 距上次检查的时间
步骤5:更新快照
将当前投资组合保存为下次检查的新基准。
输出格式
头寸发生偏移的示例输出:
📈 YES/TRUMP.25DEC → +12.5%(↑$1,250盈亏,↑350股份)
上次检查:47分钟前
📉 NO/ELEX.HOUSEGOV → -8.3%(↓$425未实现盈亏)
上次检查:42分钟前
无显著偏移:KALSHI.USD, CRYPTO.BTC
错误处理
- - 首次运行:创建基准快照,无警报
- API失败:记录错误,不更新快照
- 达到速率限制:如果间隔时间未到,跳过检查
- 无头寸:输出投资组合为空
命令示例
立即检查投资组合(绕过速率限制)
bash
PORTFOLIODRIFTTHRESHOLD=3.0 python scripts/portfolio_drift.py
使用自定义阈值检查
bash
PORTFOLIODRIFTTHRESHOLD=10.0 python scripts/portfolio_drift.py
检查并查看完整状态文件
bash
python scripts/portfoliodrift.py && cat ~/.openclaw/state/portfoliosnapshot.json | python -m json.tool
阈值调优
| 配置 | 阈值 | 间隔 | 使用场景 |
|---|
| 严格 | 3% | 30分钟 | 活跃日内交易,多头寸 |
| 标准 |
5% | 60分钟 | 默认——平衡信噪比 |
|
宽松 | 10% | 120分钟 | 长期头寸,较少警报 |
|
新闻反应 | 2% | 15分钟 | 事件驱动交易(FOMC、选举) |
OpenClaw生态系统集成
预测市场交易栈的风险管理层。安排每小时检查,以捕捉早晚简报之间的头寸波动。
调度:
yaml
skills:
portfolio-drift-monitor:
enabled: true
schedule: 0 # 每小时
timeout_seconds: 30
| 关联技能 | 连接方式 |
|---|
| 市场晨间简报 | 早晚简报作为一天的首尾;偏移警报填补中间空白 |
| Kalshi指挥中心 |
监控通过指挥中心开设的头寸 |
|
Kalshalyst | 追踪被逆向分析标记的头寸风险敞口 |
安装完整栈:
bash
clawhub install kalshalyst kalshi-command-center polymarket-command-center prediction-market-arbiter xpulse portfolio-drift-monitor market-morning-brief personality-engine
性能与成本
- - 运行时间: 每次检查<5秒
- API调用: 1次(Kalshi投资组合读取)
- 成本: $0(Kalshi免费层)
- 状态文件: 约2KB(portfolio_snapshot.json)
扩展性
每天24次检查(每小时)= 24次Kalshi API调用。完全在免费层限制范围内。
调试
检查日志和状态:
bash
查看当前快照
cat ~/.openclaw/state/portfolio_snapshot.json | python -m json.tool
手动运行并输出调试信息
python -u scripts/portfolio_drift.py 2>&1 | head -50
验证凭证
echo $KALSHI
KEYID
ls -la $KALSHI
KEYPATH
代理错误修复协议
关键:当您(代理)在对话中发现并修复任何脚本中的错误时,您必须使用文件编辑工具将修复写入磁盘上的源文件。仅在聊天中展示修复是不够的——下一次计划运行将启动新的会话,不会包含您对话中的修复。
发现错误后的检查清单:
- 1. 编辑实际的.py磁盘文件(例如~/skills/portfolio-drift-monitor/scripts/portfoliodrift.py)
- 同时更新~/.openclaw/skills/portfolio-drift-monitor/scripts/portfoliodrift.py的安装副本
- 在告知用户修复完成前,确认两个文件都已写入
- 如果修复很重要,在~/skills/中暂存git提交
切勿在仅演示了对话输出中的修复时就说已修复。
已知限制
- - 仅限Kalshi: 目前仅监控Kalshi头寸。Polymarket头寸需要单独追踪。
- 基于价格的偏移: 偏移基于中间市场价格变动计算,而非成交量或未平仓合约变动。
- 无警报传输: 输出到stdout。您的OpenClaw代理或包装脚本负责投递(iMessage、Slack等)。
其他资源
反馈与问题
发现错误?有功能请求?想分享结果?
- - GitHub Issues:[github.com/kingmadellc/openclaw-prediction-stack/issues](https://github.com/kingmadellc/openclaw-prediction-stack