self.create_order#

lumibot.strategies.strategy.Strategy.create_order(self, asset: str | Asset, quantity: int | str | Decimal, side: str, limit_price: float = None, stop_price: float = None, stop_limit_price: float = None, trail_price: float = None, trail_percent: float = None, secondary_limit_price: float = None, secondary_stop_price: float = None, secondary_stop_limit_price: float = None, secondary_trail_price: float = None, secondary_trail_percent: float = None, time_in_force: str = 'gtc', good_till_date: datetime = None, take_profit_price: float = None, stop_loss_price: float = None, stop_loss_limit_price: float = None, position_filled: float = None, exchange: str = None, quote: Asset = None, pair: str = None, order_type: OrderType | None = None, order_class: OrderClass | None = None, type: OrderType | None = None, custom_params: dict = None)

Create a new order for this strategy.

Notes#

  • Crypto venues require both a base asset and a quote asset. Supply the quote parameter when trading pairs such as BTC/USDT.

  • Supported crypto order types are market, limit, and stop_limit; compound order classes (oco, bracket) are generally unavailable and orders behave as GTC.

Parameters#

param asset:

Asset that will be traded. Stocks may be provided as strings; other asset classes must use lumibot.entities.Asset.

type asset:

str or lumibot.entities.Asset

param quantity:

Number of shares or units to trade. Accepts integers, numeric strings, or decimal.Decimal values (all normalised to Decimal).

type quantity:

int or str or Decimal

param side:

Whether the order is "buy" or "sell".

type side:

str

param order_type:

Concrete order flavour ('market', 'limit', 'stop', 'stop_limit', 'trailing_stop'). If omitted the broker integration infers a sensible default from the supplied prices.

type order_type:

lumibot.entities.Order.OrderType, optional

param order_class:

'simple', 'bracket', 'oco', 'oto', or 'multileg'.

type order_class:

lumibot.entities.Order.OrderClass, optional

param limit_price:

Limit price for entry orders.

type limit_price:

float, optional

param stop_price:

Trigger price for stop orders.

type stop_price:

float, optional

param stop_limit_price:

Stop loss with a limit price to control the fill.

type stop_limit_price:

float, optional

param secondary_limit_price:

Profit-taking leg for advanced order classes such as brackets or OTO.

type secondary_limit_price:

float, optional

param secondary_stop_price:

Protective stop leg for advanced order classes.

type secondary_stop_price:

float, optional

param secondary_stop_limit_price:

Stop-limit trigger used by child legs in advanced orders.

type secondary_stop_limit_price:

float, optional

param trail_price:

Trailing-stop offset expressed in price units.

type trail_price:

float, optional

param trail_percent:

Trailing-stop offset expressed as a percentage.

type trail_percent:

float, optional

param secondary_trail_price:

Trailing-stop offset for child orders, in price units.

type secondary_trail_price:

float, optional

param secondary_trail_percent:

Trailing-stop offset for child orders, as a percentage.

type secondary_trail_percent:

float, optional

param time_in_force:

Time the order remains active ('day', 'gtc', 'gtd').

type time_in_force:

str, optional

param good_till_date:

Timestamp used when time_in_force is 'gtd'.

type good_till_date:

datetime.datetime, optional

param take_profit_price:

Deprecated alias for secondary_limit_price.

type take_profit_price:

float, optional

param stop_loss_price:

Deprecated alias for secondary_stop_price.

type stop_loss_price:

float, optional

param stop_loss_limit_price:

Deprecated alias for secondary_stop_limit_price.

type stop_loss_limit_price:

float, optional

param position_filled:

Used internally to track partial fills.

type position_filled:

float, optional

param exchange:

Exchange routing hint for brokers that support it (defaults to 'SMART').

type exchange:

str, optional

param quote:

Quote asset to pair with the base asset (required for crypto trades).

type quote:

lumibot.entities.Asset, optional

param pair:

Explicit trading pair symbol for venues that expect it.

type pair:

str, optional

param custom_params:

Broker-specific parameters forwarded untouched (for example {"leverage": 3} for Kraken margin trades).

type custom_params:

dict, optional

param type:

Deprecated synonym for order_type maintained for backward compatibility.

type type:

lumibot.entities.Order.OrderType or None, optional

Returns#

returns:

Order object ready to be submitted for trading.

rtype:

lumibot.entities.Order

