Creating Order Specifications¶
The Client.place_order()
method expects
a rather complex JSON object that describes the desired order. TDA provides some
example order specs to
illustrate the process and provides a schema in the place order documentation, but beyond that we’re on our own.
The tda.orders
module provides an incomplete set of helpers for building
these order specs. The aim is to make it impossible to build an invalid JSON
object. For example, here is how you might use this module to place a market
order for ten shares of Apple common stock:
from tda.orders import EquityOrderBuilder, Duration, Session
builder = EquityOrderBuilder('AAPL', 10)
builder.set_instruction(EquityOrderBuilder.Instruction.SELL)
builder.set_order_type(EquityOrderBuilder.OrderType.MARKET)
builder.set_duration(Duration.DAY)
builder.set_session(Session.NORMAL)
client = ... # Get a client however you see fit
account_id = 12345678
resp = client.place_order(account_id, builder.build())
assert resp.ok
Common Values¶
-
class
tda.orders.
Duration
¶ An enumeration.
-
DAY
= 'DAY'¶
-
GOOD_TILL_CANCEL
= 'GOOD_TILL_CANCEL'¶
-
FILL_OR_KILL
= 'FILL_OR_KILL'¶
-
-
class
tda.orders.
Session
¶ An enumeration.
-
NORMAL
= 'NORMAL'¶
-
AM
= 'AM'¶
-
PM
= 'PM'¶
-
SEAMESS
= 'SEAMLESS'¶
-
-
exception
tda.orders.
InvalidOrderException
¶ Raised when attempting to build an incomplete order
Equity Orders¶
-
class
tda.orders.
EquityOrderBuilder
(symbol, quantity)¶ Helper class to construct equity orders.
-
__init__
(symbol, quantity)¶ Create an order for the given symbol and quantity. Note all unspecified parameters must be set prior to building the order spec.
Parameters: - symbol – Symbol for the order
- quantity – Quantity of the order
-
set_instruction
(instruction)¶ Set the order instruction
-
set_order_type
(order_type)¶ Set the order type
-
set_price
(price)¶ Set the order price. Must be set for
LIMIT
orders.
-
set_duration
(duration)¶ Set the order duration
-
set_session
(session)¶ Set the order’s session
-
build
()¶ Build the order spec.
Raises: InvalidOrderException – if the order is not fully specified
-
matches
(order)¶ Takes a real object, as might be returned from the TD Ameritrade API, and indicates whether this order object matches it. Returns true if the given order if the given order could have been placed by calling
Client.place_order()
with this order.This method may be called on incomplete orders builders (builders whose
build()
method would fail if called. In such a case, unset values are ignored and have no effect on filtering.
-