σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
QuantLib skill›Module 1 · Dates and schedules›Topic 1.1·~50 minIntermediate

Dates and calendars: counting the days

Skill positionModule 1 of 5 · Topic 1 of 2
1.1 Dates and calendars: counting the days
—Lab score— / 100Total XP—Streak—
Lecture0% read

11 min readCode examples: 5

  1. 01Months are not a fixed length
  2. 02Rules that name a weekday
  3. 03The calendar of a market
  4. 04Counting days into years
  5. 05Checking the calendar against the market
  6. 06What you will build

Between 2 January 2015 and 21 August 2026 there were 3,036 weekdays, but SPY traded on only 2,926 of them. The calendar you build in this topic predicts 2,928 open days and misses just two: 5 December 2018 and 9 January 2025, when the market closed for the funerals of two presidents. A pricing library has to know what day it is before it can know what anything is worth, and this is the first piece of it.

01

Months are not a fixed length

Where we are. This is the first topic of a skill in which you write miniql, a small pricing library in Python with the same ideas as the open-source library QuantLib: dates, schedules, rates, curves, instruments and pricing engines. Each topic adds one piece, and each piece is used by the next. We start with dates because every price starts there: a bond pays on certain days, interest builds up over a number of days, and a trade settles only on a day when the market is open.

The first difficulty is that a month is not a fixed number of days. A bond that pays every month on the 31st cannot pay on 31 February. The usual rule, which QuantLib applies when it adds a period of months to a date, keeps the day of the month and, when the target month is too short, cuts the day back to the last day of that month. To avoid special cases for a change of year or a negative number of months, it helps to turn the date into a single month count, year × 12 + (month − 1), add the months, and split the result again with a division.

index = 12 × year + (month − 1) + n, new year = index ÷ 12, new month = index mod 12 + 1, day = min( day, days in the new month )(1)n is the number of months to add, negative for a date in the past; ÷ is whole-number division and mod is the remainder

Worked example 1. *Problem:* add 1, 2, 3, 6 and 12 months to 29 August 2025, then add one month to 31 January 2024 and two months, subtract one month from 31 March 2024, and add a year and four years to 29 February 2024. *Plan:* use add_months. *Steps:* below.

main.pyPython
start = date(2025, 8, 29)print([str(add_months(start, n)) for n in (1, 2, 3, 6, 12)])1print(add_months(date(2024, 1, 31), 1), add_months(date(2024, 1, 31), 2), add_months(date(2024, 3, 31), -1))2print(add_months(date(2024, 2, 29), 12), add_months(date(2024, 2, 29), 48))3
['2025-09-29', '2025-10-29', '2025-11-29', '2026-02-28', '2026-08-29']
2024-02-29 2024-03-31 2024-02-29
2025-02-28 2028-02-29
  1. 1Same day of the month, until a short month cuts it.
  2. 231 January plus one month is 29 February in a leap year.
  3. 3A leap day a year later falls back to 28 February.

*Check:* from 29 August the results are 2025-09-29, 2025-10-29, 2025-11-29, 2026-02-28 and 2026-08-29: the 6-month step lands in a February that has only 28 days, so the day is cut. From 31 January 2024 one month gives 2024-02-29 and two months give 2024-03-31; one month before 31 March is 2024-02-29 again. The leap day gives 2025-02-28 after a year and 2028-02-29 after four. *What to notice:* the rule is not reversible. Adding a month and then subtracting it does not always bring you back to where you started, and that is why schedules are generated from the start date every time, never by stepping from the previous date.

02

Rules that name a weekday

Most market holidays are not given as a date but as a rule: the third Monday of January, the last Monday of May, the fourth Thursday of November. So the calendar needs a function that finds the n-th weekday of a month. Count weekdays from Monday as 0 to Sunday as 6. For the first, second, third... one, find how many days the first day of the month is from the wanted weekday and then add whole weeks. For the last one, do the same backwards from the last day of the month.

One holiday follows a rule that is not about weekdays alone: Good Friday, the Friday before Easter, which is why the market is closed on a date that moves from year to year. Easter Sunday is the first Sunday after the first full moon of spring, and the anonymous Gregorian algorithm finds it with nothing but whole-number division and remainders. You do not need to understand why it works, only to follow its steps exactly; a small correction term, zero in most years, matters in a few years such as 1981 and 2049.

Worked example 2. *Problem:* find the third Monday of January 2024, the last Monday of May 2024 and the fourth Thursday of November 2024, then Easter Sunday for 2023 to 2026. *Plan:* use nth_weekday and easter. *Steps:* below.

main.pyPython
print(nth_weekday(2024, 1, 0, 3), nth_weekday(2024, 5, 0, -1), nth_weekday(2024, 11, 3, 4))1print([str(easter(year)) for year in (2023, 2024, 2025, 2026)])2
2024-01-15 2024-05-27 2024-11-28
['2023-04-09', '2024-03-31', '2025-04-20', '2026-04-05']
  1. 1Monday is 0, Thursday is 3; -1 means the last one.
  2. 2Easter moves between 22 March and 25 April.

*Check:* the dates are 2024-01-15 (Martin Luther King Day), 2024-05-27 (Memorial Day) and 2024-11-28 (Thanksgiving). Easter is 2023-04-09, 2024-03-31, 2025-04-20 and 2026-04-05, so Good Friday 2024 is 29 March, two days before. *What to notice:* the earliest Easter possible is 22 March and the latest is 25 April; 2024 is early, and its Good Friday falls before the end of March.

03

The calendar of a market

A calendar answers one question: is this day a business day, a day on which the market is open and payments are made? For the New York stock market, a day is open when it is a Monday to Friday and it is not a scheduled holiday. The scheduled holidays are New Year's Day, Martin Luther King Day, Washington's Birthday, Good Friday, Memorial Day, Juneteenth (from 2022), Independence Day, Labor Day, Thanksgiving and Christmas.

A holiday with a fixed date needs the observation rule. If it falls on a Saturday the market is closed on the Friday before, and if it falls on a Sunday the market is closed on the Monday after. There is one exception: when New Year's Day falls on a Saturday, the market stays open on the Friday, 31 December, because that day belongs to the old year's accounts. Then come the three functions that all calendars offer: whether a day is a business day, the date n business days later, and the number of business days in a range.

Worked example 3. *Problem:* list the holidays of 2023, check 31 December 2021, and move dates by business days around Easter 2024: one day after Thursday 28 March, zero and one day after Saturday 30 March, and one day before Monday 1 April. Then count the business days in the week that starts on Monday 25 March 2024, and in the whole of 2023 and 2022. *Plan:* use market_holidays, is_business_day, advance and business_days_between. *Steps:* below.

main.pyPython
print([str(day) for day in market_holidays(2023)])1print(is_business_day(date(2021, 12, 31)), [str(day) for day in market_holidays(2021)][-1])2print(advance(date(2024, 3, 28), 1), advance(date(2024, 3, 30), 0), advance(date(2024, 3, 30), 1), advance(date(2024, 4, 1), -1))3print(business_days_between(date(2024, 3, 25), date(2024, 4, 1)), business_days_between(date(2023, 1, 1), date(2024, 1, 1)), business_days_between(date(2022, 1, 1), date(2023, 1, 1)))4
['2023-01-02', '2023-01-16', '2023-02-20', '2023-04-07', '2023-05-29', '2023-06-19', '2023-07-04', '2023-09-04', '2023-11-23', '2023-12-25']
True 2021-12-24
2024-04-01 2024-03-30 2024-04-01 2024-03-28
4 250 251
  1. 1New Year 2023 was a Sunday, so the holiday is the Monday.
  2. 22022 began on a Saturday: no Friday off.
  3. 3Zero days leaves a weekend date where it is.
  4. 4The end date is not counted.

*Check:* 2023 has ten holidays, the first 2023-01-02. 2021-12-31 is open, and the last holiday of 2021 is 2021-12-24, Christmas observed on the Friday. One business day after Thursday 28 March is 2024-04-01: Good Friday and the weekend are skipped. Zero days after Saturday 30 March is 2024-03-30 itself, one day is 2024-04-01, and one day before Monday 1 April is 2024-03-28. The week of 25 March has 4 business days, 2023 has 250 and 2022 has 251. *What to notice:* 2022 had one more open day than 2023 because it had 9 holidays against 10: New Year's Day 2022 fell on a Saturday and was not observed.