Examples#

  • Market buy order

    order = self.create_order("SPY", 100, "buy")
    self.submit_order(order)
    
  • Limit buy order

    limit_order = self.create_order("SPY", 1, "buy", limit_price=100)
    self.submit_order(limit_order)
    
  • Sell 100 shares

    order = self.create_order("TLT", 100, "sell")
    self.submit_order(order)
    
  • Stop loss order

    order = self.create_order("SPY", 100, "buy", stop_price=100.00)
    self.submit_order(order)
    
  • Stop limit order

    order = self.create_order("SPY", 100, "buy", stop_price=100.00, stop_limit_price=99.95)
    self.submit_order(order)
    
  • Market sell order

    order = self.create_order("SPY", 100, "sell")
    self.submit_order(order)
    
  • Limit sell order

    order = self.create_order("SPY", 100, "sell", limit_price=100.00)
    self.submit_order(order)
    
  • Order with trailing stop

    order = self.create_order("SPY", 100, "buy", trail_price=1.00)
    self.submit_order(order)
    
  • OCO order

    order = self.create_order(
        "SPY",
        100,
        "sell",
        limit_price=limit,              # exit profit point
        stop_price=stop_loss,           # exit loss point
        stop_limit_price=stop_loss_limit,  # optional modifier
        order_class=Order.OrderClass.OCO,
    )
    
  • Bracket order

    order = self.create_order(
        "SPY",
        100,
        "buy",
        limit_price=limit,                 # entry trigger
        secondary_limit_price=sec_limit,   # profit target
        secondary_stop_price=stop_loss,    # protective stop
        secondary_stop_limit_price=stop_loss_limit,  # optional modifier
        order_class=Order.OrderClass.BRACKET,
    )
    
  • Bracket order with trailing stop

    order = self.create_order(
        "SPY",
        100,
        "buy",
        limit_price=limit,
        secondary_limit_price=sec_limit,
        secondary_stop_price=stop_loss,
        secondary_trail_percent=trail_percent,
        order_class=Order.OrderClass.BRACKET,
    )
    
  • OTO order

    order = self.create_order(
        "SPY",
        100,
        "buy",
        limit_price=limit,
        secondary_stop_price=stop_loss,
        order_class=Order.OrderClass.OTO,
    )
    
  • Futures order

    from lumibot.entities import Asset
    asset = Asset("ES", asset_type=Asset.AssetType.FUTURE, expiration="2019-01-01")
    order = self.create_order(asset, 100, "buy", limit_price=100.00)
    self.submit_order(order)
    
  • Futures order with trailing stop

    from lumibot.entities import Asset
    asset = Asset("ES", asset_type=Asset.AssetType.FUTURE, expiration="2019-01-01")
    order = self.create_order(
        asset,
        100,
        "buy",
        limit_price=limit,
        secondary_stop_price=stop_loss,
        secondary_trail_percent=trail_percent,
        order_class=Order.OrderClass.OTO,
    )
    self.submit_order(order)
    
  • Option order

    from lumibot.entities import Asset
    asset = Asset("SPY", asset_type=Asset.AssetType.OPTION, expiration="2019-01-01", strike=100.00)
    order = self.create_order(asset, 100, "buy", limit_price=100.00)
    self.submit_order(order)
    
  • Option order with trailing stop (bracket)

    from lumibot.entities import Asset
    asset = Asset("SPY", asset_type=Asset.AssetType.OPTION, expiration="2019-01-01", strike=100.00)
    order = self.create_order(
        asset,
        100,
        "buy",
        limit_price=limit,
        secondary_stop_price=stop_loss,
        secondary_trail_percent=trail_percent,
        order_class=Order.OrderClass.BRACKET,
    )
    self.submit_order(order)
    
  • Option order with trailing stop (OTO)

    from lumibot.entities import Asset
    asset = Asset("SPY", asset_type=Asset.AssetType.OPTION, expiration="2019-01-01", strike=100.00)
    order = self.create_order(
        asset,
        100,
        "buy",
        limit_price=limit,
        secondary_stop_price=stop_loss,
        secondary_trail_percent=trail_percent,
        order_class=Order.OrderClass.OTO,
    )
    self.submit_order(order)
    
  • FOREX order

    from lumibot.entities import Asset
    asset = Asset(symbol="CHF", currency="EUR", asset_type=Asset.AssetType.FOREX)
    order = self.create_order(asset, 100, "buy", limit_price=100.00)
    self.submit_order(order)
    
  • Crypto market order

    from lumibot.entities import Asset
    base = Asset("BTC", asset_type=Asset.AssetType.CRYPTO)
    quote = Asset("USD", asset_type=Asset.AssetType.CRYPTO)
    order = self.create_order(base, 0.05, "buy", quote=quote)
    self.submit_order(order)
    
  • Crypto limit order with quote asset

    from lumibot.entities import Asset
    base = Asset("BTC", asset_type=Asset.AssetType.CRYPTO)
    quote = Asset("USD", asset_type=Asset.AssetType.CRYPTO)
    order = self.create_order(base, 0.05, "buy", limit_price=41000, quote=quote)
    self.submit_order(order)