Questrade Skill
Interact with a Questrade brokerage account and Canadian/US market data via
the Questrade REST API.
Setup
1. Activate API Access
Log in to Questrade → App Hub → API Centre → Activate API.
Generate a manual authorization token (this is your initial refresh token,
valid for 7 days).
2. Store Credentials
Option A — environment variables (recommended):
CODEBLOCK0
Option B — credentials file (~/.openclaw/credentials/questrade.json):
CODEBLOCK1
Important: Questrade refresh tokens rotate — every time the token is
used to obtain a new access token, Questrade issues a new refresh token.
This script automatically saves the new refresh token back to the credentials
file, so you only need to paste the initial token once.
3. Install Dependencies
CODEBLOCK2
Or manually:
CODEBLOCK3
Quick Reference
Server Time
CODEBLOCK4
Accounts
CODEBLOCK5
Balances
CODEBLOCK6
Positions
CODEBLOCK7
Orders
CODEBLOCK8
Order Detail
CODEBLOCK9
Executions
CODEBLOCK10
Account Activities
CODEBLOCK11
Symbol Search
CODEBLOCK12
Note: Questrade market data endpoints use integer symbol IDs, not
ticker strings. Always run symbol-search first to get the correct ID
before calling quote or candles.
Symbol Info
CODEBLOCK13
Level 1 Quotes
CODEBLOCK14
Historical Candles (OHLCV)
CODEBLOCK15
Markets
CODEBLOCK16
Place Order (partner API access required)
CODEBLOCK17
Order Guardrails:
- 1. Live quote fetch — retrieves current bid/ask before submitting
- Price sanity check — warns if buy limit > ask or sell limit < bid
- Order summary — shows notional cost and requires
y/n confirmation - Use
--force to skip all prompts (automated use only)
Note: Order placement and cancellation require Questrade partner
API access. Personal API tokens are read-only (GET requests only).
Cancel Order
CODEBLOCK18
Workflow: Finding a Symbol and Getting a Quote
Because Questrade quotes use integer IDs rather than tickers, the typical
two-step flow is:
CODEBLOCK19
Script Location
All commands use: scripts/questrade_cli.py (relative to this skill directory)
API Reference
See references/api.md for full endpoint documentation and response schemas.
Known Limitations
- - Level 1 quotes and candles (
quote, candles) may return 403 out of allowed OAuth scopes if your Questrade account does not have a market data subscription or the token was not generated with market data scope enabled. Account data commands (accounts, balances, positions, orders, executions, activities) work with all personal tokens. - Activities endpoint has a maximum window of 30 days — always provide both
--start and --end. If --end is omitted it defaults to today.
Safety Notes
- - Personal API tokens cannot place trades. Questrade restricts order placement
and cancellation (
POST/
DELETE /accounts/{id}/orders) to partner-level API
access only. Attempting either with a personal token returns
403 Forbidden.
- - The
order and cancel-order commands are included for completeness but will
not work unless you have Questrade partner API access.
- - The refresh token rotates on every use; do not share the credentials file.
- Access tokens expire in ~30 minutes; the script caches and refreshes them automatically.
Questrade 技能
通过 Questrade REST API 与 Questrade 经纪账户及加拿大/美国市场数据进行交互。
设置
1. 激活 API 访问权限
登录 Questrade → 应用中心 → API 中心 → 激活 API。
生成一个手动授权令牌(这是您的初始刷新令牌,有效期为 7 天)。
2. 存储凭据
选项 A — 环境变量(推荐):
bash
export QUESTRADEREFRESHTOKEN=在此粘贴您的令牌
export QUESTRADE_PRACTICE=false # 模拟账户设为 true
export QUESTRADEREADONLY=true # 阻止下单/撤单设为 true
选项 B — 凭据文件(~/.openclaw/credentials/questrade.json):
json
{
refreshToken: 在此粘贴您的令牌,
practice: false,
readOnly: true
}
重要提示: Questrade 刷新令牌会轮换——每次使用令牌获取新的访问令牌时,Questrade 都会颁发一个新的刷新令牌。此脚本会自动将新的刷新令牌保存回凭据文件,因此您只需粘贴一次初始令牌。
3. 安装依赖项
bash
pip install -r requirements.txt
或手动安装:
bash
pip install requests
快速参考
服务器时间
bash
python3 scripts/questrade_cli.py time
账户
bash
python3 scripts/questrade_cli.py accounts
余额
bash
python3 scripts/questrade_cli.py balances 12345678
持仓
bash
python3 scripts/questrade_cli.py positions 12345678
订单
bash
未成交订单(默认)
python3 scripts/questrade_cli.py orders 12345678
指定日期范围内的所有订单
python3 scripts/questrade_cli.py orders 12345678 --state All --start 2026-01-01 --end 2026-03-01
已成交订单
python3 scripts/questrade_cli.py orders 12345678 --state Closed
订单详情
bash
python3 scripts/questrade_cli.py order-detail 12345678 987654321
成交记录
bash
python3 scripts/questrade_cli.py executions 12345678
python3 scripts/questrade_cli.py executions 12345678 --start 2026-01-01 --end 2026-02-28
账户活动
bash
最大 30 天窗口;省略 --end 则默认为今天
python3 scripts/questrade_cli.py activities 12345678 --start 2026-02-01
python3 scripts/questrade_cli.py activities 12345678 --start 2026-02-01 --end 2026-02-28
股票代码搜索
bash
按股票代码前缀搜索
python3 scripts/questrade_cli.py symbol-search SHOP
按公司名称搜索
python3 scripts/questrade_cli.py symbol-search 加拿大皇家银行
注意: Questrade 市场数据端点使用整数股票代码 ID,而非股票代码字符串。在调用 quote 或 candles 之前,请始终先运行 symbol-search 以获取正确的 ID。
股票代码信息
bash
股票代码的完整元数据(按 Questrade ID)
python3 scripts/questrade_cli.py symbol-info 8049
一级报价
bash
单个股票代码
python3 scripts/questrade_cli.py quote 8049
多个股票代码(逗号分隔的 ID)
python3 scripts/questrade_cli.py quote 8049,38738,45340
历史 K 线(OHLCV)
bash
过去一个月的日 K 线
python3 scripts/questrade_cli.py candles 8049 --start 2026-02-01
指定范围的时 K 线
python3 scripts/questrade_cli.py candles 8049 --start 2026-01-01 --end 2026-02-01 --interval OneHour
可用时间间隔:
OneMinute, TwoMinutes, ThreeMinutes, FourMinutes, FiveMinutes,
TenMinutes, FifteenMinutes, TwentyMinutes, HalfHour,
OneHour, TwoHours, FourHours, OneDay, OneWeek, OneMonth, OneYear
市场
bash
python3 scripts/questrade_cli.py markets
下单 (需要合作伙伴 API 访问权限)
bash
市价单
python3 scripts/questrade_cli.py order buy 12345678 8049 10 Market
限价单
python3 scripts/questrade_cli.py order buy 12345678 8049 10 Limit --limit-price 145.50
止损限价单
python3 scripts/questrade_cli.py order sell 12345678 8049 5 StopLimit --stop-price 140.00 --limit-price 139.50
跳过确认提示
python3 scripts/questrade_cli.py order buy 12345678 8049 10 Market --force
订单安全措施:
- 1. 实时报价获取 — 提交前检索当前买/卖价
- 价格合理性检查 — 如果买入限价 > 卖价或卖出限价 < 买价则发出警告
- 订单摘要 — 显示名义成本并要求 y/n 确认
- 使用 --force 跳过所有提示(仅限自动化使用)
注意: 下单和撤单需要 Questrade 合作伙伴 API 访问权限。个人 API 令牌为只读(仅限 GET 请求)。
撤单
bash
python3 scripts/questrade_cli.py cancel-order 12345678 987654321
工作流程:查找股票代码并获取报价
由于 Questrade 报价使用整数 ID 而非股票代码,典型的两步流程为:
bash
步骤 1:查找股票代码 ID
python3 scripts/questrade_cli.py symbol-search AAPL
步骤 2:使用输出中的 ID
python3 scripts/questrade_cli.py quote 8049
脚本位置
所有命令均使用:scripts/questrade_cli.py(相对于此技能目录)
API 参考
请参阅 references/api.md 获取完整的端点文档和响应模式。
已知限制
- - 一级报价和 K 线(quote、candles)如果您的 Questrade 账户没有市场数据订阅或令牌生成时未启用市场数据范围,可能会返回 403 out of allowed OAuth scopes。账户数据命令(accounts、balances、positions、orders、executions、activities)适用于所有个人令牌。
- 活动端点最大窗口为 30 天——请始终提供 --start 和 --end。如果省略 --end,则默认为今天。
安全提示
- - 个人 API 令牌无法进行交易。 Questrade 将下单和撤单(POST/DELETE /accounts/{id}/orders)限制为仅限合作伙伴级别的 API 访问。使用个人令牌尝试任一操作将返回 403 Forbidden。
- order 和 cancel-order 命令仅为完整性而包含,除非您拥有 Questrade 合作伙伴 API 访问权限,否则无法使用。
- 刷新令牌每次使用都会轮换;请不要共享凭据文件。
- 访问令牌约 30 分钟过期;脚本会自动缓存和刷新它们。