or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

authentication.mdclient-api.mdenums-constants.mdexceptions.mdindex.mdorder-management.mdstreaming.mdutilities.md

order-management.mddocs/

0

# Order Management

1

2

The order management system provides comprehensive classes for building and managing trade orders, from simple buy/sell orders to complex multi-leg options strategies. The system uses builder pattern classes to construct orders with proper validation and structure.

3

4

## Core Imports

5

6

```python

7

from td.orders import Order, OrderLeg

8

from td.enums import ORDER_TYPE, ORDER_SESSION, DURATION, ORDER_INSTRUCTIONS

9

```

10

11

## Capabilities

12

13

### Order Class

14

15

Main order builder class for constructing complete trading orders with all necessary parameters and legs.

16

17

```python { .api }

18

class Order:

19

def __init__(self) -> None: ...

20

def order_price(self, price: float) -> 'Order': ...

21

def order_type(self, order_type: str) -> 'Order': ...

22

def order_session(self, session: str) -> 'Order': ...

23

def order_duration(self, duration: str, cancel_time: str = None) -> 'Order': ...

24

def stop_price(self, stop_price: float) -> 'Order': ...

25

def stop_price_offset(self, stop_price_offset: float) -> 'Order': ...

26

def stop_type(self, stop_type: str) -> 'Order': ...

27

def stop_price_link_type(self, stop_price_link_type: str) -> 'Order': ...

28

def stop_price_link_basis(self, stop_price_link_basis: str) -> 'Order': ...

29

def complex_order_type(self, complex_order_strategy_type: str) -> 'Order': ...

30

def order_strategy_type(self, order_strategy_type: str) -> 'Order': ...

31

def add_order_leg(self, order_leg: 'OrderLeg') -> None: ...

32

def delete_order_leg(self, key: str, index: int) -> None: ...

33

def create_child_order_strategy(self) -> 'Order': ...

34

def add_child_order_strategy(self, child_order_strategy: 'Order') -> None: ...

35

def delete_child_order_strategy(self, key: str, index: int) -> None: ...

36

```

37

38

**Methods:**

39

- `order_price()`: Sets the limit price for the order

40

- `order_type()`: Sets order type (MARKET, LIMIT, STOP, STOP_LIMIT, etc.)

41

- `order_session()`: Sets trading session (NORMAL, AM, PM, SEAMLESS)

42

- `order_duration()`: Sets order duration (DAY, GOOD_TILL_CANCEL, FILL_OR_KILL)

43

- `stop_price()`: Sets stop price for stop orders

44

- `stop_price_offset()`: Sets stop price offset amount

45

- `stop_type()`: Sets stop order type

46

- `stop_price_link_type()`: Sets stop price link type

47

- `stop_price_link_basis()`: Sets stop price link basis

48

- `complex_order_type()`: Sets complex order strategy type

49

- `order_strategy_type()`: Sets order strategy type (SINGLE, OCO, TRIGGER)

50

- `add_order_leg()`: Adds an order leg to the order

51

- `delete_order_leg()`: Removes an order leg by key and index

52

- `create_child_order_strategy()`: Creates a child order strategy

53

- `add_child_order_strategy()`: Adds a child order strategy

54

- `delete_child_order_strategy()`: Removes a child order strategy

55

56

### OrderLeg Class

57

58

Individual order leg builder for multi-leg orders and complex strategies.

59

60

```python { .api }

61

class OrderLeg:

62

def __init__(self) -> None: ...

63

def order_leg_instruction(self, instruction: str) -> 'OrderLeg': ...

64

def order_leg_asset(self, asset_type: str, symbol: str) -> 'OrderLeg': ...

65

def order_leg_quantity(self, quantity: int) -> 'OrderLeg': ...

66

def order_leg_price(self, price: float) -> 'OrderLeg': ...

67

def order_leg_quantity_type(self, quantity_type: str) -> 'OrderLeg': ...

68

def copy(self) -> 'OrderLeg': ...

69

```

70

71

**Methods:**

72

- `order_leg_instruction()`: Sets order instruction (BUY, SELL, BUY_TO_OPEN, SELL_TO_CLOSE, etc.)

73

- `order_leg_asset()`: Sets asset type and symbol (EQUITY, OPTION, etc.)

74

- `order_leg_quantity()`: Sets quantity for the order leg

75

- `order_leg_price()`: Sets price for the order leg

76

- `order_leg_quantity_type()`: Sets quantity type (SHARES, DOLLARS, ALL_SHARES)

77

- `copy()`: Returns a copy of the order leg

78

79

## Usage Examples

80

81

### Simple Stock Orders

82

83

