Skip to content

Commit

Permalink
Merge pull request #10 from JuliaActuary/negative
Browse files Browse the repository at this point in the history
  • Loading branch information
alecloudenback authored Nov 3, 2023
2 parents e898138 + 3a56908 commit 56cb9a5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Contracts.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# Allow Dates or real timesteps
"""
Timepoint(a)
Summary
≡≡≡≡≡≡≡≡≡
Timepoint is a type alias for Union{T,Dates.Date} that can be used to represent a point in time. It can be either a `Dates.Date` or a `Real` number. If defined as a real number, the interpretation is the number of (fractional) periods since time zero.
Currently, the usage of `Dates.Date` is not well supported across the JuliaActuary ecosystem but this type is in place such that it can be built upon further.
Supertype Hierarchy
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
Timepoint{T} = Union{T,Dates.Date} <: Any
"""
const Timepoint{T} = Union{T,Dates.Date} where {T<:Real}

abstract type AbstractContract end
Expand All @@ -19,12 +34,30 @@ end
maturity(q::Quote) = maturity(q.instrument)
Base.isapprox(a::Quote, b::Quote) = isapprox(a.price, b.price) && isapprox(a.instrument, b.instrument)


"""
Cashflow(amount,time)
A `Cahflow{A,B}` is a contract that pays an `amount` at `time`.
Cashflows can be:
- negated with the unary `-` operator.
- added/subtracted together but note that the `time` must be `isapprox` equal.
- multiplied/divided by a scalar.
Supertype Hierarchy
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
Cashflow{A<:Real, B<:Timepoint} <: FinanceCore.AbstractContract <: Any
"""
struct Cashflow{N<:Real,T<:Timepoint} <: AbstractContract
amount::N
time::T
end

maturity(c::C) where {C<:Cashflow} = c.time
Base.:-(c::C) where {C<:Cashflow} = Cashflow(-c.amount, c.time)

"""
amount(x)
Expand Down Expand Up @@ -98,6 +131,8 @@ function Base.:/(c1::C, c2::D) where {C<:Cashflow,D<:Real}
end

"""
Composite(A,B)
Summary
≡≡≡≡≡≡≡≡≡
Expand Down
1 change: 1 addition & 0 deletions test/contracts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@testset "algebra" begin
@test cf11 == cf11
@test cf11 Cashflow(1.0, 1.0)
@test -cf11 Cashflow(-1.0, 1.0)

@test cf11 + cf11 == Cashflow(2, 1)
@test cf11 - cf11 == Cashflow(0, 1)
Expand Down

0 comments on commit 56cb9a5

Please sign in to comment.