In QuantLib

The same pieces exist there under other names. ql.Date(31, 1, 2024) + ql.Period(1, ql.Months) is add_months. ql.UnitedStates(ql.UnitedStates.NYSE) is the calendar, with isBusinessDay(date), advance(date, n, ql.Days) and businessDaysBetween(d1, d2). A calendar is an object you create once and pass around, instead of a hard-coded list of holidays; that design choice is the subject of a later topic.

04

Counting days into years

Interest is paid for a length of time, and time is measured in years: interest = notional × rate × year fraction. The year fraction between two dates depends on a day-count convention, an agreement written into the contract about how to count. The convention is part of the price: two bonds with the same coupon rate but different conventions pay different amounts.

interest = notional × rate × year fraction( start, end, convention )(2)the year fraction is a number such as 0.5 for about half a year

Four conventions cover most cases. Actual/360 and Actual/365 divide the real number of days by 360 or by 365, and are used for money markets and many loans. 30E/360 pretends every month has 30 days (a 31st counts as the 30th) and every year 360, which makes half a year exactly 0.5. Actual/Actual (ISDA) gives every calendar year its own length, 365 or 366 days, so a day of a leap year is worth slightly less than a day of an ordinary one.

Worked example 4. *Problem:* compute the year fraction of all four conventions between 1 July 2023 and 1 July 2024 (a span that contains the leap day), and between 31 January and 31 March 2024. Then find the interest on one million dollars at 5% for the second span under Actual/360 and Actual/365. *Plan:* use year_fraction. *Steps:* below.

main.pyPython
for start, end in ((date(2023, 7, 1), date(2024, 7, 1)), (date(2024, 1, 31), date(2024, 3, 31))):    print(start, end, {name: round(year_fraction(start, end, name), 6) for name in CONVENTIONS})1print(round(1_000_000 * 0.05 * year_fraction(date(2024, 1, 31), date(2024, 3, 31), "act/360"), 2), round(1_000_000 * 0.05 * year_fraction(date(2024, 1, 31), date(2024, 3, 31), "act/365"), 2))2
2023-07-01 2024-07-01 {'act/360': 1.016667, 'act/365': 1.00274, '30e/360': 1.0, 'act/act': 1.001377}
2024-01-31 2024-03-31 {'act/360': 0.166667, 'act/365': 0.164384, '30e/360': 0.166667, 'act/act': 0.163934}
8333.33 8219.18
  1. 1One line per convention.
  2. 2The contract's convention changes the money.

*Check:* over the first span (366 days) Actual/360 gives 1.016667, Actual/365 gives 1.00274, 30E/360 gives exactly 1.0, and Actual/Actual gives 1.001377, that is 184 / 365 + 182 / 366. Over the second span (60 days) the fractions are 0.166667, 0.164384, 0.166667 and 0.163934. On one million dollars at 5% the interest is 8333.33 under Actual/360 and 8219.18 under Actual/365: a difference of 114.15 dollars for the same dates and the same rate. *What to notice:* only 30E/360 gives exactly 1 for this year. Actual/Actual gives exactly 1 for a span from 1 January to the next 1 January, and a little more here, because the span starts in the middle of a year and the leap day falls in its second part.

05

Checking the calendar against the market

A calendar is a set of rules, and rules can be wrong. The way to test them is to compare with what really happened. SPY is traded on the days the New York market is open, so its price table is a record of the open days. If the calendar is right, it should say "open" for every day in the table, and "closed" for every weekday missing from it. The only differences should be the days nobody could have planned for.

Worked example 5. *Problem:* test the calendar on the SPY table from 2015 to 21 August 2026: count the weekdays, the business days by the calendar and the trading days in the data, list the days the calendar says are open but that have no price, list the days with a price that the calendar says are closed, and compute the year fraction of the whole period under the four conventions. *Plan:* study walks through every day of the period. *Steps:* below.

main.pyPython
out = study(load_data())print(out["first"], out["last"], out["weekdays"], out["business_days"], out["trading_days"])1print(out["unscheduled"], out["unexpected"])2print({name: round(value, 4) for name, value in out["fractions"].items()})3
2015-01-02 2026-08-21 3036 2928 2926
['2018-12-05', '2025-01-09'] []
{'act/360': 11.8028, 'act/365': 11.6411, '30e/360': 11.6361, 'act/act': 11.6329}
  1. 1Weekdays, business days by rules, rows in the data.
  2. 2Open by the rules but no price; a price on a closed day.
  3. 3The same 11.6 years, counted four ways.

*Check:* the period has 3036 weekdays, of which the calendar calls 2928 business days, and the data has 2926 rows. The 108 weekdays that are not business days are the holidays, about nine a year. The two days the calendar gets wrong are 2018-12-05 and 2025-01-09, national days of mourning declared for the funerals of George H. W. Bush and Jimmy Carter, and no day in the data falls on a day the calendar closes. The 11.6 years are 11.8028 under Actual/360, 11.6411 under Actual/365, 11.6361 under 30E/360 and 11.6329 under Actual/Actual. *What to notice:* a calendar built from rules gets 2,926 of 2,928 days right, and the two exceptions are one-off decisions that no rule can predict. A real library lets you add such dates by hand.

Never count days by hand

Dividing by 365 for a year, adding 30 days for a month, or counting weekdays and forgetting holidays all give answers that look right and are slightly wrong, and a price made of many such pieces is slightly wrong everywhere. Put each rule in one function, test it against known cases, and call it from everywhere.

  • FunctionQuestion it answers
  • add_months(d, n)Which date is n months later?QuantLib: Date + Period(n, Months)
  • nth_weekday(year, month, weekday, n)Which date is the n-th Monday, Thursday...?the rule behind most holidays
  • easter(year)When is Easter Sunday?needed for Good Friday
  • market_holidays(year)On which days is the market closed?uses nth_weekday and easterQuantLib: UnitedStates(NYSE)
  • is_business_day(d)Is the market open today?uses market_holidaysQuantLib: isBusinessDay
  • advance(d, n)Which day is n business days later?uses is_business_dayQuantLib: advance
  • business_days_between(start, end)How many business days lie in [start, end)?uses is_business_dayQuantLib: businessDaysBetween
  • year_fraction(start, end, convention)How many years is it, by contract?QuantLib: Actual360, Thirty360, ActualActual
What the real data saysThe SPY price table, from 2 January 2015 to 21 August 2026, against the New York calendar
Weekdays in the period3,036Monday to Friday, both ends included
Business days by the calendar2,928108 weekdays are scheduled holidays
Days with a SPY price2,926two fewer than the calendar predicts
Unscheduled closings found25 December 2018 and 9 January 2025
Interest on 1,000,000 at 5%, 31 Jan to 31 Mar 20248,333.33Actual/3608,219.18 under Actual/365

Key takeaways

  • Months are not a fixed length. Add months to the month count and cut the day back to the end of a short month; schedules start from the first date every time.
  • Holidays are rules. A weekday of a month, an observation rule for weekends, and Easter for Good Friday give the whole New York calendar.
  • Build the calendar on small functions. Holidays, then business day, then advance and count: each uses the one before, so each can be tested alone.
  • The day count is part of the price. Actual/360, Actual/365, 30E/360 and Actual/Actual give different year fractions for the same dates, and a different amount of interest.
Quick check
Your turn. What is add_months(date(2023, 1, 31), 1)?
Pick an answer.
06

What you will build

The lab follows the lecture. Task 1 is the month arithmetic and task 2 the n-th weekday. Task 3 is Easter, and task 4 puts them together into the holidays of a year. Tasks 5 to 7 are the three business-day functions. Task 8 is the four day-count conventions, and task 9 tests your calendar against the days SPY really traded.

Next topic: Schedules and business-day conventions. A bond does not pay on dates picked by hand: its payment dates are generated by stepping a number of months from a start date, and some of them land on a Saturday or a holiday. The next topic builds the schedule and decides how to move a date like that, using the calendar you have just written.

Lab · what's graded

9 tasks · 100 pts
  • Month arithmetic8 pts
  • The n-th weekday8 pts
  • Easter8 pts
  • The market holidays14 pts
  • A business day8 pts
  • Moving by business days10 pts
  • Counting business days8 pts
  • Day-count conventions16 pts
  • The calendar against the market20 pts
Explorer · lab-sqt-01
▾ lab-sqt-01
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sqt-01$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sqt-01 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes