Lars Riis Olsen
05/22/2023, 11:42 AMselect firstleg_vwap - secondleg_vwap spread from (
select executedat firstleg_executedat, SUM(price*qty) / SUM(qty) firstleg_vwap from trade where instrument='[FIRST_LEG]'
SAMPLE by 5m
ALIGN TO CALENDAR
) splice join (
select executedat secondleg_executedat, SUM(price*qty) / SUM(qty) secondleg_vwap from trade where instrument='[SECOND_LEG]'
SAMPLE by 5m
ALIGN TO CALENDAR
)
What is the best way to do this?
Nicolas Hourcard
05/22/2023, 1:13 PMBolek Ziobrowski
05/22/2023, 2:13 PMselect executedat,
coalesce(SUM( case when instrument = '[FIRST_LEG]' then price*qty else 0 end) / SUM( case when instrument = '[FIRST_LEG]' then qty else 0 end),0) -
coalesce(SUM( case when instrument = '[SECOND_LEG]' then price*qty else 0 end) / SUM( case when instrument = '[SECOND_LEG]' then qty else 0 end),0) as spread
from trade
where instrument in ('[FIRST_LEG]', '[SECOND_LEG]')
SAMPLE by 5m
ALIGN TO CALENDAR
or
select executedat, coalesce(max(case when instrument = '[FIRST_LEG]' then vwap else 0 end),0) - coalesce(max(case when instrument = '[SECOND_LEG]' then vwap else 0 end),0) as spread
from
(
select executedat, instrument, SUM( price*qty ) / SUM( qty) as vwap
from trade
where instrument in ('[FIRST_LEG]', '[SECOND_LEG]')
SAMPLE by 5m
ALIGN TO CALENDAR
)
group by executedat
javier ramirez
05/22/2023, 2:56 PMwith s1 as (
SELECT
_timestamp_,
sum(price * amount) / sum(amount) AS vwap_price
FROM trades
WHERE _timestamp_ IN '2023-05-21' and _symbol_ = 'ETH-USD' SAMPLE BY 15m ALIGN TO CALENDAR ),
s2 as (
SELECT
_timestamp_,
_symbol_,
sum(price * amount) / sum(amount) AS vwap_price
FROM trades
WHERE _timestamp_ IN '2023-05-21' and _symbol_ = 'BTC-USD' SAMPLE BY 15m ALIGN TO CALENDAR )
select s1._timestamp_, s1.vwap_price, s2.vwap_price from s1 asof join s2;
------------------------------------
select
_timestamp_,
coalesce(
SUM(
CASE
WHEN _SYMBOL_ = 'ETH-USD' then price * amount
ELSE 0
END
) / SUM(
CASE
WHEN _SYMBOL_ = 'ETH-USD' then amount
ELSE 0
END
),
0
),
coalesce(
SUM(
CASE
WHEN _SYMBOL_ = 'BTC-USD' then price * amount
ELSE 0
END
) / SUM(
CASE
WHEN _SYMBOL_ = 'BTC-USD' then amount
ELSE 0
END
),
0
)
FROM
trades
WHERE
_timestamp_ IN '2023-05-21'
SAMPLE BY 15m ALIGN TO CALENDAR
Nicolas Hourcard
05/22/2023, 3:03 PMLars Riis Olsen
05/22/2023, 6:37 PMjavier ramirez
05/22/2023, 7:06 PMBolek Ziobrowski
05/22/2023, 8:27 PM