1

I need to see in a new listed contract in BSC, through python, how much liquidity has been added or possess. I can't find a library and a script as an applied example that can help me see that information.

It takes me a while to see it through Poocoin due to my network connection problems, but with Python the queries are faster.

Hope you can help me.

1 Answer 1

0

My service Trading Strategy gives you pool liquidity information you are looking for.

You can get both historical and real time liquidity added over an API. You can also interactively view this on the website.

enter image description here

A liquidity chart example for KLIMA-USDC pair.

Read our introduction post on liquidity charts and API.

Here is a partial Python example how to get the liquidity at a specific historical timepoint:

import pandas as pd
from tradingstrategy.chain import ChainId
from tradingstrategy.liquidity import GroupedLiquidityUniverse
from tradingstrategy.pair import PandasPairUniverse
from tradingstrategy.timebucket import TimeBucket

#: We use weekly liquidity candles for the calculations
CANDLE_TIMEFRAME = TimeBucket.d7

# Download exchange database
exchange_universe = client.fetch_exchange_universe()

# Download trading pair database
raw_pairs = client.fetch_pair_universe().to_pandas()

# Download liquidity OHLC candles
raw_liquidity_samples = client.fetch_all_liquidity_samples(CANDLE_TIMEFRAME).to_pandas()

# Create a Python helper to manipulate trading pairs
pair_universe = PandasPairUniverse(raw_pairs)

# Create a Python helper to manipulate pair liquidity data
liquidity_universe = GroupedLiquidityUniverse(raw_liquidity_samples)

# Do some test calculations for a single pair
# Note that PancakeSwap has two different deployments:
# you most likely want v2
our_exchange = exchange_universe.get_by_chain_and_slug(ChainId.bsc, "pancakeswap-v2")
assert our_exchange, "Could not find the DEX"

# Query the pair metadata.
# Note that PancakeSwap operates on Wrapped BNB (WBNB), not native BNB token
base_token = "WBNB"
quote_token = "BUSD"
assert our_pair, "Pair not found"

our_pair = pair_universe.get_one_pair_from_pandas_universe(our_exchange.exchange_id, "WBNB", "BUSD")

print(f"Trading pair {base_token}-{quote_token} id is {our_pair.pair_id}, the pool contract address is {our_pair.address}")


# December 2021
when = pd.Timestamp("2021-12-01")

liquidity_samples = liquidity_universe.get_liquidity_samples_by_pair(our_pair.pair_id)
if liquidity_samples is None:
    raise AssertionError(f"The universe does not contain liquidity data for pair {our_pair}")

# Get all liquidity OHLC past the certain timestamp.
# Thus, we query the first available liquidity sample *after* the timestamp.
ranged_samples = liquidity_samples[when:]

if len(ranged_samples) == 0:
    raise AssertionError(f"Pair {our_pair} had no liquidity before {when}")

# Get the first sample in the series
# This presents the liquidity at the first available sampled moment
# since the timestamp.
first_sample = ranged_samples.iloc[0]

# Convert liquidity from USD to token
token_liquidity = first_sample.open * first_sample.exchange_rate

print(f"""
Liquidity sampled at: {first_sample.timestamp}
Liquidity sample period: {CANDLE_TIMEFRAME.value}
Liquidity at the begin: {first_sample.open:,.2f} USD
Liquidity at the end: {first_sample.close:,.2f} USD
Liquidity at the begin, expressed as the quote token: {token_liquidity:.4f} {our_pair.quote_token_symbol}
Liquidity added during the period: {first_sample.add_volume:,.2f} USD
Liquidity removed during the period: {first_sample.remove_volume:,.2f} USD
""")

Read the full source code with the example output here.

Note: Currently liquidity data is not available to all trading pairs. Only for those that have a simple route to convert token valuations to US dollar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.