Order, Trade, and Position. Understanding them lets you write precise entry, exit, and risk management logic.
Lifecycle overview
Place an order
Call
self.buy() or self.sell() in next(). This creates an Order object and queues it for execution.Order fills
On a subsequent bar, the broker evaluates market conditions. If the order’s price conditions are met, it fills and becomes a
Trade. Contingent SL/TP orders are created automatically.Manage the trade
The
Trade object is available in self.trades. Adjust stop-loss and take-profit in real time, or close the trade manually.Order
AnOrder is created by self.buy() or self.sell() and waits in the broker’s queue until its price conditions are met or it is canceled. All orders are Good Till Canceled — they remain active until they fill or you explicitly cancel them.
Properties
Order size (negative for short orders). If between 0 and 1, it is a fraction of available equity. If 1 or greater, it is an absolute number of units.
Limit price.
None for market orders. A long limit order fills when the price falls to or below this value; a short limit order fills when the price rises to or above it.Stop price that activates the order. When hit, the order becomes a market order (stop-market) or a limit order (stop-limit, if
limit is also set). None once the stop has been triggered.Stop-loss price. After this order fills, a contingent stop-market order is automatically placed at this price to close the resulting trade.
Take-profit price. After this order fills, a contingent limit order is automatically placed at this price to close the resulting trade.
Arbitrary tracking value inherited by the resulting
Trade.True if the order size is positive (a buy order).True if the order size is negative (a sell order).True for OCO bracket orders (SL/TP) placed automatically upon a trade. These orders are canceled when their parent trade closes.Methods
order.cancel() — Remove the order from the queue. Also cancels contingent SL/TP bracket if applicable.
Order types
- Market order
- Limit order
- Stop-market order
- Stop-limit order
No
limit or stop set. Fills on the next bar’s open (or current bar’s close if trade_on_close=True).For long orders, price ordering must hold:
SL < limit/stop/market_price < TP. For short orders: TP < limit/stop/market_price < SL. Violations raise a ValueError.Trade
ATrade is created when an Order fills. Active trades are available in self.trades; settled trades are in self.closed_trades.
Properties
Trade volume in units. Negative for short trades.
Price at which the trade was opened.
Price at which the trade was closed.
None if the trade is still active.Integer bar index when the trade was entered.
Integer bar index when the trade was exited.
None if still active.Timestamp (or bar integer) of trade entry.
Timestamp (or bar integer) of trade exit.
None if still active.Profit (positive) or loss (negative) in cash units. Commissions are included only after the trade is closed.
Profit or loss as a percentage of the entry price, net of commissions.
Total trade value in cash:
abs(size) × current_price.True if the trade is long.True if the trade is short.Current stop-loss price. Writable — assign a new value to move the stop, or assign
None to cancel it.Current take-profit price. Writable — assign a new value to move the target, or assign
None to cancel it.Arbitrary value inherited from the order that opened this trade.
Methods
trade.close(portion=1.) — Place a market order to close portion of the trade at the next available price. portion must be between 0 and 1.
Managing SL and TP
SL and TP are stored as contingent orders on the trade. You can read and modify them at any time innext():
Assigning
trade.sl or trade.tp creates or replaces the existing contingent bracket order. Assigning None cancels it. The underlying Order is managed automatically.Tagging trades
Tags let you track orders and trades through their lifecycle:Position
self.position is the aggregate of all currently active trades. It can be used in boolean context: if self.position: is True when any trade is open.
Properties
Net position size in units, summed across all active trades. Positive for net long, negative for net short, zero for flat.
Aggregate unrealized profit or loss in cash across all active trades.
Aggregate unrealized P&L as a percentage of total invested capital.
True if net position size is positive.True if net position size is negative.Methods
position.close(portion=1.) — Close portion of every active trade. Equivalent to calling trade.close(portion) on each trade in self.trades.
FIFO position closing
By default (hedging=False), placing a trade in the opposite direction automatically closes existing trades in FIFO (First In, First Out) order before opening the new trade.
Exclusive orders mode
Withexclusive_orders=True, each new call to self.buy() or self.sell() automatically:
- Cancels all pending non-contingent orders.
- Closes all open trades.
- Opens the new trade.
Good-Till-Canceled semantics
All orders placed viaself.buy() and self.sell() are Good Till Canceled. They remain in the queue across multiple bars until one of the following occurs:
- The price condition is met and the order fills.
- You call
order.cancel()explicitly. - A new order is placed with
exclusive_orders=True, which cancels all pending non-contingent orders. - The broker cancels the order due to insufficient margin (a warning is emitted).