Account management and transaction history.
class AccountList:
"""List all authorized accounts."""
def __init__(self): ...
class AccountDetails:
"""Full account details including balance, positions, orders, trades."""
def __init__(self, accountID: str): ...
class AccountSummary:
"""Summary without full position/order/trade details."""
def __init__(self, accountID: str): ...
class AccountInstruments:
"""List tradeable instruments."""
def __init__(self, accountID: str, params: dict = None): ...
# params: instruments (comma-separated filter)
class AccountConfiguration:
"""Configure account settings."""
def __init__(self, accountID: str, data: dict): ...
# data: {"alias": str, "marginRate": str}
class AccountChanges:
"""Poll account state changes since transaction ID."""
def __init__(self, accountID: str, params: dict = None): ...
# params: sinceTransactionID (required)from oandapyV20.endpoints.accounts import (
AccountList, AccountDetails, AccountSummary, AccountInstruments, AccountChanges
)
# List accounts
accounts = api.request(AccountList())['accounts']
account_id = accounts[0]['id']
# Get details
account = api.request(AccountDetails(accountID=account_id))['account']
print(f"Balance: {account['balance']}, NAV: {account['NAV']}")
print(f"Unrealized P/L: {account['unrealizedPL']}")
print(f"Margin Used: {account['marginUsed']}, Available: {account['marginAvailable']}")
# Get summary (lighter)
summary = api.request(AccountSummary(accountID=account_id))['account']
# List instruments
instruments = api.request(AccountInstruments(accountID=account_id))['instruments']
for inst in instruments:
print(f"{inst['name']}: {inst['displayName']} ({inst['type']})")
# Filter instruments
instruments = api.request(AccountInstruments(
accountID=account_id,
params={"instruments": "EUR_USD,GBP_USD"}
))['instruments']
# Poll changes
changes = api.request(AccountChanges(
accountID=account_id,
params={"sinceTransactionID": "6368"}
))
print(f"Orders created: {len(changes['changes'].get('ordersCreated', []))}")
print(f"Trades opened: {len(changes['changes'].get('tradesOpened', []))}")class TransactionList:
"""List transactions with filtering."""
def __init__(self, accountID: str, params: dict = None): ...
# params: from, to, pageSize, type (comma-separated)
class TransactionDetails:
"""Get specific transaction."""
def __init__(self, accountID: str, transactionID: str): ...
class TransactionIDRange:
"""Get transactions within ID range."""
def __init__(self, accountID: str, params: dict = None): ...
# params: from (required), to (required), type (comma-separated)
class TransactionsSinceID:
"""Get transactions since ID."""
def __init__(self, accountID: str, params: dict = None): ...
# params: id (required), type (comma-separated)
class TransactionsStream:
"""Stream real-time transactions."""
def __init__(self, accountID: str, params: dict = None): ...
def terminate(self, message: str = ""): ...from oandapyV20.endpoints.transactions import (
TransactionList, TransactionDetails, TransactionsSinceID, TransactionsStream
)
from datetime import datetime, timedelta
# Recent transactions
txns = api.request(TransactionList(accountID=account_id))['transactions']
for txn in txns:
print(f"{txn['id']}: {txn['type']} at {txn['time']}")
# Filter by type
txns = api.request(TransactionList(
accountID=account_id,
params={"type": "ORDER_FILL,ORDER_CANCEL"}
))['transactions']
# Time range
to_time = datetime.utcnow()
from_time = to_time - timedelta(days=7)
txns = api.request(TransactionList(
accountID=account_id,
params={
"from": from_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"to": to_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
}
))['transactions']
# Since ID
response = api.request(TransactionsSinceID(
accountID=account_id,
params={"id": "6368"}
))
txns = response['transactions']
last_id = response['lastTransactionID']
# Stream transactions
stream = TransactionsStream(accountID=account_id)
for txn in api.request(stream):
if txn['type'] == 'ORDER_FILL':
print(f"Order filled: {txn['instrument']} {txn['units']} @ {txn['price']}")from oandapyV20.definitions.transactions import TransactionType
# Common types
TransactionType.CREATE
TransactionType.MARKET_ORDER
TransactionType.LIMIT_ORDER
TransactionType.STOP_ORDER
TransactionType.ORDER_FILL
TransactionType.ORDER_CANCEL
TransactionType.TRADE_CLIENT_EXTENSIONS_MODIFY
TransactionType.MARGIN_CALL_ENTER
# 50+ types available