Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] authored and femtotrader committed Apr 23, 2024
1 parent fd627fc commit 2382ebd
Show file tree
Hide file tree
Showing 13 changed files with 486 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- x64
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
Expand Down
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
name = "OnlinePortfolioAnalysis"
name = "OnlinePortfolioAnalytics"
uuid = "d39dbdee-9d7b-4a6f-a064-c0eaa0a6e939"
authors = ["FemtoTrader <[email protected]>"]
version = "1.0.0-DEV"

[deps]
OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e"
OnlineStatsBase = "925886fa-5bf2-5e8e-b522-a9147a512338"
Rocket = "df971d30-c9d6-4b37-b8ff-e965b2cb3a40"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
julia = "1.6.7"
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# OnlinePortfolioAnalysis
# OnlinePortfolioAnalytics

[![Build Status](https://github.com/femtotrader/OnlinePortfolioAnalysis.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/femtotrader/OnlinePortfolioAnalysis.jl/actions/workflows/CI.yml?query=branch%3Amain)
WIP: Work in progress - not usable currently

[![Build Status](https://github.com/femtotrader/OnlinePortfolioAnalytics.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/femtotrader/OnlinePortfolioAnalytics.jl/actions/workflows/CI.yml?query=branch%3Amain)

This project aims to provide users with functionality for performing quantitative portfolio analytics via [online algorithms](https://en.wikipedia.org/wiki/Online_algorithm).

It depends especially on [OnlineStatsBase.jl](https://joshday.github.io/OnlineStats.jl/)

It's inspired by the following projects:

- Julia
- PortfolioAnalytics.jl https://github.com/doganmehmet/PortfolioAnalytics.jl
- R
- PerformanceAnalytics https://cran.r-project.org/web/packages/PerformanceAnalytics/
- PortfolioAnalytics https://cran.r-project.org/web/packages/PortfolioAnalytics/
- Python
- pyfolio https://github.com/quantopian/pyfolio
- empyrical https://quantopian.github.io/empyrical
18 changes: 0 additions & 18 deletions src/OnlinePortfolioAnalysis.jl

This file was deleted.

29 changes: 29 additions & 0 deletions src/OnlinePortfolioAnalytics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module OnlinePortfolioAnalytics

using OnlineStatsBase
#using OnlineStats: GeometricMean
using Statistics
import StatsBase

export SimpleAssetReturn, LogAssetReturn
export Mean # from OnlineStatsBase
export GeometricMeanReturn # different from OnlineStats: GeometricMean
export StdDev
export CumulativeReturn
export DrawDowns, ArithmeticDrawDowns
export AssetReturnMoments

export fit!, value

abstract type PortfolioAnalytics{T} <: OnlineStat{T} end

include("asset_return.jl")
include("mean_return.jl")
include("cumulative_return.jl")
include("std_dev.jl")
include("drawdowns.jl")
include("moments.jl")

include("value_at_risk.jl")

end
10 changes: 4 additions & 6 deletions src/asset_return.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mutable struct SimpleAssetReturn{T} <: PortfolioAnalysis{T}
mutable struct SimpleAssetReturn{T} <: PortfolioAnalytics{T}
value::Union{Missing,T}
n::Int

Expand All @@ -8,7 +8,7 @@ mutable struct SimpleAssetReturn{T} <: PortfolioAnalysis{T}

input_values::CircBuff

function SimpleAssetReturn{T}(; period::Int=1) where {T}
function SimpleAssetReturn{T}(; period::Int = 1) where {T}
input_values = CircBuff(T, period + 1, rev = false)
new{T}(missing, 0, false, period, input_values)
end
Expand All @@ -28,9 +28,7 @@ function OnlineStatsBase._fit!(stat::SimpleAssetReturn, data)
end
end



mutable struct LogAssetReturn{T} <: PortfolioAnalysis{T}
mutable struct LogAssetReturn{T} <: PortfolioAnalytics{T}
value::Union{Missing,T}
n::Int

Expand All @@ -40,7 +38,7 @@ mutable struct LogAssetReturn{T} <: PortfolioAnalysis{T}

input_values::CircBuff

function LogAssetReturn{T}(; period::Int=1) where {T}
function LogAssetReturn{T}(; period::Int = 1) where {T}
input_values = CircBuff(T, period + 1, rev = false)
new{T}(missing, 0, false, period, input_values)
end
Expand Down
18 changes: 18 additions & 0 deletions src/cumulative_return.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mutable struct CumulativeReturn{T} <: PortfolioAnalytics{T}
value::T
n::Int

prod::Prod

function CumulativeReturn{T}() where {T}
val = zero(T)
p = Prod()
new{T}(val, 0, p)
end
end

function OnlineStatsBase._fit!(stat::CumulativeReturn, data)
fit!(stat.prod, 1 + data)
stat.n += 1
stat.value = value(stat.prod)
end
45 changes: 45 additions & 0 deletions src/drawdowns.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
mutable struct DrawDowns{T} <: PortfolioAnalytics{T}
value::T
n::Int

prod::Prod
extrema::Extrema

function DrawDowns{T}() where {T}
new{T}(T(0), 0, Prod(), Extrema(T))
end
end

function OnlineStatsBase._fit!(stat::DrawDowns, ret)
r1 = 1 + ret
fit!(stat.prod, r1)
rprod = value(stat.prod)
fit!(stat.extrema, rprod)
stat.n += 1
max_cumulative_returns = value(stat.extrema).max
ddowns = (rprod / max_cumulative_returns) - 1
stat.value = ddowns
end


mutable struct ArithmeticDrawDowns{T} <: PortfolioAnalytics{T}
value::T
n::Int

sum::Sum
extrema::Extrema

function ArithmeticDrawDowns{T}() where {T}
new{T}(T(0), 0, Sum(), Extrema(T))
end
end

function OnlineStatsBase._fit!(stat::ArithmeticDrawDowns, ret)
fit!(stat.sum, ret)
r1 = value(stat.sum) + 1
fit!(stat.extrema, r1)
stat.n += 1
max_cumulative_returns = value(stat.extrema).max
ddowns = (r1 / max_cumulative_returns) - 1
stat.value = ddowns
end
40 changes: 40 additions & 0 deletions src/mean_return.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Prod(T::Type = Float64)
Track the overall prod.
"""
mutable struct Prod{T} <: OnlineStat{Number}
prod::T
n::Int
end
Prod(T::Type = Float64) = Prod(T(1), 0)
Base.prod(o::Prod) = o.prod
OnlineStatsBase._fit!(o::Prod{T}, x::Real) where {T<:AbstractFloat} =
(o.prod *= convert(T, x); o.n += 1)
OnlineStatsBase._fit!(o::Prod{T}, x::Real) where {T<:Integer} =
(o.prod *= round(T, x); o.n += 1)
OnlineStatsBase._fit!(o::Prod{T}, x::Real, n) where {T<:AbstractFloat} =
(o.prod *= convert(T, x * n); o.n += n)
OnlineStatsBase._fit!(o::Prod{T}, x::Real, n) where {T<:Integer} =
(o.prod *= round(T, x * n); o.n += n)
OnlineStatsBase._merge!(o::T, o2::T) where {T<:Prod} = (o.prod *= o2.prod; o.n += o2.n; o)

# https://github.com/joshday/OnlineStatsBase.jl/issues/41

mutable struct GeometricMeanReturn{T} <: PortfolioAnalytics{T}
value::T
n::Int

prod::Prod

function GeometricMeanReturn{T}() where {T}
p = Prod()
new{T}(T(0), 0, p)
end
end

function OnlineStatsBase._fit!(stat::GeometricMeanReturn, data)
fit!(stat.prod, 1 + data)
stat.n += 1
stat.value = value(stat.prod)^(1 / stat.n) - 1
end
22 changes: 22 additions & 0 deletions src/moments.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
mutable struct AssetReturnMoments{T} <: PortfolioAnalytics{T}
value::NamedTuple
n::Int

moments::Moments

function AssetReturnMoments{T}() where {T}
val = (mean = T(0), std = T(0), skewness = T(0), kurtosis = T(0))
new{T}(val, 0, Moments())
end
end

function OnlineStatsBase._fit!(stat::AssetReturnMoments, ret)
stat.n += 1
fit!(stat.moments, ret)
stat.value = (
mean = Statistics.mean(stat.moments),
std = Statistics.std(stat.moments),
skewness = StatsBase.skewness(stat.moments),
kurtosis = StatsBase.kurtosis(stat.moments),
)
end
2 changes: 1 addition & 1 deletion src/std_dev.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mutable struct StdDev{T} <: PortfolioAnalysis{T}
mutable struct StdDev{T} <: PortfolioAnalytics{T}
value::T
n::Int

Expand Down
1 change: 1 addition & 0 deletions src/value_at_risk.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading

0 comments on commit 2382ebd

Please sign in to comment.