Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve doc ? value of type different from Float64 and from type of single observation value #37

Closed
femtotrader opened this issue Dec 25, 2023 · 2 comments

Comments

@femtotrader
Copy link

femtotrader commented Dec 25, 2023

Hello,

I'm working on https://github.com/femtotrader/IncTA.jl/tree/OnlineStatsBase and I'm looking for a way to pass a candlestick as a single observation value.
My single observation value looks like

struct OHLCV{Ttime,Tprice,Tvol}
    open::Tprice
    high::Tprice
    low::Tprice
    close::Tprice
    volume::Tvol
    time::Ttime

    function OHLCV(
        open::Tprice,
        high::Tprice,
        low::Tprice,
        close::Tprice;
        volume::Tvol = missing,
        time::Ttime = missing,
    ) where {Ttime,Tprice,Tvol}
        new{Ttime,Tprice,Tvol}(open, high, low, close, volume, time)
    end

end

and I'm tring to calculate AccuDist and have "sufficient statistics" as a value of type Tprice (in this case it's Float64 but in general it can be different)

I did

"""
    AccuDist{T}()

The AccuDist type implements an Accumulation and Distribution indicator.
"""
mutable struct AccuDist{T} <: OnlineStat{T}
    value::Union{Missing,Float64}
    n::Int

    function AccuDist{T}() where {T}
        new{T}(missing, 0)
    end
end

and would like to do something like

mutable struct AccuDist{T, S} <: OnlineStat{T, S}
    value::Union{Missing, S}
    n::Int

    function AccuDist{T, S}() where {T, S}
        new{T, S}(missing, 0)
    end
end
=#

function OnlineStatsBase._fit!(ind::AccuDist, candle::OHLCV)
    ind.n += 1
    if candle.high != candle.low
        # Calculate MFI and MFV
        mfi =
            ((candle.close - candle.low) - (candle.high - candle.close)) /
            (candle.high - candle.low)
        mfv = mfi * candle.volume
    else
        # In case high and low are equal (division by zero), return previous value if exists, otherwise return missing
        ind.value = value(ind)
        return
    end

    if !has_output_value(ind)
        ind.value = mfv
    else
        ind.value = value(ind) + mfv
    end
end

Unfortunatly I have to use

value::Union{Missing,Float64}

I'd like to be able to do

value::Union{Missing,Tprice}

How can I inherit OnlineStatsBase with OHLCV{Missing, Float64, Float64} as a single observation value and type Tprice as a sufficient statistics.

Maybe doc can slightly be improved on this side (and I could provide PR after understanding how to tackle that.

Especially in README example

using OnlineStatsBase

mutable struct MyMean <: OnlineStat{Number}
    value::Float64
    n::Int
    MyMean() = new(0.0, 0)
end
function OnlineStatsBase._fit!(o::MyMean, y)
    o.n += 1
    o.value += (1 / o.n) * (y - o.value)
end

value is type of Float64 but it could be more general I think (type of y, or an other type)

Some help from Julia guys experienced with types and inheritance could greatly help

Kind regards

femtotrader added a commit to femtotrader/OnlineStatsBase.jl that referenced this issue Dec 26, 2023
@femtotrader femtotrader mentioned this issue Dec 26, 2023
@joshday
Copy link
Owner

joshday commented Dec 27, 2023

Looking at your PR, you got most of the way there. For a parametric type B, you can do something like this:

struct A{T} <: OnlineStat{B{T}}
    value::T
end

I think this is a little too in-the-weeds to put in the README, but it would make sense somewhere in the OnlineStats docs.

@joshday joshday closed this as completed Dec 27, 2023
@femtotrader
Copy link
Author

thanks @joshday for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants