Check what each open position would cost if it opened beyond your stop
Before the close, a script works out each position's loss if it opened past your stop by a gap size you choose from your own data. If the total exceeds the overnight limit you wrote down, you see it before you decide to hold.
How it works
- Write two limits in your risk rules: the most you're willing to lose overnight on one position if it opens beyond your stop, and the same for all open positions together. These sit alongside your normal risk per trade.
- Choose a stress gap for each position from your own data, for example the largest opening gap against you in that instrument's last two years of daily prices, or a fixed percentage you set for anything with less history. Store it as a decimal (a 4% gap is 0.04) and write down which method you used.
- Near the close, a script reads your open positions (size, entry, stop and last price, which after the close is the session's closing price) from your own file, along with the stress gaps. It calculates each position's loss from the last price if the stop fills exactly, and its loss from the last price if the next session opened at stop × (1 − stress gap). Measuring from the last price shows what holding overnight puts at risk from here, whatever the position has already gained or lost since entry.
- It adds up the gap-case losses across positions and compares each figure with your two limits, marking any position or total that's over.
- It lists any dates you track for open positions, such as a company's report date, because gaps cluster around them. The dates come from your own calendar file, not from the model.
- You decide what, if anything, to change before the close, and the script appends the day's figures and your decision to your journal.
Copy-paste prompt
Write a Python script for my end-of-day gap check. It gives no trading advice and never suggests trades, sizes or securities.
Inputs: {{positions_csv}} (position_label, quantity, entry, stop, last_price, currency), {{gap_file}} (position_label, stress_gap_pct, method_used), {{events_file}} (position_label, event_date, note) and my limits {{per_position_limit}} and {{total_limit}} in account currency.
The script must:
1. stress_gap_pct is a decimal (0.04 means 4%), and all prices and limits are in one currency. last_price is the latest close (the prior close for the next session). For each position, compute loss_at_stop = quantity × (last_price − stop) and, with gap_open = stop × (1 − stress_gap_pct), loss_at_gap = quantity × (last_price − gap_open). Both are losses measured from the last price, not from entry. Also print open_result = quantity × (last_price − entry) for reference only; it is not used in the limit checks. If last_price is at or below the stop, flag the position instead of computing a gap figure.
2. Sum the gap-case losses (from the last price) across positions.
3. Mark any position whose gap-case loss exceeds {{per_position_limit}}, and say whether the total exceeds {{total_limit}}.
4. List events from {{events_file}} falling before the next session.
5. Print a table and a one-line journal entry with the date, the totals and any limits exceeded, and append it to {{journal_file}} only after I confirm.
6. Include a helper that measures the largest opening gap below the prior close in a daily price file, for me to use when choosing stress gaps.
It must not connect to a broker or place orders. Include two test cases I can check by hand.Guardrails
- The limits and stress gaps are yours, written in advance. The script doesn't propose them.
- A stop is treated as a trigger, not a promised exit price.
- Event dates come from your own calendar file, never from the model's memory.
- The script reads your files and appends a journal line only after you confirm. It never places orders.
What can go wrong
The worst gap in two years is not the worst gap possible, so treat the result as a floor on overnight risk, not a ceiling. Adding up every position's worst case assumes they all gap at once. That's deliberately harsh, but if the total routinely breaks your limit, your normal sizing and your overnight limit disagree, and that's worth settling on a calm weekend rather than at the close. A stress gap measured once also goes stale as an instrument gets more or less volatile, so re-measure it at each rule review.
Why it helps
Position sizing usually assumes the stop fills at the stop. Overnight and over weekends that assumption can fail: the next session can open well below your stop, and the loss is whatever the opening price makes it. Knowing each position's gap-case loss from the last price, and the total across all of them, turns overnight risk from a surprise into a number you looked at before the close, measured against a limit you set when you were calm. The last price is the right base for that decision: a position well in profit can still give back a lot overnight, and measuring from entry would understate it.
How it fits with other entries
The pre-trade sizing check (aft-0008) sizes a new position against its stop; this entry checks the whole open book against a gap past the stops, every evening. The drawdown mode check (aft-0038) changes your risk after losses, and the earnings prep pack (aft-0005) is where report dates for the events file come from.
Where this comes from
A public reply by @JacobC775 on X: a stop that only exists in regular hours doesn't cover the gap, and overnight risk is a size decision you make before the close. The regulator's investor bulletin linked below explains why stop orders don't guarantee a price. The stress gaps, the two limits and the journal line are ours.
Source and references
Based on a public reply by @JacobC775 on X pointing out that a stop that only works in regular hours doesn't protect against a gap, and that overnight risk is a sizing decision made before the close. The stress-gap method, the two limits and the journal line are our adaptation.
Original: Jacob on x
- Reply on overnight gap risk as a sizing decision made before the close · Jacob (@JacobC775) on X (checked 19 Oct 2026)
- Stop, Stop-Limit, and Trailing Stop Orders: Investor Bulletin · Investor.gov, U.S. Securities and Exchange Commission (checked 19 Oct 2026)
- Gap: Definition, Types, Examples, and Trading Strategies · Investopedia (checked 19 Oct 2026)
Spotted an error, or want your post removed? Email hello@agentsfortraders.com.
Educational content only. Not investment advice or a personal recommendation. No tickers, signals or trade alerts. Trading involves risk of loss.
Conteúdo exclusivamente educativo. Não constitui aconselhamento nem recomendação de investimento. Sem tickers, sinais ou alertas de negociação. Negociar envolve risco de perda.
Related use cases
Record each earnings date with a source, or "unknown"
Before each report, an agent gathers the confirmed report date with a source link, past post-report moves computed in code, and a flag against your own written rule for names inside that window.
Risk & position-sizing checksCheck position size and portfolio heat before you trade
Run a local Python script on your planned entry, stop and positions CSV, and paste only its stdout. The agent checks size, heat, theme concentration and, when present, correlation against your limits, and leaves the decision to you.
Risk & position-sizing checksCheck your drawdown rules before each session and record the day's mode
Before you look for anything new, a script reads your equity log and your own drawdown rules and records today's mode: normal, reduced size or review-only. If the data is missing or stale, it records review-only and explains why.
The Daily 3
Three agent workflows for swing traders, free, on US trading days.