Maximum drawdown of a stock
Risk managers judge a stock by its maximum drawdown: the largest fall from a previous peak to a later trough, in percent of that peak. Prices are in stocks.csv (60 trading days, rows in date order).
date: trading day (YYYY-MM-DD)ticker: fictional symbol such as ORBT, VELA, NOVA, KITEclose: closing price
Task: input is one line TICKER START_DATE. Use only rows of that ticker whose date is on or after START_DATE. Walking forward, track the running peak (the first day that reached the highest close so far). The drawdown of a day is (peak - close) / peak x 100. Print three lines: Max drawdown: X% (2 decimals, earliest day if tied), Peak: DATE PRICE and Trough: DATE PRICE (prices with 2 decimals; the peak is the running peak at the trough day). If the stock never falls below a previous peak, print Max drawdown: 0.00% only. If there are no rows print No data.
Files your program can read
date,ticker,close 2025-01-02,ORBT,117.11 2025-01-02,VELA,48.96 2025-01-02,NOVA,311.32 2025-01-02,KITE,22.25 2025-01-03,ORBT,116.32 2025-01-03,VELA,49.69 2025-01-03,NOVA,313.66 ...
Open them by name, for example open("stocks.csv").
Sample test
Input
ORBT 2025-01-02
Expected output
Max drawdown: 12.27% Peak: 2025-01-02 117.11 Trough: 2025-02-07 102.74
Your answer is also checked against 5 hidden tests.
Need a hint?
Two variables are enough: the highest close so far and the largest drawdown so far. Compare dd with strictly greater to keep the earliest day.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
