Exchange Interface

The Futures Exchange Interface object is similar to the Exchange object, except it is designed to work with exchanges or brokerages that trade perpetual futures.

Creation

Interfaces are pre-generated when an exchange object is created. They can then be accessed by using .get_interface() on an exchange:

import blankly
from blankly import futures

exchange = futures.BinanceFutures()

interface = exchange.get_interface()  # Use the getter

print(interface.get_account())

Functions

get_exchange_type()

Determine which exchange an interface is running on.

Response

Possible ResponseDescriptionType
coinbase_proThe interface is running on Coinbase Prostr
binanceThe interface is running on Binancestr
alpacaThe interface is running on Alpacastr

get_products()

Get all trading pairs currently on the exchange.

Response

[
    {
        "symbol": "BTC-USD",
        "base_asset": "BTC",
        "quote_asset": "USD",
        "price_precision": 2,
        "size_precision": 4,
    },
    ...
]
KeyDescriptionType
symbolThe currency pair of this futurestr
base_assetThe base asset in the trading pairstr
quote_assetThe quote asset in the trading pairstr
price_precisionPrecision that should be used for limit pricesint
size_precisionPrecision that should be used for order sizingint

get_account(symbol=None)

Gets the current account holdings if symbol is None, otherwise will return the symbol holdings. Important: If you're using futures you may want .get_positions(). The positions you take when you buy or sell contracts will show up there, .get_account() is not very useful in FuturesExchangeInterface.

Arguments

ArgDescriptionExamplesType
symbolOptionally fill with a specific account value to filter for. This should be a base asset."BTC" or "USD"str

Response

If symbol='USD':

{
    "available": 1000.23,
    "hold": 0.0
}

If symbol=None:

{
  "USD": {
      "available": 1000.23,
      "hold": 0.0
  }
}
KeyDescriptionType
USD (example)Currency or asset this account is associated withstr
availableAmount of account asset that is free to be placed on orders or soldfloat
holdAmount of account asset that is currently on orders, or generally unavailablefloat

market_order(symbol, side, size) -> MarketOrder

Create a new live market order.

Arguments

ArgDescriptionExamplesType
symbolIdentifier for the product to order"BTC-USD" or "XLM-EUR"str
sideBuy or sell your position. Sell to go short and buy to go long."buy" or "sell"str
sizeAmount of base to buy or sell.2.3, 0.03float

Response

A FuturesOrder object. Documentation can be found here

limit_order(symbol, side, price, size) -> LimitOrder

Create a new live limit order on your exchange.

Arguments

ArgDescriptionExamplesType
symbolIdentifier for the product to order"BTC-USD" or "XLM-EUR"str
sideBuy or sell your position. Sell to go short and buy to go long."buy" or "sell"str
pricePrice to place the order at.32000 or 15000float
sizeAmount of base to buy or sell.2.3, 0.03float

Response

A FuturesOrder object. Documentation can be found here

take_profit_order(symbol, price, size) -> TakeProfitOrder

Create a new live take-profit order on your exchange. This is a market sell order that will execute when the price reaches a certain high.

Arguments

ArgDescriptionExamplesType
symbolIdentifier for the product to order"BTC-USD" or "XLM-EUR"str
sideSell to go short and buy to go long."buy" or "sell"str
pricePrice to place the order at. This should be greater than the current asking price32000 or 15000float
sizeAmount of base to sell. This means "BTC" or "XLM."0.1185float

Response

A FuturesOrder object. Documentation can be found here

stop_loss_order(symbol, price, size) -> StopLossOrder

Create a new live stop-loss order on your exchange. This is a market sell order that will execute when the price reaches a certain low.

Arguments

ArgDescriptionExamplesType
symbolIdentifier for the product to order"BTC-USD" or "XLM-EUR"str
sideSell to go short and buy to go long."buy" or "sell"str
pricePrice to place the order at. This should be lower than the current asking price32000 or 15000float
sizeAmount of base to sell. This means "BTC" or "XLM."0.1185float

Response

A FuturesOrder object. Documentation can be found here

cancel_order(symbol, order_id) -> dict

Cancel a particular order.

Arguments

ArgDescriptionExamplesType
symbolThe identifier for the product to order"BTC-USD" or "XLM-USD"str
order_idThe unique identifier for the order. This can be found in the .id field of a FuturesOrder objectb6d2f951-dae0-89e8-3e79-b460b1e9eeadstr

Response

A FuturesOrder object. Documentation can be found here

get_open_orders(symbol=None)

Get a full list of open orders.

Arguments

KeyDescriptionExamplestype
symbolOptionally fill with an identifier for the product to order"BTC-USD" or "XLM-USD"str

Response

A list of FuturesOrder objects, each representing one open order. Documentation can be found here

get_order(symbol, order_id) -> dict

Get info about a particular order.

Arguments

