Utilities

These are useful utility functions that simplify interactions with the blankly module.

epoch_from_ISO8601(iso8601) -> float

Get the epoch time from an iso string.

Arguments

ArgDescriptionExamplesType
iso8601ISO8601 string2021-06-06T23:38:27Zstr

Response

DescriptionExamplesType
An epoch number representation of the ISO time that as passed in.1623022707float

ISO8601_from_epoch(epoch) -> str

Get the ISO8601 string from epoch time.

Arguments

ArgDescriptionExamplesType
epochEpoch time number1623022707float

Response

DescriptionExamplesType
An ISO representation of the epoch number that was passed in.2021-06-06T23:38:27Zstr

to_blankly_sybol(symbol, exchange, quote_guess=None) -> str

Convert an asset id returned by an exchange to one that can be easily parsed & manipulated by blankly. This generally means adding a "-" between the quote and base pairs.

Arguments

ArgDescriptionExamplesType
symbolThe identifier or trading pair returned by the non-conforming exchange."BTCUSD" or "XLMUSD"str
exchangeThe identifier for the exchange the trading pair comes from"coinbase_pro" or "binance"str
quote_guessAn optional argument that allows the function to more easily parse the trading pair. If left blank, this will attempt applying a list of known quotes."USD" if guessing "BTCUSD"str

Response

DescriptionExamplesType
A asset id string that follows blankly conventions."BTC-USD" or "XLM-USD"str

to_exchange_symbol(blankly_symbol, exchange)

Turn a blankly asset id into one that can be used by other exchanges. For example this will remove the - from BTC-USDT if exchange='binance'.

Arguments

ArgDescriptionExamplesType
blankly_symbolBlankly formatted symbol for a particular currency."BTC-USD" or "XLM-USD"str
exchangeThe identifier for the exchange to convert the trading pair to."coinbase_pro" or "binance"str

Response

DescriptionExamplesType
An asset id that follows the specified exchange's conventions."BTCUSD" or "XLM-USD"str

get_base_asset(symbol)

Get the base asset from the blankly trading pair. This will give the BTC of BTC-USD

Arguments

ArgDescriptionExamplesType
symbolBlankly formatted asset id for a particular asset."BTC-USD" or "XLM-USD" or "MSFT"str

Response

DescriptionExamplesType
The base asset of the specified symbol."BTC" from "BTC-USD" or "XLM" from "XLM-USD" or "MSFT"str

get_quote_asset(symbol)

Get the quote asset from the blankly trading pair. This will get the USD of BTC-USD.

Arguments

ArgDescriptionExamplesType
symbolBlankly formatted asset id for a particular currency."BTC-USD" or "XLM-USD" or "MSFT"str

Response

DescriptionExamplesType
The quote asset of the specified trading pair."USD" from "BTC-USD" or "USD" from "XLM-USD" or "MSFT"str

trunc(number: float, decimals: int) -> float

Cleanly truncate a number to a certain number of decimals. This is very useful for interacting with exchange resolutions.

Using the round() function can create slippage & make a number to greater than an account value leading to an Insufficient Funds error.

Arguments

ArgDescriptionExamplesType
numberA float input2.353244245 or 459.435322float
decimalsNumber of decimals to keep2 or '13'int

Response

DescriptionExamplesType
A truncated version of the input numberblankly.trunc(2.3453243, 2) == 2.34float

count_decimals(number: float) -> int

Count the number of decimals in a number given a float: 1.4335 -> 4 or 3 -> 0.

Arguments

ArgDescriptionExamplesType
numberA float input2.353244245 or 459.435322float

Response

DescriptionExamplesType
The number of decimals in the number4.545 -> 3int

aggregate_candles(history: pd.DataFrame, aggregation_size: int)

Given a set of high resolution candles, turn them into low resolution candles at a different interval. For example, turn 15 1-minute bars into 1 15-minute bar. Any remainder candles that don't evenly fit an interval are still aggregated into a final bar (which of course represents less data).

Arguments

ArgDescriptionExamplesType
historyA dataframe with open, high, low, close, volume, timeopen, high, low close, volume, timepd.DataFrame
aggregation_sizeNumber of bars to put into a single bar2 or 15int

Response

DescriptionExamplesType
A dataframe with the original candles aggregated at the aggregation_sizeopen, high, low close, volume, timepd.Dataframe

count_decimals(number: float) -> int

A simple & very useful function especially for order filters. Given a number such as 3.141 this will return the number of decimals of that number 3.141 -> 3. An integer will give a zero value.

Arguments

ArgDescriptionExamplesType
numberA float or integer value to find the number of decimals3.141 or 143int or float

Response

DescriptionExamplesType
The number of decimals in the passed number4 or 0int