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

TimeArrays integration #9

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LightweightCharts"
uuid = "d6998af1-87ca-4e7f-83d4-864c79a249fa"
version = "1.2.1"
version = "1.3.0"

[deps]
Serde = "db9b398d-9517-45f8-9a95-92af99003e0e"
Expand Down
62 changes: 53 additions & 9 deletions src/chart_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,6 @@ function lwc_convert_data!(data::T)::T where {T<:AbstractVector{<:AbstractChartD
return data
end

function lwc_convert_data(
timearray::AbstractVector{Tuple{D,T}},
)::Vector{LWCSimpleChartData} where {D<:Union{Real,TimeType},T<:Real}
data::Vector{LWCSimpleChartData} = [
LWCSimpleChartData(datetime2epochns(datetime), value) for (datetime, value) in timearray
]
return lwc_convert_data!(data)
end

function lwc_convert_data(
timearray::AbstractVector{Tuple{D,O,H,L,C}},
)::Vector{LWCCandle} where {D<:Union{Real,TimeType},O<:Real,H<:Real,L<:Real,C<:Real}
Expand All @@ -241,6 +232,59 @@ function lwc_convert_data(
return lwc_convert_data!(data)
end

function lwc_convert_data(::Type{T}, timearray::AbstractVector) where {T<:AbstractChartData}
element_type = eltype(timearray)
V = if hasmethod(convert, (Type{Pair}, element_type))
Pair
elseif hasmethod(convert, (Type{Tuple}, element_type))
Tuple
else
throw(ErrorException("Type $element_type must have a `convert` method to Tuple type or Pair type."))
end

data = lwc_convert_data(T, V, timearray)
return lwc_convert_data!(data)
end

function lwc_convert_data(::Type{LWCSimpleChartData}, ::Type{V}, timearray::AbstractVector)::Vector{LWCSimpleChartData} where {V}
return map(timearray) do element
datetime, value = convert(V, element)
return LWCSimpleChartData(datetime2epochns(datetime), value)
end
end

function lwc_convert_data(::Type{LWCCandle}, ::Type{V}, timearray::AbstractVector)::Vector{LWCCandle} where {V}
return map(timearray) do element
candle = convert(V, element)
return if length(candle) == 2
datetime, value = candle
open, high, low, close = if hasmethod(iterate, Tuple{typeof(value)})
value
elseif hasmethod(convert, (Type{Tuple}, typeof(value)))
convert(Tuple, value)
else
throw(ErrorException(
"""
Invalid custom candlestick value data ($(typeof(value))).
Candlestick value must be iterable or convertable to Tuple{Real,Real,Real,Real}.
"""
))
end
LWCCandle(datetime2epochns(datetime), open, high, low, close)
elseif length(candle) == 5
datetime, open, high, low, close = candle
LWCCandle(datetime2epochns(datetime), open, high, low, close)
else
throw(ErrorException(
"""
Invalid custom candlestick data ($(eltype(timearray))).
Candle must be convertable to Tuple{Union{TimeType,Real},NTuple{4,Real}} or Tuple{Union{TimeType,Real},Real,Real,Real,Real}
"""
))
end
end
end

function lwc_convert_data(time::D)::Int64 where {D<:Union{Real,TimeType}}
return datetime2epochns(time)
end
Expand Down
14 changes: 7 additions & 7 deletions src/charts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ function prepare_data(
return collect(zip(timestamps, values))
end

function prepare_data(
values::Vector{T};
)::Vector{Tuple{Union{Real,TimeType},T}} where {T<:Real}
timestamps = [d + Second(1) for d in DateTime(1970):Second(1):DateTime(1970) + Second(length(values) - 1)]
return prepare_data(timestamps, values)
end

function prepare_data(
timestamps::Vector{D},
open::Vector{O},
Expand All @@ -84,13 +91,6 @@ function prepare_data(
return collect(zip(timestamps, open, high, low, close))
end

function prepare_data(
values::Vector{T};
)::Vector{Tuple{Union{Real,TimeType},T}} where {T<:Real}
timestamps = [d + Second(1) for d in DateTime(1970):Second(1):DateTime(1970) + Second(length(values) - 1)]
return prepare_data(timestamps, values)
end

include("charts/line.jl")
include("charts/baseline.jl")
include("charts/area.jl")
Expand Down
20 changes: 11 additions & 9 deletions src/charts/area.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,26 @@ function lwc_area(
end

function lwc_area(
timearray::AbstractVector{Tuple{D,T}};
timestamps::AbstractVector{D},
values::AbstractVector{T};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)
data = prepare_data(timestamps, values)
return lwc_area(data; kw...)
end

function lwc_area(
timestamps::Vector{D},
values::Vector{T};
timearray::AbstractVector{T};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
return lwc_area(prepare_data(timestamps, values); kw...)
)::LWCChart where {T<:Real}
data = prepare_data(timearray)
return lwc_area(data; kw...)
end

function lwc_area(
values::Vector{T};
timearray::AbstractVector;
kw...
)::LWCChart where {T<:Real}
return lwc_area(prepare_data(values); kw...)
)::LWCChart
data = lwc_convert_data(LWCSimpleChartData, timearray)
return lwc_area(data; kw...)
end
12 changes: 12 additions & 0 deletions src/charts/bar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,15 @@ function lwc_bar(
)::LWCChart where {D<:Union{Real,TimeType},O<:Real,H<:Real,L<:Real,C<:Real}
return lwc_bar(lwc_convert_data(data); kw...)
end

function lwc_bar(
timestamp::AbstractVector{T},
ohlc::AbstractVector{Tuple{O,H,L,C}};
kw...
)::LWCChart where {T<:Union{TimeType,Real},O<:Real,H<:Real,L<:Real,C<:Real}
return lwc_bar(prepare_data(data); kw...)
end

function lwc_bar(data::AbstractVector; kw...)::LWCChart
return lwc_bar(lwc_convert_data(LWCCandle, data); kw...)
end
20 changes: 11 additions & 9 deletions src/charts/baseline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,26 @@ function lwc_baseline(
end

function lwc_baseline(
timearray::AbstractVector{Tuple{D,T}};
timestamps::AbstractVector{D},
values::AbstractVector{T};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)
data = prepare_data(timestamps, values)
return lwc_baseline(data; kw...)
end

function lwc_baseline(
timestamps::Vector{D},
values::Vector{T};
timearray::AbstractVector{T};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
return lwc_baseline(prepare_data(timestamps, values); kw...)
)::LWCChart where {T<:Real}
data = prepare_data(timearray)
return lwc_baseline(data; kw...)
end

function lwc_baseline(
values::Vector{T};
timearray::AbstractVector;
kw...
)::LWCChart where {T<:Real}
return lwc_baseline(prepare_data(values); kw...)
)::LWCChart
data = lwc_convert_data(LWCSimpleChartData, timearray)
return lwc_baseline(data; kw...)
end
12 changes: 12 additions & 0 deletions src/charts/candlestick.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ function lwc_candlestick(
)::LWCChart where {D<:Union{Real,TimeType},O<:Real,H<:Real,L<:Real,C<:Real}
return lwc_candlestick(lwc_convert_data(data); kw...)
end

function lwc_candlestick(
timestamp::AbstractVector{T},
ohlc::AbstractVector{Tuple{O,H,L,C}};
kw...
)::LWCChart where {T<:Union{TimeType,Real},O<:Real,H<:Real,L<:Real,C<:Real}
return lwc_candlestick(prepare_data(data); kw...)
end

function lwc_candlestick(data::AbstractVector; kw...)::LWCChart
return lwc_candlestick(lwc_convert_data(LWCCandle, data); kw...)
end
20 changes: 11 additions & 9 deletions src/charts/histogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,26 @@ function lwc_histogram(
end

function lwc_histogram(
timearray::AbstractVector{Tuple{D,T}};
timestamps::AbstractVector{D},
values::AbstractVector{T};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)
data = prepare_data(timestamps, values)
return lwc_histogram(data; kw...)
end

function lwc_histogram(
timestamps::Vector{D},
values::Vector{T};
timearray::AbstractVector{T};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
return lwc_histogram(prepare_data(timestamps, values); kw...)
)::LWCChart where {T<:Real}
data = prepare_data(timearray)
return lwc_histogram(data; kw...)
end

function lwc_histogram(
values::Vector{T};
timearray::AbstractVector;
kw...
)::LWCChart where {T<:Real}
return lwc_histogram(prepare_data(values); kw...)
)::LWCChart
data = lwc_convert_data(LWCSimpleChartData, timearray)
return lwc_histogram(data; kw...)
end
20 changes: 11 additions & 9 deletions src/charts/line.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,26 @@ function lwc_line(
end

function lwc_line(
timearray::AbstractVector{Tuple{D,T}};
timestamps::AbstractVector{D},
values::AbstractVector{T};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)
data = prepare_data(timestamps, values)
return lwc_line(data; kw...)
end

function lwc_line(
timestamps::Vector{D},
values::Vector{T};
timearray::AbstractVector{T};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
return lwc_line(prepare_data(timestamps, values); kw...)
)::LWCChart where {T<:Real}
data = prepare_data(timearray)
return lwc_line(data; kw...)
end

function lwc_line(
values::Vector{T};
timearray::AbstractVector;
kw...
)::LWCChart where {T<:Real}
return lwc_line(prepare_data(values); kw...)
)::LWCChart
data = lwc_convert_data(LWCSimpleChartData, timearray)
return lwc_line(data; kw...)
end