Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones authored and Eric Forgy committed Jul 4, 2020
1 parent 08f5295 commit 7831e29
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ uuid = "2a4f3d17-849a-48a1-809e-d780c70a95a0"
license = "MIT"
desc = "Financial Instruments"
authors = ["Eric Forgy <[email protected]>", "ScottPJones <[email protected]>"]
version = "0.8.0"
version = "0.9.0"

[deps]
Currencies = "0fd90b74-7c1f-579e-9252-02cd883047b9"

[compat]
Currencies = ">= 0.17"
Currencies = "0.18"

[extras]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
[pkg-url]: https://github.com/JuliaFinance/Instruments.jl.git

[release]: https://img.shields.io/github/release/JuliaFinance/Instruments.jl.svg
[release-date]: https://img.shields.io/github/release-date/JuliaFinance/Instruments.jl.svg

[license-img]: http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat
[license-url]: LICENSE.md
[julia-url]: https://github.com/JuliaLang/julia
[julia-release]: https://img.shields.io/github/release/JuliaLang/julia.svg

[travis-url]: https://travis-ci.org/JuliaFinance/Instruments.jl
[travis-img]: https://travis-ci.org/JuliaFinance/Instruments.jl.svg
[travis-s-img]: https://travis-ci.org/JuliaFinance/Instruments.jl.svg
[travis-m-img]: https://travis-ci.org/JuliaFinance/Instruments.jl.svg?branch=master

[contrib]: https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat

[codecov-url]: https://codecov.io/gh/JuliaFinance/Instruments.jl
[codecov-img]: https://codecov.io/gh/JuliaFinance/Instruments.jl/branch/master/graph/badge.svg

[![][travis-img]][travis-url] [![][codecov-img]][codecov-url]
| **Julia Version** | **Unit Tests** | **Code Coverage** |
|:------------------:|:------------------:|:---------------------:|
| [![][julia-release]][julia-url] | [![][travis-s-img]][travis-url] | [![][codecov-img]][codecov-url]
| Latest | [![][travis-m-img]][travis-url] | [![][codecov-img]][codecov-url]

# Instruments.jl

Expand Down
71 changes: 32 additions & 39 deletions src/Instruments.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
Instruments
This package provides the `Instrument` abstract type together with the `Position` parametric type
for dealing with currencies and other financial instruments.
This package provides the `Instrument` abstract type together with the `Position` parametric type for dealing with currencies and other financial instruments.
See [README](https://github.com/JuliaFinance/Instruments.jl.git/README.md) for the full documentation
Expand All @@ -12,85 +11,79 @@ Licensed under MIT License, see [LICENSE](https://github.com/JuliaFinance/Instru
"""
module Instruments

using Currencies
import Currencies: currency, symbol, unit, code, name
using Currencies: Currency
import Currencies: currency, symbol

export Instrument, Position
export instrument, position, amount, currency, symbol

"""
This is an abstract type from which all financial instruments such as `Cash`, `Stock`, etc. should subtype.
"""
"`Instrument` is an abstract type from which all financial instruments such as `Cash`, `Stock`, etc. should subtype."
abstract type Instrument{S,C<:Currency} end

symbol(::Type{Instrument{S}}) where {S} = S
currency(::Type{Instrument{S,Currency{C}}}) where {S,C} = currency(C)

symbol(::Instrument{S}) where {S} = S
currency(::Instrument{S,Currency{C}}) where {S,C} = currency(C)

"""
`Position` represents ownership of a certain quantity of a particular financial instrument.
"""
"`Position` represents ownership of a certain quantity of a particular financial instrument."
struct Position{I<:Instrument,A<:Real}
amount::A
end
Position{I}(amt) where {I<:Instrument} = Position{I,typeof(amt)}(amt)

(::Type{I})(amt) where {I<:Instrument} = Position{I}(amt)
Position(::Type{I},amt) where {I<:Instrument} = Position{I,typeof(amt)}(amt)

symbol(::Type{I}) where {S,I<:Instrument{S}} = S

symbol(::P) where {I,P<:Position{I}} = symbol(I)

currency(::Type{I}) where {S,C,I<:Instrument{S,C}} = C

currency(::P) where {I,P<:Position{I}} = currency(I)

"""
Returns the financial instrument type of a position.
"""
instrument(::Position{I}) where {I} = I
function instrument end

"""
Returns the amount of the instrument in the `Position` owned.
"""
amount(p::Position) = p.amount
instrument(::Type{I}) where {I<:Instrument} = I

instrument(::P) where {I,P<:Position{I}} = I

"""
Returns the symbol of the instrument in the `Position`.
Returns the type of a position.
"""
symbol(::Position{I}) where {I} = symbol(I)
function position end

position(::P) where {P<:Position} = P

"""
Returns the currency of the instrument in the `Position`.
Returns the amount of the instrument in the `Position` owned.
"""
currency(::Position{I}) where {I<:Instrument} = currency(I)
amount(p::Position) = p.amount

Base.zero(::Type{Position{I,A}}) where {I<:Instrument,A<:Real} = Position{I,A}(0)

Base.promote_rule(::Type{Position{I,A1}}, ::Type{Position{I,A2}}) where {I,A1,A2} =
Position{I,promote_type(A1,A2)}
Base.convert(::Type{Position{I,A}}, x::Position{I,A}) where {I,A} = x
Base.convert(::Type{Position{I,A}}, x::Position{I}) where {I,A} =
Position{I}(convert(A, x.amount))
Position(I,convert(A, x.amount))

Base.:+(::Position{I1}, ::Position{I2}) where {I1,I2} =
error("Can't add Positions of different Instruments $(I1()), $(I2())")
Base.:+(p1::Position{I}, p2::Position{I}) where {I} =
Position{I}(p1.amount + p2.amount)
Position(I,p1.amount + p2.amount)

Base.:-(::Position{I1}, ::Position{I2}) where {I1,I2} =
error("Can't subtract Positions of different Instruments $(I1()), $(I2())")
Base.:-(p1::Position{I}, p2::Position{I}) where {I} =
Position{I}(p1.amount - p2.amount)
Base.:-(p::Position{I}) where {I} = Position{I}(-p.amount)
Position(I,p1.amount - p2.amount)
Base.:-(p::Position{I}) where {I} = Position(I,-p.amount)

Base.:/(::Position{I1}, ::Position{I2}) where {I1,I2} =
error("Can't divide Positions of different Instruments $(I1()), $(I2())")
Base.:/(p1::Position{I}, p2::Position{I}) where {I} = p1.amount / p2.amount
Base.:/(p::Position{I}, k::Real) where {I} = Position{I}(p.amount / k)
Base.:/(p::Position{I}, k::Real) where {I} = Position(I,p.amount / k)

Base.:*(k::Real, p::Position{I}) where {I} = Position{I}(p.amount * k)
Base.:*(k::Real, p::Position{I}) where {I} = Position(I,p.amount * k)
Base.:*(p::Position, k::Real) = k * p

Base.:*(val::Real, ::Type{I}) where {I<:Instrument} = Position{I}(val)
Base.:*(::Type{I}, val::Real) where {I<:Instrument} = Position{I}(val)

# Handle instances of Instruments also
Base.:*(val::Real, ::I) where {I<:Instrument} = Position{I}(val)
Base.:*(::I, val::Real) where {I<:Instrument} = Position{I}(val)
Base.:*(val::Real, ::Type{P}) where {I,P<:Position{I}} = Position(I,val)

Base.show(io::IO, ::I) where {I<:Instrument} = print(io, symbol(I))
Base.show(io::IO, p::Position{I}) where {I<:Instrument} = print(io, p.amount, symbol(I))
Expand Down
3 changes: 1 addition & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Currencies, Instruments
using Instruments: currency, symbol, unit, code, name
using Instruments, Currencies

using Test

Expand Down

0 comments on commit 7831e29

Please sign in to comment.