Python Strategy Host
Relevant source files
The following files were used as context for generating this wiki page:
Python Strategy Host is a specialized trading surface within OpenAlgo (accessible via /python) that allows traders to design, host, and execute automated trading strategies directly inside the platform. It eliminates the need for external servers, Docker containers, or manual cron management by providing a self-hosted environment where Python scripts run as isolated subprocesses managed through a centralized dashboard.
The system provides a full-stack environment including an in-browser code editor, automated IST-based scheduling, real-time log streaming, and process isolation. It utilizes the OpenAlgo Python SDK to interact with the platform's unified broker API, enabling execution across 34 supported broker plugins.
Sources: README.md:31-35, CLAUDE.md:14-16, frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:325-330
Architecture and Process Isolation
The Python Strategy Host is designed for high reliability and security through strict process isolation. Each uploaded strategy is executed as a separate subprocess.Popen instance. This architectural choice ensures that a crash, memory leak, or logic error in one strategy cannot affect other running strategies or the core OpenAlgo host server.
Each strategy runs with its own PID, memory, and file descriptors. All print() output is captured and redirected into timestamped log files stored in log/strategies/.
Sources: frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:361-365, strategies/README.md:10-12, CLAUDE.md:126-128
Environment Variable Injection
The host manages strategy configuration by injecting a set of environment variables into the subprocess. This allows scripts to remain portable while receiving platform-specific details like API keys and connection strings at runtime.
Platform Injected Variables
These variables are set specifically for each strategy instance by the /python runner.
| Variable | Description |
|---|---|
OPENALGO_API_KEY | The user's decrypted API key retrieved from the database. |
STRATEGY_ID | A unique identifier for the specific strategy instance. |
STRATEGY_NAME | The descriptive name assigned during upload. |
OPENALGO_STRATEGY_EXCHANGE | The exchange selected in the configuration (e.g., NSE, MCX, CRYPTO). |
OPENALGO_HOST | Fallback alias for the REST host (default: http://127.0.0.1:5000). |
Inherited Variables
Strategies inherit all variables from the main OpenAlgo .env file via os.environ.copy().
| Variable | Description |
|---|---|
HOST_SERVER | Canonical REST host URL. |
WEBSOCKET_URL | Full WebSocket URL for market data feeds. |
WEBSOCKET_HOST | Host component for ZeroMQ/WS connections. |
WEBSOCKET_PORT | Port component for ZeroMQ/WS connections. |
Sources: frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:367-372, frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:400-435, strategies/README.md:92-120
Exchange-Aware Scheduling
The scheduling system is "Exchange-Aware," meaning the execution window is determined by the intersection of the user's defined schedule and the specific exchange's market calendar.
Key Scheduling Rules
- Intersection Rule: If a strategy is scheduled for 09:15–23:55 but the exchange (e.g., MCX) only has an evening session (17:00–23:55) due to a partial holiday, the strategy will only run during the 17:00–23:55 window.
- Holiday Protection: The host automatically pauses strategies on holidays identified in the database.
- Special Sessions: Rows in the
SPECIAL_SESSIONcalendar (e.g., Muhurat trading) override weekend/holiday rejects. - Crypto: Strategies tagged as
CRYPTOignore all holiday and weekend logic, running 24/7 if scheduled.
Sources: frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:485-500, strategies/README.md:52-58, strategies/README.md:144-150
Resource Management and Constraints
To prevent resource exhaustion, the platform applies limits to individual strategy subprocesses (primarily enforced on Linux/macOS).
Resource Limits
| Resource | Default Limit | Configuration Variable |
|---|---|---|
| Virtual Memory | 1024 MB | STRATEGY_MEMORY_LIMIT_MB |
| CPU Time | 3600 seconds | Hardcoded |
| Open File Descriptors | 256 | Hardcoded |
| Log File Count | 10 files | STRATEGY_LOG_MAX_FILES |
| Log Size | 50 MB | STRATEGY_LOG_MAX_SIZE_MB |
| Log Retention | 7 days | STRATEGY_LOG_RETENTION_DAYS |
Sources: frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:612-635, strategies/README.md:195-200
Strategy Lifecycle and Statuses
The dashboard tracks strategies through various states, reflecting their operational readiness and runtime health.
| Status | Description |
|---|---|
| Running | Actively executing trades; subprocess is alive. |
| Scheduled | Armed and waiting for the next scheduled start time. |
| Manual Stop | User explicitly stopped the process; will not auto-start. |
| Paused | Halted by the host due to a market holiday or weekend. |
| Error | The script encountered an unhandled exception or was killed by the OS. |
Sources: frontend/src/pages/python-strategy/PythonStrategyIndex.tsx:470-510, frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:555-575
Development Workflow
SDK Integration
Strategies are recommended to use the openalgo Python SDK. The canonical initialization pattern leverages platform-injected environment variables:
import os
from openalgo import api
# Recommended Pattern
API_KEY = os.getenv("OPENALGO_API_KEY")
API_HOST = os.getenv("HOST_SERVER", "http://127.0.0.1:5000")
WS_URL = os.getenv("WEBSOCKET_URL", "ws://127.0.0.1:8765")
client = api(api_key=API_KEY, host=API_HOST, ws_url=WS_URL)
Sources: frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:35-50, strategies/README.md:73-86
Directory Structure
strategies/scripts/: Contains the uploaded.pyfiles.strategies/configs.json: Atomic store for strategy metadata and schedules.log/strategies/: Directory for rotating per-strategy log files.
Sources: frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:696-705, strategies/README.md:126-132
The Python Strategy Host provides a robust, isolated environment for automated trading, ensuring that custom logic remains decoupled from the core platform while benefiting from integrated scheduling and market calendar synchronization.