Skip to content

Commit

Permalink
empty!
Browse files Browse the repository at this point in the history
  • Loading branch information
femtotrader committed Apr 27, 2024
1 parent 9ab6010 commit 0c3b5cf
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ CurrentModule = OnlinePortfolioAnalytics
OnlinePortfolioAnalytics.OnlinePortfolioAnalytics
```

### Functions

```@docs
OnlineStats.fit!
Base.empty!
```

### Asset return

```@docs
Expand Down
19 changes: 18 additions & 1 deletion src/asset_return.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,19 @@ function OnlineStatsBase._fit!(stat::SimpleAssetReturn, data)
end
end

function Base.empty!(stat::SimpleAssetReturn{T}) where {T}
stat.value = missing
stat.n = 0
stat.ready = false
stat.input_values.value = []
stat.input_values.n = 0
end

function expected_return_types(::Type{SimpleAssetReturn{T}}) where {T}
(Union{Missing,T},)
end


@doc """
$(TYPEDEF)
Expand Down Expand Up @@ -114,7 +123,7 @@ function OnlineStatsBase._fit!(stat::LogAssetReturn, data)
stat.n += 1
if stat.n > stat.period
data_prev = stat.input_values[end-stat.period]
stat.value = log(data / data_prev) # log=ln (Neperian log not decimal log)
stat.value = log(data / data_prev) # log=ln (natural log not decimal log)
return stat.value
else
stat.ready = true
Expand All @@ -123,6 +132,14 @@ function OnlineStatsBase._fit!(stat::LogAssetReturn, data)
end
end

function Base.empty!(stat::LogAssetReturn{T}) where {T}
stat.value = missing
stat.n = 0
stat.ready = false
stat.input_values.value = []
stat.input_values.n = 0
end

function expected_return_types(::Type{LogAssetReturn{T}}) where {T}
(Union{Missing,T},)
end
6 changes: 6 additions & 0 deletions src/cumulative_return.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ function OnlineStatsBase._fit!(stat::CumulativeReturn, data)
stat.n += 1
stat.value = value(stat.prod)
end

function Base.empty!(stat::CumulativeReturn{T}) where {T}
stat.value = zero(T)
stat.n = 0
stat.prod = Prod(T)
end
14 changes: 14 additions & 0 deletions src/drawdowns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ function OnlineStatsBase._fit!(stat::DrawDowns, ret)
stat.value = ddowns
end

function Base.empty!(stat::DrawDowns{T}) where {T}
stat.value = zero(T)
stat.n = 0
stat.prod = Prod(T)
stat.extrema = Extrema(T)
end


@doc """
$(TYPEDEF)
Expand Down Expand Up @@ -59,3 +66,10 @@ function OnlineStatsBase._fit!(stat::ArithmeticDrawDowns, ret)
ddowns = (r1 / max_cumulative_returns) - 1
stat.value = ddowns
end

function Base.empty!(stat::ArithmeticDrawDowns{T}) where {T}
stat.value = zero(T)
stat.n = 0
stat.sum = Sum(T)
stat.extrema = Extrema(T)
end
6 changes: 6 additions & 0 deletions src/moments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ function OnlineStatsBase._fit!(stat::AssetReturnMoments, ret)
)
end

function Base.empty!(stat::AssetReturnMoments{T}) where {T}
stat.value = (mean = zero(T), std = zero(T), skewness = zero(T), kurtosis = zero(T))
stat.n = 0
stat.moments = Moments()
end

function expected_return_types(::Type{AssetReturnMoments{T}}) where {T}
#return NamedTuple{
# (:mean, :std, :skewness, :kurtosis),
Expand Down
9 changes: 8 additions & 1 deletion src/sharpe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mutable struct Sharpe{T} <: PortfolioAnalyticsSingleOutput{T}
risk_free::T

function Sharpe{T}(; period = 252, risk_free = 0) where {T}
new{T}(zero(T), 0, Mean(), StdDev{T}(), period, risk_free)
new{T}(zero(T), 0, Mean(T), StdDev{T}(), period, risk_free)
end
end

Expand All @@ -34,3 +34,10 @@ function OnlineStatsBase._fit!(stat::Sharpe, data)
sharpe = sqrt(stat.period) * (mean_return - stat.risk_free) / std_dev
stat.value = sharpe
end

