Skip to content

examples: add LBR Anti-Setup algo trading bot for Magnificent 7#665

Open
idsts2670 wants to merge 4 commits intoalpacahq:masterfrom
idsts2670:examples/update
Open

examples: add LBR Anti-Setup algo trading bot for Magnificent 7#665
idsts2670 wants to merge 4 commits intoalpacahq:masterfrom
idsts2670:examples/update

Conversation

@idsts2670
Copy link
Copy Markdown
Contributor

Summary

  • Add examples/stocks/lbr-anti-setup-trading-bot/ — a production-ready implementation of Linda Raschke's LBR 3/10 Anti-Setup strategy using AWS Lambda and Alpaca's Trading API
  • Fix examples/stocks/README.md with correct relative paths and consistent table headers

Why

Adds an advanced, serverless algorithmic trading example for Alpaca API users who want to learn how to build and deploy a real trading bot using cloud infrastructure.

What's Included

  • Trading strategy: LBR 3/10 Anti-Setup on Magnificent 7 (AAPL, MSFT, NVDA, GOOGL, AMZN, META, TSLA) with MACD/ADX/EMA/ATR signal logic (6-rule filter)
  • Serverless deployment: AWS Lambda + EventBridge (runs 9:30 AM ET on weekdays)
  • Risk management: ATR-based trailing stops, equal-weight rebalancing
  • State persistence: DynamoDB portfolio tracking
  • Secure secrets: AWS SSM Parameter Store for API keys
  • Educational notebook: notebooks/ibr_algorithm_walkthrough.ipynb — step-by-step walkthrough with live Alpaca data
  • Tests: pytest suite (17 tests)
  • Pre-deployment docs: docs/PRE-DEPLOYMENT.md pre-flight checklist

Test plan

  • Verify notebook runs in Google Colab with Alpaca paper keys
  • Verify uv run pytest tests/ -v passes (17 tests)
  • Verify Colab and local README links resolve correctly

Made with Cursor

Adds a production-ready implementation of Linda Raschke's LBR 3/10
Anti-Setup strategy for the Magnificent 7 stocks using AWS Lambda and
Alpaca's Trading API, including:
- Serverless deployment via AWS Lambda + EventBridge (9:30 AM ET daily)
- MACD/ADX/EMA/ATR signal logic with 6-rule filter
- ATR-based trailing stops and equal-weight rebalancing
- DynamoDB portfolio state persistence
- Educational Jupyter notebook walkthrough
- pytest test suite (17 tests)
- Pre-deployment checklist docs

Update examples/stocks/README.md with correct relative paths and
consistent table headers.

Made-with: Cursor
Prevent repo-level pytest from collecting tests inside standalone
example projects under examples/.

Made-with: Cursor
## Architecture & Flow Improvements

- **Idempotency gate**: Add dedup check with DynamoDB atomic conditional put to prevent duplicate CloudWatch invocations from creating double orders
- **Buying power validation**: Insert decision gate after account snapshot to validate sufficient funds before order submission
- **Rebalance synchronization**: Replace fixed sleep delays with explicit polling confirmation; ensure sells complete before buys submit, both flow through WAIT_SELL/WAIT_BUY gates
- **Exit order accuracy**: Replace ASK price estimation with actual fill prices (GET_FILLS); compute stop levels and trail percentages from avg_fill_price and filled_qty; align TimeInForce (DAY) across entry and exit to prevent orphaned GTC orders
- **Cancel order verification**: Add verification step to detect partial fills; refresh account snapshot (RESNAPSHOT) when fills detected before proceeding to rebalance
- **Comprehensive error handling**: Add retry/backoff loops with circuit breaker patterns for all external API calls (get_clock, get_price_history, get_account, get_all_positions, get_orders, cancel_order, MarketOrderRequest, _store_portfolio); include timeout specs, max retry counts, and alerting thresholds; log clear warnings on all failure paths
- **DST handling**: Replace CloudWatch Events with EventBridge Scheduler using timezone-aware cron (America/New_York) to ensure bot runs at 9:30 AM ET year-round, not affected by daylight saving transitions

## Code Hardening

- **Defensive quote access**: Update _get_ask_price() to use .get() for nested dict access instead of direct bracket; handle missing askPrice keys and invalid values with logging; return None safely for all error cases
- **Quote validation in stop setup**: Add defensive checks before accessing entry_quotes in stop-loss calculation; log and skip symbol if quote unavailable or askPrice missing
- **Fix pytz version constraint**: Update from invalid future version 2026.1.post1 to valid range >=2025.1,<2027
- **Fix notebook title**: Correct README reference from IBR to LBR (Linda Bradford Raschke strategy)

All changes verified against current code; callers already safely handle None returns and graceful degradation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant