algo-expert
Installation
SKILL.md
OpenAlgo Execution Expert
Knowledge base for building production-grade algorithmic trading strategies on OpenAlgo. Every strategy is a single Python file that toggles between backtest mode (VectorBT) and live execution mode (OpenAlgo SDK + WebSocket) via one CLI flag (--mode backtest|live) or env var (MODE=...).
Strategies are also upload-ready for OpenAlgo's self-hosted /python strategy host.
Core principles
- One file, two modes. The same
signals(df)function feeds both VectorBT (backtest) and the live event loop. Risk thresholds and cost assumptions are honored on both sides. - OpenAlgo for everything broker-side. Data via
client.history()and WebSocket. Orders viaclient.placeorder()/placesmartorder()/optionsmultiorder(). Live vs sandbox is decided in OpenAlgo's UI analyzer toggle - the strategy code never knows. - Indicator library is user's choice -
openalgo.ta(default) ortalib. Specialty indicators (Supertrend, Donchian, Ichimoku, HMA, KAMA) always come from openalgo. Seerules/indicator-libraries.md. - Three execution types -
eoc(end-of-candle MARKET),limit(real-time pegged LIMIT),stop(broker-side SL-M trigger). User picks at strategy creation. Seerules/execution-types.md. - Real-world costs and slippage baked into every backtest (matches
vectorbt-backtesting-skills4-segment Indian model). Seerules/transaction-costs.mdandrules/slippage-handling.md. - Self-hosted
/pythoncompatible - every strategy reads env vars in the canonical priority, traps SIGTERM, logs to stdout. Seerules/self-hosted-strategies.md.
When to read which rule
| Reading the user wants... | Load these rule files |
|---|---|
| The big-picture strategy template | unified-strategy-pattern.md, mode-toggle.md, execution-types.md |
| Indicator selection | indicator-libraries.md |
| Position sizing (the most important fix) | position-sizing.md |
| Preflight checks at startup | preflight-checks.md |
| Risk on a single position | risk-management.md |
| Portfolio-level risk and daily caps | portfolio-risk.md |
| Cost / slippage modeling | transaction-costs.md, slippage-handling.md |
| Data sources (DuckDB, Historify, API) | duckdb-data.md |
| WebSocket and bar-close patterns | websocket-feeds.md, event-loop.md |
| Order placement idioms | execution-patterns.md, order-constants.md |
| Options strategies | options-execution.md |
| Volatility strategies | volatility-strategies.md |
| ML strategies | ml-strategies.md |
| Persistent state between restarts | state-persistence.md |
| Logging and Telegram alerts | logging-and-alerts.md |
| Common production mistakes | pitfalls.md |
| Strategy catalog / template selection | strategy-catalog.md |
OpenAlgo /python self-hosting |
self-hosted-strategies.md |
| Symbol formats and lot sizes | symbol-format.md, lot-sizes.md, order-constants.md |
| Full SDK reference | sdk-reference.md |
Production patterns (lifted from OpenAlgo examples)
- Two-thread live model: signal poll thread + WS callback thread (from
examples/python/emacrossover_strategy_python.py). The WS callback NEVER places orders directly - it spawns a worker thread. - Bar-close logic uses
iloc[-2]notiloc[-1]- the last bar inclient.history()is forming and would cause repaint. - Risk exits use
client.placesmartorder(position_size=0)to flatten cleanly (fromexamples/python/stoploss_target_example.py). - Multi-leg options entry via
client.optionsmultiorder()- BUY legs go first for margin efficiency. Per-leg SL viaclient.placeorder(price_type="SL")(fromexamples/python/straddle_with_stops.py). - Time-based entries via
apscheduler.schedulers.background.BackgroundSchedulerwith IST cron (fromexamples/python/straddle_scheduler.py). - Always fetch spot quote before any options order -
client.quotes()first, thenclient.optionsorder()withoffset="ATM".
Anti-patterns (always avoid)
asyncio- the OpenAlgo SDK is synchronous; usethreadinginsteaddf.iloc[-1]on live data - that's the forming bar; useiloc[-2]- Calling
client.history(start_date=end_date)- returns 1 candle; always use multi-day lookback - Placing exit orders directly inside the WS callback - spawn a worker thread
- Hardcoding
exchange="NSE"when self-hosted - readOPENALGO_STRATEGY_EXCHANGEenv var instead - Backtests with
fees=0andslippage=0- the result is fantasy; use the segment-appropriate constants - Polling
client.get_ltp()faster than 0.5s - use WS callbacks for real-time
Reference docs (in OpenAlgo repo)
- SDK:
D:/openalgo-python/openalgo/docs/prompt/openalgo python sdk.md - Services:
D:/openalgo-python/openalgo/docs/prompt/services_documentation.md - WebSocket protocol:
D:/openalgo-python/openalgo/docs/prompt/websockets-format.md - Self-hosted /python:
D:/openalgo-python/openalgo/strategies/README.md - Production examples:
D:/openalgo-python/openalgo/examples/python/