Query editor
WITH savm_trades AS (SELECTblock_time,tx_hash,tx_from,token_bought_address,token_sold_address,token_bought_amount,token_sold_amount,amount_usdFROM dex.tradesWHERE blockchain = 'ethereum'AND tx_from = 0xcc02d6fae48fe1652395d2d874332ef710a0375cAND amount_usd IS NOT NULLAND (token_bought_address = 0x15e6e0d4ebeac120f9a97e71faa6a0235b85ed12OR token_sold_address = 0x15e6e0d4ebeac120f9a97e71faa6a0235b85ed12)),buys AS (SELECTSUM(token_bought_amount) AS total_bought_savm,SUM(amount_usd) AS total_buy_usdFROM savm_tradesWHERE token_bought_address = 0x15e6e0d4ebeac120f9a97e71faa6a0235b85ed12),sells AS (SELECTSUM(token_sold_amount) AS total_sold_savm,SUM(amount_usd) AS total_sell_usdFROM savm_tradesWHERE token_sold_address = 0x15e6e0d4ebeac120f9a97e71faa6a0235b85ed12)SELECTb.total_bought_savm,b.total_buy_usd,s.total_sold_savm,s.total_sell_usd,(b.total_bought_savm - s.total_sold_savm) AS net_savm_position,b.total_buy_usd / NULLIF(b.total_bought_savm, 0) AS avg_buy_price_usd,s.total_sell_usd / NULLIF(s.total_sold_savm, 0) AS avg_sell_price_usd,(b.total_buy_usd * (s.total_sold_savm / NULLIF(b.total_bought_savm, 0)))AS cost_basis_sold_usd,s.total_sell_usd- (b.total_buy_usd * (s.total_sold_savm / NULLIF(b.total_bought_savm, 0)))AS realised_pnl_usdFROM buys bCROSS JOIN sells s