# Stop loss not covering your position after a partial fill

After a partial entry, a canceled remainder, a rejected child or a partial exit, the position can have no working stop, or one too large, and no venue message says so. After every event, compare armed stop quantity with the open position, and check separately that no exit can open the opposite position.

Canonical: <https://pitofficer.com/blog/stop-loss-not-covering-partial-fill/>
Author: Eugene Palchukovsky <https://github.com/palchukovsky>
Description: A bracket entry that fills 30 of 100 before you cancel the rest can leave the position with no working stop; here is the check to run after every event.
Published: 2026-11-12T22:08:16+02:00
Updated: 2026-11-12T22:08:16+02:00
Tags: risk-controls, order-management, trading-bots, stop-loss

Your bot buys 100 shares with a bracket, 30 fill, it cancels the rest, and 5
more fill before the cancel lands: 35 shares now sit without a working stop,
and no message says so.

:::stat
**0 of 35 shares**

What an Alpaca bracket's stop covers then, by its documented rules read
together. The quantities are illustrative.
:::

:::timeline

1. **The bot sends a bracket buy for 100.** A stop-loss sell for 100 is
   attached to it.
2. **30 fill.** The stop waits for the rest of the entry.
3. **The signal expires.** The bot sends a cancel for the remaining 70.
4. **5 more fill before the cancel lands.** The position is now long 35.
5. **The cancel completes and takes the stop with it.** Long 35, no stop, and
   nothing reports a problem.

:::

:::cards

- **An accepted bracket is not a working stop.** Exit legs can wait for a
  complete fill, a first fill or each fill in turn, depending on the venue and
  the mode; an order ID for the stop proves nothing about whether it can
  trigger.
- **Coverage is a number you compute.** No venue message says "unprotected".
  Compare the stop quantity the venue confirms as armed with the open position
  after every fill, cancel, rejection, partial exit, stop replacement and
  reconnect.
- **A gap is a finding before it is a repair.** While any exit's state is
  unknown, add only the quantity that stays safe if every unknown exit is live.
- **Too small and too large are different failures.** A gap leaves exposure
  without protection; an oversized exit is safe only where the venue caps a
  close-only order at the position, and otherwise can open a position the other
  way.
- **No exit outlives its position.** When the position reaches zero, cancel
  every exit left on it, take-profits included.

:::

## Your entry filled 30 of 100. What does the stop cover?

**It depends on the venue and the mode, and in two of the five setups below
the answer is nothing.**

The same sequence - 30 filled, a cancel, 5 more filled during it - leaves a long
35 (read the quantities as contracts on Deribit). Neither exit triggers. What
protects the 35:

:::outcomes

- **Alpaca bracket: 0 of 35.** The legs wait for a complete fill, and the
  entry's cancel cancels the group.
- **Deribit, exits after a complete fill: 0 of 35.** They never activate after
  a partial fill and a cancel.
- **Your own stop, sized at the first fill: 30 of 35.** The 5 that filled during
  the cancel have no stop.
- **Deribit, exits per fill: 35 of 35.** The stop grows with the entry: 30 at
  the first fill, 35 after the late one.
- **Deribit, exits at the first fill: a stop for 100.** All 35 covered, with 65
  more than the position.

:::

"Check that a stop exists" flags the two empty setups and passes the stop for 30
and the stop for 100. Only comparing quantities separates those two from the 35.

:::more[Terms this article uses]
The **covered quantity** is the part of the open position that armed stops are
set to close if they trigger now; whether a triggered stop then fills, and at
what price, is a separate question. An **armed stop** is an exit order the
venue has confirmed as working and able to trigger: not pending, not held
behind an unfilled parent, not waiting for a replace. A **bracket order** is an
entry with a take-profit and a stop-loss attached, the two exits linked by a
cancellation rule: when one fills, the venue cancels the other.
:::

