Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

placing a buy order using the TD Ameritrade API

Last updated on 1 month ago
C
caaSuper Admin
Posted 1 month ago
Here’s a simple example of placing a **buy order** using the TD Ameritrade API. This program assumes you already have a valid **access token**.

---

### Required Steps

1. **Obtain Access Token**:
2. **Account ID**: Get your account ID from your TD Ameritrade account.
3. **Buy Order Details**: Define the stock symbol, quantity, price, and order type.

---
C
caaSuper Admin
Posted 1 month ago
import requests
import json

# Replace these with your details
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN" # Replace with your access token
ACCOUNT_ID = "YOUR_ACCOUNT_ID" # Replace with your TD Ameritrade account ID

def place_buy_order(symbol, quantity, price):
 url = f"https://api.tdameritrade.com/v1/accounts/{ACCOUNT_ID}/orders"
 
 headers = {
 "Authorization": f"Bearer {ACCESS_TOKEN}",
 "Content-Type": "application/json"
 }
 
 # Define the order
 order_payload = {
 "orderType": "LIMIT",
 "session": "NORMAL", # Options: NORMAL, AM, PM, SEAMLESS
 "duration": "DAY", # Options: DAY, GTC, etc.
 "orderStrategyType": "SINGLE",
 "orderLegCollection": [
 {
 "instruction": "BUY", # BUY or SELL
 "quantity": quantity,
 "instrument": {
 "symbol": symbol,
 "assetType": "EQUITY" # Use "OPTION" for options trading
 }
 }
 ],
 "price": price # Limit price
 }

 # Make the API request
 response = requests.post(url, headers=headers, data=json.dumps(order_payload))

 if response.status_code == 201:
 print("Order placed successfully!")
 else:
 print(f"Error placing order: {response.status_code}")
 print(response.json())

# Example: Place a limit buy order for 10 shares of AAPL at $150
place_buy_order("AAPL", 10, 150.00)
C
caaSuper Admin
Posted 1 month ago
Key Parameters in the Order Payload
Field Description
orderType Order type, e.g., LIMIT, MARKET.
session Trading session (NORMAL, AM, PM, SEAMLESS).
duration Order duration, e.g., DAY or GTC (Good Till Canceled).
instruction BUY or SELL.
quantity Number of shares to buy/sell.
price For limit orders, the maximum price you're willing to pay.
assetType Asset type (EQUITY for stocks, OPTION for options).
Notes

Authorization: Ensure your access token is valid. Tokens expire after 30 minutes.
Sandbox Testing: TD Ameritrade does not offer a dedicated sandbox for testing live orders. Be careful when placing real orders.
Error Handling: Always check the API response for errors.
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 4
Members Online 0

Total Members: 16
Newest Member: Sunny