Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
femtotrader committed Apr 23, 2024
1 parent e05cc5e commit 70ffb0a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/OnlinePortfolioAnalytics.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module OnlinePortfolioAnalytics

using OnlineStatsBase
using OnlineStats: GeometricMean
#using OnlineStats: GeometricMean

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

Expand All @@ -13,6 +14,7 @@ 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")

Expand Down
36 changes: 36 additions & 0 deletions src/mean_return.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
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
8 changes: 1 addition & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ const weights = [0.4, 0.4, 0.2]

@testset "GeometricMeanReturn" begin
source = from(TSLA)
#=
_ret = SimpleAssetReturn{Float64}()
_mean = GeometricMean()
_mean = GeometricMeanReturn{Float64}()
mapped_source =
source |>
map(
Expand All @@ -189,14 +188,9 @@ const weights = [0.4, 0.4, 0.2]
end
subscribe!(mapped_source, observer)
@test isapprox(mean_returns[end], 0.0342, atol = ATOL)
=#
end

@testset "CumulativeReturn" begin
#cum_ret = CumulativeReturn{Float64}()
#fit!(cum_ret, TSLA)
#@test isapprox(value(cum_ret), 265.9177, atol=ATOL)

source = from(TSLA)
ret = SimpleAssetReturn{Float64}()
cum_ret = CumulativeReturn{Float64}()
Expand Down

0 comments on commit 70ffb0a

Please sign in to comment.