> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# strategy functions

> Reference for the 29 functions of the strategy namespace: strategy.cancel, strategy.cancel_all, strategy.close, strategy.close_all and 25 more, each with its definition and an example.

> [!NOTE]
>
> Language version: 2 · Reference version: 2.7 · Date: 2026-10-25 · Compiler build: df7b3b14 · Namespace: strategy · Entries: 30

## `strategy` (library)

**Definition**

Functions, variables and constants for strategies: scripts declared with `strategy()` that place simulated orders. Orders are placed with `strategy.entry`, `strategy.order`, `strategy.exit`, `strategy.close` and `strategy.close_all`, and cancelled with `strategy.cancel` and `strategy.cancel_all`.

Performance is read through variables such as `strategy.netprofit`, `strategy.equity` and `strategy.position_size`, and trade by trade through the `strategy.closedtrades_*` and `strategy.opentrades_*` functions. Constants such as `strategy.long`, `strategy.fixed` and `strategy.commission_percent` configure orders and the strategy itself.

**Example**

> [!WARNING]
>
> GoCharting is a charting and analytics tool. This is not investment advice. Trading and investing carry a high risk of loss and are not suitable for everyone. You can lose some or all of your money.

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy Library Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
fast = talib.ema(close, 9)
slow = talib.ema(close, 21)
if talib.crossover(fast, slow) {
    strategy.entry("Long", strategy.long)
}
if strategy.position_size > 0 {
    strategy.exit("Exit", from_entry="Long", limit=strategy.position_avg_price * 1.03, stop=strategy.position_avg_price * 0.98)
}
plot(fast, title="Fast EMA", color=color.green)
plot(slow, title="Slow EMA", color=color.red)
```

## `strategy.cancel` (function)

**Definition**

Cancels the pending order named `id`: a limit or stop order that has not filled yet.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.cancel(series string id) → void
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `id` | `series string` | yes | The `id` of the pending order to cancel. |

**Returns**

`void`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.cancel Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
// A buy limit 1% below the close, cancelled if not filled within the bar
if close > open {
    strategy.entry("Dip Buy", strategy.long, limit=close * 0.99)
} else {
    strategy.cancel("Dip Buy")
}
```

## `strategy.cancel_all` (function)

**Definition**

Cancels every pending order that has not filled yet.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.cancel_all() → void
```

**Returns**

`void`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.cancel_all Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if close > open {
    strategy.entry("Breakout", strategy.long, stop=high * 1.01)
}
// Cancel all waiting orders when volume dries up
if volume < talib.sma(volume, 20) * 0.5 {
    strategy.cancel_all()
}
```

## `strategy.close` (function)

**Definition**

Closes the position opened by the entry named `id` with a market order. `qty` closes only part of it; `comment` labels the closing order.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.close(series string id, series float qty, series string comment) → void
```

**Parameters**

| Name | Type | Required | Description | Default |
| - | - | - | - | - |
| `id` | `series string` | yes | The `id` of the entry whose position is closed. | |
| `qty` | `series float` | no | The quantity to close. If given, only part of the position is closed. A `qty` of zero or less, or `na`, closes the whole open quantity of the entry. | `na` |
| `comment` | `series string` | no | Comment that labels the closing order. | `""` |

**Returns**

`void`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.close Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
// Close after the position has been open for 10 bars
if strategy.opentrades > 0 and bar_index - strategy.opentrades_entry_bar_index(0) >= 10 {
    strategy.close("Long", comment="Time exit")
}
```

## `strategy.close_all` (function)

**Definition**

Closes the whole open position, whatever entries it came from, with a market order.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.close_all(series string comment) → void
```

**Parameters**

| Name | Type | Required | Description | Default |
| - | - | - | - | - |
| `comment` | `series string` | no | Comment that labels the closing order. | `""` |

**Returns**

`void`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.close_all Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
// Flatten everything when a new session starts
if session.isfirst and strategy.position_size != 0 {
    strategy.close_all(comment="New session")
}
```

## `strategy.closedtrades_commission` (function)

**Definition**

