EngineOrders & Positions

Closing positions

Request exits from real position state and understand reductions, reversals, and close attempts.

Use context.closePositions(position, options) with a real position returned by the session. Despite its plural name, this method takes one position object in the demonstrated API.

The shared close path

The generic implementation finds a matching position by instrument and constructs a market order opposite to the supplied position's side, using its quantity. It applies automation and a close-kind L3 check, removes matching existing orders, and delegates to placement.

That placement can encounter a second L3 check, duplicate/eligibility rules, and fill delay. A close request can therefore pass one check and fail later. See SJS and L3.

Because quantity comes from the supplied snapshot, use current state. Finding a matching instrument does not make an old quantity current.

A close result is not proof of flatness

The shared close method reports whether its delegated placement returned an accepted result. The resulting order can remain pending. Read positions again when execution updates arrive.

Also inspect orders when closing fails: the shared path removes matching orders before delegated placement. Failure later in that sequence does not restore the earlier pending orders automatically.

Reductions and reversals

In the shared simulator, an opposite order smaller than the position can reduce it; equal quantity can close it. An opposite order larger than the position is left unfilled by that branch, rather than closing and opening the remainder on the other side.

A strategy that needs a reversal must account for the selected runtime's behavior. The simple walkthrough closes the existing position and waits for confirmed state before considering another entry. Partial-close accounting in the shared simulator is simplified and should not be treated as broker accounting.

Closing at boundaries

Session rules and end-of-run handling can also attempt closes. After replay, the memory session calls closeAllPositions() before marking the run completed. A delayed final close can have no later observation available to fill it.

Distinguish strategy exits, session-boundary exits, and final close attempts in the result. Stopping a run is not evidence that every position and order has been resolved.

Continue with sizing.

On this page