self.create_order¶
- strategies.strategy.Strategy.create_order(self, asset, quantity, side, limit_price=None, stop_price=None, time_in_force='day', 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='SMART')¶
Creates a new order for this specific strategy. Once created, an order must still be submitted.
- 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 use Asset.
quantity (float) – The number of shares or units to trade.
side (str) – Whether the order is buy or sell.
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.
time_in_force – Amount of time the order is in force. Default: ‘day’
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. trail_price sets the trailing price in percent.
position_filled (bool) – The order has been filled.
exchange (str) – The exchange where the order will be placed. Default = SMART
- Returns
Order object ready to be submitted for trading.
- Return type
Example
>>> # 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, >>> position_filled=True, >>> )
>>> # 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, >>> )
>>> # 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, >>> )
>>> # For an OTO order >>> order = self.create_order( >>> "SPY", >>> 100, >>> "sell", >>> take_profit_price=limit, >>> stop_loss_price=stop_loss, >>> )
>>> # For a futures order >>> asset = Asset("ES", asset_type="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 >>> asset = Asset("ES", asset_type="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 >>> asset = Asset("SPY", asset_type="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 >>> asset = Asset("SPY", asset_type="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 >>> asset = Asset( >>> symbol="CHF", >>> currency="EUR", >>> asset_type="forex", >>> ) >>> order = self.create_order(asset, 100, "buy", limit_price=100.00) >>> self.submit_order(order)
>>> # For a options order with a limit price >>> asset = Asset("SPY", asset_type="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 >>> asset = Asset("SPY", asset_type="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)