Query editor
with data as((SELECT a.evt_tx_hash, a.evt_block_time, b.account,caseWHEN cast(a.realisedPnl/1e30 as INTEGER) > 0 then 1ELSE 0END as wl,a.averagePrice/1e30 as avgPrice, a.realisedPnl/1e30 as PnL, a.size/1e30 as sizefrom gmx_arbitrum.Vault_evt_ClosePosition ajoin (SELECT evt_tx_hash, account from gmx_arbitrum.Vault_evt_DecreasePosition) bon a.evt_tx_hash = b.evt_tx_hash)UNION ALL(SELECT evt_tx_hash, evt_block_time, account,caseWHEN cast(realisedPnl/1e30 as INTEGER) > 0 then 1ELSE 0END as wl,markPrice/1e30 as avgPrice, realisedPnl/1e30 as PnL, size/1e30 as sizefrom gmx_arbitrum.Vault_evt_LiquidatePosition)order by account),data_fee as (SELECT trades.time, account, key,position_side, token,ROUND(SUM(size) OVER (PARTITION BY key ORDER BY trades.time),7) as running_total_size, fee,ROW_NUMBER() OVER (PARTITION BY key ORDER BY token, time) as idFROM (SELECT evt_block_time as time, key, account, indexToken as token, price/1e30 as price, sizeDelta/1e30 as size, if(isLong, 'LONG', 'SHORT') as position_side, fee/1e30 as feeFROM gmx_arbitrum.Vault_evt_IncreasePositionWHERE account in (SELECT distinct account from data)UNION ALLSELECT evt_block_time as time, key, account, indexToken as token, price/1e30 as price, -(sizeDelta/1e30) as size, if(isLong, 'LONG', 'SHORT') as position_side, fee/1e30 as feeFROM gmx_arbitrum.Vault_evt_DecreasePositionWHERE account in (SELECT distinct account from data)) as tradesORDER BY id desc),label_grp as (SELECT tt.*,COUNT(CASE WHEN running_total_size > 0 THEN NULL ELSE 1 END) OVER (PARTITION BY key ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) as grpFROM data_fee ttORDER BY id),total_fee as (SELECT tt2.account, sum(tt2.fee) as feeFROM label_grp tt2 LEFT OUTER JOIN(SELECT account, token, position_side, grp FROM label_grp WHERE running_total_size=0) tt3on tt2.account = tt3.accountand tt2.grp = tt3.grpand tt2.token = tt3.tokenand tt2.position_side = tt3.position_sideWHERE tt3.grp is not NULLgroup by 1),all_top as(SELECT account, count(wl) as numTrade, sum(wl) as win, sum(size) as size, sum(PnL) as PnLfrom datagroup by 1),top_20 as(SELECT a.account, a.numTrade, a.win, a.size, cast(a.win as DOUBLE)/cast(a.numTrade as DOUBLE) as winrate,t.fee, (a.PnL - t.fee) as PnL