Alpaca Trading Skill
Trade stocks and crypto programmatically via Alpaca's API.
Setup
Requires API credentials stored in environment or config:
CODEBLOCK0
Or store in ~/.openclaw/credentials/alpaca.json:
CODEBLOCK1
Quick Reference
Get Quote
CODEBLOCK2
Get Bars (Historical Data)
CODEBLOCK3
Check Account
CODEBLOCK4
List Positions
CODEBLOCK5
Place Orders
CODEBLOCK6
Order Guardrails:
- 1. Symbol validation — Rejects invalid/unknown tickers
- Buying power check — Blocks orders exceeding available funds, shows max shares
- Duplicate detection — Warns if you have open orders for same symbol/side
- Price validation — Warns if limit price is worse than market
- Market hours check — Detects pre-market, after-hours, and closed sessions
- Pre-market (4:00 AM - 9:30 AM ET): Option to place pre-market order
- After-hours (4:00 PM - 8:00 PM ET): Option to place after-hours order
- Closed: Warns order will queue until market open
- 6. Cost confirmation — Shows total cost and requires confirmation
Use --force to skip all confirmation prompts (use with caution).
List Orders
CODEBLOCK7
Cancel Order
CODEBLOCK8
Get News
CODEBLOCK9
Watchlist
CODEBLOCK10
Stream Live Data (Websocket)
CODEBLOCK11
Press Ctrl+C to stop streaming.
Price Alerts
CODEBLOCK12
Alerts are stored in ~/.openclaw/data/alpaca-alerts.json.
Script Location
All commands use: scripts/alpaca_cli.py (relative to this skill directory)
API Reference
See references/api.md for detailed API documentation and response formats.
Safety Notes
- - Always confirm with user before placing real trades
- Paper trading (
ALPACA_PAPER=true) recommended for testing - Check buying power before large orders
- Verify order details before submission
Alpaca 交易技能
通过 Alpaca API 以编程方式交易股票和加密货币。
设置
需要将 API 凭据存储在环境变量或配置文件中:
bash
设置环境变量
export ALPACA
APIKEY=your-api-key
export ALPACA
SECRETKEY=your-secret-key
export ALPACA_PAPER=true # true 为模拟交易,false 为实盘交易
或者存储在 ~/.openclaw/credentials/alpaca.json 中:
json
{
apiKey: your-api-key,
secretKey: your-secret-key,
paper: true
}
快速参考
获取报价
bash
python3 scripts/alpaca_cli.py quote AAPL
python3 scripts/alpaca_cli.py quote AAPL,TSLA,NVDA
获取K线数据(历史数据)
bash
python3 scripts/alpaca_cli.py bars AAPL --timeframe 1Day --limit 10
python3 scripts/alpaca_cli.py bars AAPL --timeframe 1Hour --start 2026-02-01
查看账户
bash
python3 scripts/alpaca_cli.py account
查看持仓
bash
python3 scripts/alpaca_cli.py positions
下单
bash
市价单
python3 scripts/alpaca_cli.py order buy AAPL 10
限价单
python3 scripts/alpaca_cli.py order buy AAPL 10 --limit 150.00
止损单
python3 scripts/alpaca_cli.py order sell TSLA 5 --stop 200.00
止损限价单
python3 scripts/alpaca_cli.py order sell TSLA 5 --stop 200.00 --limit 195.00
跳过价格验证(谨慎使用)
python3 scripts/alpaca_cli.py order buy AAPL 10 --limit 999.00 --force
订单保护机制:
- 1. 代码验证 — 拒绝无效/未知的股票代码
- 购买力检查 — 阻止超出可用资金的订单,显示最大可买股数
- 重复检测 — 如果已有相同代码/方向的未成交订单,发出警告
- 价格验证 — 如果限价价格劣于市价,发出警告
- 交易时间检查 — 检测盘前、盘后和休市时段
- 盘前(美东时间 4:00 AM - 9:30 AM):可选择下达盘前订单
- 盘后(美东时间 4:00 PM - 8:00 PM):可选择下达盘后订单
- 休市:警告订单将排队等待开盘
- 6. 成本确认 — 显示总成本并要求确认
使用 --force 跳过所有确认提示(谨慎使用)。
查看订单
bash
python3 scripts/alpaca_cli.py orders
python3 scripts/alpaca_cli.py orders --status open
python3 scripts/alpaca_cli.py orders --status closed --limit 20
取消订单
bash
python3 scripts/alpaca
cli.py cancel ORDERID
python3 scripts/alpaca_cli.py cancel all # 取消所有未成交订单
获取新闻
bash
python3 scripts/alpaca_cli.py news AAPL
python3 scripts/alpaca_cli.py news AAPL,TSLA --limit 5
自选股列表
bash
python3 scripts/alpaca_cli.py watchlist list
python3 scripts/alpaca_cli.py watchlist create 科技股 AAPL,MSFT,GOOGL
python3 scripts/alpaca
cli.py watchlist add WATCHLISTID NVDA
python3 scripts/alpaca
cli.py watchlist delete WATCHLISTID
实时数据流(Websocket)
bash
流式传输成交数据(默认)
python3 scripts/alpaca_cli.py stream AAPL
流式传输报价数据
python3 scripts/alpaca_cli.py stream AAPL,TSLA --type quotes
流式传输K线数据(1分钟)
python3 scripts/alpaca_cli.py stream NVDA --type bars
流式传输所有数据类型
python3 scripts/alpaca_cli.py stream AAPL --type all
按 Ctrl+C 停止流式传输。
价格提醒
bash
添加提醒 - 当 INTU 跌破 $399 时通知
python3 scripts/alpaca_cli.py alert add --symbol INTU --price 399 --condition below
添加提醒 - 当 AAPL 涨破 $300 时通知
python3 scripts/alpaca_cli.py alert add --symbol AAPL --price 300 --condition above
查看活跃提醒
python3 scripts/alpaca_cli.py alert list
检查提醒(由定时任务使用)
python3 scripts/alpaca_cli.py alert check
移除提醒
python3 scripts/alpaca
cli.py alert remove --alertid ABC123
清除所有提醒
python3 scripts/alpaca_cli.py alert clear
提醒存储在 ~/.openclaw/data/alpaca-alerts.json 中。
脚本位置
所有命令均使用:scripts/alpaca_cli.py(相对于此技能目录)
API 参考
详细 API 文档和响应格式请参见 references/api.md。
安全提示
- - 在执行真实交易前务必与用户确认
- 建议使用模拟交易(ALPACA_PAPER=true)进行测试
- 大额订单前请检查购买力
- 提交前请核实订单详情