Drawdown from the peak

running totals

Drawdown from the peak

Coinbase SQL Interview Question

Coinbase shows how far an asset sits below its own running high, because that is the number a holder actually feels. Someone who bought at the top does not care that the coin is up over the year. They care that it is well below the best price they have seen.

Return the symbol, the priced_on date, the close_usd price, and the highest close seen for that asset up to and including that day as peak_to_date. Add how far below that peak the day closed as drawdown_pct, a percentage rounded to 2 decimal places. Sort by symbol and then by priced_on.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

coinbase_daily_pricesTable24 rows

Column NameType
symbolVARCHAR
priced_onDATE
close_usdDOUBLE

coinbase_daily_pricesExample Input

symbolpriced_onclose_usd
BTC2024-05-0158000
BTC2024-05-0259200
BTC2024-05-0360500
BTC2024-05-0461100
BTC2024-05-0559800
ETH2024-05-012900
ETH2024-05-022860
ETH2024-05-032945
ETH2024-05-043010
ETH2024-05-053080

Example Output

symbolpriced_onclose_usdpeak_to_datedrawdown_pct
BTC2024-05-0158000580000
BTC2024-05-0259200592000
BTC2024-05-0360500605000
BTC2024-05-0461100611000
BTC2024-05-055980061100-2.13
ETH2024-05-01290029000
ETH2024-05-0228602900-1.38
ETH2024-05-03294529450
ETH2024-05-04301030100
ETH2024-05-05308030800

Explanation

BTC rises for four days, and on each of them the peak so far is the day it closed at, so there is no drawdown. On the fifth day it closes at 59800 while the peak so far is 61100, which puts it 2.13 percent below. ETH dips on its second day and sets a new peak on the third.

The example above is a small slice of the data. Your query runs against the full tables.