Dip buyer with an AI second opinion
A monitor watches QQQ's 5-minute bars for a fall of more than a set percent from the window's first close, asks the AI whether it reads as a steady slide or one sharp drop, and signals its strategy only on "drop". The strategy then buys with a bracket. The strategy never asks the AI itself: only a monitor may, and the strategy lists it as its own, so both start and stop together.
A monitor watches QQQ's 5-minute bars for a fall of more than a set percent from the window's first close, asks the AI whether it reads as a steady slide or one sharp drop, and signals its strategy only on "drop". The strategy then buys with a bracket. The strategy never asks the AI itself: only a monitor may, and the strategy lists it as its own, so both start and stop together.
Shows: Monitor inside a Strategy (monitors = [...]), while25.ai.ask(data=, choices=, then=, tag=), emit(), @on.signal(name, sender=Class), shared inputs by name, a bracket with take_profit=.
"""Dip buyer with an AI second opinion
A monitor watches QQQ's 5-minute bars for a fall of more than a set percent
from the window's first close, asks the AI whether it reads as a steady slide
or one sharp drop, and signals its strategy only on "drop". The strategy then
buys with a bracket. The strategy never asks the AI itself: only a monitor
may, and the strategy lists it as its own, so both start and stop together.
Shows: Monitor inside a Strategy (monitors = [...]), while25.ai.ask(data=, choices=, then=, tag=), emit(), @on.signal(name, sender=Class), shared inputs by name, a bracket with take_profit=.
"""
import while25
from while25 import Bars, Monitor, Strategy, on
from while25.inputs import Float
KEY = "EQ:US:QQQ"
SHARED = [Float("dip_pct", default=1.5, minimum=0.2, maximum=10.0, step=0.1,
label="Dip from the window's first close (%)")]
def dip_pct(rows):
"""How far the last close sits under the window's first close, in percent
(negative for a rise); None for fewer than two bars."""
if len(rows) < 2 or not rows[0]["close"]:
return None
return (rows[0]["close"] - rows[-1]["close"]) / rows[0]["close"] * 100
def compact(rows):
"""The bars as the AI reads them: few keys, rounded, oldest first."""
return [{"t": r["time_ms"], "o": round(r["open"], 2), "h": round(r["high"], 2),
"l": round(r["low"], 2), "c": round(r["close"], 2), "v": r["volume"]} for r in rows]
class DipWatch(Monitor):
id = "qqq_dip_watch"
label = "QQQ dip watch"
inputs = SHARED # starts with the strategy's value
qqq = Bars(KEY, "5m", keep=24) # two hours
def on_start(self, ctx):
self.asked_at = None # one question per dip, not per bar
@on.bar(qqq)
def bar(self, ctx, bar):
d = dip_pct(self.qqq.rows)
if d is None or d < while25.params["dip_pct"]:
self.asked_at = None
return # the free check: most bars end here
if self.asked_at is not None:
return
self.asked_at = bar["time_ms"]
while25.ai.ask("QQQ has fallen over the last two hours. Is this a steady slide "
"or one sharp drop that may bounce? Rows are 5-minute bars, oldest first.",
data=compact(self.qqq.rows), choices=["slide", "drop"],
timeout=30, then=self.judged, tag=bar["close"])
def judged(self, ctx, answer, tag):
while25.log(f"AI says {answer!r} at {tag}")
if answer == "drop": # None (no answer) sends nothing
while25.emit("dip_ok", {"close": tag})
class AiCheckedDipBuyer(Strategy):
id = "qqq_ai_dip_buyer"
label = "QQQ dip buyer, AI-checked"
monitors = [DipWatch]
inputs = SHARED + [Float("stop_pct", default=1.0, minimum=0.2, maximum=10.0, step=0.1),
Float("target_pct", default=1.5, minimum=0.2, maximum=20.0, step=0.1)]
def on_start(self, ctx):
self.inst = while25.data.instrument(KEY)
@on.signal("dip_ok", sender=DipWatch) # hears DipWatch, and nobody else
def go(self, ctx, payload):
mine = while25.portfolio.my_position(KEY)
if mine and mine["brackets"]:
return # one dip trade at a time
p, close = while25.params, payload["close"]
stop = self.inst.snap(close * (1 - p["stop_pct"] / 100), mode="down")
target = self.inst.snap(close * (1 + p["target_pct"] / 100), mode="up")
while25.bracket(KEY, "BUY", 10, stop_loss=stop, take_profit=target, tag="qqq_dip")
while25.log(f"dip bought near {close}: stop {stop}, target {target}")Examples
Ready-to-run US examples for every part of the SDK: strategies, indicators, monitors, panels and backtests.
Buy an at-the-money call on a breakout, manage it on its own ticks
When a 15-minute AAPL bar closes above the high of the day's earlier bars, buys the call the description names (ATM by default) for the expiry you set, then follows that contract's own prices to take a profit or cut a loss as a percentage of what was paid. The contract is only known at run time, so its prices are turned on with `subscribe(on_tick=...)`.