Strategy with every hook

Forge API 1Updated 1 Oct 2026

Strategy

Every hook a strategy may define is in this file, each with one line that does nothing harmful and a comment naming when it runs. Delete the ones you do not need: a strategy with only compute is the fastest kind.

every_hook.pypythonOpen in the editor
import forge as fg
from forge import param, ta

FORGE_API = 1


class EveryHook(fg.Strategy):
    """A strategy that defines every optional hook, for reference."""

    meta = fg.Meta(title="Every hook", short="EVH", tags=["template"])
    bars = fg.bars.Time("H1")
    direction = "long"

    period = param.Int(14, min=2, max=200)

    def compute(self, d) -> fg.Signals:
        rsi = ta.rsi(d.close, self.period)
        return fg.Signals(long_entry=rsi < 30, long_exit=rsi > 55)

    def setup(self, ctx) -> None:
        # Once, before any bar: check settings and prepare state.
        ctx.log.info("setup")

    def on_warmup_end(self, ctx) -> None:
        # Once, when warm-up ends and trading is allowed.
        ctx.log.info("on_warmup_end")

    def on_bar(self, ctx) -> None:
        # On every closed bar. Defining it makes every bar an event, the slowest kind.
        ctx.log.info("on_bar")

    def on_order_accepted(self, ctx, order) -> None:
        # When the market accepts an order.
        ctx.log.info("on_order_accepted")

    def on_fill(self, ctx, fill) -> None:
        # When an order fills, with the fill.
        ctx.log.info("on_fill")

    def on_order_cancelled(self, ctx, order) -> None:
        # When an order is cancelled.
        ctx.log.info("on_order_cancelled")

    def on_order_rejected(self, ctx, order) -> None:
        # When an order is rejected.
        ctx.log.info("on_order_rejected")

    def on_order_expired(self, ctx, order) -> None:
        # When an order expires.
        ctx.log.info("on_order_expired")

    def on_order_event(self, ctx, event) -> None:
        # After the specific order hook, for every order event.
        ctx.log.info("on_order_event")

    def on_position_opened(self, ctx, position, reason) -> None:
        # When a position opens.
        ctx.log.info("on_position_opened")

    def on_position_changed(self, ctx, position, reason) -> None:
        # When a position changes size.
        ctx.log.info("on_position_changed")

    def on_position_closed(self, ctx, position, reason) -> None:
        # When a position closes, with the reason.
        ctx.log.info("on_position_closed")

    def confirm_entry(self, ctx, intent) -> bool:
        # A last yes or no before an entry order is sent.
        return True

    def confirm_exit(self, ctx, intent) -> bool:
        # A last yes or no before an exit order is sent.
        return True

    def on_day_start(self, ctx) -> None:
        # At the start of a trading day.
        ctx.log.info("on_day_start")

    def on_day_end(self, ctx) -> None:
        # At the end of a trading day.
        ctx.log.info("on_day_end")

    def on_week_start(self, ctx) -> None:
        # At the start of a trading week.
        ctx.log.info("on_week_start")

    def on_session_open(self, ctx, name) -> None:
        # When a named session opens.
        ctx.log.info("on_session_open")

    def on_session_close(self, ctx, name) -> None:
        # When a named session closes.
        ctx.log.info("on_session_close")

    def on_end(self, ctx, reason) -> None:
        # Once, after the last step, with the reason the run ended.
        ctx.log.info("on_end")

    def on_prop_warning(self, ctx, rule, remaining) -> None:
        # Once, when most of a prop rule's room is used up, with the room left.
        ctx.log.info("on_prop_warning")

    def on_prop_breach(self, ctx, rule, action) -> None:
        # Once, after the engine has acted on a broken prop rule, with what it did.
        ctx.log.info("on_prop_breach")

    def size(self, ctx, intent) -> float:
        # Sets the size of an entry. This one keeps the size so far.
        return intent.size

    def score(self, stats) -> float:
        # Defines what best means when results are ranked.
        return 0.0

    def accept(self, stats) -> bool:
        # Discards a result that fails a minimum.
        return True

    def report(self, stats) -> dict:
        # Adds named numbers to the report.
        return {}
Open in the editor
Test in Forge ↗

Opens oathra.app.

Was this page helpful?