Query editor
/*HW: Treating Nouns DAO's treasury as a portfolio, what is its Portfolio Variance [as of Feb 15, 2023]?- Portfolio Variance is a measurement of risk, to learn more -> https://www.investopedia.com/terms/p/portfolio-variance.asp- Formula: Portfolio variance = w_1^2 * σ_1^2 + w_2^2 * σ_2^2 + 2 * w_1 * w_2 * Cov_1,2 (source: investopedia)- w1 = the portfolio weight of the first asset- w2 = the portfolio weight of the second asset- σ1= the standard deviation of the first asset- σ2 = the standard deviation of the second asset- Cov1,2 = the covariance of the two assets, which can thus be expressed as p_(1,2) * σ_1 * σ_2, where p_(1,2) is the correlation coefficient between the two assets- Note: As the number of assets in the portfolio grows, the terms in the formula for variance increase exponentially.For example, a three-asset portfolio has six terms in the variance calculation, while a five-asset portfolio has 15.Steps:--- Addresses- Find address(es) of Nouns DAO Treasury--- Portfolio Weights- Find the current balance of each asset in this portfolio (need to account for ERC20s and native ETH)- Find the sum TVL (total value locked) of this portfolio- _Let's filter for token with at least $1k USD balance_- Find the portfolio weight for each asset--- Standard Deviations- Define a period to look at the asset volatility (e.g. 90 days)- Find prices data for the assets for the past 90 days- For each asset, find the standard deviation- Trino stddev function: https://trino.io/docs/current/functions/aggregate.html?highlight=standard+deviation--- Covariance- For each asset combo, get the covariance- Trino covariance function: https://trino.io/docs/current/functions/aggregate.html?highlight=covariance- Combine them all together using the formula :)*/-- - Find addresses of Nouns DAO treasury addresswith addresses(address) as ( -- here we are creating a CTE (common table expression), basically a temporary table with table name as addresses and column name addressvalues-- resource: https://deepdao.io/organization/37aba1a5-2f93-4c15-9171-5df14ac4e237/organization_data/finance(0x0BC3807Ec262cB779b38D65b38158acC3bfedE10) -- input the address you find in the URL above here), min_block_time as ( -- this CTE helps us optimize other CTEs below, it limits scanning of huge tables to only periods that are necessarilyselect min(block_time) as min_time -- finds the earliest creation time for these addresses we are looking atfrom ethereum.creation_traces ca where ca.address in (select address from addresses))-- @_@ QA time: uncomment this to run to verify if you've got things right till now-- result should be "2021-08-08 16:04"-- select * from min_block_time-- - Find the current balance of each asset in this portfolio-- modify line 11 to 99 in this query https://dune.com/queries/1983746-- in that query we are group by both wallet address and the individual assets/tokens in each wallet-- but here for this problem we need to only group by individual asset because we are looking at all the addresses as one whole portfolio-- native ETH block, eth_balance as (WITH eth_in as (SELECT date_trunc('week', tx_block_time) as time, sum(value_decimal) as eth_amt