← Back to PRs

#20927: feat(trading): add /buy and /sell order commands

by Sjeror11 open 2026-02-19 13:09 View on GitHub →
size: XL
## Summary - Add `OrderSide`, `OrderType`, `OrderRequest`, `OrderResult` types and extend `BrokerProvider` with `placeOrder()` - Implement `placeOrder` in `AlpacaProvider` via `POST /v2/orders` (paper trading by default) - Implement mock `placeOrder` returning `status: "accepted"` with a fake ID - Add `/buy` and `/sell` auto-reply commands with market and limit order support - Add `trading_order` agent tool (TypeBox schema) for LLM-driven order placement - Add `trading buy` / `trading sell` CLI subcommands with `--limit` flag ## Usage ``` /buy AAPL 10 → market buy 10 shares /buy AAPL 10 limit 150 → limit buy @ $150 /sell AAPL 5 → market sell 5 shares /sell AAPL 5 limit 180 → limit sell @ $180 openclaw trading buy AAPL 10 openclaw trading sell AAPL 5 --limit 180 ``` ## Test plan - [ ] Mock provider: `/buy AAPL 10` → confirmation with fake ID - [ ] Mock provider: `/sell AAPL 5 limit 180` → limit order confirmation - [ ] Mock provider: `/buy` (no args) → usage hint - [ ] CLI: `openclaw trading buy AAPL 10` → JSON output - [ ] TypeScript: `pnpm tsc --noEmit` passes 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- greptile_comment --> <h3>Greptile Summary</h3> Added trading plugin with `/buy` and `/sell` order commands via Alpaca Markets API. Implemented order placement with market and limit order types, plus portfolio tracking and price quotes. Added CLI subcommands (`openclaw trading buy/sell`) and agent tools (`trading_order`, `trading_portfolio`, `trading_price`). - Uses `Type.Union` in tool schemas which violates AGENTS.md:184 guardrails (produces `anyOf` that some providers reject) — must use `stringEnum` helper instead - AGENTS.md documentation incomplete — missing `/buy`, `/sell` commands and `trading_order` tool in Registered Components table - Clean provider pattern with mock and Alpaca implementations - Proper lazy initialization and error handling throughout - Warning messages for irreversible sell orders in live mode <h3>Confidence Score: 3/5</h3> - Safe to merge with fixes — schema issue will cause tool failures on some LLM providers - Score reflects one critical syntax issue (`Type.Union` in tool schemas violates established guardrails and will break on providers like Vertex AI) and incomplete documentation. The implementation is otherwise solid with proper validation, error handling, and safety warnings for live trading. The schema fix is straightforward (swap `Type.Union` for `stringEnum`), and docs need updating to match implemented features. - extensions/trading/index.ts requires schema fix, AGENTS.md needs complete feature documentation <sub>Last reviewed commit: 04de311</sub> <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> **Context used:** - Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8)) - Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13)) <!-- /greptile_comment -->

Most Similar PRs