ArgDescriptionExamplesType
symbolThe identifier for the product to order"BTC-USD" or "XLM-USD"str
order_idThe exchange-given unique identifier for the order. This can be found using an order object. With .get_id()b6d2f951-dae0-89e8-3e79-b460b1e9eeadstr

Response

A FuturesOrder object. Documentation can be found here

get_fees() -> dict

Get the maker and taker fee rates of a particular exchange.

Response

{
    "maker": 0.0050,
    "taker": 0.0050
}
KeyDescriptionType
maker_fee_rateExchange maker fee rate. (89% = 0.89)float
taker_fee_rateExchange taker fee rate. (89% = 0.89)float

get_leverage(symbol=None) -> leverage, set_leverage(leverage, symbol=None)

Used for getting and setting leverage. If symbol is None, this will set account-wide leverage. Note that not all exchanges support this.

Arguuments

ArgDescriptionExamplesType
symbolThe identifier for the product to order"BTC-USD" or "XLM-USD"str
leverageThe amount of leverage.3, 20float

Response

get_leverage(symbol=None) will return the leverage for symbol, or account wide leverage if symbol is None.

history(symbol, to = 200, resolution = '1d', start_date = None, end_date = None, return_as = 'df') -> pandas.DataFrame

Download historical data with rows of at least time (epoch seconds), low', high, open, close, volume as columns. This is a wrapper for get_product_history(), and exists to allow users to more easily download OHCLV data

Arguments

ArgDescriptionExamplesType
symbolThe identifier for the product to order"BTC-USD" or "XLM-USD"str
toTime from exactly now to begin downloading from ('1y' would download the last year of data at the granularity resolution). If passed an integer that will be the number of bars instead of a length of time.86453 or '1y' or '2h'int or str
resolutionResolution as a time string (1m, 4h, 1d, 1y)1m, 1d, 4hstr
start_dateStarts history collection at a given start date (must have an end_date associated with it). This can be a datetime, a date string or an epoch timestamp'2018-05-02', 123849str, float, datetime
end_dateEnd Date for history collection, can be used in conjunction with start_date or to'2018-05-02', 123849str, float, datetime
return_asChoose if the return type should be a dataframe with columns or a dictionary with keys that match the columns. It also allows pandas orient values of 'dict', 'list', 'series', 'split', 'records', 'index'. We highly recommned 'deque' for any operations with technical indicators'list' or 'df' or 'deque'str

Response

Pandas dataframe with at least these columns.

timelowhighopenclosevolume
15911100009270.09602.09583.369464.465979.77327365
15911136009417.389510.949464.449478.951185.12835638

Or if return_as is set to list:

{
    'time': [1591110000, 1591113600],
    'low': [9270.0, 9417.38],
    'open': [9583.36, 9464.44],
    'close': [9464.46, 9478.95],
    'volume': [5979.77327365, 1185.12835638]
}

Example Use Case

a = Alpaca()
interface = a.Interface
interface.history('MSFT', to=300, resolution='15m',
                  end_date='2020-05-03')  # get 300 points from May 3rd, 2020 as a dataframe
interface.history('MSFT', to=300, resolution='15m', end_date='2020-05-03', return_as='list')[
    'close']  # get 300 points from May 3rd, 2020 as a dictionary of np.arrays

get_product_history(symbol, epoch_start, epoch_stop, resolution) -> pandas.DataFrame

Download historical data with rows of at least time (epoch seconds), low', high, open, close, volume as columns.

Arguments

ArgDescriptionExamplesType
symbolThe identifier for the product to order"BTC-USD" or "XLM-USD"str
epoch_startStarting download time in epoch1591389962float
epoch_stopEnding download time in epoch1622925962float
resolutionResolution in seconds in each candle (ex: 60 = 1 per minute, 3600 = 1 per hour)3600str or int

Response

Pandas dataframe with at least these columns.

timelowhighopenclosevolume
15911100009270.09602.09583.369464.465979.77327365
15911136009417.389510.949464.449478.951185.12835638

get_price(symbol) -> float

Get the quoted price of the trading pair.

Arguments

ArgDescriptionExamplesType
symbolThe identifier for the product"BTC-USD" or "XLM-USD"str

Response

Returns a float which is the price of the trading pair, such as 53000 or 35000.

get_funding_rate(symbol) -> float

Get the current funding rate of the symbol.

Arguments

ArgDescriptionExamplesType
symbolThe identifier for the product"BTC-USD" or "XLM-USD"str

Response

Returns a float which is the current funding rate of the trading pair, such as 0.0001 or -0.0001.

.cash -> float

Get the amount of cash (generally the quote currency) inside the portfolio. This default quote can be set in the settings.json file for each exchange independently. This is used as a shortcut for easily determining buying power when making a transaction. This property is configurable because users may be using a variety of cash quotes such as USD, USDT, USDC or EUR. See here.

Response

A single float which represents the size value of the account defined in settings.json.

5931.533