Skip to content

Commit

Permalink
Add TimeTick conversion methods (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
artememelin authored Jun 18, 2024
1 parent 4646acd commit 2777483
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ struct TimeTick{T<:TimeLike,V<:Any} <: AbstractTick{T,V}
end

function TimeTick{T,V}(x::Tuple{TimeLike,Any}) where {T<:TimeLike,V}
return new{T,V}(x...)
return new{T,V}(x[1], x[2])
end

function TimeTick{T,V}(x::NamedTuple{names,<:Tuple{TimeLike,Any}} where {names}) where {T<:TimeLike,V}
return new{T,V}(x...)
return new{T,V}(x[1], x[2])
end

function TimeTick{T,V}(x::Pair{<:TimeLike,<:Any}) where {T<:TimeLike,V}
Expand Down Expand Up @@ -86,7 +86,7 @@ TimeTick(2024-01-01T00:00:00, 100)
```
"""
function TimeTick(x::Tuple{T,V}) where {T<:TimeLike,V}
return TimeTick{T,V}(x...)
return TimeTick{T,V}(x[1], x[2])
end

"""
Expand All @@ -107,7 +107,7 @@ TimeTick(2024-01-01T00:00:00, 100)
```
"""
function TimeTick(x::NamedTuple{names,Tuple{T,V}}) where {names,T<:TimeLike,V}
return TimeTick{T,V}(x...)
return TimeTick{T,V}(x[1], x[2])
end

"""
Expand Down Expand Up @@ -144,28 +144,44 @@ Base.eltype(::Type{TimeTick{T,V}}) where {T,V} = Union{T,V}
Base.isnan(x::TimeTick) = isnan(ta_value(x))
Base.isinf(x::TimeTick) = isinf(ta_value(x))

function Base.convert(::Type{TimeTick{T,V}}, value::TimeTick{T,V}) where {T<:TimeLike,V}
return value
function Base.convert(::Type{TimeTick{T,V}}, x::TimeTick{T,V}) where {T<:TimeLike,V}
return x
end

function Base.convert(::Type{TimeTick{T,V}}, value::TimeTick{TimeLike,Any}) where {T<:TimeLike,V}
return TimeTick{T,V}(value)
function Base.convert(::Type{TimeTick{T,V}}, x::TimeTick) where {T<:TimeLike,V}
return TimeTick{T,V}(x)
end

function Base.convert(::Type{TimeTick{T,V}}, value::Tuple{T,V}) where {T<:TimeLike,V}
return TimeTick{T,V}(value)
function Base.convert(::Type{TimeTick{T,V}}, x::Tuple{TimeLike,Number}) where {T<:TimeLike,V}
return TimeTick{T,V}(x)
end

function Base.convert(::Type{TimeTick{T,V}}, value::Tuple{TimeLike,Number}) where {T<:TimeLike,V}
return TimeTick{T,V}(value)
function Base.convert(::Type{TimeTick}, x::Tuple{T,V}) where {T<:TimeLike,V}
return TimeTick{T,V}(x)
end

function Base.convert(::Type{TimeTick{T,V}}, value::Pair{T,V}) where {T<:TimeLike,V}
return TimeTick{T,V}(value)
function Base.convert(::Type{TimeTick{T,V}}, x::Pair{<:TimeLike,<:Any}) where {T<:TimeLike,V}
return TimeTick{T,V}(x)
end

function Base.convert(::Type{TimeTick{T,V}}, value::Pair{TimeLike,Any}) where {T<:TimeLike,V}
return TimeTick{T,V}(value)
function Base.convert(::Type{TimeTick}, x::Pair{T,V}) where {T<:TimeLike,V}
return TimeTick{T,V}(x)
end

function Base.convert(::Type{Tuple}, x::TimeTick)
return Tuple(x)
end

function Base.convert(::Type{Tuple{T,V}}, x::TimeTick) where {T<:TimeLike,V}
return Tuple{T,V}(x)
end

function Base.convert(::Type{Pair}, x::TimeTick)
return Pair(ta_timestamp(x), ta_value(x))
end

function Base.convert(::Type{Pair{T,V}}, x::TimeTick) where {T<:TimeLike,V}
return Pair{T,V}(ta_timestamp(x), ta_value(x))
end

function Base.getindex(t::TimeTick, i::Integer)
Expand Down Expand Up @@ -203,7 +219,7 @@ end
Supertype for `TimeArray{T,V}` with timestamps of type `T` and values of type `V`.
"""
abstract type AbstractTimeArray{T,V} <: AbstractVector{TimeTick{T,V}} end
abstract type AbstractTimeArray{T,V} <: AbstractVector{AbstractTick{T,V}} end

"""
TimeArray{T,V} <: AbstractTimeArray{T,V}
Expand Down
20 changes: 20 additions & 0 deletions test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@
@test TimeTick{Time,Int64}(TimeTick{NanoDate,Float64}(DateTime("2023-01-01"), 2)) == TimeTick(Time("00:00:00"), 2.0)
@test TimeTick{DateTime,Int64}(TimeTick{NanoDate,Float64}(DateTime("2023-01-01"), 2)) == TimeTick(DateTime("2023-01-01"), 2.0)
end

@testset "Case №5: TimeTick conversions" begin
common_tick = TimeTick{DateTime,Float64}(DateTime("2024-01-01T01:02:03"), 1.0)
@test convert(TimeTick{DateTime,Float64}, common_tick) == common_tick
@test convert(TimeTick{Date,Int64}, common_tick) == TimeTick{Date,Int64}(Date("2024-01-01"), 1)
@test convert(Tuple{Date,Int64}, common_tick) == (Date("2024-01-01"), 1)
@test convert(Tuple, common_tick) == (DateTime("2024-01-01T01:02:03"), 1.0)
@test convert(Pair{Date,Int64}, common_tick) == (Date("2024-01-01") => 1)
@test convert(Pair, common_tick) == (DateTime("2024-01-01T01:02:03") => 1.0)

tuple_tick = (DateTime("2024-01-01T01:02:03"), 1.0)
@test convert(TimeTick{DateTime,Float64}, tuple_tick) == common_tick
@test convert(TimeTick{Date,Int64}, tuple_tick) == TimeTick{Date,Int64}(Date("2024-01-01"), 1)
@test convert(TimeTick, tuple_tick) == TimeTick{DateTime,Float64}(DateTime("2024-01-01T01:02:03"), 1.0)

pair_tick = DateTime("2024-01-01T01:02:03") => 1.0
@test convert(TimeTick{DateTime,Float64}, pair_tick) == common_tick
@test convert(TimeTick{Date,Int64}, pair_tick) == TimeTick{Date,Int64}(Date("2024-01-01"), 1)
@test convert(TimeTick, pair_tick) == TimeTick{DateTime,Float64}(DateTime("2024-01-01T01:02:03"), 1.0)
end
end

@testset verbose = true "TimeArrays constructors" begin
Expand Down

2 comments on commit 2777483

@gryumov
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

Release notes:

  • Added TimeTick Conversion Methods
  • Added WMA and EMA Indicators

Examples of New Features

using TimeArrays

julia> prices = ta_price_sample_data()
7777-element TimeArray{DateTime, Float64}:
 TimeTick(2024-04-01T00:00:00.661, 0.6501)
 TimeTick(2024-04-01T00:05:57.481, 0.6505)
 
 TimeTick(2024-04-30T23:42:11.920, 0.4417)

julia> sma_prices = ta_sma(prices, 20)
7777-element TimeArray{DateTime, Float64}:
 TimeTick(2024-04-01T00:00:00.661, NaN)
 TimeTick(2024-04-01T00:05:57.481, NaN)
 
 TimeTick(2024-04-30T23:42:11.920, 0.4403)

julia> wma_prices = ta_wma(prices, 20)
7777-element TimeArray{DateTime, Float64}:
 TimeTick(2024-04-01T00:00:00.661, NaN)
 TimeTick(2024-04-01T00:05:57.481, NaN)
 
 TimeTick(2024-04-30T23:42:11.920, 0.4409)

julia> ema_prices = ta_ema(prices, 20)
7777-element TimeArray{DateTime, Float64}:
 TimeTick(2024-04-01T00:00:00.661, 0.6501)
 TimeTick(2024-04-01T00:05:57.481, 0.6501)
 
 TimeTick(2024-04-30T23:42:11.920, 0.4399)

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/109237

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" 27774838d5c06ef96807fea25f6901bf995846d1
git push origin v1.1.0

Please sign in to comment.