Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"

[weakdeps]
DayCounts = "44e31299-2c53-5a9b-9141-82aa45d7972f"
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"

[extensions]
FinanceCoreDayCountsExt = "DayCounts"
FinanceCoreLoopVectorizationExt = "LoopVectorization"

[compat]
Dates = "1"
DayCounts = "0.1"
LoopVectorization = "0.12.166"
Roots = "1.0.12, 2"
julia = "1.10"
59 changes: 59 additions & 0 deletions ext/FinanceCoreDayCountsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module FinanceCoreDayCountsExt

import FinanceCore
import DayCounts
import Dates

"""
discount(rate::Union{Real, Rate}, from::Date, to::Date, dc::DayCounts.DayCount)

Discount `rate` over the interval between two `Date`s, with the interval measured as a
year fraction under the day count convention `dc` (via `DayCounts.yearfrac(from, to, dc)`).
As with the scalar-time methods, a non-`Rate` `rate` is assumed to be `Periodic(rate, 1)`.

Only a constant rate is accepted: its discount factor depends only on the length of the
interval. A yield curve's does not, since the curve is anchored at its valuation date, so it
cannot be discounted over two dates without that anchor.

Available when DayCounts.jl is loaded.

# Examples

```julia-repl
julia> using FinanceCore, DayCounts, Dates

julia> discount(0.05, Date(2024, 1, 1), Date(2024, 7, 1), DayCounts.Actual365Fixed())
0.9759653002306966

julia> discount(0.05, DayCounts.yearfrac(Date(2024, 1, 1), Date(2024, 7, 1), DayCounts.Actual365Fixed()))
0.9759653002306966
```
"""
function FinanceCore.discount(rate::Union{Real, FinanceCore.Rate}, from::Dates.Date, to::Dates.Date, dc::DayCounts.DayCount)
return FinanceCore.discount(rate, DayCounts.yearfrac(from, to, dc))
end

"""
accumulation(rate::Union{Real, Rate}, from::Date, to::Date, dc::DayCounts.DayCount)

Accumulate `rate` over the interval between two `Date`s, with the interval measured as a
year fraction under the day count convention `dc` (via `DayCounts.yearfrac(from, to, dc)`).
As with the scalar-time methods, a non-`Rate` `rate` is assumed to be `Periodic(rate, 1)`.
Only a constant rate is accepted (see `discount`).

Available when DayCounts.jl is loaded.

# Examples

```julia-repl
julia> using FinanceCore, DayCounts, Dates

julia> accumulation(0.05, Date(2024, 1, 1), Date(2024, 7, 1), DayCounts.Thirty360())
1.02469507659596
```
"""
function FinanceCore.accumulation(rate::Union{Real, FinanceCore.Rate}, from::Dates.Date, to::Dates.Date, dc::DayCounts.DayCount)
return FinanceCore.accumulation(rate, DayCounts.yearfrac(from, to, dc))
end

end
10 changes: 8 additions & 2 deletions src/Rates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,11 @@ Base.hash(r::Rate{<:Any, Continuous}, h::UInt) = hash(r.continuous_value, hash(:
"""
discount(rate, t)
discount(rate, from, to)
discount(rate::Union{Real, Rate}, from::Date, to::Date, dc::DayCounts.DayCount)

Discount `rate` for a time `t` or for an interval `(from, to)`. If `rate` is not a `Rate`, it will be assumed to be a `Periodic` rate compounded once per period, i.e. `Periodic(rate,1)`.
Discount `rate` for a time `t` or for an interval `(from, to)`. If `rate` is not a `Rate`, it will be assumed to be a `Periodic` rate compounded once per period, i.e. `Periodic(rate,1)`.

When [DayCounts.jl](https://github.com/JuliaFinance/DayCounts.jl) is loaded, a constant rate can be discounted over a `Date` interval directly by passing a day count convention, which measures the interval via `DayCounts.yearfrac(from, to, dc)`.

# Examples

Expand All @@ -396,8 +399,11 @@ discount(rate, from, to) = discount(rate, to - from)
"""
accumulation(rate, t)
accumulation(rate, from, to)
accumulation(rate::Union{Real, Rate}, from::Date, to::Date, dc::DayCounts.DayCount)

Accumulate `rate` for a time `t` or for an interval `(from, to)`. If `rate` is not a `Rate`, it will be assumed to be a `Periodic` rate compounded once per period, i.e. `Periodic(rate,1)`.

Accumulate `rate` for a time `t` or for an interval `(from, to)`. If `rate` is not a `Rate`, it will be assumed to be a `Periodic` rate compounded once per period, i.e. `Periodic(rate,1)`.
When [DayCounts.jl](https://github.com/JuliaFinance/DayCounts.jl) is loaded, a constant rate can be accumulated over a `Date` interval directly by passing a day count convention, which measures the interval via `DayCounts.yearfrac(from, to, dc)`.

# Examples

Expand Down
42 changes: 42 additions & 0 deletions test/daycounts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# A non-flat curve: 2% for the first year, 6% after.
struct TwoRateCurve end
FinanceCore.discount(::TwoRateCurve, t) = t <= 1 ? exp(-0.02t) : exp(-0.02 - 0.06(t - 1))

@testset "DayCounts extension" begin
d1 = Date(2024, 1, 1)
d2 = Date(2024, 7, 1)

conventions = (
DayCounts.Actual365Fixed(),
DayCounts.Actual360(),
DayCounts.Thirty360(),
DayCounts.ActualActualISDA(),
)

@testset "$(nameof(typeof(dc)))" for dc in conventions
t = DayCounts.yearfrac(d1, d2, dc)

# date methods agree exactly with the scalar-time methods at the
# convention's year fraction, for Real and both Rate types
@test discount(0.05, d1, d2, dc) == discount(0.05, t)
@test accumulation(0.05, d1, d2, dc) == accumulation(0.05, t)
@test discount(Continuous(0.05), d1, d2, dc) == exp(-0.05 * t)
@test discount(Periodic(0.05, 2), d1, d2, dc) == discount(Periodic(0.05, 2), t)
@test accumulation(Periodic(0.05, 2), d1, d2, dc) == accumulation(Periodic(0.05, 2), t)

# discount and accumulation invert each other
@test discount(Periodic(0.05, 2), d1, d2, dc) * accumulation(Periodic(0.05, 2), d1, d2, dc) ≈ 1.0
end

# a degenerate interval discounts to exactly 1
@test discount(Continuous(0.03), d1, d1, DayCounts.Actual365Fixed()) == 1.0

# Only constant rates: a curve's discount factor over two dates depends on where the
# interval sits relative to its valuation date, not just on its length.
@test_throws MethodError discount(TwoRateCurve(), d1, d2, DayCounts.Actual365Fixed())
@test_throws MethodError accumulation(TwoRateCurve(), d1, d2, DayCounts.Actual365Fixed())

# reversed dates give a negative year fraction, so discount inverts
t = DayCounts.yearfrac(d1, d2, DayCounts.Actual365Fixed())
@test discount(Continuous(0.03), d2, d1, DayCounts.Actual365Fixed()) ≈ exp(0.03 * t)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include("Rates.jl")
include("irr.jl")
include("present_value.jl")
include("contracts.jl")
include("daycounts.jl")

using Aqua
@testset "Aqua.jl" begin
Expand Down
Loading