|
| 1 | +# Firstrade Platform |
| 2 | + |
| 3 | +Firstrade platform layer for QuantStrategyLab-style US equity runtimes. |
| 4 | + |
| 5 | +This repository wraps the unofficial `firstrade` Python package and exposes a |
| 6 | +QuantStrategyLab platform layer for shared `UsEquityStrategies` profiles. It |
| 7 | +provides QuantPlatformKit broker ports for quotes, OHLC history, portfolio |
| 8 | +snapshots, and guarded order submission. |
| 9 | + |
| 10 | +## Status |
| 11 | + |
| 12 | +- API source: unofficial, reverse-engineered `firstrade` package |
| 13 | +- Upstream package: https://pypi.org/project/firstrade/ |
| 14 | +- Upstream repository: https://github.com/MaxxRK/firstrade-api |
| 15 | +- Local default order mode: dry-run / preview only |
| 16 | +- Live order path: blocked unless both CLI confirmation and |
| 17 | + `FIRSTRADE_ENABLE_LIVE_TRADING=true` are set |
| 18 | +- Strategy domain: shared `us_equity` profiles from `UsEquityStrategies` |
| 19 | + |
| 20 | +This project is not affiliated with, endorsed by, or supported by Firstrade |
| 21 | +Securities Inc. The upstream API can break without notice when Firstrade |
| 22 | +changes its web/mobile backend. |
| 23 | + |
| 24 | +## Why This Exists |
| 25 | + |
| 26 | +Firstrade does not expose the same kind of official retail trading API as |
| 27 | +brokers such as IBKR or LongPort. The integration here is therefore treated as |
| 28 | +an experimental platform layer, not a production-grade broker connector. |
| 29 | + |
| 30 | +Use it first for: |
| 31 | + |
| 32 | +- login/MFA validation |
| 33 | +- account and position reads |
| 34 | +- quote/OHLC checks |
| 35 | +- dry-run order preview |
| 36 | +- very small, explicitly approved live validation only after manual review |
| 37 | + |
| 38 | +## Strategy Runtime Boundary |
| 39 | + |
| 40 | +This platform is intended to mirror the role of `InteractiveBrokersPlatform`, |
| 41 | +`CharlesSchwabPlatform`, and `LongBridgePlatform`: strategy logic stays in |
| 42 | +`UsEquityStrategies`, while this repository owns Firstrade authentication, |
| 43 | +account reads, market data reads, order translation, runtime safety controls, |
| 44 | +and deployment wiring. |
| 45 | + |
| 46 | +Firstrade is not yet a first-class `platform_id` inside the pinned |
| 47 | +`UsEquityStrategies` version. Until that support lands upstream, this |
| 48 | +repository reports runtime identity as `firstrade` while loading the same |
| 49 | +value-native strategy adapter shape used by LongBridge/Schwab. The default |
| 50 | +source is: |
| 51 | + |
| 52 | +```bash |
| 53 | +FIRSTRADE_STRATEGY_ADAPTER_SOURCE_PLATFORM=longbridge |
| 54 | +``` |
| 55 | + |
| 56 | +Print the current Firstrade strategy matrix: |
| 57 | + |
| 58 | +```bash |
| 59 | +.venv/bin/python scripts/print_strategy_profile_status.py |
| 60 | +``` |
| 61 | + |
| 62 | +The long-term target is first-class `firstrade` coverage in |
| 63 | +`UsEquityStrategies` so this bridge can be removed. |
| 64 | + |
| 65 | +## Environment |
| 66 | + |
| 67 | +Copy `.env.example` into your secret manager or shell environment. Do not |
| 68 | +commit credentials. |
| 69 | + |
| 70 | +| Variable | Required | Description | |
| 71 | +| --- | --- | --- | |
| 72 | +| `FIRSTRADE_USERNAME` | Yes | Firstrade login username | |
| 73 | +| `FIRSTRADE_PASSWORD` | Yes | Firstrade login password | |
| 74 | +| `FIRSTRADE_MFA_SECRET` | Optional | TOTP secret for unattended MFA | |
| 75 | +| `FIRSTRADE_PIN` | Optional | PIN flow supported by upstream package | |
| 76 | +| `FIRSTRADE_MFA_EMAIL` | Optional | Email OTP recipient selector | |
| 77 | +| `FIRSTRADE_MFA_PHONE` | Optional | SMS OTP recipient selector | |
| 78 | +| `FIRSTRADE_MFA_CODE` | Optional | One-time OTP code for the current validation run | |
| 79 | +| `FIRSTRADE_ACCOUNT` | Optional | Required when multiple accounts are returned | |
| 80 | +| `STRATEGY_PROFILE` | Yes for runtime | Shared US equity strategy profile | |
| 81 | +| `FIRSTRADE_DRY_RUN_ONLY` | Optional | Defaults to `true` for platform runtime | |
| 82 | +| `FIRSTRADE_STRATEGY_ADAPTER_SOURCE_PLATFORM` | Optional | `longbridge` default; `schwab` also allowed | |
| 83 | +| `ACCOUNT_PREFIX` | Optional | Alert/log prefix, default `FIRSTRADE` | |
| 84 | +| `ACCOUNT_REGION` | Optional | Runtime account scope, default `US` | |
| 85 | +| `FIRSTRADE_COOKIE_DIR` | Optional | Cookie cache directory, default `.runtime/firstrade-cookies` | |
| 86 | +| `FIRSTRADE_ENABLE_LIVE_TRADING` | Optional | Must be `true` before any live order can be submitted | |
| 87 | +| `FIRSTRADE_RUN_SMOKE_ON_HTTP` | Optional | Must be `true` before `/smoke` performs a real login/quote | |
| 88 | + |
| 89 | +## Local Validation |
| 90 | + |
| 91 | +Install dependencies in a venv: |
| 92 | + |
| 93 | +```bash |
| 94 | +python3 -m venv .venv |
| 95 | +.venv/bin/python -m pip install -r requirements.txt |
| 96 | +``` |
| 97 | + |
| 98 | +Quote-only smoke check: |
| 99 | + |
| 100 | +```bash |
| 101 | +.venv/bin/python scripts/firstrade_smoke_check.py --quote-only --symbol SPY |
| 102 | +``` |
| 103 | + |
| 104 | +Dry-run order preview for a tiny notional buy: |
| 105 | + |
| 106 | +```bash |
| 107 | +.venv/bin/python scripts/firstrade_smoke_check.py \ |
| 108 | + --preview-order \ |
| 109 | + --symbol YOUR_SYMBOL \ |
| 110 | + --side buy \ |
| 111 | + --notional-usd 5 \ |
| 112 | + --max-notional-usd 25 |
| 113 | +``` |
| 114 | + |
| 115 | +Live order validation requires all of the following: |
| 116 | + |
| 117 | +- `FIRSTRADE_ENABLE_LIVE_TRADING=true` |
| 118 | +- `--live-order` |
| 119 | +- `--yes-i-understand-unofficial-api-risk` |
| 120 | +- order notional at or below `--max-notional-usd` |
| 121 | + |
| 122 | +Example shape: |
| 123 | + |
| 124 | +```bash |
| 125 | +FIRSTRADE_ENABLE_LIVE_TRADING=true \ |
| 126 | +.venv/bin/python scripts/firstrade_smoke_check.py \ |
| 127 | + --live-order \ |
| 128 | + --symbol YOUR_SYMBOL \ |
| 129 | + --side buy \ |
| 130 | + --notional-usd 5 \ |
| 131 | + --max-notional-usd 25 \ |
| 132 | + --yes-i-understand-unofficial-api-risk |
| 133 | +``` |
| 134 | + |
| 135 | +The example does not recommend any security. Choose the validation symbol |
| 136 | +yourself and confirm Firstrade account permissions, fractional trading |
| 137 | +agreement status, market session, and order preview before live use. |
| 138 | + |
| 139 | +## Cloud Run Shape |
| 140 | + |
| 141 | +`main.py` exposes: |
| 142 | + |
| 143 | +- `/` health metadata only |
| 144 | +- `/precheck` health metadata only |
| 145 | +- `/probe` health metadata only |
| 146 | +- `/profiles` shared US equity strategy matrix |
| 147 | +- `/smoke` login + quote only when `FIRSTRADE_RUN_SMOKE_ON_HTTP=true` |
| 148 | + |
| 149 | +The HTTP entrypoint does not place orders. |
| 150 | + |
| 151 | +## License And Upstream Compliance |
| 152 | + |
| 153 | +This repository is MIT licensed. The upstream `firstrade` package is also MIT |
| 154 | +licensed. Keep `NOTICE.md` and upstream attribution when distributing this |
| 155 | +project or derivative work. |
| 156 | + |
| 157 | +Users are responsible for reviewing Firstrade account agreements, platform |
| 158 | +terms, applicable law, and the upstream open-source license before using this |
| 159 | +integration. |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +## 中文说明 |
| 164 | + |
| 165 | +这是一个 QuantStrategyLab 风格的 Firstrade 平台层仓库。它接入的是 |
| 166 | +`firstrade` 这个非官方、逆向工程 Python 包,不是 Firstrade 官方 API。 |
| 167 | + |
| 168 | +当前目标是对齐 `InteractiveBrokersPlatform`、`CharlesSchwabPlatform` 和 |
| 169 | +`LongBridgePlatform`:策略逻辑放在 `UsEquityStrategies`,这个仓库只负责 |
| 170 | +Firstrade 登录、账户/行情读取、下单转换、安全闸和部署 wiring。 |
| 171 | + |
| 172 | +当前定位是小规模验证到通用美股平台层的过渡: |
| 173 | + |
| 174 | +- 登录和 MFA 验证 |
| 175 | +- 账户、持仓、行情、OHLC 读取 |
| 176 | +- dry-run / preview 下单验证 |
| 177 | +- 在你再次确认后,才允许极小金额实盘验证 |
| 178 | +- 通用 `us_equity` 策略 profile 的平台层接入 |
| 179 | + |
| 180 | +默认所有订单都是 preview。实盘必须同时满足: |
| 181 | + |
| 182 | +- 设置 `FIRSTRADE_ENABLE_LIVE_TRADING=true` |
| 183 | +- CLI 使用 `--live-order` |
| 184 | +- CLI 使用 `--yes-i-understand-unofficial-api-risk` |
| 185 | +- 金额不超过 `--max-notional-usd` |
| 186 | + |
| 187 | +请不要把 Firstrade 登录凭据、MFA secret、cookie 文件提交到 Git。`.env`、 |
| 188 | +`.runtime/` 和 `ft_cookies*.json` 已经在 `.gitignore` 中。 |
| 189 | + |
| 190 | +开源协议方面:本仓库使用 MIT;上游 `firstrade` 包也是 MIT。发布或二次分发 |
| 191 | +时保留 `NOTICE.md` 和上游项目信息。 |
| 192 | + |
| 193 | +注意:当前 `UsEquityStrategies` 尚未内置 `firstrade` 平台 adapter。本仓库 |
| 194 | +临时复用 LongBridge/Schwab 的 value-native 策略输入形状,并在运行报告中保留 |
| 195 | +Firstrade 平台身份。后续应在 `UsEquityStrategies` 里补齐 first-class |
| 196 | +`firstrade` 兼容矩阵。 |
0 commit comments