Skip to Content
While25SDK docs
Examples

Examples

Ready-to-run US examples for every part of the SDK: strategies, indicators, monitors, panels and backtests.

Every example targets US markets and runs on While25 Desktop. Copy one into your workspace, or read it here. Each is tested in the SDK repo, so the code on these pages runs as written.

Strategies

ExampleWhat it shows
Dip buyer with an AI second opinionA 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.
Buy an at-the-money call on a breakout, manage it on its own ticksWhen 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=...).
Placing an order exactly once, even when the call times outA TIMEOUT means the order may or may not have been placed. This strategy buys MSFT once a day at a set time and routes the call through place_once, which retries a timed-out order under the same idempotency key: if the first attempt went through, the platform answers DUPLICATE_ORDER and nothing is placed twice.
Index futures trend with a manual rollTrades one S&P 500 or Nasdaq-100 futures contract (ES, MES, NQ or MNQ) with the hourly trend: long above the 20-hour average, flat below, always with a bracket stop a few average true ranges away. future() names the contract the chart's continuous line is on now; when that moves to the next month, the strategy closes the old contract and opens the new one itself. A redeploy picks its bracket back up from my_positions().
Keltner breakout from the indicator's own numbersReads the Keltner channel from indicators/keltner_channel.py on its own 5-minute AAPL bars, so the strategy decides on exactly the lines the chart draws. It enters on a tick that breaks the upper line while the bar is still forming, and exits when a closed bar falls back to the middle line.
Patient limit entry: work a bid, follow it, give up in timeRests a limit buy for AAPL a few ticks under the bid, moves it when the bid walks away by more than a set number of ticks, and cancels it if it has not filled within a time limit. Every price is put on the venue's tick grid before it is sent, so nothing is refused for an off-grid price.
Opening-range breakout with a bracket orderBuys NVDA when a 5-minute bar closes above the first 30 minutes' high, with a stop at the range's low and a target two ranges up, moves the stop to break-even once the trade is one range in profit, and squares off at 15:55 New York time. Use it as the template for any intraday bracket strategy.
Risk-sized stop-limit entry from the account's buying powerEach day MSFT closes above its 50-day average, rests a stop-limit buy just above that day's high, with a protective stop under its low. The share count risks a fixed fraction of the money the broker says is available for a new trade, and is checked against the venue's size and tick rules before anything is sent. When the broker states no such figure, it does not trade.
Scale-in strategy that survives a restartBuys SPY in up to three equal tranches, each a set percent below the first entry, and sells everything once the price is back a set percent above the average cost. Its plan (the first entry and how many tranches are in) is saved in a file on every change and on stop; on start it reads the file back and checks it against what the platform says this strategy holds, which wins.
Moving-average trend on a Bars lineHolds MSFT while its fast average is above its slow one and is flat otherwise, deciding on each closed hourly bar. The Bars line arrives warmed up, so the first decision already has its history; exits are sized from what this strategy holds, never from the account.
SPY bull call spread as one orderOnce a day at a set time, if SPY is above its 20-day average, buys the at-the-money call and sells the call a few strikes higher as ONE combo at a limit built from each leg's mid price. An unfilled combo is cancelled whole after a few minutes; a filled one is squared off whole if SPY closes a 15-minute bar below the long strike.
Daily breakout with a trailing-stop exitBuys AAPL when a daily bar closes above the highest high of the bars before it, and on the fill puts a trailing stop under the position, a few average true ranges away. The stop can be held by While25 or, where the broker holds trailing stops (IBKR), by the broker, so it works while the computer sleeps.
Two strategies on one instrument, one accountA slow trend strategy and a fast mean-reversion strategy both trade NVDA. The broker nets their fills into one position, but each strategy keeps its own part: each tags its orders, listens only to its own tag, and sizes every exit from my_position(), never from the account's position(). Each also reads its part again when the whole position is squared off.
Weekly dollar-amount buysBuys a fixed dollar amount of one ETF (SPY by default) once a week, on the weekday you pick, during regular hours. The order is sized by amount=, so the broker decides the shares; where the broker takes no amount for that instrument it falls back to whole shares from the last price. The week it last bought is kept in a file, so a restart never buys twice.

Indicators

ExampleWhat it shows
Keltner channel: an average with a band of average true rangesAn exponential average of the close, with an upper and a lower line a multiple of the average true range away, the space between them shaded. It draws on the price itself, and the breakout strategy in strategies/keltner_breakout.py reads the same numbers from its own bars.
Return z-score in its own paneHow unusual the latest N-bar return is against the returns of the recent past, in standard deviations, drawn as a histogram coloured by sign, with lines at +/-2 and the zone between +/-1 shaded. The scale is fixed so two charts read alike, and it recomputes as the bar forms.
Session VWAP with one- and two-sigma bandsThe volume-weighted average price since the New York open, restarted every session, with bands one and two volume-weighted standard deviations away. It works on any intraday chart and draws nothing on a daily one, where a session VWAP means nothing.
Trend significance: is the drift in the last N closes real, or noise?Fits a least-squares line through the last length closes and draws the slope's t-statistic as a histogram in its own pane, with the +/-2 line shaded. Use it to compare trend strength across symbols: a t-statistic has no units.

