AutoTrader Broker Interface#

To make the transition from backtesting to live-trading seamless, each broker integrated into AutoTrader interfaces using a set of common methods. This means that the same strategy can be run with the virtual broker or any other broker without changing a single line of code in your strategy. This page provides a general overview of the methods contained within each broker interface module. Of course, the mechanics behind each method will change depending on the broker, however each method will behave in the same way, accepting the same input arguments and outputting the same objects.

See also

A great way to learn the broker interface is to do some click trading.

Methods#

The shared methods of the broker interfaces are described below. Note that each broker may have their own additional methods to extend their functionality based on the brokers API capabilities.

Tip

A template for integrating new brokers into AutoTrader is included in the ‘templates/’ directory of the GitHub repository.

Method

Function

get_NAV

Returns Net Asset Value of account.

get_balance

Returns balance of account.

place_order

Place order with broker.

get_orders

Returns orders.

cancel_order

Cancels an order by ID.

get_trades

Returns open trades for the specified instrument.

get_positions

Returns the open positions in the account.

Accessing Exchange API Methods#

When trading with a real exchange/broker, you are also able to communicate directly with their API endpoints via the api attribute of the broker instance. This allows you to access all methods offered by an exchange, outside of the unified methods listed in the table above.

Accessing the Broker Instance#

To access the broker instance from your strategy, set INCLUDE_BROKER to True in your strategy configuration. Doing so, the broker instance and broker utilities will be passed as named arguments broker and broker_utils to your strategy’s __init__ method.

Module Structure#

Each new broker API is contained within its own submodule of the autotrader.brokers module. This submodule must contain two more submodules:

  1. A core API module which communicates with the broker.

  2. A utility module containing helper functions related to the core API module.

autotrader.brokers
├── broker_utils.py
└── broker_name
    ├── broker.py
    ├── __init__.py
    └── utils.py

Supported Brokers and Exchanges#

Virtual Broker#

broker='virtual'

At the heart of AutoTrader’s backtesting algorithm is the virtual broker, a Python class intended to replicate the functionality of a real broker. See the documentation of the Virtual Broker for more information about how this functions.

Oanda v20 REST API#

broker='oanda'

AutoTrader supports Oanda’s v20 REST API. See the documentation of the Oanda Broker module for more information.

Interactive Brokers#

broker='ib'

As of AutoTrader v0.6.0, Interactive Brokers is also supported.

DYDX Cryto Exchange#

broker='dydx'

dYdX is a decentralised cryptocurrency derivatives exchange.

CCXT#

broker='ccxt:<exchange name>'

The CryptoCurrency eXchange Trading (CCXT) library is an open-source Python library supporting over 100 cryptocurrency exchange markets and trading APIs.

API Reference#

class autotrader.brokers.broker.AbstractBroker(config: dict, utils: BrokerUtils | None = None)#
abstract cancel_order(order_id: int, *args, **kwargs) None#

Cancels order by order ID.

abstract get_NAV(*args, **kwargs) float#

Returns the net asset/liquidation value of the account.

abstract get_balance(*args, **kwargs) float#

Returns account balance.

abstract get_orders(instrument: str | None = None, *args, **kwargs) dict#

Returns all pending orders (have not been filled) in the account.

abstract get_positions(instrument: str | None = None, *args, **kwargs) dict#

Gets the current positions open on the account.

Parameters:

instrument (str, optional) – The trading instrument name (symbol). The default is None.

Returns:

open_positions – A dictionary containing details of the open positions.

Return type:

dict

abstract get_trades(instrument: str | None = None, *args, **kwargs) dict#

Returns the trades (fills) made by the account.

abstract place_order(order: Order, *args, **kwargs) None#

Translate order and place via exchange API.