```python

84

from td.orders import Order, OrderLeg

85

from td.enums import ORDER_TYPE, ORDER_SESSION, DURATION, ORDER_INSTRUCTIONS

86

87

# Market order to buy 100 shares of AAPL

88

buy_order = Order()

89

buy_order.order_type(ORDER_TYPE.MARKET)

90

buy_order.order_session(ORDER_SESSION.NORMAL)

91

buy_order.order_duration(DURATION.DAY)

92

93

# Create order leg

94

buy_leg = OrderLeg()

95

buy_leg.order_leg_instruction(ORDER_INSTRUCTIONS.BUY)

96

buy_leg.order_leg_asset('EQUITY', 'AAPL')

97

buy_leg.order_leg_quantity(100)

98

99

# Add leg to order

100

buy_order.add_order_leg(buy_leg)

101

102

# Limit order to sell 50 shares of MSFT at $300

103

sell_order = Order()

104

sell_order.order_type(ORDER_TYPE.LIMIT)

105

sell_order.order_price(300.00)

106

sell_order.order_session(ORDER_SESSION.NORMAL)

107

sell_order.order_duration(DURATION.GOOD_TILL_CANCEL)

108

109

sell_leg = OrderLeg()

110

sell_leg.order_leg_instruction(ORDER_INSTRUCTIONS.SELL)

111

sell_leg.order_leg_asset('EQUITY', 'MSFT')

112

sell_leg.order_leg_quantity(50)

113

114

sell_order.add_order_leg(sell_leg)

115

```

116

117

### Stop Loss Orders

118

119

```python

120

# Stop loss order for TSLA at $200

121

stop_loss = Order()

122

stop_loss.order_type(ORDER_TYPE.STOP)

123

stop_loss.stop_price(200.00)

124

stop_loss.order_session(ORDER_SESSION.NORMAL)

125

stop_loss.order_duration(DURATION.GOOD_TILL_CANCEL)

126

127

stop_leg = OrderLeg()

128

stop_leg.order_leg_instruction(ORDER_INSTRUCTIONS.SELL)

129

stop_leg.order_leg_asset('EQUITY', 'TSLA')

130

stop_leg.order_leg_quantity(100)

131

132

stop_loss.add_order_leg(stop_leg)

133

134

# Stop limit order

135

stop_limit = Order()

136

stop_limit.order_type(ORDER_TYPE.STOP_LIMIT)

137

stop_limit.stop_price(195.00)

138

stop_limit.order_price(194.00)

139

stop_limit.order_session(ORDER_SESSION.NORMAL)

140

stop_limit.order_duration(DURATION.DAY)

141

142

stop_limit_leg = OrderLeg()

143

stop_limit_leg.order_leg_instruction(ORDER_INSTRUCTIONS.SELL)

144

stop_limit_leg.order_leg_asset('EQUITY', 'TSLA')

145

stop_limit_leg.order_leg_quantity(100)

146

147

stop_limit.add_order_leg(stop_limit_leg)

148

```

149

150

### Options Orders

151

152

```python

153

# Buy to open call option

154

call_order = Order()

155

call_order.order_type(ORDER_TYPE.LIMIT)

156

call_order.order_price(2.50)

157

call_order.order_session(ORDER_SESSION.NORMAL)

158

call_order.order_duration(DURATION.DAY)

159

160

call_leg = OrderLeg()

161

call_leg.order_leg_instruction(ORDER_INSTRUCTIONS.BUY_TO_OPEN)

162

call_leg.order_leg_asset('OPTION', 'AAPL_021724C150')

163

call_leg.order_leg_quantity(1)

164

165

call_order.add_order_leg(call_leg)

166

167

# Sell to close put option

168

put_order = Order()

169

put_order.order_type(ORDER_TYPE.LIMIT)

170

put_order.order_price(1.25)

171

put_order.order_session(ORDER_SESSION.NORMAL)

172

put_order.order_duration(DURATION.DAY)

173

174

put_leg = OrderLeg()

175

put_leg.order_leg_instruction(ORDER_INSTRUCTIONS.SELL_TO_CLOSE)

176

put_leg.order_leg_asset('OPTION', 'AAPL_021724P140')

177

put_leg.order_leg_quantity(2)

178

179

put_order.add_order_leg(put_leg)

180

```

181

182

### Multi-Leg Options Strategies

183

184

