CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-oandapyv20

Python wrapper for the OANDA REST-V20 API enabling programmatic access to forex and CFD trading

Overview
Eval results
Files

core.mddocs/

Core

API client initialization, authentication, and error handling.

API Client

class API:
    """Main client for OANDA REST-V20."""

    def __init__(
        self,
        access_token: str,
        environment: str = "practice",  # "practice" | "live"
        headers: dict = None,
        request_params: dict = None
    ): ...

    def request(self, endpoint) -> dict:
        """Execute API request. Raises V20Error on failure."""

    def close(self):
        """Close session."""

    def __enter__(self): ...
    def __exit__(self, exc_type, exc_val, exc_tb): ...

Usage

from oandapyV20 import API

# Basic
api = API(access_token="token", environment="practice")
response = api.request(endpoint)
api.close()

# Context manager (auto-close)
with API(access_token="token") as api:
    response = api.request(endpoint)

# Custom headers
api = API(
    access_token="token",
    headers={"User-Agent": "MyApp/1.0"}
)

Error Handling

class V20Error(Exception):
    """API error (HTTP status >= 400)."""
    code: int      # HTTP status code
    msg: str       # Error message

class StreamTerminated(Exception):
    """Stream manually terminated."""

Usage

from oandapyV20.exceptions import V20Error, StreamTerminated

try:
    response = api.request(endpoint)
except V20Error as e:
    print(f"API Error {e.code}: {e.msg}")

# Streaming
stream = PricingStream(accountID=account_id, params={"instruments": "EUR_USD"})
try:
    for update in api.request(stream):
        if condition:
            stream.terminate("reason")
except StreamTerminated:
    print("Stream stopped")

Environment Configuration

TRADING_ENVIRONMENTS = {
    "practice": {
        "stream": "https://stream-fxpractice.oanda.com",
        "api": "https://api-fxpractice.oanda.com"
    },
    "live": {
        "stream": "https://stream-fxtrade.oanda.com",
        "api": "https://api-fxtrade.oanda.com"
    }
}

Install with Tessl CLI

npx tessl i tessl/pypi-oandapyv20

docs

account.md

core.md

helpers.md

index.md

market-data.md

trading.md

tile.json