openalgo-backtrader
Backtrader integration for OpenAlgo (India) - stores, feeds, and brokers.
Installation
uv pip install openalgo-backtrader
uv pip install backtrader # Required to run Backtrader strategies
Usage
Import the package in your code as follows:
from openalgo_bt.stores.oa import OAStore
from openalgo_bt.feeds.oa import OAData
Local five-minute simulation
The examples are repository-only and are not installed by the wheel or included in the source distribution. Clone the repository before running these commands:
git clone https://github.com/p2c2e/openalgo-backtrader.git
cd openalgo-backtrader
Try the self-contained SMA crossover example before adding custom strategy helpers:
uv sync --extra dev
uv run python examples/sma_crossover_5m.py
uv run python examples/sma_crossover_5m.py --broker oa-sim
Both modes keep orders local. The default uses synthetic data and ordinary
Backtrader next-bar-open fills; oa-sim tests OABroker's immediate current-close
fills, not realistic execution. Read the example guide
for optional read-only OpenAlgo history, JSON reports and limitations.
Current modernization plan
This repo is being refreshed because the OpenAlgo API and broker integrations may have evolved since the original Backtrader integration was written.
Tracking documents:
docs/updatation-plan.md- overall plan, phases, status, and release milestonesdocs/compatibility.md- compatibility matrix for OpenAlgo, Backtrader, Python, intervals, websocket, and broker featuresdocs/task-log.md- running log of intermediate tasks and findings
Status is tracked with [todo], [doing], [done], [deferred], and [blocked] markers.
Project Structure
openalgo_bt/- Main packagestores/oa.py- OAStore for OpenAlgo APIfeeds/oa.py- OAData for Backtrader feedsbrokers/oabroker.py- OABroker for Backtrader brokers
Sample Store/Broker Creation
This development release is not validated for production live trading. Offline broker tests do not verify a deployed OpenAlgo order contract, and websocket behavior remains experimental.
For local integration simulation with an existing cerebro instance and exported
OpenAlgo configuration:
from openalgo_bt.stores.oa import OAStore
store = OAStore()
broker = store.getbroker(
product="MIS",
strategy="Local Integration Simulation",
mode="backtest",
simulate_fills=True,
use_funds=False,
)
cerebro.setbroker(broker)
Simulation fills immediately at the current close; it is not a realistic execution model. Store construction still requires an API key. Use the self-contained synthetic example above when no OpenAlgo configuration is available.
Non-simulated backtest orders are rejected. Paper routing requires mode="paper"
and an OpenAlgo analyzer response identifying an analyze/paper target with
analyze_mode enabled. Live routing additionally requires mode="live",
allow_live=True, OPENALGO_BT_ALLOW_LIVE=1, and a matching live analyzer state.
These checks are safeguards, not evidence of live-trading readiness. Process
restart recovery and atomic account reconciliation are unsupported.
The following live-data configuration is illustrative only; verify websocket authentication and subscriptions against your intended deployment before use.
data = OAData(
symbol=symbol,
interval="1m", # Explicit OpenAlgo interval to bypass timeframe/compression mapping
compression=1, # 1-minute bars
fromdate=datetime.now() - timedelta(days=1), # Some historical data for warmup
live=True, # Enable live streaming
ws_url=ws_url,
ws_mode=2, # Quote mode
)
cerebro.adddata(data, name=symbol)
Using higher compression values (intraday aggregation)
When your backend only provides 1-minute data, OAData can now deliver higher intraday compressions by resampling locally:
- Historical:
- If you request Minutes/Hours with compression>1 and the OpenAlgo backend does not support that interval, OAData fetches 1m candles and aggregates them into N-minute buckets.
- Live:
- Websocket ticks are aggregated into the requested bucket size on the fly.
Notes:
- Bucket alignment is done in Asia/Kolkata time by flooring to the nearest multiple of the bucket size.
- Volume comes from per-tick last_quantity when available, or from deltas of cumulative session volume otherwise.
- If OpenAlgo supports the requested interval natively (e.g. 5m), OAData will use it. If not, it will fallback to 1m + local aggregation transparently.
Examples:
# Historical 5-minute bars (local 1m->5m if backend interval unsupported)
import backtrader as bt
from datetime import datetime, timedelta
from openalgo_bt.feeds.oa import OAData
TF_MINUTES = getattr(getattr(bt, "TimeFrame", object), "Minutes", 1)
data = OAData(
symbol="NSE:RELIANCE",
timeframe=TF_MINUTES,
compression=5,
fromdate=datetime.now() - timedelta(days=5),
todate=datetime.now(),
)
# Live 15-minute bars (aggregated locally from 1m ticks)
import os
import backtrader as bt
from datetime import datetime
from openalgo_bt.feeds.oa import OAData
TF_MINUTES = getattr(getattr(bt, "TimeFrame", object), "Minutes", 1)
data = OAData(
symbol="NSE:RELIANCE",
timeframe=TF_MINUTES,
compression=15,
fromdate=datetime(2025, 8, 8), # optional historical warmup
live=True,
ws_url=os.getenv("WEBSOCKET_URL"),
ws_mode=2, # Quote mode
)
Tip: Some editors' type stubs don't expose bt.TimeFrame.Minutes; use: TF_MINUTES = getattr(getattr(bt, "TimeFrame", object), "Minutes", 1)
Requirements
- Python 3.12+
- python-dotenv
- backtrader (user must install)
- openalgo (installed automatically)
Development test command:
uv run pytest
License
See LICENSE.
Metadata
Release files for openalgo-backtrader 0.1.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| openalgo_backtrader-0.1.12.tar.gz | 67.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| openalgo_backtrader-0.1.12-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 109.4 kB
Release files / openalgo_backtrader-0.1.12.tar.gz
| Download URL | openalgo_backtrader-0.1.12.tar.gz |
|---|---|
| Size | 67.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c348e293ebf536090fe30d6a22faaa4c597bda7c011f10925e87e03fda2f8e2c
|
|
BLAKE2b-256 checksum How to use checksums |
685f6716e30a7c31f383e70ccd2a84681ac10d5ccfa6bda63f524df7ef836b3b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.12
|
Release files / openalgo_backtrader-0.1.12-py3-none-any.whl
| Download URL | openalgo_backtrader-0.1.12-py3-none-any.whl |
|---|---|
| Size | 42.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
db8ce52f7404c9ed065daf40ba379cb2087511c7c5beeae2bb952cc579a7c8c7
|
|
BLAKE2b-256 checksum How to use checksums |
55f746465a4a01d0b18430bab52ef7b46714bab0ab522a9d266ef283356ac898
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.12
|
Security for openalgo-backtrader 0.1.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.