Skip to Content
While25SDK docs
ExamplesMonitors

Breakout watch across several stocks, with an AI check

Watches AAPL, MSFT and NVDA on 15-minute bars and, when one closes above the high of its previous 20 bars, asks the AI whether it looks like a clean breakout or a likely fakeout. Each question is asked without waiting for the last, and only a "breakout" answer becomes a notice. It never trades.

Watches AAPL, MSFT and NVDA on 15-minute bars and, when one closes above the high of its previous 20 bars, asks the AI whether it looks like a clean breakout or a likely fakeout. Each question is asked without waiting for the last, and only a "breakout" answer becomes a notice. It never trades.

Shows: Monitor, warmup() per symbol in on_start, several @on.bar lines on one handler, bar["symbol"], ai.ask(then=, tag=), notify(title=, level=, symbol=).

View on GitHub

monitors/breakout_watch.py
"""Breakout watch across several stocks, with an AI check

Watches AAPL, MSFT and NVDA on 15-minute bars and, when one closes above the
high of its previous 20 bars, asks the AI whether it looks like a clean
breakout or a likely fakeout. Each question is asked without waiting for the
last, and only a "breakout" answer becomes a notice. It never trades.

Shows: Monitor, warmup() per symbol in on_start, several @on.bar lines on one handler, bar["symbol"], ai.ask(then=, tag=), notify(title=, level=, symbol=).
"""
import while25
from while25 import Monitor, on
from while25.inputs import Int

SYMBOLS = ["EQ:US:AAPL", "EQ:US:MSFT", "EQ:US:NVDA"]
WINDOW = 20


def is_breakout(window, bar):
    """True when `bar` closed above the highest high of the bars before it."""
    return bool(window) and bar["close"] > max(r["high"] for r in window)


def push(window, bar, n=WINDOW):
    """The window with `bar` added and only the newest n kept."""
    return (window + [bar])[-n:]


class BreakoutWatch(Monitor):
    id = "us_breakout_watch"
    label = "US breakout watch"
    inputs = [Int("max_questions", default=20, minimum=1, maximum=200,
                  label="Most AI questions per run")]

    def on_start(self, ctx):
        self.rows = {s: list(while25.warmup(s, "15m", WINDOW)) for s in SYMBOLS}
        self.asked = 0

    @on.bar("EQ:US:AAPL", "15m")
    @on.bar("EQ:US:MSFT", "15m")
    @on.bar("EQ:US:NVDA", "15m")
    def bar(self, ctx, bar):
        key = bar["symbol"]
        window = self.rows[key]
        self.rows[key] = push(window, bar)
        if not is_breakout(window, bar) or self.asked >= while25.params["max_questions"]:
            return                          # the free check: most bars end here
        self.asked += 1
        while25.ai.ask("Is this a clean breakout or a likely fakeout? Rows are 15-minute "
                       "bars, oldest first; the last row is the breakout bar.",
                       data=self.rows[key], choices=["breakout", "fakeout"],
                       then=self.judged, tag=key)

    def judged(self, ctx, answer, tag):
        if answer == "breakout":            # the price has moved since: this is a prompt to look
            last = self.rows[tag][-1]["close"]
            while25.notify(f"closed at {last}, above the 20-bar high", title=tag.split(":")[-1],
                           level="warn", symbol=tag)
        elif answer is None:
            while25.log(f"no AI answer for {tag}")