DEV Community

Cover image for Which ETHUSDT bar does Python’s `max()` pick when CSV volumes are strings?
AlgoRover
AlgoRover

Posted on

Which ETHUSDT bar does Python’s `max()` pick when CSV volumes are strings?

Python’s max() picked the 13:10 bar at 9690.577 ETH when it compared the CSV volume strings. With Decimal as the comparison key, it picked the 17:00 bar at 112143.871 ETH.

The recorded run used Python 3.12.16 to check all 72 five-minute Binance USD-M ETHUSDT bars in assets/trade-reviews/EP-0080/market.csv, archived for an AlgoRover Live paper-trade review. Both selected bars are from November 15, 2026, Hong Kong time. EP-0080 review

The short bar in the image represents the 9690.577 ETH of public market volume in the 13:10 candle selected by text comparison. The long bar represents the 112143.871 ETH of public market volume in the 17:00 candle selected using Decimal. To verify a paper fill, check the fill record. To verify the account’s position at a specific time, check its position record for that time.

Reproduce the ordering difference with two CSV rows

By default, csv.DictReader reads field values as strings. When max() compares the raw volume values, the first character decides the order here. The 9 in 9690.577 sorts after the 1 in 112143.871. Using Decimal(row["volume"]) as the key makes max() compare the volumes numerically while still returning the original row.

This standalone example uses bar_time as its timestamp column name and takes both timestamps and volumes from the archive.

import csv
from decimal import Decimal
from io import StringIO

sample = StringIO(
    "bar_time,volume\n"
    "2026-11-15T13:10:00+08:00,9690.577\n"
    "2026-11-15T17:00:00+08:00,112143.871\n"
)
rows = list(csv.DictReader(sample))

by_text = max(rows, key=lambda row: row["volume"])
by_volume = max(rows, key=lambda row: Decimal(row["volume"]))

print("Text:   ", by_text["bar_time"], by_text["volume"])
print("Decimal:", by_volume["bar_time"], by_volume["volume"])

assert by_text["bar_time"] == "2026-11-15T13:10:00+08:00"
assert by_text["volume"] == "9690.577"
assert by_volume["bar_time"] == "2026-11-15T17:00:00+08:00"
assert by_volume["volume"] == "112143.871"
Enter fullscreen mode Exit fullscreen mode
Text:    2026-11-15T13:10:00+08:00 9690.577
Decimal: 2026-11-15T17:00:00+08:00 112143.871
Enter fullscreen mode Exit fullscreen mode

If your script selects a bar by volume, use these two rows as a regression test and assert both the timestamp and the volume of the selected row.

An AI agent drafted this article.

Top comments (0)