1
$\begingroup$

First time to algorithm trading, python, and Quantitate Finance so apologies up front. I have noticed a lack of any good documentation for the TD Ameritrade API anywhere and especially with any of the less common API calls. I am having trouble with the "Get Orders By Query" API call and am looking for either a place that has example code/documentation or perhaps make this post the go to for future people trying to get this call working. The following is my current code with the variable account_id being my 9 digit TD account number and the headers variable being {'Authorization': 'Bearer PhE7Be0aFAKsp2DSfXtCciiz...} .

def Standing_Orders(headers, account_id):

endpoint = 'https://api.tdameritrade.com/v1/orders'

payload = {

'accountId': account_id,

'fromEnteredTime': '2020-08-03',

'toEnteredTime': '2020-08-03',

'status': 'QUEUED'

}

content = requests.post(url=endpoint, json=payload, headers=headers)

return content

The code keeps returning the error <Response [400]>. I am only using the requests library for the code above. My authorization is working because I am able to place buy/sell orders with my header variable. I have also had success pulling my queued orders from https://developer.tdameritrade.com/account-access/apis/get/orders-0 using the same variables and payload showed above. I am a bit lost. Thanks in advance!

$\endgroup$
1
$\begingroup$

This will hopefully clear up any confusion that could exist around TD Amertirade "Get Orders By Query" API call. There are three parts to using the "Get Orders By Query" API function, the endpoint, the payload and the headers.

The endpoint is the url your payload and headers will be sent to. The the case of "Get Orders By Query", this will always be the string endpoint = 'https://api.tdameritrade.com/v1/orders'.

Headers is the TD Ameritrade grants access to the account. The headers should be a dictionary looking like {'Authorization': 'Bearer PhE7Be0aFAKsp2DSfXtCciiz...}. I highly suggest heading over the the YouTube channel Sigma Coding and watch the series "How to Use the TD Ameritrade API" if anyone is having trouble with get their authorization token for their header. Head up that TD Ameritrade is constantly playing with the authorization so it will probably take some fiddling even after following Sigma Coding's tutorials.

The payload lets you tailor your request to get exactly what you want. It is possible to have the payload be an empty dictionary however you will only get back queued orders for all of your accounts. https://developer.tdameritrade.com/account-access/apis/get/orders-0 is a good place to go to have each variable of the payload explained. Just heads up, as of August, 2020, 'accountID' and 'maxResults' should be interger/float/etc, and 'fromEnteredTime', 'toEnteredTime' and 'status' should all be strings.

The code below is an example of what a call should look like:

def Standing_Orders(headers, account_id):
    endpoint = 'https://api.tdameritrade.com/v1/orders'

    payload = {
        'accountId': account_id,
        'maxResults': 6
        'fromEnteredTime': '2020-08-03',
        'toEnteredTime': '2020-08-03',
        'status': 'FILLED'
    }

    content = requests.get(url=endpoint, json=payload, headers=headers)
    order_info = content.json()
    return order_info

The function returns a list of the 6 most recent filled orders between the above dates. The requests library is required.

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.