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)¶
Creates a new order for this specific strategy. Once created, an order must still be submitted.
Some notes on Crypto markets:
Crypto markets require both a base currency and a quote currency to create an order. For example, use the quote parameter.:
>>> from lumibot.entities import Asset >>> >>> self.create_order( >>> Asset(symbol='BTC', asset_type=Asset.AssetType.CRYPTO), >>> .50, >>> 'buy', >>> quote=Asset(symbol='USDT', asset_type='crypto'), >>> )
Orders for crypto markets are restriced to:
market
,limit
,stop_limit
.Crypto markets’ orders are simple. There are no compound orders such
oco
orbracket
. Also, duration of orders are all GTC.- Parameters:
asset (str or Asset) – The asset that will be traded. If this is just a stock, then
str
is sufficient. However, all assets other than stocks must useAsset
.quantity (int string Decimal (float will deprecate)) – The number of shares or units to trade. One may enter an int, a string number eg: “3.213”, or a Decimal obect, eg: Decimal(“3.213”). Internally all will convert to Decimal.
side (str) – Whether the order is
buy
orsell
.order_type (Order.OrderType) – The type of order. Order types include:
'market'
,'limit'
,'stop'
,'stop_limit'
,trailing_stop
We will try to determine the order type if you do not specify it.order_class (Order.OrderClass) – The class of the order. Order classes include:
'simple'
,'bracket'
,'oco'
,'oto'
,'multileg'
limit_price (float) – A Limit order is an order to buy or sell at a specified price or better. The Limit order ensures that if the order fills, it will not fill at a price less favorable than your limit price, but it does not guarantee a fill.
stop_price (float) – A Stop order is an instruction to submit a buy or sell market order if and when the user-specified stop trigger price is attained or penetrated.
stop_limit_price (float) – Stop loss with limit price used to ensure a specific fill price when the stop price is reached. For more information, visit: https://www.investopedia.com/terms/s/stop-limitorder.asp
secondary_limit_price (float) – Limit price used for child orders of Advanced Orders like Bracket Orders and One Triggers Other (OTO) orders. One Cancels Other (OCO) orders do not use this field as the primary prices can specify all info needed to execute the OCO (because there is no corresponding Entry Order).
secondary_stop_price (float) – Stop price used for child orders of Advanced Orders like Bracket Orders and One Triggers Other (OTO) orders. One Cancels Other (OCO) orders do not use this field as the primary prices can specify all info needed to execute the OCO (because there is no corresponding Entry Order).
secondary_stop_limit_price (float) – Stop limit price used for child orders of Advanced Orders like Bracket Orders and One Triggers Other (OTO) orders. One Cancels Other (OCO) orders do not use this field as the primary prices can specify all info needed to execute the OCO (because there is no corresponding Entry Order).
stop_loss_limit_price (float) – Stop loss with limit price used for bracket orders and one cancels other orders. (Depricated, use ‘stop_limit_price` instead)
time_in_force (str) –
- Amount of time the order is in force. Order types include:
'day'
Orders valid for the remainder of the day.'gtc'
Good until cancelled.'gtd'
Good until date.
(Default: ‘day’)
good_till_date (datetime.datetime) – This is the time order is valid for Good Though Date orders.
take_profit_price (float) – Limit price used for bracket orders and one cancels other orders. (Depricated, use ‘secondary_limit_price` instead)
stop_loss_price (float) – Stop price used for bracket orders and one cancels other orders. (Depricated, use ‘secondary_stop_price` instead)
trail_price (float) – Trailing stop orders allow you to continuously and automatically keep updating the stop price threshold based on the stock price movement. trail_price sets the trailing price in dollars.
trail_percent (float) – Trailing stop orders allow you to continuously and automatically keep updating the stop price threshold based on the stock price movement. E.g. 0.05 would be a 5% trailing stop. trail_percent sets the trailing price in percent.
secondary_trail_price (float) – Trailing stop price for child orders of Advanced Orders like Bracket or OTO orders.
secondary_trail_percent (float) – Trailing stop percent for child orders of Advanced Orders like Bracket or OTO orders.
exchange (str) – The exchange where the order will be placed.
Default = 'SMART'
quote (Asset) – This is the currency that the main coin being bought or sold will exchange in. For example, if trading
BTC/ETH
this parameter will be ‘ETH’ (as an Asset object).custom_params (dict) – A dictionary of custom parameters that can be used to pass additional information to the broker. This is useful for passing custom parameters to the broker that are not supported by Lumibot. E.g. custom_params={“leverage”: 3} for Kraken margin trading.
type (Order.OrderType) – Deprecated, use ‘order_type’ instead
Reading (Further)
-------
confusing (- Although the term "stop_limit" can be) – not a true limit price nor bracket-style order. For more information, visit: https://www.investopedia.com/terms/s/stop-limitorder.asp
modifier (it is important to remember that this is a StopLoss) – not a true limit price nor bracket-style order. For more information, visit: https://www.investopedia.com/terms/s/stop-limitorder.asp
- Returns:
Order object ready to be submitted for trading.
- Return type:
Examples
>>> # For a market buy order >>> order = self.create_order("SPY", 100, "buy") >>> self.submit_order(order)
>>> # For a limit order where limit price = 100 >>> limit_order = self.create_order("SPY", 1, "buy", limit_price=100) >>> self.submit_order(limit_order)
>>> # Sell 100 shares of TLT >>> order = self.create_order("TLT", 100, "sell") >>> self.submit_order(order)
>>> # For a stop loss order >>> order = self.create_order("SPY", 100, "buy", stop_price=100.00) >>> self.submit_order(order)
>>> # For a stop limit order >>> order = self.create_order("SPY", 100, "buy", stop_price=100.00, stop_limit_price=99.95) >>> self.submit_order(order)
>>> # For a market sell order >>> order = self.create_order("SPY", 100, "sell") >>> self.submit_order(order)
>>> # For a limit sell order >>> order = self.create_order("SPY", 100, "sell", limit_price=100.00) >>> self.submit_order(order)
>>> # For an order with a trailing stop >>> order = self.create_order("SPY", 100, "buy", trail_price=100.00) >>> self.submit_order(order)
>>> # For an OCO order - No entry order specified, only exit order info. >>> 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, # Stop loss modifier (optional) >>> order_class=Order.OrderClass.OCO, >>> )
>>> # For a bracket order - One Entry order with a Profit and Loss exit orders (2 child orders). >>> order = self.create_order( >>> "SPY", >>> 100, >>> "buy", >>> limit_price=limit, # When the Entry order will execute >>> secondary_limit_price=sec_limit, # When the child Profit Exit order will execute >>> secondary_stop_price=stop_loss, # When the child Loss Exit order will execute >>> secondary_stop_limit_price=stop_loss_limit, # Child loss modifier (optional) >>> order_class=Order.OrderClass.BRACKET, >>> )
>>> # For a bracket order with a trailing stop >>> order = self.create_order( >>> "SPY", >>> 100, >>> "buy", >>> limit_price=limit, # When to Enter >>> secondary_limit_price=sec_limit, # When to Exit Profit >>> secondary_stop_price=stop_loss, # When to Exit Loss >>> secondary_trail_percent=trail_percent, # Exit stop modifier (optional) >>> order_class=Order.OrderClass.BRACKET, >>> )
>>> # For an OTO order - One Entry, only a single Exit criteria is allowed (1/2 of a bracket order) >>> order = self.create_order( >>> "SPY", >>> 100, >>> "buy", >>> limit_price=limit, # When to Enter >>> secondary_stop_price=stop_loss, # When to Exit >>> order_class=Order.OrderClass.OTO, >>> )
>>> # For a 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)
>>> # For a futures order with a 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, # When to Enter >>> secondary_stop_price=stop_loss, # When to Exit >>> secondary_trail_percent=trail_percent, # Exit modifier (optional) >>> order_class=Order.OrderClass.OTO, >>> ) >>> self.submit_order(order)
>>> # For an 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)
>>> # For an option order with a trailing stop >>> 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)
>>> # For a 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)
>>> # For a options order with a limit price >>> 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)
>>> # For a options order with a trailing stop >>> 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, # When to Enter >>> secondary_stop_price=stop_loss, # When to Exit >>> secondary_trail_percent=trail_percent, # Exit modifier (optional) >>> order_class=Order.OrderClass.OTO, >>> ) >>> self.submit_order(order)
>>> # For a cryptocurrency order with a market price >>> 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)
>>> # Placing a limit order with a quote asset for cryptocurrencies >>> from lumibot.entities import Asset >>> >>> base = Asset("BTC", asset_type=Aset.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)