Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Nautechsystems Nautilus trader OrderFactory Market Limit

From Leeroopedia
Revision as of 16:01, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Nautechsystems_Nautilus_trader_OrderFactory_Market_Limit.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Field Value
sources https://github.com/nautechsystems/nautilus_trader , https://nautilustrader.io/docs/
domains algorithmic trading, order creation, order factory, NautilusTrader
last_updated 2026-02-10 12:00 GMT

Overview

Concrete tool for creating market, limit, stop-market, and other order types through a factory interface provided by NautilusTrader.

Description

The OrderFactory class provides factory methods for creating all order types supported by NautilusTrader. It is automatically assigned to every Strategy instance as self.order_factory during registration. The factory handles:

  • Client order ID generation: Each call generates a unique ClientOrderId using a configurable strategy (sequential or UUID4).
  • Timestamp assignment: Every order is stamped with the current clock time (supports both live and simulated clocks).
  • Identity binding: Orders are automatically associated with the correct trader_id and strategy_id.
  • Parameter defaulting: Reasonable defaults are provided for optional parameters (e.g., time_in_force=GTC, reduce_only=False).

The primary factory methods are:

  • market(): Creates a MarketOrder for immediate execution.
  • limit(): Creates a LimitOrder with a specified price.
  • stop_market(): Creates a StopMarketOrder with a trigger price.
  • stop_limit(): Creates a StopLimitOrder with both a trigger price and a limit price.
  • bracket(): Creates a coordinated OrderList with entry, stop-loss, and take-profit orders.

Usage

Access the order factory via self.order_factory within any Strategy subclass after the strategy has been registered with a Trader. Typically called within on_bar, on_trade_tick, or other event handlers when the trading logic determines an order should be placed.

Code Reference

Source Location

nautilus_trader/common/factories.pyx, lines 236-1707.

Signatures

market():

cpdef MarketOrder market(
    self,
    InstrumentId instrument_id,
    OrderSide order_side,
    Quantity quantity,
    TimeInForce time_in_force = TimeInForce.GTC,
    bint reduce_only = False,
    bint quote_quantity = False,
    ExecAlgorithmId exec_algorithm_id = None,
    dict exec_algorithm_params = None,
    list[str] tags = None,
    ClientOrderId client_order_id = None,
)

limit():

cpdef LimitOrder limit(
    self,
    InstrumentId instrument_id,
    OrderSide order_side,
    Quantity quantity,
    Price price,
    TimeInForce time_in_force = TimeInForce.GTC,
    datetime expire_time = None,
    bint post_only = False,
    bint reduce_only = False,
    bint quote_quantity = False,
    Quantity display_qty = None,
    TriggerType emulation_trigger = TriggerType.NO_TRIGGER,
    InstrumentId trigger_instrument_id = None,
    ExecAlgorithmId exec_algorithm_id = None,
    dict exec_algorithm_params = None,
    list[str] tags = None,
    ClientOrderId client_order_id = None,
)

stop_market():

cpdef StopMarketOrder stop_market(
    self,
    InstrumentId instrument_id,
    OrderSide order_side,
    Quantity quantity,
    Price trigger_price,
    TriggerType trigger_type = TriggerType.DEFAULT,
    TimeInForce time_in_force = TimeInForce.GTC,
    datetime expire_time = None,
    bint reduce_only = False,
    bint quote_quantity = False,
    TriggerType emulation_trigger = TriggerType.NO_TRIGGER,
    InstrumentId trigger_instrument_id = None,
    ExecAlgorithmId exec_algorithm_id = None,
    dict exec_algorithm_params = None,
    list[str] tags = None,
    ClientOrderId client_order_id = None,
)

Import

The OrderFactory is automatically available as self.order_factory on any registered Strategy. For direct import:

from nautilus_trader.common.factories import OrderFactory

For order type classes used in type annotations:

from nautilus_trader.model.orders.market import MarketOrder
from nautilus_trader.model.orders.limit import LimitOrder
from nautilus_trader.model.orders.stop_market import StopMarketOrder

I/O Contract

Inputs (market)

Parameter Type Required Description
instrument_id InstrumentId Yes The instrument to trade.
order_side OrderSide Yes BUY or SELL.
quantity Quantity Yes Order quantity (must be > 0).
time_in_force TimeInForce No Time in force. Default GTC. Cannot be GTD for market orders.
reduce_only bool No Reduce-only instruction. Default False.
quote_quantity bool No Whether quantity is in quote currency. Default False.
exec_algorithm_id ExecAlgorithmId or None No Execution algorithm to route through.
exec_algorithm_params dict or None No Parameters for the execution algorithm.
tags list[str] or None No Custom tags for the order.
client_order_id ClientOrderId or None No Custom client order ID. Auto-generated if None.

