Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Order

All orders are limit orders in roboquant. If you want them to behave more like a market order, you can set a generous limit price.

Also the only difference between a BUY and a SELL order is the sign of their size. So SELL orders have a negative size and BUY orders a positive size.

There are 2 tif (time-in-force) values supported, namely “DAY” and “GTC”.

All the attributes of an order are:

class Order:
    """
    A trading order for a particular asset. Orders are immutable.

    Each order has a mandatory `size` and a `limit` price. Orders with a positive `size`
    are buy orders, and with a negative `size` are sell orders.

    The `gtd` (good till date) is optional, and if not set implies the order is valid
    for the DAY. The `info` can hold any arbitrary properties set on the order.

    The `id`, `fill` and `time` properties are managed by the `Broker`.
    """

    asset: Asset
    """The underlying asset of this order."""

    size: Decimal
    """The size (number of contracts) of the order.
    Positive size for buy orders, negative size for sell orders.
    """

    limit: float
    """The limit price of the order, denoted in the currency of the asset.
    The limit price is the maximum price you are willing to pay for a buy order,
    or the minimum price you are willing to accept for a sell order.
    Make sure to set the limit price in the currency of the asset
    and not include more decimal places than supported by the broker.
    """

    tif: Literal["GTC", "DAY"] = "DAY"
    """The time in force of the order.
    `GTC` = Good Till Cancelled, `DAY` = valid for a day only.
    """

    info: dict[str, Any] | None = None
    """Any additional information about the order.
    Enables to pass information to the broker if required.
    """

    id: str = ""
    """The unique id of the order. This is set by the broker only and should not be updated elsewhere.
    The id is an empty string for new orders and set to a non-empty string when the order is placed with the broker.
    The id is used to identify the order when modifying or cancelling it.
    """

    fill: Decimal = Decimal()
    """The filled size of the order, set by the broker only. Just like the size, positive for buy orders,
    negative for sell orders. So the remaining size is `size - fill`"""

    time: datetime | None = None
    """Time when was the order placed at the exchange.
    So typically the first trading day after the order was submitted
    to the broker.
    """

Initial Orders

Creating a new initial order is simple. One thing to be aware of is that order sizes are of the type Decimal.

from decimal import Decimal
from roboquant import Stock, Order

asset = Stock("AAPL")
buy_order = Order(asset, size = Decimal(10), limit = 200.0, tif="DAY")
sell_order = Order(asset, size = -Decimal(10), limit = 200.0, tif="GTC")

Cancel & Modify orders

Existing orders are orders with a non empty id. This id is assigned by the broker when placed at that broker. These are the orders that are found in account.orders. Only these type of orders can be cancelled or modified.

assert order.id
modified_order = order.modify(limit=51.0)
cancelled_order = order.cancel()

Or to cancel all open orders:

cancellations = [order.cancel() for order in account.orders]
broker.place_orders(cancellations)

Decimals

When creating an order, it is important two realize two things regarding decimal places:

  1. What are the number of decimals (sometimes referred to as ndigits in Python) for the order size that is allowed by the broker. For example Forex, Crypto or fractional equity trading all allow for fractional sizes.

  2. What is the number of decimals allowed for setting the limit price.

Most traders that ome with roboquant out of the box allow you to configure this behavior and if you want to use them in Forex or Crypto, you likely will have to change the defaults.