-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd627fc
commit 2382ebd
Showing
13 changed files
with
486 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
# OnlinePortfolioAnalysis | ||
# OnlinePortfolioAnalytics | ||
|
||
[](https://github.com/femtotrader/OnlinePortfolioAnalysis.jl/actions/workflows/CI.yml?query=branch%3Amain) | ||
WIP: Work in progress - not usable currently | ||
|
||
[](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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Oops, something went wrong.