Order

This object represents an order. Each order belongs to a specific strategy.

A simple market order can be constructed as follows:

symbol = "SPY"
quantity = 50
side = "buy"
order = self.create_order(symbol, quantity, side)

With:

  • symbol (str): the string representation of the asset e.g. “GOOG” for Google

  • quantity (int): the number of shares to buy/sell

  • side (str): must be either “buy” for buying order or “sell” for selling order

Order objects have the following helper methods

  • to_position(): convert an order to a position belonging to the same strategy with order.quantity amount of shares.

  • get_increment(): for selling orders returns -order.quantity, for buying orders returns order.quantity

  • wait_to_be_registered: wait for the order to be registered by the broker

  • wait_to_be_closed: wait for the order to be closed by the broker (Order either filled or closed)

Advanced Order Types

limit order

A limit order is an order to buy or sell at a specified price or better.

To create a limit order object, add the keyword parameter limit_price

my_limit_price = 500
order = self.create_order(symbol, quantity, side, limit_price=my_limit_price)
self.submit_order(order)

stop order

A stop (market) order is an order to buy or sell a security when its price moves past a particular point, ensuring a higher probability of achieving a predetermined entry or exit price.

To create a stop order object, add the keyword parameter stop_price.

my_stop_price = 400
order = self.create_order(symbol, quantity, side, stop_price=my_stop_price)
self.submit_order(order)

stop limit order

A stop_limit order is a stop order with a limit price (combining stop orders and limit orders)

To create a stop_limit order object, add the keyword parameters stop_price and limit_price.

my_limit_price = 405
my_stop_price = 400
order = self.create_order(symbol, quantity, side, stop_price=my_stop_price, limit_price=my_limit_price)
self.submit_order(order)

trailing stop order

Trailing stop orders allow you to continuously and automatically keep updating the stop price threshold based on the stock price movement.

To create trailing_stop orders, add either a trail_price or a trail_percent keyword parameter.

my_trail_price = 20
order_1 = self.create_order(symbol, quantity, side, trail_price=my_trail_price)
self.submit_order(order_1)

my_trail_percent = 2.0 # 2.0 %
order_2 = self.create_order(symbol, quantity, side, trail_percent=my_trail_percent)
self.submit_order(order_2)

Order With Legs

bracket order

A bracket order is a chain of three orders that can be used to manage your position entry and exit.

The first order is used to enter a new long or short position, and once it is completely filled, two conditional exit orders will be activated. One of the two closing orders is called a take-profit order, which is a limit order, and the other closing order is a stop-loss order, which is either a stop or stop-limit order. Importantly, only one of the two exit orders can be executed. Once one of the exit orders fills, the other order cancels. Please note, however, that in extremely volatile and fast market conditions, both orders may fill before the cancellation occurs.

To create a bracket order object, add the keyword parameters take_profit_price and stop_loss_price. A stop_loss_limit_price can also be specified to make the stop loss order a stop-limit order.

my_take_profit_price = 420
my_stop_loss_price = 400
order = self.create_order(
   symbol,
   quantity,
   side,
   take_profit_price=my_take_profit_price,
   stop_loss_price=my_stop_loss_price
)
self.submit_order(order)

Interactive Brokers requires the main or parent order to be a limit order. Add limit_price=my_limit_price.

OTO (One-Triggers-Other) order

OTO (One-Triggers-Other) is a variant of bracket order. It takes one of the take-profit or stop-loss order in addition to the entry order.

To create an OTO order object, add either a take_profit_price or a stop_loss_price keyword parameter. A stop_loss_limit_price can also be specified in case of stop loss exit.

Interactive Brokers requires the main or parent order to be a limit order. Add limit_price=my_limit_price.

OCO (One-Cancels-Other) order

OCO orders are a set of two orders with the same side (buy/buy or sell/sell). In other words, this is the second part of the bracket orders where the entry order is already filled, and you can submit the take-profit and stop-loss in one order submission.

To create an OCO order object, add the keyword parameters take_profit_price and stop_loss_price and set position_filled to True. A stop_loss_limit_price can also be specified to make the stop loss order a stop-limit order.

