Search code examples
polygon.io

polygon.io api confusing results


I am trying to use the polygon.io api by making the following request:

'https://api.polygon.io/v2/aggs/ticker/X:BTCUSD/range/1/day/2024-01-04/2024-01-06?adjusted=true&sort=asc&limit=120&apiKey=APIKEYHERE'

This should give me the daily bars for 3 different days:

    {
    "ticker": "X:BTCUSD",
    "queryCount": 3,
    "resultsCount": 3,
    "adjusted": true,
    "results": [
        {
            "v": 23483.26711392685,
            "vw": 43804.8366,
            "o": 42860.58,
            "c": 44193.29,
            "h": 44810,
            "l": 42650,
            "t": 1704326400000,
            "n": 367559
        },
        {
            "v": 23295.117121494848,
            "vw": 43711.2836,
            "o": 44199.04,
            "c": 44186.59,
            "h": 44449,
            "l": 42413,
            "t": 1704412800000,
            "n": 373665
        },
        {
            "v": 7422.341112331486,
            "vw": 43857.4888,
            "o": 44179.9,
            "c": 44018,
            "h": 44263,
            "l": 43436.5,
            "t": 1704499200000,
            "n": 180768
        }
    ],
    "status": "OK",
    "request_id": "id_here",
    "count": 3
}

what I'm confused on, is why the api does not provide data where the close of one day is the same as the open of the next day. This is not like the stock market where there is after hours trading as bitcoin runs 24/7, so when one day closes that price is what the next day starts at no? Additionally, this data does not match up with what trading view shows for the same days as open and closing pricing. Is polygon.io's api not a reliable source for this data or is there something else going on that I'm not considering?

TIA!


Solution

  • Justin from Polygon.io here. To understand why the closing price of one day and the opening price of the next day for an asset that trades 24/7 like Bitcoin, it really helps to look at the mechanics of trade data aggregation and what "open" and "close" prices (aka trades) really are.

    Stream of Trades

    Imagine a continuous flow of trades happening around the clock where trade data is gathered from a bunch of places (aka exchanges) where Bitcoin is traded. Each trade is a transaction where Bitcoin is exchanged at a specific price. This can be visualized as a series of boxes, each representing a trade with its own unique price and timestamp.

    Segmentation into Days

    Despite the 24/7 nature of the Bitcoin market, for analysis, this continuous stream is divided into arbitrary days, typically based on a specific timezone (e.g., UTC). This segmentation is somewhat artificial but necessary for daily statistics.

    Closing Trade of the Day

    The "closing" price for any given day is determined by the price of the last trade within that day's boundary. It's important to note that this closing trade is just another trade in the continuous stream and doesn't have any relation to the first trade of the following day. The separation into days means that we only consider trades within each day's bounds for daily statistics, leading to the difference between one day's close and the next day's open.

    Opening Trade of the Next Day

    The "opening" price for a day is the price of the first trade that occurs after the previous day's closing. Since the market operates continuously, this trade could happen just seconds after the previous day's last trade, but it's considered part of a new day.

    Continuation of the Cycle

    This process repeats daily, with each day treated as a separate entity for the purposes of open, high, low, and close (OHLC) statistics. The cycle of opening and closing continues, with each day's statistics encapsulated within the trades of that specific 24-hour period.

    So, the reason the closing price of one day and the opening price of the next day in a 24/7 market like Bitcoin's may not match is due to the nature of daily statistical boundaries in a continuous trading environment. The same thing happens with stocks like TSLA for example. Each day is considered in isolation for the purposes of OHLC statistics, and the open and close prices are simply the prices of the first and last trades within those arbitrary daily boundaries.