function Base.empty!(stat::Sharpe{T}) where {T}
stat.value = zero(T)
stat.n = 0
stat.mean = Mean(T)
stat.stddev = StdDev(T)
end
10 changes: 8 additions & 2 deletions src/std_dev.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ mutable struct StdDev{T} <: PortfolioAnalyticsSingleOutput{T}
variance::Variance

function StdDev{T}() where {T}
variance = Variance()
new{T}(1, 0, variance)
variance = Variance(T)
new{T}(one(T), 0, variance)
end
end

Expand All @@ -22,3 +22,9 @@ function OnlineStatsBase._fit!(stat::StdDev, data)
stat.n += 1
stat.value = sqrt(value(stat.variance))
end

function Base.empty!(stat::StdDev{T}) where {T}
stat.value = one(T)
stat.n = 0
stat.variance = Variance(T)
end
51 changes: 51 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ const ATOL = 0.0001
]
@test sum(ismissing.(returns)) == sum(ismissing.(expected_returns))
@test all(isapprox.(returns[2:end], expected_returns[2:end], atol = ATOL))

empty!(stat)
@test ismissing(value(stat))
@test stat.n == 0

end
end

Expand Down Expand Up @@ -104,6 +109,10 @@ const ATOL = 0.0001
]
@test sum(ismissing.(returns)) == sum(ismissing.(expected_returns))
@test all(isapprox.(returns[2:end], expected_returns[2:end], atol = ATOL))

empty!(stat)
@test ismissing(value(stat))
@test stat.n == 0
end
end

Expand Down Expand Up @@ -134,6 +143,11 @@ const ATOL = 0.0001
end
subscribe!(mapped_source, observer)
@test isapprox(stddev_returns[end], 0.1496, atol = ATOL)

empty!(_stddev)
@test value(_stddev) == 1
@test _stddev.n == 0
@test value(_stddev.variance) == 1
end
end

Expand Down Expand Up @@ -220,6 +234,10 @@ const ATOL = 0.0001
]
@test all(isapprox.(cum_returns, expected_cum_returns, atol = ATOL))

empty!(cum_ret)
@test cum_ret.n == 0
@test value(cum_ret) == 0.0
@test value(cum_ret.prod) == 1.0
end

@testset "DrawDowns" begin
Expand Down Expand Up @@ -259,6 +277,12 @@ const ATOL = 0.0001
-0.0768,
]
@test all(isapprox.(drawdowns, expected_drawdowns, atol = ATOL))

empty!(_ddowns)
@test _ddowns.n == 0
@test value(_ddowns) == 0.0
@test value(_ddowns.prod) == 1.0
@test _ddowns.extrema == Extrema()
end

@testset "Arithmetic" begin
Expand Down Expand Up @@ -297,6 +321,12 @@ const ATOL = 0.0001
-0.0482,
]
@test all(isapprox.(drawdowns, expected_drawdowns, atol = ATOL))

empty!(_ddowns)
@test _ddowns.n == 0
@test value(_ddowns) == 0.0
@test value(_ddowns.sum) == 0.0
@test _ddowns.extrema == Extrema()
end

end
Expand Down Expand Up @@ -333,6 +363,14 @@ const ATOL = 0.0001
@test isapprox(moments_latest.std, 0.1496, atol = ATOL)
@test isapprox(moments_latest.skewness, 1.3688, atol = ATOL)
@test isapprox(moments_latest.kurtosis, 2.1968, atol = ATOL)

empty!(_moments)
@test _moments.n == 0
@test value(_moments).mean == 0.0
@test value(_moments).std == 0.0
@test value(_moments).skewness == 0.0
@test value(_moments).kurtosis == 0.0
@test _moments.moments == Moments()
end

@testset "Sharpe" begin
Expand All @@ -357,6 +395,12 @@ const ATOL = 0.0001
subscribe!(mapped_source, observer)

@test isapprox(sharpes[end], 0.2886, atol = ATOL)

#empty!(_sharpe)
#@test _sharpe.n == 0
#@test value(_sharpe) == 0.0
#@test _sharpe.mean == Mean()
#@test _sharpe.stddev == StdDev()
end

@testset "Sortino" begin
Expand All @@ -381,6 +425,13 @@ const ATOL = 0.0001
subscribe!(mapped_source, observer)

@test isapprox(sortinos[end], 11.4992, atol = ATOL)

#empty!(_sortino)
#@test _sortino.n == 0
#@test value(_sortino) == 0.0
#@test _sortino.mean == Mean()
#@test _sortino.stddev == StdDev()

end
end

Expand Down

0 comments on commit 0c3b5cf

Please sign in to comment.