r/alpacamarkets • u/alpacahq • 6h ago
User Story: Building a trading bot with duplicate-order guardrails on Alpaca's Trading API
Hi everyone,
You can use AI coding assistants to build a trading bot without a computer science background, and with a duplicate-order guard, you can stop agent retries from placing the same order twice.
Anthony Zender, an Alpaca community member, is an accountant who built SafeAgent, an open-source duplicate-order guard and a momentum trading bot using Alpaca's Trading API. Alpaca provides the bot's market data, order submission, order status, and position records, and the bot treats Alpaca's account state as authoritative.
In his new Trader to Trader article, he walks through how he built it:
✅ Testing code paths, order handling, and state transitions in Alpaca's paper environment
✅ Claiming a stable identity for each action before it runs, so help prevent retries from submitting duplicate orders.
✅ Adding exits, cooldowns, a regime filter, and daily limits around a simple entry signal
✅ Building a reconciliation step that checks Alpaca's orders and positions before any new action is taken
Anthony is clear about the limits: blocking a duplicate is not the same as recovering, and when an exit was rejected, his bot stayed blocked until he stepped in manually.
Read the full article: https://alpaca.markets/learn/eighteen-years-later-building-a-trading-bot-with-safety-guardrails-on-alpaca
Are you building automated or agentic workflows with Alpaca? We'd love to hear about it! Share what you're working on below.
- SafeAgent on PyPI
- SafeAgent in Model Context Protocol Registry
- How to Start Paper Trading Alpaca's Trading API
- Paper Trading vs Live Trading
- From Z-Score to a Multi-agent Trading Committee
- Building My Own Quant Trading Agent with Code and AI
SafeAgent is an independent, third-party educational project and is not endorsed or warranted by Alpaca. AI-generated code may contain errors, and automated trading safeguards do not eliminate trading or operational risks. Paper trading is simulated, may not reflect actual market conditions, and is not indicative of future performance. This content is for educational purposes only and is not investment advice. All investments involve risk. See Alpaca's Disclosure Library.
