> ## 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.

# Orders and positions

> The order functions, when and at what price each order fills, how entries, exits and reversals change the position, and the errors orders can raise.

An order call queues an order; the broker emulator fills it on a later price. The account holds one net position (`strategy.position_size`: positive long, negative short) made of open entries, which close oldest first.

## When orders fill

On historical bars the emulator steps through each bar's prices (open, then high and low, then close; high first when the open is nearer the high), filling queued orders at each step. The script runs once, at the close, so an order placed on bar N is first tested on bar N+1:

* A market order fills at the open of bar N+1. With `process_orders_on_close=true`, an order placed on a confirmed bar also fills at that bar's close.
* A limit order fills at the first step at the limit or better (long: at or below). A stop order fills at the first step at the stop or worse (long: at or above). A stop-limit order arms at the stop, then fills by the limit rule.
* The fill price is that step's price, not the limit or stop level: a long limit at 99 on a bar that opens at 98 fills at 98.

## The order calls

| Call | What it does |
| - | - |
| `strategy.entry(id, direction, qty, limit, stop, oca_name, oca_type, comment)` | Opens or adds to a position. `direction` is `strategy.long` or `strategy.short`. `limit` and/or `stop` make it wait; otherwise market. |
| `strategy.order(id, direction, qty, limit, stop, oca_name, oca_type, comment)` | A raw net order: long adds, short subtracts, whatever the position. Can open, add, reduce or reverse; `pyramiding` does not apply. |
| `strategy.exit(id, from_entry, qty, profit, limit, loss, stop, oca_name, comment)` | Take-profit (`limit`) and stop-loss (`stop`) for the open position or for the entry `from_entry`. `profit` and `loss` are distances in ticks (`syminfo.minmove`) from the entry price; given with a price, the nearer level wins. |
| `strategy.close(id, qty, comment)` | Closes the entry named `id` at market, all or `qty`. |
| `strategy.close_all(comment)` | Closes the whole position at market. |
| `strategy.cancel(id)`, `strategy.cancel_all()` | Remove the queued orders with that `id`, or all; nothing happens if there are none. |

`qty` defaults to `default_qty_value`. For `entry` and `order`, a `qty` of zero or less counts as not given; for `close` it means the entry's whole open quantity; for `exit` it stops the script with `Invalid order quantity`, as does a default size that cannot be worked out.

* **Reversal:** `strategy.entry` against the open position closes it and opens the other side in one fill.
* **Pyramiding:** an entry is ignored, without an error, when `pyramiding` entries are already open in its direction.
* **Same id:** `entry` or `order` with an id still queued replaces that order; flipping its direction stops the script (`Entry order direction cannot be changed...`).
* **Exit:** ignored when the position is flat at the call, or when no `limit`, `stop`, `profit` or `loss` is given, so call it on bars where you are already in the trade, not on the entry bar. It queues two orders and a fill removes the other; a new call with the same `id` replaces both. `qty` defaults to the whole open quantity.
* **OCA:** `oca_name` works only with `oca_type`: `strategy.oca_cancel` removes the group's other orders on a fill, `strategy.oca_reduce` reduces their quantity.
* **Direction** is a plain string: `"buy"` compiles, then stops the script: `invalid strategy direction "buy"`.

> [!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("Breakout with exit", overlay=true)
hh = talib.highest(high, 20)
if close > hh[1] and strategy.position_size == 0 {
    strategy.entry("Long", strategy.long)
}
if strategy.position_size > 0 {
    strategy.exit("Exit", from_entry="Long", stop=strategy.position_avg_price * 0.97, limit=strategy.position_avg_price * 1.06)
}
plot(hh, title="20-bar high")
```


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