# Orders

> Market, limit, stop, close, modify, cancel.

Source: https://algobarsx.com/docs/orders/

```algobarsx
when was(trend_up, within: 5 bars) and held(close > open, for: 3 bars) every bar:
    buy limit: lowest(low, 5), size: 1 lot, expires: 10 bars
    buy stop: high + 2 pips, risk: 0.5%, stop_loss: low - 2 pips, target: 2R, ghost: true
    sell market: GBPUSD, size: 0.3 lots

when history.today.pnl < -(2% of account.balance) or account.margin_level < 150%: close_all

for trade in trades.open(tag: "breakout"):
    if trade.r >= 3 and rsi(close, 14) > 75:
        close trade, size: 50%

for trade in trades.open(side: long):
    modify trade, stop: trade.entry_price

cancel orders.pending(tag: "grid")
```

- [`buy`](https://algobarsx.com/docs/ref-cmd-orders/#ref-buy) and [`sell`](https://algobarsx.com/docs/ref-cmd-orders/#ref-sell) with no `limit:` or entry `stop:` are market orders. They fill at the next bar's open ([E1](https://algobarsx.com/docs/rules-timing/#E1)).
- `buy limit: price` places a pending limit order. `buy stop: price` places a pending stop order, and its protective stop is then `stop_loss:`.
- `expires:` cancels a pending order after a number of bars or a length of time ([E17](https://algobarsx.com/docs/rules-trade-management/#E17)).
- `ghost: true` keeps the stop and target off the broker. AlgoBars enforces them instead.
- `tag:` labels an order and its trade, so you can close, cancel or report on a group.
- [`close`](https://algobarsx.com/docs/ref-var-bars/#ref-close), [`close_all`](https://algobarsx.com/docs/ref-cmd-orders/#ref-close-all), [`modify`](https://algobarsx.com/docs/ref-cmd-orders/#ref-modify) and [`cancel`](https://algobarsx.com/docs/ref-cmd-orders/#ref-cancel) act on open trades and pending orders.

What happens when a signal arrives opposite an open trade is the header setting `opposite`: `ignore` (the default), `close`, `reverse` or `hedge` ([E18](https://algobarsx.com/docs/rules-strategy-controls/#E18)).