Inputs (limit)

Parameter Type Required Description
instrument_id InstrumentId Yes The instrument to trade.
order_side OrderSide Yes BUY or SELL.
quantity Quantity Yes Order quantity (must be > 0).
price Price Yes The limit price.
time_in_force TimeInForce No Time in force. Default GTC.
expire_time datetime or None No Expiration time (required for GTD).
post_only bool No Post-only (maker) instruction. Default False.
reduce_only bool No Reduce-only instruction. Default False.
quote_quantity bool No Whether quantity is in quote currency. Default False.
display_qty Quantity or None No Iceberg display quantity.
emulation_trigger TriggerType No Local emulation trigger. Default NO_TRIGGER (no emulation).
trigger_instrument_id InstrumentId or None No Instrument for emulation trigger.
tags list[str] or None No Custom tags.
client_order_id ClientOrderId or None No Custom ID. Auto-generated if None.

Inputs (stop_market)

Parameter Type Required Description
instrument_id InstrumentId Yes The instrument to trade.
order_side OrderSide Yes BUY or SELL.
quantity Quantity Yes Order quantity (must be > 0).
trigger_price Price Yes The stop trigger price.
trigger_type TriggerType No Trigger type. Default DEFAULT.
time_in_force TimeInForce No Time in force. Default GTC.
expire_time datetime or None No Expiration time (for GTD).
reduce_only bool No Reduce-only instruction. Default False.
emulation_trigger TriggerType No Local emulation trigger. Default NO_TRIGGER.
tags list[str] or None No Custom tags.
client_order_id ClientOrderId or None No Custom ID. Auto-generated if None.

Outputs

Factory Method Return Type Description
market() MarketOrder A fully constructed market order ready for submission.
limit() LimitOrder A fully constructed limit order ready for submission.
stop_market() StopMarketOrder A fully constructed stop-market order ready for submission.

Usage Examples

Creating and submitting a market order:

from nautilus_trader.model.enums import OrderSide
from nautilus_trader.model.objects import Quantity
from nautilus_trader.model.identifiers import InstrumentId


class MarketOrderStrategy(Strategy):
    def on_bar(self, bar):
        if self._should_buy(bar):
            order = self.order_factory.market(
                instrument_id=bar.bar_type.instrument_id,
                order_side=OrderSide.BUY,
                quantity=Quantity.from_str("100"),
            )
            self.submit_order(order)

Creating a limit order with post-only:

from nautilus_trader.model.objects import Price, Quantity
from nautilus_trader.model.enums import OrderSide


class LimitOrderStrategy(Strategy):
    def on_quote_tick(self, tick):
        order = self.order_factory.limit(
            instrument_id=tick.instrument_id,
            order_side=OrderSide.BUY,
            quantity=Quantity.from_str("50"),
            price=Price.from_str(str(float(tick.bid_price) - 0.01)),
            post_only=True,
        )
        self.submit_order(order)

Creating a stop-market order for a stop-loss:

from nautilus_trader.model.objects import Price, Quantity
from nautilus_trader.model.enums import OrderSide


class StopLossStrategy(Strategy):
    def on_order_filled(self, event):
        if event.order_side == OrderSide.BUY:
            stop_price = Price.from_str(
                str(float(event.last_px) * 0.97)  # 3% stop-loss
            )
            stop_order = self.order_factory.stop_market(
                instrument_id=event.instrument_id,
                order_side=OrderSide.SELL,
                quantity=event.last_qty,
                trigger_price=stop_price,
                reduce_only=True,
            )
            self.submit_order(stop_order)

Creating a limit order with local emulation:

from nautilus_trader.model.enums import TriggerType


class EmulatedOrderStrategy(Strategy):
    def on_bar(self, bar):
        order = self.order_factory.limit(
            instrument_id=bar.bar_type.instrument_id,
            order_side=OrderSide.BUY,
            quantity=Quantity.from_str("25"),
            price=Price.from_str("150.00"),
            emulation_trigger=TriggerType.DEFAULT,  # Emulated locally using bid/ask
        )
        self.submit_order(order)

Creating a market order with custom tags:

class TaggedOrderStrategy(Strategy):
    def on_bar(self, bar):
        order = self.order_factory.market(
            instrument_id=bar.bar_type.instrument_id,
            order_side=OrderSide.BUY,
            quantity=Quantity.from_str("100"),
            tags=["ENTRY", "MOMENTUM_SIGNAL"],
        )
        self.submit_order(order)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment