Skip to content

Programming

Programming turns a financial rule into executable instructions. Follow a checked USD valuation function from input data to repeatable, tested outputs.

Programming is the process of designing, writing and checking instructions that a computer can execute. In quant work, it turns a financial rule into a repeatable calculation: the same valuation rule can process a changed position without someone rewriting the arithmetic by hand.
An algorithm describes a procedure. A program expresses instructions in a language with precise execution rules, such as Python. Programming also includes choosing data representations, handling invalid inputs and checking whether the result answers the financial question.

Turn a rule into a callable calculation

Consider a fictional long holding. Its marked value is the number of units held multiplied by the USD mark per unit. This example deliberately accepts only non-negative whole-unit quantities and whole-dollar marks; it excludes fees, currency conversion and instrument multipliers.

def position_value(quantity: int, mark_usd: int) -> int: """Value a long holding in whole USD under the stated input contract.""" if type(quantity) is not int or type(mark_usd) is not int: raise TypeError("use whole units and whole USD") if quantity < 0 or mark_usd < 0: raise ValueError("quantity and mark must be non-negative") return quantity * mark_usd print(position_value(2, 100)) # 200 print(position_value(3, 100)) # 300

The function definition gives the rule a name. Each call supplies inputs, runs the checks and returns an output. Changing the quantity from 2 to 3 changes the value from USD 200 to USD 300; the definition stays the same. A real position can require fractional quantities, more precise prices or signed holdings, so this teaching contract must change before using the function for those cases.

Two fictional calls share the rule quantity times USD mark per unit. Quantity 2 at USD 100 gives USD 200; quantity 3 at USD 100 gives USD 300. The rule is unchanged.
Two fictional calls share the rule quantity times USD mark per unit. Quantity 2 at USD 100 gives USD 200; quantity 3 at USD 100 gives USD 300. The rule is unchanged.
One rule, two sets of inputs: increasing the holding by one unit adds USD 100 at the unchanged mark. Fictional whole-unit holdings and whole-dollar marks.

The example connects four building blocks:

Building blockIts job hereWhat it grows into
Data and data typesIntegers represent quantity and the whole-dollar markInstrument records, dated observations and position tables
ExpressionsMultiplication computes the marked valuePricing formulas and transformations
Control flowif chooses whether to raise an error or continueBranches for different cases; loops over many positions
Functionsposition_value packages the rule behind explicit inputs and an outputSmall calculations composed into a valuation pipeline
Python's official control-flow tutorial develops branches, loops and function definitions. A function's returned number can feed another calculation; displaying that number with print is a separate step.

Running successfully is only the first check

The int annotations describe intended types. They do not enforce them at runtime; the explicit if statements perform the checks shown here. Even those checks cannot establish that a mark is in USD, belongs to the right instrument or is valid at the valuation time. Those are parts of the domain contract: what each input means, which cases are permitted and what the output represents.
A unit test checks a small piece of code against a specified expectation. For this function, useful cases include an ordinary holding, a zero holding and rejected inputs. These simple checks can run after the function definition:
assert position_value(2, 100) == 200 assert position_value(0, 100) == 0 try: position_value(-1, 100) except ValueError as error: print(error) # quantity and mark must be non-negative
The expected USD 200 comes from independently adding two USD 100 units. The zero case checks a boundary; the final call exercises the rejection path. For a maintained test suite, Python's unittest supplies assertions for values and expected exceptions, including a failure when an expected exception never occurs.

Tests supply evidence for the cases they check. They cannot rescue an incorrect financial specification: a perfectly implemented multiplication still misvalues a contract if the required multiplier was omitted. Programming makes the rule inspectable and repeatable; domain review and tests make mistakes easier to find.

Both blocks were executed with Python 3.14.4 using only built-ins. The integers make these whole-dollar calculations exact; this is not a prescription for representing every financial amount as an integer number of dollars.

Start building the pieces

Both belong to Python Foundations, whose full lessons are free with sign-in. The public links show previews.

Sources

Last updated
  • programming
  • python
  • functions
  • valuation
  • testing
← Back to the glossaryBack to top ↑