Skip to content
This repository has been archived by the owner on Apr 4, 2020. It is now read-only.

Update for Julia 1.0 #31

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ For example:

```julia
# Load packages
using Dates, FinancialMarkets
# Create US Dollar object
USD = USD()
using Dates, FinancialMarkets

# Create 3m USD LIBOR
depo = Deposit(USD, Month(3), 0.05)
# Price depo at trade date
price(depo)
depo = Deposit(USD(), Month(3), 0.05)

# Price depo at trade date with default par of 1e6
price(depo) # returns 987518.8588670965
```
## Features

Expand Down
5 changes: 0 additions & 5 deletions src/FinancialMarkets.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
module FinancialMarkets

if VERSION < v"0.4-"
using Dates
else
using Base.Dates
end
import DataFrames.DataFrame

export
Expand Down
27 changes: 14 additions & 13 deletions src/businessdayconventions.jl
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
import Dates: adjust
#####
# Types
#####


abstract BusinessDayConvention
abstract type BusinessDayConvention end

# Sources:
# 1. ISDA 2006 definitions
# 2. Opengamma: Interest rate instruments and market conventions guide

immutable Unadjusted <: BusinessDayConvention end
immutable Preceding <: BusinessDayConvention end
immutable ModifiedPreceding <: BusinessDayConvention end
immutable Following <: BusinessDayConvention end
immutable ModifiedFollowing <: BusinessDayConvention end
immutable Succeeding <: BusinessDayConvention end
struct Unadjusted <: BusinessDayConvention end
struct Preceding <: BusinessDayConvention end
struct ModifiedPreceding <: BusinessDayConvention end
struct Following <: BusinessDayConvention end
struct ModifiedFollowing <: BusinessDayConvention end
struct Succeeding <: BusinessDayConvention end

#####
# Methods
#####

