Order Templates¶
tda-api
strives to be easy to use. This means making it easy to do simple
things, while making it possible to do complicated things. Order construction is
a major challenge to this mission: both simple and complicated orders use the
same format, meaning simple orders require a surprising amount of sophistication
to place.
We get around this by providing templates that make it easy to place common orders, while allowing advanced users to modify the orders returned from the templates to create more complex ones. Very advanced users can even create their own orders from scratch. This page describes the simple templates, while the OrderBuilder Reference page documents the order builder in all its complexity.
Using These Templates¶
These templates serve two purposes. First, they are designed to choose defaults so you can immediately place them. These defaults are:
- All orders execute during the current normal trading session. If placed outside of trading hours, the execute during the next normal trading session.
- Time-in-force is set to
DAY
.- All other fields (such as requested destination, etc.) are left unset, meaning they receive default treatment from TD Ameritrade. Note this treatment depends on TDA’s implementation, and may change without warning.
Secondly, they serve as starting points for building more complex order types.
All templates return a pre-populated OrderBuilder
object, meaning complex
functionality can be specified by modifying the returned object. For example,
here is how you would place an order to buy GOOG
for no more than $1250 at
any time in the next six months:
from tda.orders.equities import equity_buy_limit
from tda.orders.common import Duration, Session
client = ... # See "Authentication and Client Creation"
client.place_order(
equity_buy_limit('GOOG', 1, 1250.0)
.set_duration(Duration.GOOD_TILL_CANCEL)
.set_session(Session.SEAMLESS)
.build())
You can find a full reference for all supported fields in OrderBuilder Reference.
Equity Templates¶
Buy orders¶
-
tda.orders.equities.
equity_buy_market
(symbol, quantity)¶ Returns a pre-filled
OrderBuilder
for an equity buy market order.
-
tda.orders.equities.
equity_buy_limit
(symbol, quantity, price)¶ Returns a pre-filled
OrderBuilder
for an equity buy limit order.
Sell orders¶
-
tda.orders.equities.
equity_sell_market
(symbol, quantity)¶ Returns a pre-filled
OrderBuilder
for an equity sell market order.
-
tda.orders.equities.
equity_sell_limit
(symbol, quantity, price)¶ Returns a pre-filled
OrderBuilder
for an equity sell limit order.
Sell short orders¶
-
tda.orders.equities.
equity_sell_short_market
(symbol, quantity)¶ Returns a pre-filled
OrderBuilder
for an equity short sell market order.
-
tda.orders.equities.
equity_sell_short_limit
(symbol, quantity, price)¶ Returns a pre-filled
OrderBuilder
for an equity short sell limit order.
Buy to cover orders¶
-
tda.orders.equities.
equity_buy_to_cover_market
(symbol, quantity)¶ Returns a pre-filled
OrderBuilder
for an equity buy-to-cover market order.
-
tda.orders.equities.
equity_buy_to_cover_limit
(symbol, quantity, price)¶ Returns a pre-filled
OrderBuilder
for an equity buy-to-cover limit order.
Options Templates¶
TD Ameritrade supports over a dozen options strategies, each of which involve a
precise structure in the order builder. tda-api
is slowly gaining support
for these strategies, and they are documented here as they become ready for use.
As time goes on, more templates will be added here.
In the meantime, you can construct all supported options orders using the OrderBuilder, although you will have to construct them yourself.
Note orders placed using these templates may be rejected, depending on the user’s options trading authorization.
Building Options Symbols¶
All templates require option symbols, which are somewhat more involved than equity symbols. They encode the underlying, the expiration date, option type (put or call) and the strike price. They are especially tricky to extract because both the TD Ameritrade UI and the thinkorswim UI don’t reveal the symbol in the option chain view.
Real trading symbols can be found by requesting the Option Chain. They
can also be built using the OptionSymbol
helper, which provides utilities
for creating options symbols. Note it only emits syntactically correct symbols
and does not validate whether the symbol actually represents a traded option:
from tda.order.options import OptionSymbol
symbol = OptionSymbol(
'TSLA', datetime.date(year=2020, month=11, day=20), 'P', '1360').build()
-
class
tda.orders.options.
OptionSymbol
(underlying_symbol, expiration_date, contract_type, strike_price_as_string)¶ Construct an option symbol from its constituent parts. Options symbols have the following format:
[Underlying]_[Two digit month][Two digit day][Two digit year]['P' or 'C'][Strike price]
. Examples include:GOOG_012122P620
: GOOG Jan 21 2022 620 PutTSLA_112020C1360
: TSLA Nov 20 2020 1360 CallSPY_121622C335
: SPY Dec 16 2022 335 Call
Note while each of the individual parts is validated by itself, the option symbol itself may not represent a traded option:
- Some underlyings do not support options.
- Not all dates have valid option expiration dates.
- Not all strike prices are valid options strikes.
You can use
get_option_chain()
to obtain real option symbols for an underlying, as well as extensive data in pricing, bid/ask spread, volume, etc.Parameters: - underlying_symbol – Symbol of the underlying. Not validated.
- expiration_date – Expiration date. Accepts
datetime.date
,datetime.datetime
, or strings with the format[Two digit month][Two digit day][Two digit year]
. - contract_type –
P
for put orC
for call. - strike_price_as_string – Strike price, represented by a string as you would see at the end of a real option symbol.
Single Options¶
Buy and sell single options.
-
tda.orders.options.
option_buy_to_open_market
(symbol, quantity)¶ Returns a pre-filled
OrderBuilder
for a buy-to-open market order.
-
tda.orders.options.
option_buy_to_open_limit
(symbol, quantity, price)¶ Returns a pre-filled
OrderBuilder
for a buy-to-open limit order.
-
tda.orders.options.
option_sell_to_open_market
(symbol, quantity)¶ Returns a pre-filled
OrderBuilder
for a sell-to-open market order.
-
tda.orders.options.
option_sell_to_open_limit
(symbol, quantity, price)¶ Returns a pre-filled
OrderBuilder
for a sell-to-open limit order.
-
tda.orders.options.
option_buy_to_close_market
(symbol, quantity)¶ Returns a pre-filled
OrderBuilder
for a buy-to-close market order.
-
tda.orders.options.
option_buy_to_close_limit
(symbol, quantity, price)¶ Returns a pre-filled
OrderBuilder
for a buy-to-close limit order.
-
tda.orders.options.
option_sell_to_close_market
(symbol, quantity)¶ Returns a pre-filled
OrderBuilder
for a sell-to-close market order.
-
tda.orders.options.
option_sell_to_close_limit
(symbol, quantity, price)¶ Returns a pre-filled
OrderBuilder
for a sell-to-close limit order.
Vertical Spreads¶
Vertical spreads are a complex option strategy that provides both limited upside and limited downside. They are constructed using by buying an option at one strike while simultaneously selling another option with the same underlying and expiration date, except with a different strike, and they can be constructed using either puts or call. You can find more information about this strategy on Investopedia
tda-api
provides utilities for opening and closing vertical spreads in
various ways. It follows the standard (bull/bear) (put/call)
naming
convention, where the name specifies the market attitude and the option type
used in construction.
For consistency’s sake, the option with the smaller strike price is always passed first, followed by the higher strike option. You can find the option symbols by consulting the return value of the Option Chain client call.
Call Verticals¶
-
tda.orders.options.
bull_call_vertical_open
(long_call_symbol, short_call_symbol, quantity, net_debit)¶ Returns a pre-filled
OrderBuilder
that opens a bull call vertical position. See Vertical Spreads for details.
-
tda.orders.options.
bull_call_vertical_close
(long_call_symbol, short_call_symbol, quantity, net_credit)¶ Returns a pre-filled
OrderBuilder
that closes a bull call vertical position. See Vertical Spreads for details.
-
tda.orders.options.
bear_call_vertical_open
(short_call_symbol, long_call_symbol, quantity, net_credit)¶ Returns a pre-filled
OrderBuilder
that opens a bear call vertical position. See Vertical Spreads for details.
-
tda.orders.options.
bear_call_vertical_close
(short_call_symbol, long_call_symbol, quantity, net_debit)¶ Returns a pre-filled
OrderBuilder
that closes a bear call vertical position. See Vertical Spreads for details.
Put Verticals¶
-
tda.orders.options.
bull_put_vertical_open
(long_put_symbol, short_put_symbol, quantity, net_credit)¶ Returns a pre-filled
OrderBuilder
that opens a bull put vertical position. See Vertical Spreads for details.
-
tda.orders.options.
bull_put_vertical_close
(long_put_symbol, short_put_symbol, quantity, net_debit)¶ Returns a pre-filled
OrderBuilder
that closes a bull put vertical position. See Vertical Spreads for details.
-
tda.orders.options.
bear_put_vertical_open
(short_put_symbol, long_put_symbol, quantity, net_debit)¶ Returns a pre-filled
OrderBuilder
that opens a bear put vertical position. See Vertical Spreads for details.
-
tda.orders.options.
bear_put_vertical_close
(short_put_symbol, long_put_symbol, quantity, net_credit)¶ Returns a pre-filled
OrderBuilder
that closes a bear put vertical position. See Vertical Spreads for details.
Utility Methods¶
These methods return orders that represent complex multi-order strategies,
namely “one cancels other” and “first triggers second” strategies. Note they
expect all their parameters to be of type OrderBuilder
. You can construct
these orders using the templates above or by
creating them from scratch.
-
tda.orders.common.
one_cancels_other
(order1, order2)¶ If one of the orders is executed, immediately cancel the other.
-
tda.orders.common.
first_triggers_second
(first_order, second_order)¶ If
first_order
is executed, immediately placesecond_order
.
What happened to EquityOrderBuilder
?¶
Long-time users may notice that this documentation no longer mentions the
EquityOrderBuilder
class. This class used to be used to create equities
orders, and offered a subset of the functionality offered by the
OrderBuilder. This class has been deprecated in favor of
the order builder and the above templates, and will be removed from a future
release.
In the meantime, you can continue using this order builder, although you really
should migrate to the new one soon. You can find documentation for this class in
the older versions of tda-api
’s
documentation.