- variables
- int
- float
- str
- bool
- type()
- floating-point precision
Variables & Financial Data Types
Scene: 9:31 AM, your first day on the trading floor. A trader calls across the desk: "Book it — 200 Microsoft at 372.15, Quant desk, flag me when it settles." One sentence, five pieces of data — and each one is a different kind of thing: a symbol, a price, a share count, a desk name, a yes/no. Before Python can analyze markets for you, it must first hold that trade — and holding it precisely is exactly what variables and types are for.
In quantitative finance, every number tells a story. Before you can analyze markets, you need to represent financial data precisely.
Python's Core Types
| Type | Finance Use Case | Example |
|---|---|---|
int | Share counts, trade IDs | quantity = 500 |
float | Prices, returns, rates | close_price = 174.55 |
str | Tickers, desk names | ticker = "AAPL" |
bool | Trade flags, settled? | is_settled = True |
🧠 The mental model: a variable is a labelled box on the trade ticket
Picture a paper trade ticket with printed boxes: ticker, price, qty, desk, settled?. A Python variable is exactly that — a name stuck onto a value. And the value's type decides which operations make sense: price * quantity is a dollar amount, while ticker * desk is nonsense and Python will refuse to compute it. You can ask any value what it is with type(price).__name__ → 'float' — the starter code does this for every field, like a back-office clerk ticking each box on the ticket.
Precision Matters in Finance
Python# Floating-point gotcha — critical in P&L calculations! >>> 0.1 + 0.2 0.30000000000000004 # Use round() for display; Decimal for exact arithmetic import decimal >>> decimal.Decimal('0.10') + decimal.Decimal('0.20') Decimal('0.30')
Your Task
Define a trade: ticker MSFT, price 372.15, quantity 200 shares, desk "Quant", settled True.
The starter code already prints the full trade ticket — every field with its type, checked via type(...).__name__. But the notional value (price × quantity — the total dollars changing hands) is stubbed to 0.0, and a $0 trade would raise eyebrows in any back office. Replace the placeholder with the real arithmetic.
Predict before you run: 372.15 × 200 — do it in your head: 372 × 200 = 74,400, plus 0.15 × 200 = 30. What should the last line of the ticket say? And what type will the result be — a float times an int gives...? Run and check both predictions.
📜 Why These Fields Exist
The five fields you are about to define — ticker, price, quantity, desk, settled — are not arbitrary. They map directly to real financial infrastructure with deep historical roots.
Tickers were born on the stock ticker tape invented by Edward Calahan in 1867 and improved by Thomas Edison in 1869. The machine printed abbreviated company names on paper tape at up to 500 characters per minute. The abbreviations became standardised over decades of trading — "AAPL" for Apple, "MSFT" for Microsoft.
Settlement (is_settled) dates to the physical delivery of share certificates. In the 19th century, settlement took weeks because clerks had to physically move paper. The US moved to T+5 (five days) in the 1980s, then T+3 in 1995 after the 1987 crash exposed how slow settlement amplified losses, T+2 in 2017, and finally T+1 in 2024 — 157 years after the first ticker tape.
Float vs integer precision (price as float, quantity as int) reflects real exchange rules. The NYSE quoted prices in fractions until April 9, 2001, when US markets fully "decimalised" — switching to pennies ($0.01). The minimum tick had been 1/8 ($0.125) for most of the 20th century, narrowed to 1/16 ($0.0625) in 1997, then abolished entirely. This single regulatory change tightened bid-ask spreads dramatically and cost market-makers billions in revenue.
💡 Career note: In a front-office OMS (Order Management System), every trade record carries exactly these fields. Getting types right prevents P&L mismatches at month-end.