SocioAnalyticaspread (blitz)
    Updated 2024-04-25
    -- forked from spread (blitz) @ https://flipsidecrypto.xyz/edit/queries/e18a6bd8-e18b-44b8-93f3-7bd79944aab3

    with asks as (
    select
    hour,
    ticker_id,
    product_id,
    price,
    volume,
    row_number () over (partition by hour, ticker_id, product_id order by price asc) as rank
    from blast.blitz.ez_market_depth_stats
    where orderbook_side = 'asks'
    )
    ,
    bids as (
    select
    hour,
    ticker_id,
    product_id,
    price,
    volume,
    row_number () over (partition by hour, ticker_id, product_id order by price desc) as rank
    from blast.blitz.ez_market_depth_stats
    where orderbook_side = 'bids'
    )

    select
    a.hour,
    a.ticker_id,
    (ABS(a.price - b.price)) as spread,
    ((a.price+b.price)/2) as mid_price,
    100 * spread / mid_price as percentage_spread
    from asks a
    join bids b on a.hour = b.hour and a.ticker_id = b.ticker_id and a.product_id = b.product_id
    and a.rank = b.rank
    QueryRunArchived: QueryRun has been archived