Trading pairs

Trading pair information and analysis.

exception tradingstrategy.pair.DuplicatePair

Found multiple trading pairs for the same naive lookup.

class tradingstrategy.pair.PairType

What kind of an decentralised exchange, AMM or other the pair is trading on.

Note that each type can have multiple implementations. For example QuickSwap, Sushi and Pancake are all Uniswap v2 types.

uniswap_v2 = 'uni_v2'

Uniswap v2 style exchange

uniswap_v3 = 'uni_v3'

Uniswap v3 style exchange

class tradingstrategy.pair.DEXPair

A trading pair information.

This dataclass presents information we have available for a trading pair. Note that you do not directly read or manipulate this class, but instead use pyarrow.Table in-memory analytic presentation of the data.

The dataset server maintains trading pair and associated token information. Some tokens may have more information available than others, as due to the high number of pairs it is impractical to get full information for all pairs.

  • Non-optional fields are always available

  • Optional fields may be available if the candle server 1) detected the pair popular enough 2) managed to fetch the third party service information related to the token

pair_id: tradingstrategy.types.PrimaryKey

Internal primary key for any trading pair

chain_id: tradingstrategy.chain.ChainId

The chain id on which chain this pair is trading. 1 for Ethereum.

exchange_id: tradingstrategy.types.PrimaryKey

The exchange where this token trades

address: tradingstrategy.types.NonChecksummedAddress

Smart contract address for the pair. In the case of Uniswap this is the pair (pool) address

dex_type: tradingstrategy.pair.PairType

What kind of exchange this pair is on

base_token_symbol: str

Naturalised base and quote token. Uniswap may present the pair in USDC-WETH or WETH-USDC order based on the token address order. However we humans always want the quote token to be USD, or ETH or BTC. For the reverse token orders, the candle serve swaps the token order so that the quote token is the more natural token of the pair (in the above case USD)

token0_symbol: str

token0 as in raw Uniswap data

token1_symbol: str

token1 as in raw Uniswap data

token0_address: str

Token pair contract address on-chain

token1_address: str

Token pair contract address on-chain

flag_inactive: bool

Pair has been flagged inactive, because it has not traded at least once during the last 30 days.

flag_blacklisted_manually: bool

Pair is blacklisted by operators. Current there is no blacklist process so this is always false.

flag_unsupported_quote_token: bool

Quote token is one of USD, ETH, BTC, MATIC or similar popular token variants. Because all candle data is outputted in the USD, if we have a quote token for which we do not have an USD conversation rate reference price source, we cannot create candles for the pair.

flag_unknown_exchange: bool

Pair is listed on an exchange we do not if it is good or not

first_swap_at_block_number: Optional[tradingstrategy.types.BlockNumber] = None

Block number of the first Uniswap Swap event

last_swap_at_block_number: Optional[tradingstrategy.types.BlockNumber] = None

Block number of the last Uniswap Swap event

first_swap_at: Optional[tradingstrategy.types.UNIXTimestamp] = None

Timestamp of the first Uniswap Swap event

last_swap_at: Optional[tradingstrategy.types.UNIXTimestamp] = None

Timestamp of the first Uniswap Swap event

flag_not_enough_swaps: Optional[bool] = None

Various risk analyis flags

fee: Optional[tradingstrategy.types.BasisPoint] = None

Swap fee in basis points if known

buy_count_all_time: Optional[int] = None

Risk assessment summary data

sell_count_all_time: Optional[int] = None

Risk assessment summary data

buy_volume_all_time: Optional[float] = None

Risk assessment summary data

sell_volume_all_time: Optional[float] = None

Risk assessment summary data

buy_count_30d: Optional[int] = None

Risk assessment summary data

sell_count_30d: Optional[int] = None

Risk assessment summary data

buy_volume_30d: Optional[float] = None

Risk assessment summary data

sell_volume_30d: Optional[float] = None

Risk assessment summary data

get_friendly_name(exchange_universe: tradingstrategy.exchange.ExchangeUniverse) str

Get a very human readable name for this trading pair.

We need to translate the exchange id to someething human readable, and for this we need to have the access to the exchange universe.

classmethod to_pyarrow_schema() pyarrow.lib.Schema

Construct schema for reading writing Parquet filss for pair information.

classmethod convert_to_pyarrow_table(pairs: List[tradingstrategy.pair.DEXPair]) pyarrow.lib.Table

Convert a list of Python instances to a columnar data.

Parameters

pairs – The list wil be consumed in the process

