Query editor
with pool_trades as ( -- pull the uniswap protocol pool trades and adj priceselect *,POW(cast(sqrtPriceX96 as real) / POW(2, 96), 2) as pxfrom uniswap_v3_ethereum.Pair_evt_Swapwhere contract_address = '{{pair}}'), factory_joined as ( -- find the fee tier of the pool to find the fee amountsselect *from pool_trades tleft join (select fee,token0,token1from uniswap_v3_ethereum.Factory_evt_PoolCreatedwhere pool = '{{pair}}') fon 1 = 1), calculated_variables as (select *,case when cast(amount0 as real) > 0 then 1else -1end as direction,case when cast(amount1 as real) > 0 then amount0 -- we want to calculate markout for token0else amount0 * (1 - fee / 1e6) -- however amount0 includes the fee when its the tokenInend as adj_amt0,case when cast(amount0 as real) > 0 then abs(amount1 / (amount0 * (1 - fee / 1e6)))else abs((amount1 * (1 - fee / 1e6)) / amount0)end as effective_er,case when cast(amount1 as real) > 0 then amount1 * fee / 1e6else amount0 * fee / 1e6end as fee_amt,last_value(px) over (order byevt_block_time asc range between current rowand interval 5 minutes following) as 5m,last_value(px) over (order byevt_block_time asc range between current rowand interval 1 hour following) as 1h,last_value(px) over (order byevt_block_time asc range between current rowand interval 24 hour following) as 24h,last_value(px) over (order byevt_block_time asc range between current rowand interval 336 hour following) as 336hfrom factory_joined), markout as (select *,date_trunc('minute', evt_block_time) as as_of,direction * (abs(adj_amt0) / pow(10, dec0)) * ((24h - effective_er) / pow(10, dec1) * pow(10, dec0)) * token1_usd as markout_24hr,direction * (abs(adj_amt0) / pow(10, dec0)) * ((336h - effective_er) / pow(10, dec1) * pow(10, dec0)) * token1_usd as markout_336hr,direction * (abs(adj_amt0) / pow(10, dec0)) * ((1h - effective_er) / pow(10, dec1) * pow(10, dec0)) * token1_usd as markout_1hr,direction * (abs(adj_amt0) / pow(10, dec0)) * ((5m - effective_er) / pow(10, dec1) * pow(10, dec0)) * token1_usd as markout_5m,case when cast(amount1 as real) > 0 then fee_amt * token1_usd / POW(10, dec1)else fee_amt * token0_usd / POW(10, dec0)end as fee_usdfrom calculated_variables vleft join(select minute as token1_minute,