Skip to main content

Backtrader integration for OpenAlgo (India) - stores, feeds, and brokers

Project description

openalgo-backtrader

Backtrader integration for OpenAlgo (India) - stores, feeds, and brokers.

Installation

uv pip install openalgo-backtrader

Usage

Import the package in your code as follows:

from openalgo_bt.stores.oa import OAStore
from openalgo_bt.feeds.oa import OAData

Project Structure

  • openalgo_bt/ - Main package
    • stores/oa.py - OAStore for OpenAlgo API
    • feeds/oa.py - OAData for Backtrader feeds
    • brokers/oabroker.py - OABroker for Backtrader brokers

Sample Store/Broker Creation

Here is how to setup a Store/Broker (example tested with Zerodha backend at OpenAlgo). Both blocks are for "LIVE" setup

from openalgo_bt.stores.oa import OAStore
store = OAStore()
broker = store.getbroker(
    product="MIS", 
    strategy="Live Consistent Trend Bracket", 
    debug=True,
    simulate_fills=False,  # Use real broker for live trading
    use_funds=False        # Use local cash management
)
cerebro.setbroker(broker)

And to setup a Data (or multiple for that matter)

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

License

See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openalgo_backtrader-0.1.11.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openalgo_backtrader-0.1.11-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file openalgo_backtrader-0.1.11.tar.gz.

File metadata

  • Download URL: openalgo_backtrader-0.1.11.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for openalgo_backtrader-0.1.11.tar.gz
Algorithm Hash digest
SHA256 983d84dbf1a7eabd66ddc862807137b3d245b319fbb0653a97fae9d6bc48ae52
MD5 ffcc977703767a3ed06ccb06df65ee4c
BLAKE2b-256 f69a5a4adea8c101808f5dcd5eccf3e539771eb5de24d502932d01406952b0ba

See more details on using hashes here.

File details

Details for the file openalgo_backtrader-0.1.11-py3-none-any.whl.

File metadata

File hashes

Hashes for openalgo_backtrader-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 d791f7b44df1219f21ac55b9c8e2b1eabf5003533f3ae3503d11bc96855a6773
MD5 1e4a20e8283b2fe6b2003b7af7307ce2
BLAKE2b-256 c7a59e54e272b82b416cf5078c354369f123ffaad2c9eb8fc5dc0a4dcd534cc2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page