my_take_profit_price = 420
my_stop_loss_price = 400
order = self.create_order(
   symbol,
   quantity,
   side,
   take_profit_price=my_take_profit_price,
   stop_loss_price=my_stop_loss_price,
   position_filled=True
)
self.submit_order(order)

Interactive Brokers requires the main or parent order to be a limit order. Add limit_price=my_limit_price.

Documentation

class entities.order.Order(strategy, asset=None, quantity=None, side=None, limit_price=None, stop_price=None, take_profit_price=None, stop_loss_price=None, stop_loss_limit_price=None, trail_price=None, trail_percent=None, time_in_force='day', good_till_date=None, exchange=None, position_filled=False, quote=None, pair=None, date_created=None, type=None, order_class=None, trade_cost: float = None, custom_params={}, identifier=None, avg_fill_price=None, error_message=None, child_orders=None, tag='', status='unprocessed')

Bases: object

class OrderClass

Bases: object

BRACKET = 'bracket'
MULTILEG = 'multileg'
OCO = 'oco'
OTO = 'oto'
class OrderSide

Bases: object

BUY = 'buy'
BUY_TO_CLOSE = 'buy_to_close'
BUY_TO_COVER = 'buy_to_cover'
BUY_TO_OPEN = 'buy_to_open'
SELL = 'sell'
SELL_SHORT = 'sell_short'
SELL_TO_CLOSE = 'sell_to_close'
SELL_TO_OPEN = 'sell_to_open'
class OrderStatus

Bases: object

CANCELED = 'canceled'
CANCELLING = 'cancelling'
CASH_SETTLED = 'cash_settled'
ERROR = 'error'
EXPIRED = 'expired'
FILLED = 'fill'
NEW = 'new'
OPEN = 'open'
PARTIALLY_FILLED = 'partial_fill'
SUBMITTED = 'submitted'
UNPROCESSED = 'unprocessed'
class OrderType

Bases: object

BRACKET = 'bracket'
LIMIT = 'limit'
MARKET = 'market'
OCO = 'oco'
OTO = 'oto'
STOP = 'stop'
STOP_LIMIT = 'stop_limit'
TRAIL = 'trailing_stop'
class Transaction(quantity, price)

Bases: tuple

price

Alias for field number 1

quantity

Alias for field number 0

add_child_order(o)

Add a child order to the parent order.

Parameters:

o (Order) – The child order to add to the parent order.

add_transaction(price, quantity)
property avg_fill_price
cash_pending(strategy)
equivalent_status(status) bool

Returns if the status is equivalent to the order status.

classmethod from_dict(order_dict)
get_fill_price()

Get the weighted average filled price for this order. Option contracts often encounter partial fills, so the weighted average is the only valid price that can be used for PnL calculations.

Returns:

The weighted average filled price for this order. 0.0 will be returned if the order has not yet been filled.

Return type:

float

get_increment()
is_active()

Returns whether this order is active. :returns: True if the order is active, False otherwise. :rtype: bool

is_buy_order()
is_canceled()

Returns whether this order has been cancelled.

Returns:

True if the order has been cancelled, False otherwise.

Return type:

bool

is_filled()

Returns whether this order has been filled.

Returns:

True if the order has been filled, False otherwise.

Return type:

bool

is_option()

Return true if this order is an option.

is_parent() bool

Check if the order is a parent order. Parent orders are typically Multileg orders where the child orders do the actual trading and cash settlements and the parent order is a container that holds them all together. Lumibot should not update any positions/cash balances when parent orders fill.

Returns:

True if the order is a parent order, False otherwise.

Return type:

bool

is_sell_order()
property quantity
set_canceled()
set_error(error)
set_filled()
set_identifier(identifier)
set_new()
set_partially_filled()
property status
to_dict()
to_position(quantity)
update_raw(raw)
update_trail_stop_price(price)

Update the trail stop price. This will be used to determine if a trailing stop order should be triggered in a backtest.

Parameters:

price (float) – The last price of the asset. For trailing stop orders, this is the price that will be used to update the trail stop price.

wait_to_be_closed()
wait_to_be_registered()
was_transmitted()