__init__(pair_id: tradingstrategy.types.PrimaryKey, chain_id: tradingstrategy.chain.ChainId, exchange_id: tradingstrategy.types.PrimaryKey, address: tradingstrategy.types.NonChecksummedAddress, dex_type: tradingstrategy.pair.PairType, base_token_symbol: str, quote_token_symbol: str, token0_symbol: str, token1_symbol: str, token0_address: str, token1_address: str, flag_inactive: bool, flag_blacklisted_manually: bool, flag_unsupported_quote_token: bool, flag_unknown_exchange: bool, first_swap_at_block_number: Optional[tradingstrategy.types.BlockNumber] = None, last_swap_at_block_number: Optional[tradingstrategy.types.BlockNumber] = None, first_swap_at: Optional[tradingstrategy.types.UNIXTimestamp] = None, last_swap_at: Optional[tradingstrategy.types.UNIXTimestamp] = None, flag_not_enough_swaps: Optional[bool] = None, flag_on_trustwallet: Optional[bool] = None, flag_on_etherscan: Optional[bool] = None, flag_code_verified: Optional[bool] = None, fee: Optional[tradingstrategy.types.BasisPoint] = None, trustwallet_info_checked_at: Optional[tradingstrategy.types.UNIXTimestamp] = None, etherscan_info_checked_at: Optional[tradingstrategy.types.UNIXTimestamp] = None, etherscan_code_verified_checked_at: Optional[tradingstrategy.types.UNIXTimestamp] = None, blacklist_reason: Optional[str] = None, trustwallet_info: Optional[Dict[str, str]] = None, etherscan_info: Optional[Dict[str, str]] = None, buy_count_all_time: Optional[int] = None, sell_count_all_time: Optional[int] = None, buy_volume_all_time: Optional[float] = None, sell_volume_all_time: Optional[float] = None, buy_count_30d: Optional[int] = None, sell_count_30d: Optional[int] = None, buy_volume_30d: Optional[float] = None, sell_volume_30d: Optional[float] = None, same_pair_on_other_exchanges: Optional[List[tradingstrategy.types.PrimaryKey]] = None, bridged_pair_on_other_exchanges: Optional[List[tradingstrategy.types.PrimaryKey]] = None, clone_pairs: Optional[List[tradingstrategy.types.PrimaryKey]] = None) None
class tradingstrategy.pair.PairUniverse

The queries universe, as returned by the server.

The universe presents tradeable token pairs that fulfill certain criteria.

The server supports different token pair universes depending on the risk appetite. As generating the universe data is heavy process, the data is generated as a scheduled job and cached.

Risks include

  • Fake tokens designed to fool bots

  • Tokens that may be rug pulls

  • Legit tokens that may have high volatility due to a hack

  • Legit tokens that may stop working in some point

Depending on your risk apetite, you might want to choose between safe and wild west universes.

pairs: Dict[int, tradingstrategy.pair.DEXPair]

Pair info for this universe

last_updated_at: Optional[tradingstrategy.types.UNIXTimestamp] = None

When this universe was last refreshed

classmethod create_from_pyarrow_table(table: pyarrow.lib.Table) tradingstrategy.pair.PairUniverse

Convert columnar presentation to a Python in-memory objects.

Some data manipulation is easier with objects instead of columns.

Note

This seems to quite slow operation. It is recommend you avoid this if you do not need row-like data.

get_pair_by_id(pair_id: int) Optional[tradingstrategy.pair.DEXPair]

Resolve pair by its id.

Only useful for debugging. Does a slow look

get_pair_by_ticker(base_token, quote_token) Optional[tradingstrategy.pair.DEXPair]

Get a trading pair by its ticker symbols.

Note that this method works only very simple universes, as any given pair is poised to have multiple tokens and multiple trading pairs on different exchanges.

Raises

DuplicatePair – If the universe contains more than single entry for the pair.

Returns

None if there is no match

get_active_pairs() Iterable[tradingstrategy.pair.DEXPair]

Filter for pairs that have see a trade for the last 30 days

get_inactive_pairs() Iterable[tradingstrategy.pair.DEXPair]

Filter for pairs that have not see a trade for the last 30 days

__init__(pairs: Dict[int, tradingstrategy.pair.DEXPair], last_updated_at: Optional[tradingstrategy.types.UNIXTimestamp] = None) None
class tradingstrategy.pair.PandasPairUniverse

A pair universe that holds the source data as Pandas dataframe.

pd.DataFrame is somewhat more difficult to interact with, but offers tighter in-memory presentation for filtering and such.

The data frame has the same columns as described by DEXPair.

__init__(df: pandas.core.frame.DataFrame)
Parameters

df – The source DataFrame that contains all DEXPair entries

get_count() int

How many trading pairs there are.

get_unflagged_count() int

How many trading pairs there are that seem to be legit for analysis.

get_pair_by_id(pair_id: tradingstrategy.types.PrimaryKey) Optional[tradingstrategy.pair.DEXPair]

Look up pair information and return its data.

Returns

Nicely presented DEXPair.

get_one_pair_from_pandas_universe(exchange_id: tradingstrategy.types.PrimaryKey, base_token: str, quote_token: str, pick_by_highest_vol=False) Optional[tradingstrategy.pair.DEXPair]

Get a trading pair by its ticker symbols.

Note that this method works only very simple universes, as any given pair is poised to have multiple tokens and multiple trading pairs on different exchanges.

Parameters

pick_by_highest_vol – If multiple trading pairs with the same symbols are found, pick one with the highest volume. This is because often malicious trading pairs are create to attract novice users.

Raises

DuplicatePair – If the universe contains more than single entry for the pair.

Returns

None if there is no match