Query editor
WITH eth_price as (SELECTdate_trunc('day',minute) as day,MAX_BY(price, minute) AS eth_usdFROMprices.usdWHEREcontract_address = 0x4200000000000000000000000000000000000006group by 1order by 1 desc),trades AS (SELECTsubject,date_trunc('day',evt_block_time) as day,(CASEWHEN isBuy = True THEN CAST(shareAmount as int)WHEN isBuy = False THEN CAST(shareAmount as int) * -1ELSE 0END) as shares_change,(case when isBuy = Truethen (ethAmount + protocolEthAmount + subjectEthAmount) / 1e18when isBuy = Falsethen ethAmount*-0.9 / 1e18else 0end) as total_investFROM friendtech_base.FriendtechSharesV1_evt_TradeWHERE trader = {{Wallet Address}}),cumulative_trades AS (SELECTday,subject,SUM(shares_change) OVER (PARTITION BY subject ORDER BY day) as total_sharesFROM trades),dates AS (with day_seq as (SELECT (sequence(CAST((SELECT MIN(day) FROM trades) as date),CAST(current_timestamp(6) as date),interval '1' day)) AS day)SELECTdays.dayFROM day_seqCROSS JOIN unnest(day) as days(day)),portfolio AS (SELECTd.day,ct.subject,MAX_BY(ct.total_shares,ct.day) as share_numberFROM dates dJOIN cumulative_trades ct ON d.day >= ct.day--WHERE ct.total_shares > 0group by 1,2