Monitors

ExampleWhat it shows
Breakout watch across several stocks, with an AI checkWatches 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.
Fill journal for the whole accountWrites every execution on the account, from any strategy or by hand, to a CSV file in the workspace, one row per fill, and logs each order that ends refused or cancelled. A journal you can open in a spreadsheet at the end of the day.
Nasdaq-100 opening-gap scannerTurns on live prices for every Nasdaq-100 member, and in the first minutes after the open lists the biggest gaps from yesterday's close, then sends one notice with the top names. Members come from the platform's dated index list, whose date it logs: the list is as of that day, not today.
News alerts for a watchlistSends a notice for each live news article on AAPL, MSFT or NVDA whose title contains one of your keywords, and on start backfills the last day's articles to the console so nothing from the morning is missed. With no news vendor configured it says so once and keeps running.
New SEC filings for a watchlistEvery 30 minutes, fetches the SEC's filing index for a few companies through the platform's open-data registry and sends a notice for each 8-K, 10-Q or 10-K filed since the last check, with a link to the filing. It checks the columns it needs against describe() at start, and treats each refusal code the way the guide says to.
Price alerts on symbols picked in the settingsThree symbol settings and a percent: whichever symbols are filled in, the monitor turns their prices on while it runs and sends a notice the first time each moves more than that percent from where it was when the monitor started. A symbol a setting names cannot go in a decorator, so its ticks are turned on with subscribe(on_tick=...).

Apps and panels

ExampleWhat it shows
Account overview: every figure the broker states, and the day's P&LAn HTML page of the account as the broker reports it: day, realised and unrealised P&L, each funds figure under the broker's own label (with the money it lets you put into a new trade marked), per-asset balances where the venue has them, and a small inline SVG bar per position's P&L. Any figure the broker did not state says so in words, never as 0.
Account desk built from blocksA whole panel described as what to show: header, tiles and a positions table in a ui.view, drawn by the platform in its own components. Start here for any dashboard that needs no custom layout.
Account desk as an HTML pageThe same desk as blocks_dashboard.py, written as one HTML page on the platform's brand classes. Use ui.html when the arrangement needs markup of your own; the page has no script permission, so every value is computed here.
Index breadth: how much of the S&P 500 or Nasdaq-100 is taking partPulls a year and a half of daily closes for every member of the chosen index once a day, then shows the share of members above their 50- and 200-day averages, new 52-week highs and lows, the day's advancers, and the five best and worst members. Members come from the platform's dated index list, and the panel says which date.
Quote recorder: a long-running loop that writes a CSVSamples the quotes of a few ETFs once a second during regular hours and appends them to a CSV in the workspace, one file per day, with a small panel that shows how many rows it has written. A run() loop is the escape hatch for work the decorators do not cover; it checks alive() every pass so it stops the moment it is asked to.
A strategy with its own deskA SPY pullback strategy (buy a 5-minute close back above the 20-bar average after a bar that dipped below it, in an uptrend) whose panel is its desk: the platform's reconciled figures for the strategy, each entry rule met or not, the price against the stop and target, what happened today, and the bars it decides on. The strategy sends its state after each bar; the desk only shows.

Backtests

ExampleWhat it shows
One strategy, two fill assumptions, side by sideA dip-buying limit order on AAPL (buy 1% under the previous close, sell at that day's close) is very sensitive to whether a limit fills when the price merely touches it or only when it trades through. This runs the same logic under both and pushes the two sealed results into one panel, so the gap between them is the first thing you see.
Daily trend filter, 15-minute entries, no look-aheadTrades AAPL's opening-range breakout on 15-minute bars, only on days the daily trend is up. The trend is read from the daily bars that had CLOSED by the moment each 15-minute bar closed, so the filter never sees the day it is trading. Entries fill at the next bar's open; a bracket stop and target are tested bar by bar, and anything still open exits at the session's last close.
SPY / QQQ pair: two instruments in one backtestTrades the spread between SPY and QQQ: when the log price ratio is more than two standard deviations from its 60-day mean, sells the rich one and buys the cheap one in equal dollar amounts, and closes both when the ratio is back within half a deviation. Both legs fill at the next day's open. Costs are a flat custom figure per order, stated in the block.
Moving-average crossover on AAPL, with real costs and slippageThe simplest honest backtest: daily AAPL bars for a fixed window, a fast and a slow average, decided on each close and filled at the next bar's open, costed on the connected broker's real fee schedule with a few basis points of slippage against every fill. The platform re-derives every trade from the bars and seals the result, or refuses it naming the rule broken.
Walk-forward logistic regression on SPY, numpy onlyFits a small logistic regression (five price features, L2 penalty, plain gradient descent) to say whether SPY's next session closes above its open, and trades that session open-to-close when the model is confident. It walks forward: fit on a training window, trade the next window, move both along. Every scaling figure and weight comes from bars before the test window, and no training label reaches into it; only test-window trades are sealed.