Skip to content

Open and close positions

Use position-aware commands for a target quantity, and close() to exit the current position.

Request a target

long(10)
short(10)
close()

long(10) requests a target of 10 units. If already at that target, it does not add another 10 each bar. A different target can increase, reduce or reverse the current position. short(10) requests a negative target; close() requests a flat position.

Choose one sizing argument

Argument Example Meaning
Quantity long(10) Target 10 units.
Equity fraction long(size_pct=0.05) Target a 5% equity allocation.
Money amount long(cash=1000) Target that money amount.
Planned stop risk long(risk_pct=0.01, stop=0.05) Calculate quantity from a 1% risk budget and 5% stop.

Choose exactly one of quantity, size_pct, cash or risk_pct. Fractions are used here: 0.05 means 5%, not 5.

Raw orders behave differently

buy(10) and sell(10) submit raw quantities. Repeated calls can stack exposure; they do not target a final holding. Start with long, short and close unless you deliberately need raw-order behaviour.

Add protection

long(10, stop=0.02, target=0.04)

These request market entries with protective exit settings. They are not resting limit entries. Capacity, instrument limits, fills and protection availability still apply. Read stops and targets, or consult the order reference.

Work through target quantities

Suppose the current holding is long six units:

Request Desired result
long(10) Long ten: an increase of four before execution.
long(4) Long four: a reduction of two.
short(3) Short three: a reversal toward a negative target.
close() Flat: close the actual holding.
close(2) Close two units of the current holding.

A target request and confirmed execution are separate. Returned order-submission status does not prove a broker fill. Partial closing is a quantity action, not size_pct allocation syntax.

Read the money arguments

With an equity basis of 10,000 and price 100, size_pct=0.05 desires approximately 500 of exposure, or five units before constraints. cash=500 expresses that money amount directly. risk_pct=0.01, stop=0.05 starts from a planned 100 loss budget and a five-per-unit stop distance, giving a desired twenty units before admission.

Use exactly one sizing argument; quantities, fractions, money and planned risk are different units. Instrument conversion, rounding and capacity can affect admissibility. symbol= selects an explicit instrument; omitted symbol uses the current dispatch instrument.

Choose target orders or raw orders deliberately

buy(10) while long six submits ten more units; it does not request a final holding of ten. A recurring raw order can therefore accumulate exposure. Use a state guard and understand the order lifecycle before using raw buy/sell.

Protection arguments include stop, target, trail, absolute stop_price/target_price, and hold in bars. A price level and a percentage fraction are alternatives, not interchangeable numbers. See protection and the argument reference.

Understand simulated fills

The default local backtest fills market requests at the instrument's next available open. If A signals at 100, the next event contains only B, and A's next open is 120, A fills at 120, not its old 100 quote. A composite needs current quotes for every leg in the same event. Orders without another usable event remain unfilled; a cached valuation is not an executable quote.

Configured slippage uses that executable price, not the signal bar's old close. For example, a buy after a close of 100 with the next open at 200 and 10 basis points of slippage fills at 200.20. It does not fill at 100.10. Commission is included when checking entry capacity: cash of 100 cannot fund a 100 purchase plus a fee of 1. Explicitly setting commission_model=None disables commission; a broken configured model is an error, not permission to charge zero costs.

A simulated group commits only when every leg has an executable quote and satisfies its limit/stop condition in the same observed event. A buy limit of 50 cannot fill while that instrument's range is 90 to 110, even when its other leg is executable. A triggered stop-limit remains active while the group waits, including across checkpoints. Each leg receives the configured slippage and commission. This models an atomic OHLC simulation, not simultaneous venue liquidity.

Re-submitting a terminal order object does not buy or sell again. Repeating an already queued order ID also does not create another fill. New raw orders still stack exposure as described above; idempotence is not target-position sizing. Duplicate-ID retention is bounded by the simulator's submission history; a host needing durable broker replay protection must use its identity-bearing fill ledger.

For advanced Python Order users, a stop-limit retains its activation. A buy with stop 105 and limit 103 can activate on a 106 open without filling. If a later bar opens at 102, the active limit fills at 102. Sell orders mirror this rule. Checkpoint restarts retain activation. OHLC bars still cannot establish every intrabar sequence; use finer data when stop and limit touch in one bar.

The local columnar simulator supports unit-multiplier accounting only. An option or future with contract_size=100 is rejected in both cash and margin accounts, not treated as one underlying unit. Forex lot sizing resolves to native units; it does not require a non-unit order multiplier. Before 4.10.0, cash accounts could accept these unsupported contracts and report incorrect cash/P&L. Do not compare those results as valid derivative backtests.

Rerun older affected backtests: corrected prices, fees and capacity can change their order trace and P&L. Python authoring and TradeScript language version 1 remain supported; the correction does not make a screening engine a full simulator.