Skip to content

Commit

Permalink
Make daily rebelancing query start at 10k (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
fleupold authored Sep 11, 2024
1 parent 581fe76 commit be845be
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@ daily_price_change as (
p1.day = ds.day
and p1.contract_address = {{token_a}}
and p1.blockchain = 'ethereum'
inner join prices.usd_daily as previous_p1
left join prices.usd_daily as previous_p1
on
previous_p1.day = ds.day - interval '1' day
-- avoid computing price change on first day
and previous_p1.day >= date(timestamp '{{start}}')
and previous_p1.contract_address = {{token_a}}
and previous_p1.blockchain = 'ethereum'
inner join prices.usd_daily as p2
on
p2.day = ds.day
and p2.contract_address = {{token_b}}
and p2.blockchain = 'ethereum'
inner join prices.usd_daily as previous_p2
left join prices.usd_daily as previous_p2
on
previous_p2.day = ds.day - interval '1' day
-- avoid computing price change on first day
and previous_p2.day >= date(timestamp '{{start}}')
and previous_p2.contract_address = {{token_b}}
and previous_p2.blockchain = 'ethereum'
)
Expand All @@ -53,6 +57,7 @@ daily_price_change as (
select
day,
-- SQL doesn't support PRODUCT() over (...), but luckily "the sum of logarithms" is equal to "logarithm of the product",
exp(sum(ln((p1 + p2) / 2)) over (order by day asc)) * 10000 as current_value_of_investment
-- coalesce to factor 1 on first day
coalesce(exp(sum(ln((p1 + p2) / 2)) over (order by day asc)), 1) * 10000 as current_value_of_investment
from daily_price_change
order by 1 desc

0 comments on commit be845be

Please sign in to comment.