For the closed trade number `trade_num`, returns the commission paid on the trade, in the account currency.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_commission(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_commission Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_commission(strategy.closedtrades - 1) : na
plot(value, title="Commission", color=color.blue)
```

## `strategy.closedtrades_entry_bar_index` (function)

**Definition**

For the closed trade number `trade_num`, returns the bar index of the bar on which the entry order was placed (where `strategy.entry` or `strategy.order` ran), not the bar on which it filled; the two differ when the order fills on a later bar.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`. The result is `na` for a number that is not a closed trade, and for a trade that has dropped out of the 1000 most recent closed trades kept.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_entry_bar_index(series int trade_num) → series int
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series int`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_entry_bar_index Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_entry_bar_index(strategy.closedtrades - 1) : na
plot(value, title="Entry Bar Index", color=color.blue)
```

## `strategy.closedtrades_entry_comment` (function)

**Definition**

For the closed trade number `trade_num`, returns the comment of the entry order that opened the trade.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_entry_comment(series int trade_num) → series string
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series string`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_entry_comment Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
if barstate.islast and strategy.closedtrades > 0 {
    label.new(bar_index, high, text="Last closed trade: " + strategy.closedtrades_entry_comment(strategy.closedtrades - 1))
}
```

## `strategy.closedtrades_entry_id` (function)

**Definition**

For the closed trade number `trade_num`, returns the `id` of the entry order that opened the trade.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_entry_id(series int trade_num) → series string
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series string`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_entry_id Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
if barstate.islast and strategy.closedtrades > 0 {
    label.new(bar_index, high, text="Last closed trade: " + strategy.closedtrades_entry_id(strategy.closedtrades - 1))
}
```

## `strategy.closedtrades_entry_price` (function)

**Definition**

For the closed trade number `trade_num`, returns the price at which the trade was entered.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_entry_price(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_entry_price Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_entry_price(strategy.closedtrades - 1) : na
plot(value, title="Entry Price", color=color.blue)
```

## `strategy.closedtrades_entry_time` (function)

**Definition**

For the closed trade number `trade_num`, returns the UNIX time, in milliseconds, at which the trade was entered.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_entry_time(series int trade_num) → series int
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series int`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_entry_time Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_entry_time(strategy.closedtrades - 1) : na
plot(value, title="Entry Time", color=color.blue)
```

## `strategy.closedtrades_exit_bar_index` (function)

**Definition**

For the closed trade number `trade_num`, returns the bar index at which the trade was closed. In this build it is always 0: the broker does not record the exit bar. `strategy.closedtrades_exit_time` holds the time of the exit.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`. The result is `na` for a number that is not a closed trade, and for a trade that has dropped out of the 1000 most recent closed trades kept.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_exit_bar_index(series int trade_num) → series int
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series int`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_exit_bar_index Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_exit_bar_index(strategy.closedtrades - 1) : na
plot(value, title="Exit Bar Index", color=color.blue)
```

## `strategy.closedtrades_exit_price` (function)

**Definition**

For the closed trade number `trade_num`, returns the price at which the trade was closed.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_exit_price(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_exit_price Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_exit_price(strategy.closedtrades - 1) : na
plot(value, title="Exit Price", color=color.blue)
```

## `strategy.closedtrades_exit_time` (function)

**Definition**

For the closed trade number `trade_num`, returns the UNIX time, in milliseconds, at which the trade was closed.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_exit_time(series int trade_num) → series int
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series int`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_exit_time Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_exit_time(strategy.closedtrades - 1) : na
plot(value, title="Exit Time", color=color.blue)
```

## `strategy.closedtrades_max_drawdown` (function)

**Definition**

For the closed trade number `trade_num`, returns the largest loss the trade showed while it was open, in the account currency. In this build it is always 0 for a closed trade: the figure measured while the trade was open is not carried into the closed-trade record. `strategy.opentrades_max_drawdown` and `strategy.opentrades_max_runup` are measured.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`. The result is `na` for a number that is not a closed trade, and for a trade that has dropped out of the 1000 most recent closed trades kept.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_max_drawdown(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_max_drawdown Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_max_drawdown(strategy.closedtrades - 1) : na
plot(value, title="Max Drawdown", color=color.blue)
```

## `strategy.closedtrades_max_runup` (function)

**Definition**

For the closed trade number `trade_num`, returns the largest profit the trade showed while it was open, in the account currency. In this build it is always 0 for a closed trade: the figure measured while the trade was open is not carried into the closed-trade record. `strategy.opentrades_max_drawdown` and `strategy.opentrades_max_runup` are measured.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`. The result is `na` for a number that is not a closed trade, and for a trade that has dropped out of the 1000 most recent closed trades kept.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_max_runup(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_max_runup Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_max_runup(strategy.closedtrades - 1) : na
plot(value, title="Max Runup", color=color.blue)
```

## `strategy.closedtrades_profit` (function)

**Definition**

For the closed trade number `trade_num`, returns the profit of the trade, in the account currency; a loss is negative.

Trades are numbered from 0 in the order they were closed, so the most recent is `strategy.closedtrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_profit(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_profit Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_profit(strategy.closedtrades - 1) : na
plot(value, title="Profit", color=color.blue)
```

## `strategy.closedtrades_size` (function)

**Definition**

For the closed trade number `trade_num`, returns the size of the trade. The size is never negative: a short trade has a positive size, because the direction is stored apart from the quantity.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.closedtrades_size(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the closed trade. Trades are numbered from 0 in the order they were closed. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.closedtrades_size Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.closedtrades > 0 ? strategy.closedtrades_size(strategy.closedtrades - 1) : na
plot(value, title="Size", color=color.blue)
```

## `strategy.entry` (function)

**Definition**

Opens a position, or adds to one, in `direction` (`strategy.long` or `strategy.short`). An entry in the opposite direction to the current position first closes that position, then reverses it.

`id` names the entry so that `strategy.close` and `strategy.exit` can refer to it. Without `limit` or `stop` it is a market order; with `limit` or `stop` it waits for that price. `qty` overrides the default quantity set in `strategy()`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.entry(series string id, series string direction, series float qty, series float limit, series float stop, series string oca_name, series string oca_type, series string comment) → void
```

**Parameters**

| Name | Type | Required | Description | Values | Default |
| - | - | - | - | - | - |
| `id` | `series string` | yes | Name of the entry. `strategy.close` and `strategy.exit` refer to the entry by this name. | | |
| `direction` | `series string` | yes | The direction of the entry: `strategy.long` or `strategy.short`. | `strategy.long`, `strategy.short` | |
| `qty` | `series float` | no | The quantity of the order. It overrides the default quantity set in `strategy()`. A `qty` of zero or less, or `na`, is ignored and the default quantity is used. With `default_qty_type` set to "cash" or "percent\_of\_equity", a market order has no price and stops the script with the error "Invalid order quantity"; give the order a `limit` or `stop`, or a `qty` above zero. | | `na` |
| `limit` | `series float` | no | The limit price of the order. The order waits for that price. | | `na` |
| `stop` | `series float` | no | The stop price of the order. The order waits for that price. | | `na` |
| `oca_name` | `series string` | no | Name of the one-cancels-all group the order belongs to. | | `""` |
| `oca_type` | `series string` | no | The one-cancels-all type: `strategy.oca_none`, `strategy.oca_cancel` or `strategy.oca_reduce`. | `strategy.oca_cancel`, `strategy.oca_none`, `strategy.oca_reduce` | `strategy.oca_none` |
| `comment` | `series string` | no | Comment of the entry order. | | `""` |

**Returns**

`void`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.entry Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.entry("Short", strategy.short)
}
```

## `strategy.exit` (function)

**Definition**

Places exit orders for an open position: a take-profit, a stop-loss, or both. `from_entry` names the entry to exit; leave it out to exit all entries.

Prices can be given as levels (`limit` for the take-profit, `stop` for the stop-loss) or as distances in ticks from the entry price (`profit` and `loss`). When both kinds are given for the same side, the one that would fill first is used.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.exit(series string id, series string from_entry, series float qty, series float profit, series float limit, series float loss, series float stop, series string oca_name, series string comment) → void
```

**Parameters**

| Name | Type | Required | Description | Default |
| - | - | - | - | - |
| `id` | `series string` | yes | Name of the exit order. | |
| `from_entry` | `series string` | no | Name of the entry to exit. If left out, all entries are exited. | `""` |
| `qty` | `series float` | no | The quantity to exit. Left out, the whole open quantity of `from_entry` (or of the position) is exited. With `default_qty_type` set to "cash" or "percent\_of\_equity", leaving it out stops the script with the error "Invalid order quantity": pass `qty`. A `qty` of zero or less stops the script with the same error. | `na` |
| `profit` | `series float` | no | The take-profit as a distance in ticks from the entry price. A tick is `syminfo.minmove`. | `na` |
| `limit` | `series float` | no | The take-profit as a price level. | `na` |
| `loss` | `series float` | no | The stop-loss as a distance in ticks from the entry price. A tick is `syminfo.minmove`. | `na` |
| `stop` | `series float` | no | The stop-loss as a price level. | `na` |
| `oca_name` | `series string` | no | Name of the one-cancels-all group the order belongs to. | `""` |
| `comment` | `series string` | no | Comment of the exit order. | `""` |

**Returns**

`void`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.exit Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
// Take profit 2% above and stop 1% below the average entry price
if strategy.position_size > 0 {
    strategy.exit("Exit Long", from_entry="Long", limit=strategy.position_avg_price * 1.02, stop=strategy.position_avg_price * 0.99)
}
```

## `strategy.opentrades_entry_bar_index` (function)

**Definition**

For the open trade number `trade_num`, returns the bar index of the bar on which the entry order was placed (where `strategy.entry` or `strategy.order` ran), not the bar on which it filled; the two differ when the order fills on a later bar.

Trades are numbered from 0 in the order they were opened, so the most recent is `strategy.opentrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_entry_bar_index(series int trade_num) → series int
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series int`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_entry_bar_index Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.opentrades > 0 ? strategy.opentrades_entry_bar_index(strategy.opentrades - 1) : na
plot(value, title="Entry Bar Index", color=color.blue)
```

## `strategy.opentrades_entry_comment` (function)

**Definition**

For the open trade number `trade_num`, returns the comment of the entry order that opened the trade.

Trades are numbered from 0 in the order they were opened, so the most recent is `strategy.opentrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_entry_comment(series int trade_num) → series string
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series string`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_entry_comment Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
if barstate.islast and strategy.opentrades > 0 {
    label.new(bar_index, high, text="Latest open trade: " + strategy.opentrades_entry_comment(strategy.opentrades - 1))
}
```

## `strategy.opentrades_entry_id` (function)

**Definition**

For the open trade number `trade_num`, returns the `id` of the entry order that opened the trade.

Trades are numbered from 0 in the order they were opened, so the most recent is `strategy.opentrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_entry_id(series int trade_num) → series string
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series string`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_entry_id Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
if barstate.islast and strategy.opentrades > 0 {
    label.new(bar_index, high, text="Latest open trade: " + strategy.opentrades_entry_id(strategy.opentrades - 1))
}
```

## `strategy.opentrades_entry_price` (function)

**Definition**

For the open trade number `trade_num`, returns the price at which the trade was entered.

Trades are numbered from 0 in the order they were opened, so the most recent is `strategy.opentrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_entry_price(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_entry_price Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.opentrades > 0 ? strategy.opentrades_entry_price(strategy.opentrades - 1) : na
plot(value, title="Entry Price", color=color.blue)
```

## `strategy.opentrades_entry_time` (function)

**Definition**

For the open trade number `trade_num`, returns the UNIX time, in milliseconds, at which the trade was entered.

Trades are numbered from 0 in the order they were opened, so the most recent is `strategy.opentrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_entry_time(series int trade_num) → series int
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series int`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_entry_time Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.opentrades > 0 ? strategy.opentrades_entry_time(strategy.opentrades - 1) : na
plot(value, title="Entry Time", color=color.blue)
```

## `strategy.opentrades_max_drawdown` (function)

**Definition**

For the open trade number `trade_num`, returns the largest fall of the trade's profit from its highest profit so far, in the account currency. The highest profit starts at 0, so for a trade that has never been in profit this is its largest loss.

Trades are numbered from 0 in the order they were opened, so the most recent is `strategy.opentrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_max_drawdown(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_max_drawdown Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.opentrades > 0 ? strategy.opentrades_max_drawdown(strategy.opentrades - 1) : na
plot(value, title="Max Drawdown", color=color.blue)
```

## `strategy.opentrades_max_runup` (function)

**Definition**

For the open trade number `trade_num`, returns the highest profit the trade has shown so far, in the account currency (0 until the trade is in profit).

Trades are numbered from 0 in the order they were opened, so the most recent is `strategy.opentrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_max_runup(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_max_runup Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.opentrades > 0 ? strategy.opentrades_max_runup(strategy.opentrades - 1) : na
plot(value, title="Max Runup", color=color.blue)
```

## `strategy.opentrades_profit` (function)

**Definition**

For the open trade number `trade_num`, returns the current, unrealised profit of the trade, in the account currency; a loss is negative.

Trades are numbered from 0 in the order they were opened, so the most recent is `strategy.opentrades - 1`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_profit(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_profit Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.opentrades > 0 ? strategy.opentrades_profit(strategy.opentrades - 1) : na
plot(value, title="Profit", color=color.blue)
```

## `strategy.opentrades_size` (function)

**Definition**

For the open trade number `trade_num`, returns the size of the trade. The size is never negative: a short trade has a positive size, because the direction is stored apart from the quantity.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.opentrades_size(series int trade_num) → series float
```

**Parameters**

| Name | Type | Required | Description |
| - | - | - | - |
| `trade_num` | `series int` | yes | Number of the open trade. Trades are numbered from 0 in the order they were opened. |

**Returns**

`series float`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.opentrades_size Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
if talib.crossover(close, talib.sma(close, 20)) {
    strategy.entry("Long", strategy.long)
}
if talib.crossunder(close, talib.sma(close, 20)) {
    strategy.close("Long")
}
value = strategy.opentrades > 0 ? strategy.opentrades_size(strategy.opentrades - 1) : na
plot(value, title="Size", color=color.blue)
```

## `strategy.order` (function)

**Definition**

Places an order without any position logic: a buy adds to the position and a sell subtracts from it, whatever the current position is. Unlike `strategy.entry`, it does not reverse a position automatically and it is not limited by `pyramiding`.

**Syntax**

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
strategy.order(series string id, series string direction, series float qty, series float limit, series float stop, series string oca_name, series string oca_type, series string comment) → void
```

**Parameters**

| Name | Type | Required | Description | Values | Default |
| - | - | - | - | - | - |
| `id` | `series string` | yes | Name of the order. | | |
| `direction` | `series string` | yes | The direction of the order: `strategy.long` (a buy) or `strategy.short` (a sell). | `strategy.long`, `strategy.short` | |
| `qty` | `series float` | no | The quantity of the order. A `qty` of zero or less, or `na`, is ignored and the default quantity is used. With `default_qty_type` set to "cash" or "percent\_of\_equity" and no `qty` above zero, a market order has no price and stops the script with the error "Invalid order quantity"; give the order a `limit` or `stop`, or a `qty` above zero. | | `na` |
| `limit` | `series float` | no | The limit price of the order. The order waits for that price. | | `na` |
| `stop` | `series float` | no | The stop price of the order. The order waits for that price. | | `na` |
| `oca_name` | `series string` | no | Name of the one-cancels-all group the order belongs to. | | `""` |
| `oca_type` | `series string` | no | The one-cancels-all type: `strategy.oca_none`, `strategy.oca_cancel` or `strategy.oca_reduce`. | `strategy.oca_cancel`, `strategy.oca_none`, `strategy.oca_reduce` | `strategy.oca_none` |
| `comment` | `series string` | no | Comment of the order. | | `""` |

**Returns**

`void`

**Example**

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
strategy("strategy.order Example", overlay=true, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1)
// Buy one unit on every up bar, sell one on every down bar
if close > open {
    strategy.order("Buy", strategy.long, qty=1)
} else if close < open {
    strategy.order("Sell", strategy.short, qty=1)
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.