oandapyV20 Documentation

repository·master·Indexed 19 days ago

https://github.com/hootnot/oanda-api-v20

A Python wrapper for the OANDA REST-V20 API. It provides structured access to trading endpoints, including account management, instrument data, and ForexLabs services. The library includes helper utilities in the `contrib` module for generating complex requests, such as Market, Limit, Stop, and Trailing Stop Loss orders, as well as tools for mapping granularity to time and managing order metadata via ClientExtensions.

Tokens
19K
Snippets
75
Records
102
Agent score
64%

What's inside oandapyV20

  1. Explore OANDA REST-V20 API definitions

    master

    The oandapyV20.definitions module contains the data structures and object definitions that mirror the official OANDA REST-V20 specifications. You can use these definitions to construct valid request bodies and parse response data for various API resources.

    The module is organized into submodules corresponding to specific OANDA resource types:

    • oandapyV20.definitions.accounts: Account-related definitions.
    • oandapyV20.definitions.instruments: Instrument-related definitions.
    • oandapyV20.definitions.orders: Order-related definitions.
    • oandapyV20.definitions.pricing: Pricing-related definitions.
    • oandapyV20.definitions.trades: Trade-related definitions.
    • oandapyV20.definitions.transactions: Transaction-related definitions.
  2. How the library design works

    master

    The library is structured around APIRequest objects.

    1. APIRequest: All endpoints are represented as APIRequest objects derived from a base APIRequest class.
    2. Endpoint Groups: Functionality is organized into abstract classes representing endpoint groups (e.g., accounts, trades).
    3. Specific Endpoints: Each specific endpoint within a group is implemented as a class derived from that group's abstract class.
    4. Client: The API client class is responsible for processing these APIRequest objects.
  3. Use oandapyV20.types for API data validation

    master
    The oandapyV20.types module provides Python type representations that map to the OANDA REST-API specifications. These types act as a bridge between Python native types and the specific formats required by the API, ensuring that data sent to or received from the API conforms to the expected schema. These types also perform validation; passing invalid values will raise an exception.
  4. Use Client Extensions on Order Requests

    master

    You can optionally use ClientExtensions on Order Requests to attach custom metadata to your orders. This allows you to set a custom id, tag, and/or comment for tracking and identification purposes within the OANDA API.

    from oandapyV20.contrib.requests import ClientExtensions
    
    # Example of initializing client extensions
    extensions = ClientExtensions(id="my_custom_id", tag="my_tag", comment="my_comment")
  5. Use support classes for complex Order Requests

    master

    The oandapyV20.contrib.requests module provides optional support classes to simplify the creation of complex Order Requests. These classes are specifically useful when you need to define dependent orders that trigger once a primary position is filled, such as Take Profit and Stop Loss orders.

    To implement these, you can use:

    • contrib.requests.*Order classes to define the order specifications.
    • contrib.requests.*Details classes to provide additional data required by those specifications.
  6. Use the oandapyV20 factories module to generate requests

    master
    The oandapyV20.contrib.factories module provides optional classes and methods designed to simplify the creation of OANDA REST-V20 API requests. Instead of manually constructing complex request dictionaries, you can use these factory utilities to generate properly formatted request objects.
  7. Use Order and Details classes for dependent orders

    master

    When creating an order to open a position, you can use the oandapyV20.contrib.requests module to define dependent orders that are triggered once the initial position is filled. This is commonly used for setting Take Profit and Stop Loss instructions.

    To implement this, use the following types of classes from oandapyV20.contrib.requests:

    • *Order classes: To specify the order type and logic.
    • *Details classes: To provide the additional data required by those order specifications.
  8. Run tests and install from source

    master

    To run the project's test suite or install from a local source clone, follow these steps:

    1. Clone the repository.
    2. Navigate into the directory.
    3. Run the tests using setup.py.
    4. Install the package locally using setup.py.
    $ git clone https://github.com/hootnot/oanda-api-v20.git
    $ cd oanda-api-v20
    $ python setup.py test
    $ python setup.py install
  9. Configure logging for oandapyV20

    master

    The oandapyV20 package has integrated logging. You can capture API activity, including request URLs and error messages, by configuring the standard Python logging module. This is useful for debugging failed requests or monitoring connection activity.

    import logging
    from oandapyV20 import API
    import oandapyV20.endpoints.orders as orders
    from oandapyV20.exceptions import V20Error
    
    # Configure logging to output to a file
    logging.basicConfig(
        filename="v20.log",
        level=logging.INFO,
        format='%(asctime)s [%(levelname)s] %(name)s : %(message)s',
    )
    
    # Example usage
    client = API(access_token='YOUR_TOKEN')
    try:
        request = orders.OrderList(account_id='YOUR_ACCOUNT_ID')
        response = client.request(request)
    except V20Error as e:
        logging.error(f"API Error: {e}")
  10. How the Client and Requests architecture works

    master

    The oandapyV20 library uses a decoupled architecture consisting of a Client and APIRequest objects:

    • Client: Acts as the central API processor. It takes APIRequest objects, performs the network operation, and assigns the status and response properties to the request object. The client also returns the response.
    • Requests: Endpoints are represented as APIRequest objects derived from a base APIRequest class. Endpoints are organized into hierarchical groups (e.g., accounts, trades) where an abstract class covers the group's functionality, and specific endpoint classes derive from those abstract classes.