[Python] How to visualize recent orders for a crypto exchange

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to visualize the recent trades from a crypto exchange. I use ccxt to get the recent trades:

    import ccxt
    binance = ccxt.binance({
    'apiKey': 'KEY',
    'secret': 'SECRET'
    })

    the_tape = binance.fetch_trades('ETHBUSD')


    here is what my API call returns:

    [
    {
    'info': {
    'a': '157594085',
    'p': '3224.42000000',
    'q': '0.00820000',
    'f': '198701298',
    'l': '198701298',
    'T': '1631528034145',
    'm': False,
    'M': True
    },
    'timestamp': 1631528034145,
    'datetime': '2021-09-13T10:13:54.145Z',
    'symbol': 'ETH/BUSD',
    'id': '157594085',
    'order': None,
    'type': None,
    'side': 'buy',
    'takerOrMaker': None,
    'price': 3224.42,
    'amount': 0.0082,
    'cost': 26.440244,
    'fee': None
    },
    {
    'info': {
    'a': '157594086',
    'p': '3224.48000000',
    'q': '0.10080000',
    'f': '198701299',
    'l': '198701302',
    'T': '1631528034545',
    'm': False,
    'M': True
    },
    'timestamp': 1631528034545,
    'datetime': '2021-09-13T10:13:54.545Z',
    'symbol': 'ETH/BUSD',
    'id': '157594086',
    'order': None,
    'type': None,
    'side': 'buy',
    'takerOrMaker': None,
    'price': 3224.48,
    'amount': 0.1008,
    'cost': 325.027584,
    'fee': None
    },


    And I make it into a DataFrame:

    new_tape = [{'datetime': el['datetime'], 'side': el['side'], 'price': el['price'],
    'amount': el['amount'], 'total': el['cost']} for el in the_tape]


    This is my pandas DataFrame:

    datetime side price amount total
    0 2021-09-13 09:34:01.042 buy 3208.97 0.0234 75.089898
    1 2021-09-13 09:34:01.091 buy 3208.97 0.0200 64.179400
    2 2021-09-13 09:34:01.148 buy 3208.97 0.1444 463.375268
    3 2021-09-13 09:34:01.242 buy 3209.16 0.6205 1991.283780
    4 2021-09-13 09:34:01.420 sell 3209.00 0.2500 802.250000


    I would like to get two histograms: (1) cumulated histogram with two bars: total bought and total sold (2) weighted histogram with the amounts bought and sold, something that would look like this: [​IMG]

    I would like to use seaborn in order to be able to save the images and maybe upload them to a website or word document.

    I found this code but I don't know how to adapt it and what "binwidth" is:

    fig, ax = plt.subplots()

    ax.set_title(f"Last update: {t} (ID: {last_update_id})")

    sns.histplot(x="price", weights="quantity", hue="side", binwidth=binwidth, data=data, ax=ax)
    sns.scatterplot(x="price", y="quantity", hue="side", data=data, ax=ax)

    ax.set_xlabel("Price")
    ax.set_ylabel("Quantity")

    plt.show()


    Any help or suggestion as to how to best represent this data would be deeply appreciated.

    Continue reading...

Compartilhe esta Página