adjust(dt::TimeType, bdc::Unadjusted, c::FinCalendar = NoFCalendar()) = dt
function adjust(dt::TimeType, bdc::Preceding, c::FinCalendar = NoFCalendar())
Dates.adjust(dt::TimeType, bdc::Unadjusted, c::FinCalendar = NoFCalendar()) = dt
function Dates.adjust(dt::TimeType, bdc::Preceding, c::FinCalendar = NoFCalendar())
while !isgood(dt, c)
dt -= Day(1)
end
return dt
end
function adjust(dt::TimeType, bdc::Following, c::FinCalendar = NoFCalendar())
function Dates.adjust(dt::TimeType, bdc::Following, c::FinCalendar = NoFCalendar())
while !isgood(dt, c)
dt += Day(1)
end
return dt
end
function adjust(dt::TimeType, bdc::ModifiedPreceding,
function Dates.adjust(dt::TimeType, bdc::ModifiedPreceding,
c::FinCalendar = NoFCalendar())
pre_dt = adjust(dt, Preceding(), c)
month(dt) != month(pre_dt) ? adjust(dt, Following(), c) : pre_dt
end
function adjust(dt::TimeType, bdc::ModifiedFollowing,
function Dates.adjust(dt::TimeType, bdc::ModifiedFollowing,
c::FinCalendar = NoFCalendar())
follow_dt = adjust(dt, Following(), c)
month(dt) != month(follow_dt) ? adjust(dt, Preceding(), c) : follow_dt
end
function adjust(dt::TimeType, bdc::Succeeding, c::FinCalendar = NoFCalendar())
function Dates.adjust(dt::TimeType, bdc::Succeeding, c::FinCalendar = NoFCalendar())
follow_dt = adjust(dt, Following(), c)
is_barrier_crossed = (month(follow_dt) != month(dt) ||
day(dt) ≤ 15 && day(follow_dt) > 15)
Expand Down
14 changes: 7 additions & 7 deletions src/calendars.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
# Type declarations
#####

abstract FinCalendar
abstract SingleFCalendar <: FinCalendar
immutable JointFCalendar <: FinCalendar
abstract type FinCalendar end
abstract type SingleFCalendar <: FinCalendar end
struct JointFCalendar <: FinCalendar
calendars::Vector{SingleFCalendar}
onbad::Bool
end
immutable NoFCalendar <: SingleFCalendar end
struct NoFCalendar <: SingleFCalendar end


#####
# Methods
#####

JointFCalendar(c::SingleFCalendar...) = JointFCalendar([ ci for ci in c ], true)
+(c1::SingleFCalendar, c2::SingleFCalendar) = JointFCalendar([c1, c2], true)
*(c1::SingleFCalendar, c2::SingleFCalendar) = JointFCalendar([c1, c2], false)
+(jc::JointFCalendar, c::SingleFCalendar) = JointFCalendar([jc.calendars, c],
Base.:+(c1::SingleFCalendar, c2::SingleFCalendar) = JointFCalendar([c1, c2], true)
Base.:*(c1::SingleFCalendar, c2::SingleFCalendar) = JointFCalendar([c1, c2], false)
Base.:+(jc::JointFCalendar, c::SingleFCalendar) = JointFCalendar([jc.calendars, c],
jc.onbad)
Base.convert(::Type{JointFCalendar}, c::SingleFCalendar) = JointFCalendar(c)

Expand Down
2 changes: 1 addition & 1 deletion src/calendars/calendars_au.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Type declarations
#####

abstract AUFCalendar <: SingleFCalendar
abstract type AUFCalendar <: SingleFCalendar end

#####
# Methods
Expand Down
2 changes: 1 addition & 1 deletion src/calendars/calendars_aume.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Type declarations
#####

immutable AUMEFCalendar <: AUFCalendar end
struct AUMEFCalendar <: AUFCalendar end

#####
# Methods
Expand Down
2 changes: 1 addition & 1 deletion src/calendars/calendars_ausy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Type declarations
#####

immutable AUSYFCalendar <: AUFCalendar end
struct AUSYFCalendar <: AUFCalendar end

#####
# Methods
Expand Down
6 changes: 3 additions & 3 deletions src/calendars/calendars_euta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Type declarations
#####

abstract EUFCalendar <: SingleFCalendar
immutable EUTAFCalendar <: EUFCalendar end
immutable EULIBORFCalendar <: EUFCalendar end
abstract type EUFCalendar <: SingleFCalendar end
struct EUTAFCalendar <: EUFCalendar end
struct EULIBORFCalendar <: EUFCalendar end

#####
# Methods
Expand Down
4 changes: 2 additions & 2 deletions src/calendars/calendars_gb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Type declarations
#####

abstract GBFCalendar <: SingleFCalendar
immutable GBLOFCalendar <: GBFCalendar end
abstract type GBFCalendar <: SingleFCalendar end
struct GBLOFCalendar <: GBFCalendar end

#####
# Methods
Expand Down
4 changes: 2 additions & 2 deletions src/calendars/calendars_jp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#####


abstract JPFCalendar <: SingleFCalendar
immutable JPTOFCalendar <: JPFCalendar end
abstract type JPFCalendar <: SingleFCalendar end
struct JPTOFCalendar <: JPFCalendar end

#####
# Methods
Expand Down
2 changes: 1 addition & 1 deletion src/calendars/calendars_nz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Type declarations
#####

abstract NZFCalendar <: SingleFCalendar
abstract type NZFCalendar <: SingleFCalendar end

#####
# Methods
Expand Down
2 changes: 1 addition & 1 deletion src/calendars/calendars_nzau.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Type declarations
#####

immutable NZAUFCalendar <: NZFCalendar end
struct NZAUFCalendar <: NZFCalendar end

#####
# Methods
Expand Down
2 changes: 1 addition & 1 deletion src/calendars/calendars_nzwe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Type declarations
#####

immutable NZWEFCalendar <: NZFCalendar end
struct NZWEFCalendar <: NZFCalendar end

#####
# Methods
Expand Down
6 changes: 3 additions & 3 deletions src/calendars/calendars_us.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Type declarations
#####

abstract USFCalendar <: SingleFCalendar
immutable USNYFCalendar <: USFCalendar end
immutable USLIBORFCalendar <: USFCalendar end
abstract type USFCalendar <: SingleFCalendar end
struct USNYFCalendar <: USFCalendar end
struct USLIBORFCalendar <: USFCalendar end

#####
# Methods
Expand Down
4 changes: 3 additions & 1 deletion src/calendars/epochs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ function seasonstart(y::Integer, m::Integer)
y = (y - 2000) / 1000
k = SEASON[m]
jde0 = 0
for i in 1:length(k) jde0 += y ^ (i - 1) * k[i] end
for i in 1:length(k)
jde0 += (y ^ (i - 1) * k[i])[1]
end
# Calculate corrections
tt = (jde0 - 2451545) / 36525
w = 35999.373tt - 2.47
Expand Down
5 changes: 2 additions & 3 deletions src/cashflow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Types
####

type CashFlow
struct CashFlow
currency::Vector{Currency}
date::Vector{TimeType}
amount::Vector{Real}
Expand All @@ -17,8 +17,7 @@ end

CashFlow(currency::Currency, date::TimeType, amount::Real) = (
CashFlow([currency], [date], [amount]))
function CashFlow{C<:Currency, T<:TimeType, R<:Real}(currency::Vector{C},
date::Vector{T}, amount::Vector{R})
function CashFlow(currency::Vector{C}, date::Vector{T}, amount::Vector{R}) where {C<:Currency, T<:TimeType, R<:Real}
currency = Currency[ccy for ccy in currency]
date = TimeType[dt for dt in date]
amount = Real[amt for amt in amount]
Expand Down
12 changes: 7 additions & 5 deletions src/constants.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Dates

# Session variables
const EVAL_DATE = today()
const EVAL_DATE = Dates.today()

# Seasonal constants to determine start of seasons (more precisely, equinoxes
# ands solstices)
Expand All @@ -8,8 +10,8 @@ const SEASON_MAR = [2451623.80984, 365242.37404, 0.05169, -0.00411, -0.00057]
const SEASON_JUN = [2451716.56767, 365241.62603, 0.00325, 0.00888, -0.00030]
const SEASON_SEP = [2451810.21715, 365242.01767, -0.11575, 0.00337, 0.00078]
const SEASON_DEC = [2451900.05952, 365242.74049, 0.06223, 0.00823, 0.00032]
const SEASON = [Mar => SEASON_MAR, Jun => SEASON_JUN, Sep => SEASON_SEP,
Dec => SEASON_DEC]
const SEASON = Dict(Mar => SEASON_MAR, Jun => SEASON_JUN, Sep => SEASON_SEP,
Dec => SEASON_DEC)
const SEASON_A = [485, 203, 199, 182, 156, 136, 77, 74, 70, 58, 52, 50, 45, 44,
29, 18, 17, 16, 14, 12, 12, 12, 9, 8]
const SEASON_B = [324.96, 337.23, 342.08, 27.85, 73.14, 171.52, 222.54, 296.72,
Expand All @@ -24,7 +26,7 @@ const SEASON_C = [1934.136, 32964.467, 20.186, 445267.112, 45036.886, 22518.443,
(const Simply, Annually, SemiAnnually, TriAnnually, Quarterly, BiMonthly,
Monthly, Fortnightly, Weekly, Daily, Continuously =
0, 1, 2, 3, 4, 6, 12, 24, 52, 365, 1000)
const COMPOUNDINGS = [0 => "simply", 1 => "annually", 2 => "semi-annually",
const COMPOUNDINGS = Dict(0 => "simply", 1 => "annually", 2 => "semi-annually",
3 => "tri-annually", 4 => "quarterly", 6 => "bi-monthly", 12 => "monthly",
24 => "fornightly", 52 => "weekly", 365 => "daily", 1000 => "continuously"]
24 => "fornightly", 52 => "weekly", 365 => "daily", 1000 => "continuously")

21 changes: 10 additions & 11 deletions src/currencies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
# Types
####

abstract Currency
abstract type Currency end

# Sources:
# 1. ISDA 2006 definitions
# 2. Opengamma: Interest rate instruments and market conventions guide

immutable AUD <: Currency
struct AUD <: Currency
calendar::JointFCalendar
AUD(calendar::AUSYFCalendar) = new(calendar)
end
AUD() = AUD(AUSYFCalendar())

immutable EUR <: Currency
struct EUR <: Currency
calendar::JointFCalendar
EUR(calendar::EUTAFCalendar) = new(calendar)
end
EUR() = EUR(EUTAFCalendar())

immutable GBP <: Currency
struct GBP <: Currency
calendar::JointFCalendar
GBP(calendar::GBLOFCalendar) = new(calendar)
end
GBP() = GBP(GBLOFCalendar())

immutable JPY <: Currency
struct JPY <: Currency
calendar::JointFCalendar
JPY(calendar::JPTOFCalendar) = new(calendar)
end
JPY() = JPY(JPTOFCalendar())

immutable NZD <: Currency
struct NZD <: Currency
calendar::JointFCalendar
function NZD(calendar::JointFCalendar)
cals = [NZAUFCalendar(), NZWEFCalendar()]
Expand All @@ -43,7 +43,7 @@ immutable NZD <: Currency
end
NZD() = NZD(+(NZAUFCalendar(), NZWEFCalendar()))

immutable USD <: Currency
struct USD <: Currency
calendar::JointFCalendar
USD(calendar::USNYFCalendar) = new(calendar)
end
Expand All @@ -52,14 +52,13 @@ USD() = USD(USNYFCalendar())
# Source:
# 2. Opengamma: Interest rate instruments and market conventions guide

const CCY_STRENGTH = [EUR => 1, GBP => 2, AUD => 3, NZD => 4, USD => 5,
JPY => 6]
const CCY_STRENGTH = Dict(EUR => 1, GBP => 2, AUD => 3, NZD => 4, USD => 5,
JPY => 6)

Base.isless(x::Currency, y::Currency) = (CCY_STRENGTH[typeof(x)] >
CCY_STRENGTH[typeof(y)])

=={T<:Currency}(ccy1::T, ccy2::T) = true
==(ccy1::T, ccy2::T) where {T<:Currency} = true
==(ccy1::Currency, ccy2::Currency) = false
Base.string(ccy::Currency) = string(typeof(ccy))
Base.show(io::IO, ccy::Currency) = print(io, string(ccy))
@vectorize_1arg Currency Base.string
Loading