:::more[Where each outcome comes from]
The sequence is illustrative: prices and timing are invented. The bot is flat,
sends a bracket buy for 100 with a stop-loss sell for 100, and cancels the
remainder once the signal expires. The late fill follows scenario B.1.b of the
[FIX order-state document](https://www.fixtrading.org/wp-content/uploads/download-manager-files/FIX-Latest-as-of-EP284-Order-State-Changes.pdf)
(November 2023): an execution while the cancel is pending, with the cumulative
quantity kept when the cancel completes.

- **Alpaca.** The
  [order reference](https://docs.alpaca.markets/us/docs/orders-at-alpaca)
  says the exit legs "won't be active until the first order is completely
  filled", and that if any order in the group is canceled, the remaining open
  orders in the group are canceled. It does not document the
  partial-fill-then-cancel case; the outcome is those two rules read together,
  so run the case on a paper account before relying on it either way.
- **Deribit.** Its
  [order API](https://docs.deribit.com/api-reference/trading/private-buy)
  lets the exits activate at the first execution (all of them, at their
  configured amount), only after a complete execution, or per fill (a
  proportional part, "rounded down to the contract size"). The
  [order-management guide](https://docs.deribit.com/articles/order-management-best-practices)
  adds that in the complete-fill mode a primary that partially fills and is
  then canceled never activates its exits, and that in the per-fill mode the
  part lost to rounding waits for a complete fill. Here the proportions are
  whole, so nothing is lost; with a stop sized differently from the entry, a
  remainder can be. The guide's example response lists exit order IDs on a
  primary with nothing filled: the IDs exist before the orders do.
- **Your own stop.** A bot that places its stop on the first execution report
  and never revisits it gets the 30 of 35: a fill can land after the bot has
  decided the entry is over.

The mode names are in [the venue notes](#what-does-each-venue-document).
:::

## When do bracket children activate, and for how much?

**The modes below document when exits activate; several leave open what
happens to them after a partial exit or when the position closes.**

Deribit lets the exits activate at the first fill, after a complete fill or per
fill; Bybit attaches a stop either to the entry order or to the open position.

:::matrix

| Venue and mode | Active on a partial entry | Resized on a partial exit | Canceled when the position closes |
| --- | --- | --- | --- |
| Alpaca bracket | ✗ [1] | ~ [2] | ~ [3] |
| Deribit, first fill | ✓ [4] | ? [5] | ~ [6] |
| Deribit, complete fill | ✗ [4] | ? [5] | ~ [6] |
| Deribit, per fill | ✓ [4] | ? [5] | ~ [6] |
| Bybit, on the order | ✓ [7] | ✗ [8] | ~ [9] |
| Bybit, on the position | ~ [10] | ✓ [10] | ✓ [10] |

1. The exit legs are active only after the entry fills completely. Alpaca
   [order reference](https://docs.alpaca.markets/us/docs/orders-at-alpaca).
2. Only after a partially filled take-profit: the stop is adjusted to the
   remaining quantity. A partial stop fill or a close outside the bracket is
   not documented (same page).
3. One exit filled cancels the other; a close outside the bracket is not
   documented (same page).
4. At the first fill, all exits at the configured amount; after a complete
   fill only; or per fill, a proportional part rounded down to the contract
   size. Deribit
   [order API](https://docs.deribit.com/api-reference/trading/private-buy).
5. Not documented. Deribit's
   [linked orders announcement](https://insights.deribit.com/education/conditional-orders-have-arrived-on-deribit/)
   does not say what size a sibling has after a partial exit.
6. Once placed, the take-profit and the stop are linked: one hitting cancels
   the other. A close outside the pair is not documented. Deribit
   [order-management guide](https://docs.deribit.com/articles/order-management-best-practices)
   and the announcement above.
7. Placed per fill, for the quantity filled. Bybit
   [Place Order](https://bybit-exchange.github.io/docs/v5/order/create-order)
   and its
   [TP/SL help](https://www.bybit.com/en/help-center/article/Take-Profit-and-Stop-Loss).
8. Keeps its quantity and executes at most the open position. A limit
   take-profit paired with it cancels the stop when the take-profit triggers,
   even before it fills. Bybit
   [futures TP/SL guide](https://www.bybit.com/en/help-center/article/Introduction-to-Take-Profit-Stop-Loss-Perpetual-Futures-Contracts),
   scenario 4 and its FAQ.
9. An entire-position stop that closes the position cancels a resting
   take-profit limit, and a stop priced beyond the liquidation price is
   canceled at liquidation; other closes are not documented (the same guide,
   scenario 1 and its FAQ).
10. Not a bracket and not attached to an entry: a conditional order set
    separately on an open position, its quantity adjusted to the open
    position, canceled when the position closes. Bybit
    [Set Trading Stop](https://bybit-exchange.github.io/docs/v5/position/trading-stop).

:::

"Not documented" is the cell that matters: the behavior is whatever the
matching engine does today. A run on the venue's test environment is evidence
for that environment, not a contract, and live behavior can differ; where you
can, get the venue to confirm
the rule in writing. For a venue missing here, the three questions are the
column headings.

:::more[Alpaca warns that both exits can fill]
In fast markets, Alpaca's
[order reference](https://docs.alpaca.markets/us/docs/orders-at-alpaca) says,
both orders "may fill before the cancellation occurs". Long 100 with two sells
of 100, that is a short of 100 if both fill in full and the account can short.
:::

:::more[Bybit unpairs exits on a one-sided amendment]
Amend only the stop or only the take-profit of a position's pair and the two
lose their binding, after which a cancel by order ID removes only that side.
The other side is unaffected and can still trigger on its own. Bybit
[Set Trading Stop](https://bybit-exchange.github.io/docs/v5/position/trading-stop).
:::

## What breaks coverage after the entry?

**Any event that changes the position without the stops, or the stops without
the position.**

:::cards

- **A late fill.** The entry fills during a pending cancel. The position grows;
  a stop sized at the last fill the bot saw does not.
- **A rejected stop leg.** If the venue rejects the stop leg after the entry has
  filled, coverage is zero.
- **A partial exit.** A take-profit fills 20 of 35. Alpaca resizes its own
  bracket stop to the remaining 15; a stop the bot placed itself stays at 35,
  20 more than the position.
- **A stop replacement.** The bot moves the stop and the response times out:
  the old stop, the new one, or for a moment neither is working, and either may
  already have triggered or filled.
- **A reconnect.** Every exit the bot has not seen since the reconnect is
  unknown: triggered, canceled by the venue, or still working.
- **A manual cancel.** Someone removes the stop in the venue's UI.

:::

:::more[What each event needs from the bot]

- **A rejected stop leg.** Neither the Alpaca nor the Deribit documentation
  cited here says what happens to the take-profit sibling in that case.
- **A partial exit.** OKX's
  [TP/SL help](https://www.okx.com/en-gb/help/how-do-i-modify-take-profit-tp-and-stop-loss-sl)
  tells futures traders to check the stop quantity again after a partial
  take-profit for this reason.
- **A stop replacement.** On Alpaca, an order waiting for its replace rejects
  cancel requests while that lasts. Reconcile executions and the position along
  with both orders.
- **A reconnect.** Rebuild the exits with the orders and the position, not
  after them.
- **A manual cancel.** The order stream reports the cancel if the bot receives
  it; a periodic query of open orders finds it if the bot missed the message.

:::

## How does the bot check coverage after every event?

**Compare the stop quantity the venue confirms as armed with the open position,
and repair only what stays safe if every unknown exit turns out to be live.**

```d2 title="The coverage check, run after every fill, update, rejection and reconnect"
direction: down
flat: "Position\nzero?"
clear: "Cancel\nevery exit"
gap: "Stops cover\nless?"
fix: "Block entries;\nadd a stop only if\nevery exit still fits;\nat the deadline,\nhalt or close"
flip: "Exits can\nclose more?"
reduce: "Shrink the\nuncapped exits"
flat -> clear: "yes"
flat -> gap: "no"
gap -> fix: "yes"
gap -> flip: "no"
fix -> flip
flip -> reduce: "yes"
```

Run it per symbol and position side. A new stop is safe only if it still fits
within the position with every exit that may be live, take-profits included,
or if the venue caps it at the position. Open 35 and an armed stop for 30: with
a stop or a take-profit for 5 not yet acknowledged, adding 5 now would leave
exits for 40 if that order is live; the lookup settles it first.

:::more[What the check counts]
Run one check per symbol and position side after every execution report, order
update, acknowledgment or rejection, and after every reconnect. In hedge mode
the long and short sides are separate positions with separate exits. Take the
position from a ledger that counts each execution once and keeps the cumulative
filled quantity the venue reports
([why a bot's position drifts from the broker's](/blog/trading-bot-position-out-of-sync-with-broker/)).

The exits are every order that closes this side and can still execute - stops,
take-profits, triggered children - working or unknown. Exits held behind a
working entry count once they activate. An OCO pair counts once, at its larger
leg. Counting it once trusts the venue's cancel, which Alpaca says can lose the
race; only a close-only cap removes that risk.

Of the stops, the check separates those the venue confirms working and
triggerable from those in an unknown state: unacknowledged, waiting for a
replace, not seen since the reconnect. The gap if no unknown stop is live is
what it records. What it adds is the gap if every unknown stop is live, cut to
the room that every exit that may be live leaves under the position, unless the
venue caps the new stop. The full logic is in [the pseudocode](#where-is-the-check-in-pseudocode).
:::

:::more[When a venue bracket holds the exits]
On Alpaca the bracket stop is held during a partial fill, so the check reports
a gap. Add a separate stop for the 30 and, if the venue accepts both and the
entry later completes, the two are set to sell 130 against a long of 100; if it
rejects one, the gap stays. Either drop the bracket and attach your own stop per
fill, which costs a request per fill and a gap between each fill and its stop's
acknowledgment, or cancel the remainder after a bounded time and place your own
stop for what filled once the legs are confirmed canceled.
:::

:::more[What each choice in the check costs]

- **Event-driven costs requests.** A stop amended on every partial fill spends
  request budget on every fill. Freqtrade's
  [stop on exchange](https://www.freqtrade.io/en/2026.8/stoploss/) (2026.8,
  released 2026-08-31)
  [polls its stop](https://github.com/freqtrade/freqtrade/blob/2026.8/freqtrade/freqtradebot.py#L1467-L1487)
  once per bot loop (`process_throttle_secs`, at least 5 s apart by default),
  re-creates a stop it finds canceled and falls back to an emergency market
  exit when it cannot place one. Keep a periodic reconciliation for the events
  you never hear about; do not make it the only check.
- **Replacing a stop opens a window.** Cancel-then-new has a moment with no
  stop: a gap. New-then-cancel has a moment with two: an excess, safe only where
  the venue caps close-only orders. Amend in place where the venue supports it,
  and treat the amendment as unknown until it is confirmed.
- **The deadline is a policy.** When it passes with a gap, the bot halts, or
  closes the gap with a reduce-only market order where the venue has one, by a
  policy set in advance; the close shrinks the position, so the flip check runs
  again. Too short, and a slow acknowledgment halts the bot; too long, and the
  position sits uncovered through it. Protection the bot cannot restore is a
  reason to stop trading, not to keep entering
  ([what a halt needs](/blog/trading-bot-crashed-open-position/#how-does-a-halt-survive-the-restart)).
- **Reducing an oversized exit.** Reduce confirmed uncapped exits only, never
  an unknown one: take-profits and other non-stop exits first, both legs of an
  OCO pair, armed stops never below the position. A flip that remains stays a
  finding, under a deadline like the gap's, until the venue caps those exits at
  the position.

:::

Four edge cases decide whether the check is right:

:::more[A fill during a pending cancel]
Run the check on every fill, not only at the cancel's outcome. The entry is over
at its terminal state, and that may be a full fill followed by a rejected cancel
(FIX scenario B.1.c), not a confirmed cancel.
:::

:::more[A stop replacement with an unknown outcome]
Neither the old nor the new stop covers the gap, and both count as executable.
Resolve the outcome by looking the order up, never by sending another
replacement
([what the bot does while the outcome is unknown](/blog/duplicate-orders-after-timeout/#what-should-the-bot-do-while-the-outcome-is-unknown)).
:::

:::more[An oversized stop after a partial close]
The position shrank and the stop did not. With a venue cap it closes no more
than the position; without one, the next trigger sells more than the bot holds.
:::

:::more[An exit left working after the position reaches zero]
A take-profit filled in full and the stop was not canceled, or the stop closed
the position and a take-profit is still resting. The next trigger opens a
position the bot never decided on; the check's first branch, a position of
zero, exists for this.
:::

## Can an exit open a position the other way?

**Yes, unless the venue enforces that it only closes.**

An exit without that cap is an ordinary order: triggered against a smaller position, or none, it executes
its full quantity.

Bybit's
[Close On Trigger help](https://www.bybit.com/en/help-center/article/Close-On-Trigger-Order)
walks the case: long 1 BTC, a take-profit sell and a stop-loss
sell, each for the full position. The take-profit fills; when the price later
falls, the stop triggers and opens a short. With its close-on-trigger option,
the system cuts the triggered order to the current position, or cancels it when
there is no position to close.

:::matrix

| Venue | An exit can be capped at the position |
| --- | --- |
| Bybit | ✓ [1] |
| OKX futures | ✓ [2] |
| Deribit | ✓ [3] |
| Binance USDⓈ-M futures | ✓ [4] |
| Alpaca equities | ? [5] |

1. Close On Trigger and Reduce Only; the help page walks the case above. Bybit
   [Close On Trigger](https://www.bybit.com/en/help-center/article/Close-On-Trigger-Order)
   and [Place Order](https://bybit-exchange.github.io/docs/v5/order/create-order).
2. A reduce-only TP/SL triggered against a smaller position closes what is
   left, and the rest is not executed. OKX
   [TP/SL help](https://www.okx.com/en-gb/help/how-do-i-modify-take-profit-tp-and-stop-loss-sl).
3. A reduce-only flag on the entry and on each exit, off by default: set it on
   every exit. Deribit
   [order API](https://docs.deribit.com/api-reference/trading/private-buy).
4. Reduce-only in One-way Mode; in Hedge Mode, where reduce-only is not
   accepted, only a close-all stop or take-profit, which closes the whole side
   and takes no quantity. What a larger hedge-mode exit with a quantity does
   at trigger is not documented on that endpoint. Binance
   [New Algo Order](https://developers.binance.com/en/docs/catalog/core-trading-derivatives-trading-usd-s-m-futures/api/rest-api/trade#new-algo-order-trade).
5. No execution-time close-only guarantee for equity exits; the protection it
   describes is the OCO cancel, which it says can lose the race. Alpaca
   [order reference](https://docs.alpaca.markets/us/docs/orders-at-alpaca).

:::

Keep this check apart from coverage: the two fail independently. A venue cap
makes an oversized stop safe to trigger; it does nothing for a missing stop,
which still covers zero. So the bot records, per exit, whether the venue
enforces close-only, and reduces the exits it does not.

:::more[Reduce-only, defined]
**Reduce-only** (close-only) is an order flag with which the venue lets the
order reduce the current position and never increase or reverse it. An exit
that matches the position today can reverse it after a partial exit if nothing
caps it.
:::

## What should you verify before you trust your stops?

- For every venue and mode you use, you know when exit legs become active, what
  a partial exit does to them, and what happens to them when the position
  closes; where the documentation is silent, you tested it and know that the
  test is evidence, not a contract.
- The bot counts a stop as armed only after the venue confirms it working and
  triggerable, never on an accepted order ID.
- Coverage is checked per symbol and position side after every execution report,
  order update, acknowledgment or rejection, and reconnect, with a periodic
  reconciliation behind it.
- A gap is recorded and blocks new entries before anything is repaired; while an
  exit's state is unknown, the bot adds only what stays safe if it is live.
- A gap has a deadline, after which the bot halts or closes it by a policy
  written in advance.
- Every exit carries reduce-only or close-on-trigger where the venue has it, and
  the bot knows which of its exits lack it.
- Stops are amended in place where possible, and every amendment is unknown
  until confirmed.
- When the position reaches zero, no exit for it, take-profits included, is
  left working.
- You have run the 30-of-100 case, a rejected stop leg and a partial take-profit
  on a test account, and recorded what the venue did.

## What does each venue document?

**The modes, flags and fields behind everything above, venue by venue.**

:::more[Alpaca]

- **Bracket activation.** The exit legs "won't be active until the first order
  is completely filled"; if any order in the group is canceled, the remaining
  open orders in the group are canceled. The partial-fill-then-cancel case is
  not documented.
- **After a partial exit.** A partially filled take-profit resizes the stop to
  the remaining quantity. One exit filled cancels the other; a close outside
  the bracket is not documented.
- **Both exits can fill.** In fast markets both orders "may fill before the
  cancellation occurs". The bracket reference gives no execution-time
  close-only guarantee for equity exits.
- **Replace.** The `pending_replace` status rejects cancel requests while it
  lasts.
- **Groups.** Since 2026-10-08 orders carry an optional
  [`group_id`](https://docs.alpaca.markets/us/changelog/2026-10-08-order-group-db02af7),
  shared by every order of a bracket, OTO or OCO group, replacements included,
  and a `group_role` that marks each order as `entry`, `take_profit` or
  `stop_loss`, so a bot can match the orders it finds after a reconnect to
  their group.

Source: Alpaca
[order reference](https://docs.alpaca.markets/us/docs/orders-at-alpaca), and
the changelog linked above.
:::

:::more[Deribit]

- **Fill condition.**
  [`private/buy`](https://docs.deribit.com/api-reference/trading/private-buy)
  defines `trigger_fill_condition`: under `first_hit` any execution of the
  primary places all secondary orders, under `complete_fill` only a complete
  execution does, and under `incremental` each fill places a proportional part,
  "rounded down to the contract size".
- **The guide's additions.** The
  [order-management guide](https://docs.deribit.com/articles/order-management-best-practices)
  says that under `complete_fill` a primary that partially fills and is then
  canceled never activates its secondaries, and that under `incremental` the
  part lost to rounding waits for a complete fill. Its example response lists
  secondary order IDs on a primary with nothing filled.
- **Reduce-only.** `private/buy` takes `reduce_only`, "intended to only reduce
  a current position", on the primary and on each secondary in `otoco_config`,
  `false` by default.
- **OCO.** The
  [linked orders announcement](https://insights.deribit.com/education/conditional-orders-have-arrived-on-deribit/)
  (2024-03-13): an OCO order is canceled when the order it is linked to is
  filled or canceled.

:::

:::more[Bybit]

- **Stop attached to the order.**
  [Place Order](https://bybit-exchange.github.io/docs/v5/order/create-order)
  with `tpslMode=Partial` places the stop per fill, for the quantity filled
  (it has no size option); it keeps its quantity after a partial exit and
  executes at most the open position. It is canceled when an entire-position
  stop closes the position; other closes are not documented. See also the
  [TP/SL help](https://www.bybit.com/en/help-center/article/Take-Profit-and-Stop-Loss)
  (updated 2026-10-08).
- **Stop on the position.**
  [Set Trading Stop](https://bybit-exchange.github.io/docs/v5/position/trading-stop)
  with `tpslMode=Full` is the entire-position stop: a conditional order on the
  open position. With `tpslMode=Partial` it takes `tpSize` and `slSize`, which
  must be equal. The page says the system "will cancel these orders if the
  position is closed, and adjust the qty according to the size of the open
  position". The TP/SL modes arrived in the
  [V5 changelog of 2023-05-23](https://bybit-exchange.github.io/docs/changelog/v5#2023-05-23).
- **One-sided amendment.** Amending only the stop or only the take-profit of a
  pair breaks their binding; a cancel by order ID then removes only that side.
- **Close-only.** The
  [Close On Trigger help page](https://www.bybit.com/en/help-center/article/Close-On-Trigger-Order)
  (updated 2026-06-29) walks the long 1 BTC case. Place Order describes
  `closeOnTrigger`, for linear and inverse contracts, as an order that "can
  only reduce your position, not increase it"; `reduceOnly`, for linear,
  inverse and options, says the position "can only reduce in size".

:::

:::more[OKX]
The
[TP/SL help](https://www.okx.com/en-gb/help/how-do-i-modify-take-profit-tp-and-stop-loss-sl)
(updated 2026-09-09) tells futures traders to check the stop quantity again
after a partial take-profit, and documents that a reduce-only futures TP/SL
triggered against a smaller position closes what is left, and the rest is not
executed.
:::

:::more[Binance USDⓈ-M futures]
`reduceOnly` is not accepted in Hedge Mode. The
[New Algo Order](https://developers.binance.com/en/docs/catalog/core-trading-derivatives-trading-usd-s-m-futures/api/rest-api/trade#new-algo-order-trade)
endpoint offers `closePosition=true` on `STOP_MARKET` and
`TAKE_PROFIT_MARKET`: when triggered it closes the whole current long (a sell)
or short (a buy), takes no quantity and cannot be combined with `reduceOnly`.
What a hedge-mode exit larger than its side's position does at trigger is not
documented there; test it.
:::

:::more[FIX]
The
[FIX order-state document](https://www.fixtrading.org/wp-content/uploads/download-manager-files/FIX-Latest-as-of-EP284-Order-State-Changes.pdf)
(November 2023): scenario B.1.b is an execution while a cancel is pending, with
the cumulative quantity kept when the cancel completes; scenario B.1.c is a
full fill followed by a rejected cancel.
:::

## Where is the check in pseudocode?

:::more[The coverage check, one position side]

```text
# Pseudocode. One position side: the only side in one-way mode,
# long and short separately in hedge mode.

check(symbol, side):
    open = abs(confirmed_position(symbol, side))    # from the ledger

    # Exits: every order that closes this side and can still execute -
    # stops, take-profits, triggered children - working or unknown.
    # Exits held behind a working entry count once they activate.
    # An OCO pair counts once, at its larger leg.
    stop_armed   = qty of stops the venue confirms working and triggerable
    stop_unknown = qty of stops in an unknown state: unacknowledged,
                   pending replace, not seen since the reconnect
    exits_all    = qty of all exits, working or unknown
    exits_open   = qty of those exits the venue does not cap at the position
    held         = a working entry holds venue exits for this side

    if open == 0:
        if exits_all > 0:
            cancel every exit counted in exits_all  # take-profits too
        return

    gap_worst = open - stop_armed                   # no unknown stop is live
    gap_sure  = open - stop_armed - stop_unknown    # every unknown stop is live
    room      = open - exits_all                    # every unknown exit is live

    if gap_worst > 0:                               # a finding first
        record it; block new entries on symbol
        look up every unknown exit by order ID
        if held:                                    # a stop would collide with the legs
            cancel the remainder when its bounded time ends;
            once the legs are confirmed canceled, the next check adds the stop
        else:
            add = gap_sure if the venue caps the new stop at the position
                  else min(gap_sure, room)
            if add > 0:
                add a stop for add                  # cannot oversize
        start a deadline; when it passes with a gap:
            halt, or close the gap with a reduce-only market order
            where the venue has one, by a policy set in advance;
            the close shrinks the position, so check flip again

    flip = min(exits_all - open, exits_open)        # can reverse the side
    if flip > 0:                                    # a finding, urgent
        record it; look up every unknown exit by order ID
        recompute flip from what the lookup returns
        reduce confirmed uncapped exits by flip:    # never an unknown one
            take-profits and other non-stop exits first
            both legs of an OCO pair                # Alpaca amends prices only:
                                                    # cancel-replace, a gap
            armed stops never below open
        a flip that remains stays a finding, under a deadline like the
            gap's, until the venue caps those exits at the position
```

With the example above, an unknown stop for 5 makes `gap_worst` 5 and
`gap_sure` 0; an unknown take-profit for 5 instead makes `gap_sure` 5 and
`room` 0, so an uncapped stop waits for the lookup.
:::

## What else do people ask?

:::more[Do bracket orders activate on a partial fill?]
It depends on the venue and mode. Alpaca's bracket exits stay inactive until the
entry fills completely. Deribit lets you choose: the exits can be placed at the
first fill, wait for a complete fill, or be placed a proportional part per fill,
rounded down to the contract size.
:::

:::more[Why was my stop loss canceled after a partial entry?]
Canceling the unfilled remainder of a bracket entry can cancel its exits too.
Alpaca cancels the remaining orders of a group when any of them is canceled, and
Deribit's complete-fill exits never activate if the primary partially fills and
is then canceled. The venue reports the fill and the cancel; that the filled
quantity has no stop shows only in the quantities.
:::

:::more[Do I need to resize my stop loss after a partial take-profit?]
Alpaca resizes its own bracket stop to the remaining quantity, and Bybit adjusts
its entire-position stop to the open position. A stop your bot placed as a
separate order keeps its original size; resize it, or make sure the venue caps
it at the position when it triggers.
:::

:::more[What happens to a reduce-only stop after the position closes?]
On Bybit, a Close On Trigger order with no position to close is canceled when it
triggers, and Set Trading Stop orders are canceled when the position closes.
Where your venue does not document it, assume the order stays working, and
cancel it when the position reaches zero rather than relying on the flag.
:::

:::more[Can a stop loss open an opposite position?]
Yes, if it is not reduce-only or close-on-trigger and it triggers against a
position smaller than its quantity, or none: after a take-profit filled while
the stop was not canceled, or when both fill in a fast market.
:::

## Where should I read next?

- [Why your trading bot's position drifts from the broker's](/blog/trading-bot-position-out-of-sync-with-broker/) -
  the position the coverage check compares against.
- [Duplicate orders after a timeout](/blog/duplicate-orders-after-timeout/) - a
  stop replacement whose outcome is unknown.
- [What happens if my trading bot crashes with a position open?](/blog/trading-bot-crashed-open-position/) -
  halting when protection is lost.
- [Deribit: order management best practices](https://docs.deribit.com/articles/order-management-best-practices) -
  linked orders and their fill conditions.
- [Bybit: Set Trading Stop](https://bybit-exchange.github.io/docs/v5/position/trading-stop) -
  position-level stops that follow the position size.
