# Trade management

> Breakeven, partial closes, trailing stops and exits.

Source: https://algobarsx.com/docs/trade-management/

Management is written under the order it belongs to, as an indented block after the order line.

```algobarsx
when liquidity_grab.completed from 08:00 to 11:00 Europe/London:
    buy risk: 1%, stop: liquidity_grab.sweep.low - 3 points, target: 2R + 5 pips, tag: "breakout":
        breakeven at: 1R, offset: 2 pips
        partial 30% at: 1.5R
        partial 30% at: 2.5R
        trail by: atr(14), after: 2R
        exit after: 48 bars
        exit when: crosses_below(close, ema(close, 20))
```

| Line | What it does | Rule |
| --- | --- | --- |
| [`breakeven`](https://algobarsx.com/docs/ref-cmd-management/#ref-breakeven) | Moves the stop to the entry price, plus an optional offset, once a level is reached. | [E13](https://algobarsx.com/docs/rules-trade-management/#E13) |
| [`partial`](https://algobarsx.com/docs/ref-cmd-management/#ref-partial) | Closes part of the original size at a level. Each partial fires once. | [E15](https://algobarsx.com/docs/rules-trade-management/#E15) |
| [`trail`](https://algobarsx.com/docs/ref-cmd-management/#ref-trail) | Follows the best price reached by a distance, after a level. It only ever tightens. | [E14](https://algobarsx.com/docs/rules-trade-management/#E14) |
| [`exit`](https://algobarsx.com/docs/ref-events/#ref-exit) | Closes after a number of bars, a length of time, or when a condition is true. | [E16](https://algobarsx.com/docs/rules-trade-management/#E16) |

You can also manage trades from anywhere with a loop over `trades.open(...)`, using [`close`](https://algobarsx.com/docs/ref-var-bars/#ref-close) and [`modify`](https://algobarsx.com/docs/ref-cmd-orders/#ref-modify). Example: [Active Trade Manager](https://algobarsx.com/docs/ex-47-active-trade-manager/).

## Rules about a trade that is still open

An open trade knows how long it has been open and where it stands. `trades.last()` is the trade being held when there is one, `bars_open` counts its bars, and `r` is its result so far measured against its own risk.

```algobarsx
when close > open:
    buy risk: 1%, stop: 50 pips, target: 4R

when trades.last().bars_open > 3:
    close_all
```
