Skip to Content
While25SDK docs
ExamplesBacktests

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.

View on GitHub

backtests/compare_fill_assumptions.py
"""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})