Close

[[close]] closes a currently open long or short position. It can also reduce the position size.

For the overall sizing logic and the difference between absolute quantity, equity percentage, and position percentage, see Position sizing, margin, and leverage. This page only describes the [[close]] block semantics.

By default, after queuing the close order, the block validates on the current candle and the next block is evaluated immediately. The wait_candles key only delays this transition; it does not change how orders are executed.

Block declaration

A strategy can contain multiple [[close]] blocks. Each block closes all or part of the current position and can then execute another block.

In [[close]], the order_id key refers to the order_id value of an already open [[entry]] block. It does not refer to the id of that [[entry]] block and cannot target a [[order]] block.

The actual closing order then depends on the close_entries_rule key in the [backtest] block.

Examples

Full close

This example closes the entire position, targets the main_entry entry via order_id, then moves to next_condition.

[[close]]
id            = "close_full"
order_id      = "main_entry"
next_block_id = "next_condition"

Partial close

This example closes 25% of the open position, targets the main_entry entry via order_id, then moves to next_condition.

[[close]]
id            = "stop_partial"
order_id      = "main_entry"
qty_percent   = 25
next_block_id = "next_condition"

Block parameters

ParameterDescription
id
 Text
 Required
Unique identifier for the block.
next_block_id
 Text
 Required
Identifier of the block executed next.
order_id
 Text
 Required
order_id value of the [[entry]] block that the [[close]] block must target. This key does not refer to the id of the [[entry]] block and cannot target a [[order]] block.
qty
 Decimal or Text
Absolute quantity to close. If qty evaluates to a strictly positive value, that quantity is used.
qty_percent
 Decimal or Text
Percentage of the open position to close. This key does not depend on [backtest].default_qty_type.
comment
 Text
 Optional
Populates the Signal column in the trade list when provided; otherwise order_id is used. Also exported as the Pine Script comment. No impact on backtest calculations.
alert_message
 Text
 Optional
This parameter has no effect on backtests. It is only used to set the Pine Script alert_message parameter.
wait_candles
 Integer
 Optional
Number of candles to wait before validating the block and moving to the next one. Use 0 to disable the delay. This delay does not change order execution.
Default value: 0