# Limit Order Book

The LimitOrderBook is deployed on xDai at `0x02e7B722E178518Ae07a596A7cb5F88B313c453a`

The source code can be found at [LimitOrderBook.sol](https://github.com/perpfutui/perpetual-limit-orders/blob/main/contracts/LimitOrderBook.sol)

## Functions

The functions allowing you to create orders are functionally very similar. We will give an overview of the parameters common to all orders and then discuss their differences.

Please ensure that you have familiarised yourself with the different [order types](/apex-docs/order-types.md).

### addLimitOrder

```javascript
function addLimitOrder(
    IAmm _asset,
    Decimal.decimal memory _limitPrice,
    SignedDecimal.signedDecimal memory _positionSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _slippage,
    Decimal.decimal memory _tipFee,
    bool _reduceOnly,
    uint256 _expiry
  )
```

This function is used to add Limit Orders.

* `_limitPrice` the price for the limit order.&#x20;
  * A limit buy order will be executed when the current price is less than limitPrice.&#x20;
  * A limit sell order is executed when the current price is greater than limitPrice

### addStopOrder

```javascript
function addStopOrder(
    IAmm _asset,
    Decimal.decimal memory _stopPrice,
    SignedDecimal.signedDecimal memory _positionSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _slippage,
    Decimal.decimal memory _tipFee,
    bool _reduceOnly,
    uint256 _expiry
  )
```

This function is used to add Stop Orders.

* `_stopPrice` the price for the stop order.&#x20;
  * A stop market buy order will be executed when the current price is greater than stopPrice.&#x20;
  * A stop market sell order is executed when the current price is lesser than stopPrice

### addStopLimitOrder

```javascript
function addStopLimitOrder(
    IAmm _asset,
    Decimal.decimal memory _stopPrice,
    Decimal.decimal memory _limitPrice,
    SignedDecimal.signedDecimal memory _positionSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _slippage,
    Decimal.decimal memory _tipFee,
    bool _reduceOnly,
    uint256 _expiry
  )
```

This function is used to add Stop-Limit orders. Stop-Limit orders

* `_stopPrice` the price for the stop limit order.&#x20;
  * A stop limit buy order will be executed when the current price is greater than stopPrice.&#x20;
  * A stop limit sell order is executed when the current price is lesser than stopPrice
* `_limitPrice` the order must also satisfy the limit conditions in order to be valid

### addTrailingStopMarketOrderAbs

```javascript
function addTrailingStopMarketOrderAbs(
    IAmm _asset,
    Decimal.decimal memory _trail,
    SignedDecimal.signedDecimal memory _positionSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _tipFee,
    bool _reduceOnly,
    uint256 _expiry
  )
```

This function is used to add a trailing Stop order with an absolute value

* `_trail` represents the absolute value of the trail between the price of the asset and the trigger(stop) price. This will clearly be different for different assets.

### addTrailingStopMarketOrderPct

```javascript
function addTrailingStopMarketOrderPct(
    IAmm _asset,
    Decimal.decimal memory _trailPct,
    SignedDecimal.signedDecimal memory _positionSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _tipFee,
    bool _reduceOnly,
    uint256 _expiry
  )
```

This function is used to add trailing Stop orders with percentage value

* `_trailPct` This number represents the trail value as a percentage of the asset price (must be between 0 and 1)
  * A value of 0.1 (formatted to 18 decimal places) will therefore signify 10%

### addTrailingStopLimitOrderAbs

```javascript
function addTrailingStopLimitOrderAbs(
    IAmm _asset,
    Decimal.decimal memory _trail,
    Decimal.decimal memory _gap,
    SignedDecimal.signedDecimal memory _positionSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _slippage,
    Decimal.decimal memory _tipFee,
    bool _reduceOnly,
    uint256 _expiry
  )
```

This function is used to add trailing Stop Limit orders with absolute values

* `_trail` is exactly similar to the trailing stop market orders
* `_gap` will specify the gap between the trailing stop price and the trailing limit price.&#x20;

### addTrailingStopLimitOrderPct

```javascript
function addTrailingStopLimitOrderPct(
    IAmm _asset,
    Decimal.decimal memory _trailPct,
    Decimal.decimal memory _gapPct,
    SignedDecimal.signedDecimal memory _positionSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _slippage,
    Decimal.decimal memory _tipFee,
    bool _reduceOnly,
    uint256 _expiry
  )
```

This function is used to add a trailing Stop Limit order with percentage values.

* `_trailPct` is exactly similar to the trailing stop market orders
* `_gapPct` will specify the gap between the trailing stop price and the trailing limit price as a percentage.&#x20;

### modifyOrder

```javascript
function modifyOrder(
    uint order_id,
    Decimal.decimal memory _stopPrice,
    Decimal.decimal memory _limitPrice,
    SignedDecimal.signedDecimal memory _orderSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _slippage,
    bool _reduceOnly,
    uint _expiry
  )
```

This function allows a user to modify their own order. All the parameters are functionally similar to their counterparts in the addOrder functions.

* `order_id` is the id of the order you are attempting to change.

Although the option exists to modify the stop price and limit price for all order types, changing the limit price of a stop order (or vice versa) will not turn it into a stop-limit order.

{% hint style="info" %}
Please note: It is not possible to modify the order type, or the botFee.
{% endhint %}

### modifyTrailingOrder

```javascript
function modifyTrailingOrder(
    uint order_id,
    Decimal.decimal memory _newStop,
    Decimal.decimal memory _newLimit,
    SignedDecimal.signedDecimal memory _orderSize,
    Decimal.decimal memory _collateral,
    Decimal.decimal memory _leverage,
    Decimal.decimal memory _slippage,
    bool _reduceOnly,
    uint _expiry
  )
```

This function allows users to modify their trailing orders.

* `_newStop` and `_newLimit` are functionally similar to the trail and gap parameters discussed above.
* If the order is percentage, this function will expect percentage inputs.

{% hint style="info" %}
Please note: It is not possible to change from a trailing order that uses absolute values to one that uses percentages.
{% endhint %}

###

### deleteOrder

```javascript
function deleteOrder(
    uint order_id
  )
```

This function allows users to delete their own orders

### execute

```javascript
function execute(uint order_id)
```

This function is used by keepers/users to execute orders once their conditions have been achieved

### pokeContract

```javascript
function pokeContract(
    uint order_id,
    uint _reserveIndex
  )
```

This function is used to update the trigger prices for the trailing orders.

* `_reserveIndex` is used by the contract to verify previous prices of assets in order to update trailing prices. The ClearingHouse contract keeps a store of previous prices from ReserveSnapshotted. These prices can be queries using `getPriceAtSnapshot()`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.apex.win/apex-docs/developer-guide/smart-contracts/limit-order-contract.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
