Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to fetch account transaction history #40

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
print("Getting account holdings information")
account_info = api.get_account_info_v2()
pprint.pprint(account_info)
print("The following account numbers were found: " + str(account_info.keys()))
account_numbers = list(account_info.keys())
print("The following account numbers were found: " + str(account_numbers))

# Get transaction history for an account
print("Getting full transaction history for account " + str(account_numbers[0]))
transaction_history = api.get_transaction_history_v2(account_numbers[0])
pprint.pprint(transaction_history)

print("Placing a dry run trade for PFE stock")
# Place a dry run trade for each account
Expand Down
38 changes: 38 additions & 0 deletions schwab_api/schwab.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,44 @@ def get_account_info(self):

return account_info

def get_transaction_history_v2(self, account_id):
"""
account_id (int) - The account ID to place the trade on. If the ID is XXXX-XXXX,
we're looking for just XXXXXXXX.

Returns a dictionary of transaction history entries for the provided account ID.
"""

data = {
"timeFrame": "All",
"selectedTransactionTypes": [
"Adjustments",
"AtmActivity",
"BillPay",
"CorporateActions",
"Checks",
"Deposits",
"DividendsAndCapitalGains",
"ElectronicTransfers",
"Fees",
"Interest",
"Misc",
"SecurityTransfers",
"Taxes",
"Trades",
"VisaDebitCard",
"Withdrawals"
],
"exportType": "Json",
"selectedAccountId": str(account_id),
"sortColumn": "Date",
"sortDirection": "Descending"
}
r = requests.post(urls.transaction_history_v2(), json=data, headers=self.headers)
if r.status_code != 200:
return [r.text], False
return json.loads(r.text)

def trade(self, ticker, side, qty, account_id, dry_run=True):
"""
ticker (Str) - The symbol you want to trade,
Expand Down
3 changes: 3 additions & 0 deletions schwab_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def orders_v2():
def cancel_order_v2():
return "https://ausgateway.schwab.com/api/is.TradeOrderStatusWeb/ITradeOrderStatusWeb/ITradeOrderStatusWebPort/orders/cancelorder"

def transaction_history_v2():
return "https://ausgateway.schwab.com/api/is.TransactionHistoryWeb/TransactionHistoryInterface/TransactionHistory/brokerage/transactions/export"

# Old API
def positions_data():
return "https://client.schwab.com/api/PositionV2/PositionsDataV2"
Expand Down