self.create_order¶
- lumibot.strategies.strategy.Strategy.create_order(self, asset, quantity, side, limit_price=None, stop_price=None, time_in_force='gtc', good_till_date=None, take_profit_price=None, stop_loss_price=None, stop_loss_limit_price=None, trail_price=None, trail_percent=None, position_filled=False, exchange=None, quote=None, pair=None, type=None, custom_params={})¶
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
.type (str) – The type of order. Order types include:
'market'
,'limit'
,'stop'
,'stop_limit'
,trailing_stop
,'oco'
,'bracket'
,'oto'
. We will try to determine the order type if you do not specify it. It is mandatory to set the type for advanced order types such as'oco'
,'bracket'
,'oto'
.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.
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.
stop_loss_price (float) – Stop price used for bracket orders and one cancels other orders.
stop_loss_limit_price (float) – Stop loss with limit price used for bracket orders and one cancels other orders.
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.
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.
- 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", limit_price=100.00, stop_price=100.00) >>> 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 >>> order = self.create_order( >>> "SPY", >>> 100, >>> "sell", >>> take_profit_price=limit, >>> stop_loss_price=stop_loss, >>> type="oco", >>> )
>>> # For a bracket order >>> order = self.create_order( >>> "SPY", >>> 100, >>> "sell", >>> take_profit_price=limit, >>> stop_loss_price=stop_loss, >>> stop_loss_limit_price=stop_loss_limit, >>> type="bracket", >>> )
>>> # For a bracket order with a trailing stop >>> order = self.create_order( >>> "SPY", >>> 100, >>> "sell", >>> trail_percent=trail_percent, >>> take_profit_price=limit, >>> stop_loss_price=stop_loss, >>> stop_loss_limit_price=stop_loss_limit, >>> type="bracket", >>> )
>>> # For an OTO order >>> order = self.create_order( >>> "SPY", >>> 100, >>> "sell", >>> take_profit_price=limit, >>> stop_loss_price=stop_loss, >>> type="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", >>> trail_percent=trail_percent, >>> limit_price=limit, >>> stop_price=stop_loss, >>> ) >>> 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", >>> trail_percent=trail_percent, >>> limit_price=limit, >>> stop_price=stop_loss, >>> ) >>> 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", >>> trail_percent=trail_percent, >>> limit_price=limit, >>> stop_price=stop_loss, >>> ) >>> 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)