Place an Order
Placing an order
# Version 3.6.1
#Imports
import json
import requests
import sys
#Tokens and Account Numbers
API_TOKEN = "<PRODUCTION TOKEN>"
LIVE_ACCOUNT = "<PRODUCTION ACCOUNT NUMBER>"
SANDBOX_TOKEN = "<SANDBOX TOKEN>"
SANDBOX_ACCOUNT = "<SANDBOX ACCOUNT NUMBER>"
#Endpoints
ENDPOINT = "sandbox" # api or sandbox
# Sample Orders
TEST_EQUITY = {'class': 'equity',
'symbol': 'AAPL',
'side': 'buy',
'quantity': '1',
'type': 'market',
'duration': 'day',
'preview': 'false'
}
TEST_OPTION = {'class': 'option',
'symbol': 'AAPL',
'option_symbol': 'AAPL250926C00260000',
'side': 'buy_to_open',
'quantity': '1',
'type': 'limit',
'duration': 'day',
'price': '2.45'
}
TEST_OTOCO = {'class': 'otoco',
'symbol': 'AAPL',
'option_symbol[0]': 'AAPL250926C00210000',
'side[0]': 'buy_to_open',
'quantity[0]': '1',
'duration[0]': 'day',
'type[0]': 'limit',
'price[0]': '0.55',
'option_symbol[1]': 'AAPL250926C00210000',
'side[1]': 'sell_to_close',
'quantity[1]': '1',
'duration[1]': 'gtc',
'type[1]': 'limit',
'price[1]': '0.75',
'option_symbol[2]': 'AAPL250926C00210000',
'side[2]': 'sell_to_close',
'quantity[2]': '1',
'duration[2]': 'gtc',
'type[2]': 'stop',
'stop[2]' : '0.05',
'tag': 'TEST-OTOCO'
}
#Post Request
response = requests.post(f'https://{ENDPOINT}.tradier.com/v1/accounts/{SANDBOX_ACCOUNT}/orders',
data= TEST_EQUITY,
headers={'Authorization': f'Bearer {SANDBOX_TOKEN}', 'Accept': 'application/json'}
)
#Checking Order Submission
try:
result = response.json()
except Exception as e:
print ("Error: {}".format(e))
print(response)
print(type(response))
print("Status: {} -- {}".format(response.status_code, response.text))
sys.exit(1)
print(result)
#Getting the order number
try:
ORDERNUM = result['order']['id']
except Exception as e:
print ("Error: {}".format(e))
sys.exit(1)
# Get the actual order status
response = requests.get(f'https://{ENDPOINT}.tradier.com/v1/accounts/{SANDBOX_ACCOUNT}/orders/{ORDERNUM}',
params={'includeTags': 'true'},
headers={'Authorization': f'Bearer {SANDBOX_TOKEN}', 'Accept': 'application/json'}
)
#Checking the Order Response
try:
json_response = response.json()
json_formatted_str = json.dumps(json_response, indent=2)
except Exception as e:
print (e)
print(response)
print(response.text)
sys.exit(1)
#Print Order Status
print(response.status_code)
print(json_formatted_str)
Sample Response
{'order': {'id': 19803614, 'status': 'ok', 'partner_id': '3a8bbee1-5184-4ffe-8a0c-294fbad1aee9'}}
200
{
"order": {
"id": 19803614,
"type": "market",
"symbol": "AAPL",
"side": "buy",
"quantity": 1.0,
"status": "filled",
"duration": "day",
"avg_fill_price": 255.01,
"exec_quantity": 1.0,
"last_fill_price": 255.01,
"last_fill_quantity": 1.0,
"remaining_quantity": 0.0,
"create_date": "2025-09-26T15:09:09.685Z",
"transaction_date": "2025-09-26T15:09:09.803Z",
"class": "equity"
}
}
Explaination
Imports
Whatever codebase you are using, make sure you have the ability to make HTTP requests and to parse JSON
Tokens and Account Numbers
Insert line top Insert line below Delete. Whether you are using your live account or your sandbox account, you'll need your matching API token and account number.
Endpoint
For ease of switching between live or paper trading, you can use an endpoint variable with values of "api" or "sandbox"
Sample Orders
There are some sample order payloads for an equity order, an option order, or an OTOCO.
POST Request
For our actual order submission, we use a post request and harness all our variables for easy future editing. In this order submission, we are using our sandbox account and purchasing one share of AAPL
Checking order submission
The first step in confirming our order is to check if we get back JSON as error payloads will come back in the response.text If we decode JSON we know we are on the right track and can skip to printing the response on line 74.
If we catch an error with the JSON decoding, then we should check the error, the returned HTTP status code, and the response.text for clues as to what went wrong.
If successful, we print the return from the order submission, which will have a status and an order number, among other details. NOTE - A 200 OK response from order submission is not an indication that the order was correct/valid, only that the request was valid and received by our OMS system. An order can still be rejected even if the HTTP request/submission was accepted.
Getting the order number
If we were successful in the POST request, then we should get back an order number if the order was accepted by the OMS system. To confirm this, we check for an order number in: "result['order']['id']" If we hit an exception here, we again throw an error and end the process.
Get the Order Status
With a valid order number, we can check the actual status of the order. Remember, the 200 OK from submission is not a validation of the order, only a validation of the HTTP request.
Now we use the proper endpoint, account number, and order number to get the order status and check if it is pending, open, partially filled, filled, or rejected.
Checking the Order Response
Just like before, our first test is JSON decoding the response. If it isn't JSON, then something was wrong with our request, and we want to make sure we have properly captured a valid order number and properly called for just that order's details.
Printing Order Status
If we make it through the try/except, we have an order payload that we can print out to check the status of the order we just submitted. This is the best practice to be sure that you not only submitted an order via the HTTP POST but that your order was accepted and not rejected, and to confirm its current status.
On this page
- Place an Order