# Order commands reference

> All 6 order commands in AlgoBarsX, each with its parameters, defaults, ranges and an example: buy, sell, close, close_all, modify, cancel.

Source: https://algobarsx.com/docs/ref-cmd-orders/

### `buy [options]`

Open a long position or place a buy order.

#### Options

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `size` | lots \| percent \| money | `0.01 lots` | Position size in lots, or % / $ of balance as notional. |
| `risk` | percent \| money | `none` | Risk per trade; size is computed from the stop distance and rounded down to the lot step. |
| `stop` | price \| distance | `none` | Protective stop. For a pending stop order, the entry price (its protective stop is then stop_loss). |
| `target` | price \| distance | `none` | Take-profit price or distance, such as 2R. |
| `limit` | price | `none` | Entry price of a pending limit order. |
| `stop_loss` | price \| distance | `none` | Protective stop of a pending stop order. |
| `ghost` | bool | `false` | Keep stop and target off the broker; AlgoBars enforces them. |
| `expires` | bars \| duration | `none` | When a pending order expires. |
| `market` | symbol | `market.symbol` | Which symbol, in strategies with several markets. |
| `tag` | string | `""` | Label carried by the order and its trade. |

**Where it goes** Inside a when rule, event or action fn of a strategy.

**Block** management

```algobarsx
buy risk: 1%, stop: 20 pips, target: 2R
```

Works in: strategy. Since 1.0.

### `sell [options]`

Open a short position or place a sell order.

#### Options

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `size` | lots \| percent \| money | `0.01 lots` | Position size in lots, or % / $ of balance as notional. |
| `risk` | percent \| money | `none` | Risk per trade; size is computed from the stop distance and rounded down to the lot step. |
| `stop` | price \| distance | `none` | Protective stop. For a pending stop order, the entry price (its protective stop is then stop_loss). |
| `target` | price \| distance | `none` | Take-profit price or distance, such as 2R. |
| `limit` | price | `none` | Entry price of a pending limit order. |
| `stop_loss` | price \| distance | `none` | Protective stop of a pending stop order. |
| `ghost` | bool | `false` | Keep stop and target off the broker; AlgoBars enforces them. |
| `expires` | bars \| duration | `none` | When a pending order expires. |
| `market` | symbol | `market.symbol` | Which symbol, in strategies with several markets. |
| `tag` | string | `""` | Label carried by the order and its trade. |

**Where it goes** Inside a when rule, event or action fn of a strategy.

**Block** management

```algobarsx
sell risk: $200, stop: highest(high, 10) + 2 pips, target: 2R
```

Works in: strategy. Since 1.0.

### `close <trade> [options]`

Close a trade, fully or partly.

#### Arguments

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `trade` | Trade | required | Trade to close. |

#### Options

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `size` | percent \| lots | `100%` | How much to close. |

**Where it goes** Inside a when rule, event, loop or action fn of a strategy.

```algobarsx
close trades.last(), size: 50%
```

Works in: strategy. Since 1.0.

### `close_all [options]`

Close all of the strategy's open trades, optionally filtered.

#### Options

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `side` | string | `both` | long, short or both. |
| `tag` | string | `""` | Only trades with this tag. |
| `market` | symbol | `market.symbol` | Only this symbol. |

**Where it goes** Inside a when rule, event or action fn of a strategy.

```algobarsx
close_all side: long
```

Works in: strategy. Since 1.0.

### `modify <trade> [options]`

Change the stop or target of a trade.

#### Arguments

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `trade` | Trade | required | Trade to change. |

#### Options

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `stop` | price \| distance | `unchanged` | New stop. |
| `target` | price \| distance | `unchanged` | New target. |

**Where it goes** Inside a when rule, event, loop or action fn of a strategy.

```algobarsx
modify trades.last(), stop: trades.last().entry_price
```

Works in: strategy. Since 1.0.

### `cancel <orders>`

Cancel pending orders.

#### Arguments

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `orders` | list<Order> | required | Orders to cancel. |

**Where it goes** Inside a when rule, event or action fn of a strategy.

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

Works in: strategy. Since 1.0.