```python

185

from td.enums import COMPLEX_ORDER_STRATEGY_TYPE

186

187

# Covered call strategy

188

covered_call = Order()

189

covered_call.order_type(ORDER_TYPE.NET_DEBIT)

190

covered_call.complex_order_type(COMPLEX_ORDER_STRATEGY_TYPE.COVERED)

191

covered_call.order_session(ORDER_SESSION.NORMAL)

192

covered_call.order_duration(DURATION.DAY)

193

194

# Long stock leg

195

stock_leg = OrderLeg()

196

stock_leg.order_leg_instruction(ORDER_INSTRUCTIONS.BUY)

197

stock_leg.order_leg_asset('EQUITY', 'AAPL')

198

stock_leg.order_leg_quantity(100)

199

200

# Short call leg

201

call_leg = OrderLeg()

202

call_leg.order_leg_instruction(ORDER_INSTRUCTIONS.SELL_TO_OPEN)

203

call_leg.order_leg_asset('OPTION', 'AAPL_030724C160')

204

call_leg.order_leg_quantity(1)

205

206

covered_call.add_order_leg(stock_leg)

207

covered_call.add_order_leg(call_leg)

208

209

# Vertical spread

210

vertical = Order()

211

vertical.order_type(ORDER_TYPE.NET_DEBIT)

212

vertical.complex_order_type(COMPLEX_ORDER_STRATEGY_TYPE.VERTICAL)

213

vertical.order_price(1.50)

214

vertical.order_session(ORDER_SESSION.NORMAL)

215

vertical.order_duration(DURATION.DAY)

216

217

# Buy lower strike

218

buy_leg = OrderLeg()

219

buy_leg.order_leg_instruction(ORDER_INSTRUCTIONS.BUY_TO_OPEN)

220

buy_leg.order_leg_asset('OPTION', 'AAPL_030724C150')

221

buy_leg.order_leg_quantity(1)

222

223

# Sell higher strike

224

sell_leg = OrderLeg()

225

sell_leg.order_leg_instruction(ORDER_INSTRUCTIONS.SELL_TO_OPEN)

226

sell_leg.order_leg_asset('OPTION', 'AAPL_030724C155')

227

sell_leg.order_leg_quantity(1)

228

229

vertical.add_order_leg(buy_leg)

230

vertical.add_order_leg(sell_leg)

231

```

232

233

### Conditional Orders (OCO)

234

235

```python

236

# One-Cancels-Other order

237

oco_order = Order()

238

oco_order.order_strategy_type('OCO')

239

oco_order.order_session(ORDER_SESSION.NORMAL)

240

241

# Profit target child order

242

profit_target = Order()

243

profit_target.order_type(ORDER_TYPE.LIMIT)

244

profit_target.order_price(110.00)

245

profit_target.order_duration(DURATION.GOOD_TILL_CANCEL)

246

247

profit_leg = OrderLeg()

248

profit_leg.order_leg_instruction(ORDER_INSTRUCTIONS.SELL)

249

profit_leg.order_leg_asset('EQUITY', 'AAPL')

250

profit_leg.order_leg_quantity(100)

251

profit_target.add_order_leg(profit_leg)

252

253

# Stop loss child order

254

stop_loss = Order()

255

stop_loss.order_type(ORDER_TYPE.STOP)

256

stop_loss.stop_price(95.00)

257

stop_loss.order_duration(DURATION.GOOD_TILL_CANCEL)

258

259

stop_leg = OrderLeg()

260

stop_leg.order_leg_instruction(ORDER_INSTRUCTIONS.SELL)

261

stop_leg.order_leg_asset('EQUITY', 'AAPL')

262

stop_leg.order_leg_quantity(100)

263

stop_loss.add_order_leg(stop_leg)

264

265

# Add child strategies

266

oco_order.add_child_order_strategy(profit_target)

267

oco_order.add_child_order_strategy(stop_loss)

268

```

269

270

### Working with Orders

271

272

```python

273

# Copy an order leg for similar orders

274

original_leg = OrderLeg()

275

original_leg.order_leg_instruction(ORDER_INSTRUCTIONS.BUY)

276

original_leg.order_leg_asset('EQUITY', 'AAPL')

277

original_leg.order_leg_quantity(100)

278

279

# Create a copy for a different order

280

copied_leg = original_leg.copy()

281

282

# Modify the copy

283

copied_leg.order_leg_asset('EQUITY', 'MSFT')

284

285

# Remove order legs

286

order = Order()

287

order.add_order_leg(original_leg)

288

order.add_order_leg(copied_leg)

289

290

# Delete specific leg (key and index depend on internal structure)

291

order.delete_order_leg('orderLegCollection', 1)

292

293

# Delete child order strategies

294

order.delete_child_order_strategy('childOrderStrategies', 0)

295

```

296

297

## Order Validation

298

299

Orders built with these classes are automatically structured for the TD Ameritrade API format. The builder pattern ensures required fields are set and provides validation through the enum values. Use the enums module for valid parameter values to avoid API errors.