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 after the first trusted market boundary."""
        ctx.schedule(self.rebalance, every="8h", offset="5m")

    def handle_bar(self, ctx, bars):
        """Decision boundary; bars contains real observations only."""
        quality = ctx.data_quality
        ...

    def on_funding(self, ctx, ev):
        """Optional legacy hook for one applied funding event"""
        ...

    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 at the first trusted initialization boundary; dynamic Paper may use setup-only selection PIT evidence before execution KLinesYes
on_parameters_changed(ctx, previous, current)At a safe boundary after an accepted runtime parameter snapshot; initialize is not replayedNo
handle_bar(ctx, bars)At each admitted decision boundary; bars contains only real observations, while ctx.data_quality explains partial/empty clocksNo
on_funding(ctx, ev)Verified settlements in Backtest and Paper; Live does not currently generate this callback from estimated fundingNo
on_adl(ctx, ev)Current production source is the explicitly enabled Backtest research archive; Paper/Live do not yet emit trusted account-position ADL eventsNo
on_finish(ctx)Once, after the last barNo

Module rules#

ONEPORT RESEARCH · DOCUMENTATION