Trade is created when an Order is filled. Access active trades through Strategy.trades and settled (closed) trades through Strategy.closed_trades.
Properties
Entry and exit
Trade size 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.Candlestick bar index at the time of entry.
Candlestick bar index at the time of exit.
None if the trade is still active.Datetime (or integer period) of the bar at entry. The type matches the index type of the data passed to
Backtest.Datetime (or integer period) of the bar at exit.
None if the trade is still active.Profit and loss
Profit (positive) or loss (negative) in cash units.For active trades, this is the current unrealized P&L based on the last close price. Commissions are reflected only once the trade is closed.
P&L as a percentage relative to the entry price, adjusted for commissions after close.
Total trade value in cash:
abs(size) × current price.Direction
True if the trade is long (i.e. size > 0).True if the trade is short (i.e. size < 0).SL/TP management
Stop-loss price for this trade. Writable.
- Assigning a price creates or replaces the existing contingent stop-market order.
- Assigning
Nonecancels the current SL order.
For long trades,
sl must be below the current price. For short trades, it must be above.Take-profit price for this trade. Writable.
- Assigning a price creates or replaces the existing contingent limit order.
- Assigning
Nonecancels the current TP order.
Tracking
Arbitrary value inherited from the See also
Order that opened this trade. Use for grouping, conditional logic, or post-run analysis.Order.tag.Methods
Trade.close()
portion of this trade at the next available price.
Fraction of the trade to close. Must satisfy
0 < portion <= 1.1.0(default) closes the entire trade.0.5closes half the position; the remaining half stays open.