One strategy, two fill assumptions, side by side
A 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.
A 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.
Shows: backtest.assume(fills="touch"/"through"), a limit fill that returns None, two backtest blocks in one view, ui.backtest_block(title=, subtitle=), the same history id behind both.
"""One strategy, two fill assumptions, side by side
A 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.
Shows: backtest.assume(fills="touch"/"through"), a limit fill that returns None, two backtest blocks in one view, ui.backtest_block(title=, subtitle=), the same history id behind both.
"""
import while25
from while25 import App, backtest, ui
KEY = "EQ:US:AAPL"
START, END = "2020-01-02", "2025-12-31"
ui.panel("bt_fill_assumptions", "Touch vs through")
ui.layout(ui.view("main"))
def dip_trades(bars, inst, a, *, dip_pct=1.0, qty=100):
"""Each day: a buy limit `dip_pct` under the previous close; if it fills,
sell at that day's close. The limit's price is known before the day opens."""
trades = []
for prev, bar in zip(bars, bars[1:]):
limit = inst.snap(prev["close"] * (1 - dip_pct / 100), mode="down")
entry = backtest.fill(bar, "buy", "limit", instrument=inst, price=limit, assume=a)
if entry is None:
continue # the day never reached the limit
out = backtest.fill(bar, "sell", "close", instrument=inst, assume=a)
trades.append(backtest.trade(inst, "buy", qty, entry, out, product="intraday", assume=a))
return trades
class CompareFills(App):
id = "bt_compare_fill_assumptions"
label = "Touch vs through"
def run(self, ctx):
span = while25.history_span(KEY, "1D", START, END)
inst = while25.data.instrument(KEY)
blocks = []
for style in ("touch", "through"):
a = backtest.assume(costs="real", fills=style)
trades = dip_trades(span["bars"], inst, a)
blocks.append(ui.backtest_block(
trades, data=[span["id"]], capital=100_000, assume=a,
title=f"AAPL 1% dip limit, fills on {style}",
subtitle=f"{len(trades)} trades, {START} to {END}",
logic={"Entry": "buy limit 1% under the previous close",
"Exit": "the same day's close"}))
while25.ui.push("main", {"blocks": blocks})A strategy with its own desk
A 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.
Daily trend filter, 15-minute entries, no look-ahead
Trades 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.