Exchange

Exchanges determine the universe of tradable instruments within a trading environment, return observations to the environment on each time step, and execute trades made within the environment. There are two types of exchanges: live and simulated.

Live exchanges are implementations of Exchange backed by live pricing data and a live trade execution engine. For example, CCXTExchange is a live exchange, which is capable of returning pricing data and executing trades on hundreds of live cryptocurrency exchanges, such as Binance and Coinbase.

import ccxt
from tensortrade.exchanges.live import CCXTExchange

coinbase = ccxt.coinbasepro()
exchange = CCXTExchange(exchange=coinbase, base_instrument='USD')

There are also exchanges for stock and ETF trading, such as RobinhoodExchange and InteractiveBrokersExchange, but these are still works in progress.

Simulated exchanges, on the other hand, are implementations of Exchange backed by simulated pricing data and trade execution.

For example, FBMExchange is a simulated exchange, which generates pricing and volume data using fractional brownian motion (FBM). Since its price is simulated, the trades it executes must be simulated as well. The exchange uses a simple slippage model to simulate price and volume slippage on trades, though like almost everything in TensorTrade, this slippage model can easily be swapped out for something more complex.

from tensortrade.exchanges.simulated import FBMExchange

exchange = FBMExchange(base_instrument='BTC', timeframe='1h')

Though the FBMExchange generates fake price and volume data using a stochastic model, it is simply an implementation of SimulatedExchange. Under the hood, SimulatedExchange only requires a data_frame of price history to generate its simulations. This data_frame can either be provided by a coded implementation such as FBMExchange, or at runtime.

import pandas as pd
from tensortrade.exchanges.simulated import SimulatedExchange

df = pd.read_csv('./data/btc_ohclv_1h.csv')
exchange = SimulatedExchange(data_frame=df, base_instrument='USD')

Purpose of Exchange?

Inside of the README, you’ll have seen the reason why we have the higher level abstract. It reiterate, Exchange is the highest level abstract for all other exchanges. It has functions used through all others.

Class Parameters

  • base_instrument
    • The exchange symbol of the instrument to store/measure value in.
  • dtype
    • A type or str corresponding to the dtype of the observation_space.
  • feature_pipeline
    • A pipeline of feature transformations for transforming observations.

Properties and Setters

  • base_instrument
    • The exchange symbol of the instrument to store/measure value in.
  • dtype
    • A type or str corresponding to the dtype of the observation_space.
  • feature_pipeline
    • A pipeline of feature transformations for transforming observations.
  • base_precision
    • The floating point precision of the base instrument.
  • instrument_precision
    • The floating point precision of the instrument to be traded.
  • initial_balance
    • The initial balance of the base symbol on the exchange.
  • balance
    • The current balance of the base symbol on the exchange.
  • portfolio
    • The current balance of each symbol on the exchange (non-positive balances excluded).
  • trades
    • A list of trades made on the exchange since the last reset.
  • performance
    • The performance of the active account on the exchange since the last reset.
  • generated_space
    • The initial shape of the observations generated by the exchange, before feature transformations.
  • generated_columns
    • The list of column names of the observation data frame generated by the exchange, before feature transformations.
  • observation_space
    • The final shape of the observations generated by the exchange, after feature transformations.
  • net_worth
    • Calculate the net worth of the active account on the exchange.
  • profit_loss_percent
    • Calculate the percentage change in net worth since the last reset.
  • has_next_observation
    • If False, the exchange’s data source has run out of observations.
    • Resetting the exchange may be necessary to continue generating observations.

Functions

Below are the functions that the Exchange uses to effectively operate.

Private

  • _create_observation_generator

Public

  • has_next_observation
    • Return the reward corresponding to the selected risk-adjusted return metric.
  • next_observation
    • Generate the next observation from the exchange.
  • instrument_balance
    • The current balance of the specified symbol on the exchange, denoted in the base instrument.
  • current_price
    • The current price of an instrument on the exchange, denoted in the base instrument.
  • execute_trade
    • Execute a trade on the exchange, accounting for slippage.
  • reset
    • Reset the feature pipeline, initial balance, trades, performance, and any other temporary stateful data.

Use Cases