TD Ameritrade APIs GetAccount

本文演示了如何通过TD Ameritrade API获取账户详细信息的过程。首先,通过授权URL进行身份验证,然后获取请求令牌,接着用令牌获取访问令牌。最后,使用访问令牌成功获取账户余额、权益、保证金等关键数据。
摘要由CSDN通过智能技术生成

Today we will test the API GetAccount:

https://developer.tdameritrade.com/account-access/apis/get/accounts/%7BaccountId%7D-0

Here are the test codes:

get_auth.py

import urllib.parse

client_id = "XXX"
print("client_id", client_id + "@AMER.OAUTHAP")
print("")

base_url = "https://auth.tdameritrade.com/auth?response_type=code&redirect_uri={redirect_uri}&client_id={client_id}%40AMER.OAUTHAP"

redirect_uri = "https://td.miaowa.pro/getauth.html"
redirect_uri = urllib.parse.quote(redirect_uri)

# step 1: the the auth_url
# this url works ok
print("STEP 1: click the url to authenticate")
print(base_url.format(redirect_uri=redirect_uri, client_id=client_id))
print("")

# step 2: get the request token
# after authentication with you TD account
redirect_url = """
https://td.miaowa.pro/getauth.html?code=XXX
"""
code = urllib.parse.unquote(redirect_url[redirect_url.find("code=") + 5:])
print("STEP 2: copy request token below")
print(code)

# step 3: get the access token
get_access_token_url = "https://developer.tdameritrade.com/authentication/apis/post/token-0"
print("STEP 3: parse it then click the following url to get access token")
print(get_access_token_url)
print("")

# step 4: copy response from page https://developer.tdameritrade.com/authentication/apis/post/token-0
get_auth_resp = {
    "access_token": "XXX",
    "refresh_token": "XXX",
    "scope": "PlaceTrades AccountAccess MoveMoney",
    "expires_in": 1800,
    "refresh_token_expires_in": 7776000,
    "token_type": "Bearer"
}
print("STEP 4: copy authorization value for test apis on https://developer.tdameritrade.com/account-access/apis")
print("Bearer", urllib.parse.unquote(get_auth_resp["access_token"]))

get_account.py

import requests

get_auth_resp = {
    "access_token": "XXX",
    "refresh_token": "XXX",
    "scope": "PlaceTrades AccountAccess MoveMoney",
    "expires_in": 1800,
    "refresh_token_expires_in": 7776000,
    "token_type": "Bearer"
}
access_token = get_auth_resp["access_token"]

get_account_url = "https://api.tdameritrade.com/v1/accounts/XXX"
headers = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Host": "api.tdameritrade.com",
    "User-Agent": "client.miaowa.pro",
    "Authorization": "Bearer " + access_token
}
print(headers)

get_account_resp = requests.get(get_account_url, headers=headers)
print(get_account_resp.status_code)
print(get_account_resp.text)

Great! we got the result of get_account.py

200
{
  "securitiesAccount" : {
    "type" : "MARGIN",
    "accountId" : "XXX",
    "roundTrips" : 0,
    "isDayTrader" : false,
    "isClosingOnlyRestricted" : false,
    "initialBalances" : {
      "accruedInterest" : 0.0,
      "availableFundsNonMarginableTrade" : 2149.95,
      "bondValue" : 0.0,
      "buyingPower" : 4299.9,
      "cashBalance" : 2149.95,
      "cashAvailableForTrading" : 0.0,
      "cashReceipts" : 0.0,
      "dayTradingBuyingPower" : 0.0,
      "dayTradingBuyingPowerCall" : 0.0,
      "dayTradingEquityCall" : 0.0,
      "equity" : 2149.95,
      "equityPercentage" : 100.0,
      "liquidationValue" : 2149.95,
      "longMarginValue" : 0.0,
      "longOptionMarketValue" : 0.0,
      "longStockValue" : 0.0,
      "maintenanceCall" : 0.0,
      "maintenanceRequirement" : 0.0,
      "margin" : 2149.95,
      "marginEquity" : 2149.95,
      "moneyMarketFund" : 0.0,
      "mutualFundValue" : 0.0,
      "regTCall" : 0.0,
      "shortMarginValue" : 0.0,
      "shortOptionMarketValue" : 0.0,
      "shortStockValue" : 0.0,
      "totalCash" : 2149.95,
      "isInCall" : false,
      "pendingDeposits" : 0.0,
      "marginBalance" : 0.0,
      "shortBalance" : 0.0,
      "accountValue" : 2149.95
    },
    "currentBalances" : {
      "accruedInterest" : 0.0,
      "cashBalance" : 2149.95,
      "cashReceipts" : 0.0,
      "longOptionMarketValue" : 0.0,
      "liquidationValue" : 2149.95,
      "longMarketValue" : 0.0,
      "moneyMarketFund" : 0.0,
      "savings" : 0.0,
      "shortMarketValue" : 0.0,
      "pendingDeposits" : 0.0,
      "availableFunds" : 2149.95,
      "availableFundsNonMarginableTrade" : 2149.95,
      "buyingPower" : 4299.9,
      "buyingPowerNonMarginableTrade" : 2149.95,
      "dayTradingBuyingPower" : 0.0,
      "equity" : 2149.95,
      "equityPercentage" : 100.0,
      "longMarginValue" : 0.0,
      "maintenanceCall" : 0.0,
      "maintenanceRequirement" : 0.0,
      "marginBalance" : 0.0,
      "regTCall" : 0.0,
      "shortBalance" : 0.0,
      "shortMarginValue" : 0.0,
      "shortOptionMarketValue" : 0.0,
      "sma" : 2149.95,
      "mutualFundValue" : 0.0,
      "bondValue" : 0.0
    },
    "projectedBalances" : {
      "availableFunds" : 2149.95,
      "availableFundsNonMarginableTrade" : 2149.95,
      "buyingPower" : 4299.9,
      "dayTradingBuyingPower" : 0.0,
      "dayTradingBuyingPowerCall" : 0.0,
      "maintenanceCall" : 0.0,
      "regTCall" : 0.0,
      "isInCall" : false,
      "stockBuyingPower" : 4299.9
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值