Skip to content
DOCUMENTATION / Strategy anatomy
OPBT SDK · 02

Strategy anatomy

On this page
02

Strategy anatomy

Skeleton#

A strategy entrypoint defines exactly one class extending Strategy and implements initialize. Supporting project files may contain importable functions and classes without another Strategy subclass.

strategy.py
from opbt import Strategy

class MyStrategy(Strategy):
    def setup_runtime(self, ctx):
        """Once per sandbox process; rebuild process-local helpers."""
        self.signal = build_signal(ctx.params)

    def initialize(self, ctx):
        """Runs once before the first bar."""
        ctx.schedule(self.rebalance, every="8h", offset="5m")

    def handle_bar(self, ctx, bars):
        """After each bar close. bars: dict[instrument, Bar]"""
        ...

    def on_funding(self, ctx, ev):
        """After a funding settlement (non-zero cashflow only)"""
        ...

    def on_finish(self, ctx):
        """Once after the last bar (optional)"""
        ...

    def rebalance(self, ctx):
        """Registered via ctx.schedule"""
        ...

Callbacks#

CallbackWhenRequired
setup_runtime(ctx)Once per sandbox process; read-only bootstrap, before callbacksNo
initialize(ctx)Once, before the first barYes
handle_bar(ctx, bars)After each bar close; only instruments with data at that timestampNo
on_funding(ctx, ev)Verified settlements in Backtest and Paper; Live does not currently generate this callback from estimated fundingNo
on_finish(ctx)Once, after the last barNo

Module rules#

ONEPORT RESEARCH · DOCUMENTATION