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

Replace find -> findall and fix issue 432 #433

Merged
merged 9 commits into from
Oct 17, 2019
Merged
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
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### 0.16.1

* Fix Candlestick plotting. ([#433])


[#433]: https://github.com/JuliaStats/TimeSeries.jl/pull/433


### 0.16.0

* Improve performance of `moving` function. (#414)
Expand Down
3 changes: 2 additions & 1 deletion docs/src/plotting.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ found in the Plots
[documentation](http://docs.juliaplots.org/latest/supported/).


Plotting candlestick:
Plotting candlestick, the ``Candlestick`` constructor requires four columns
exist in the input. They are `open`, `high`, `low` and `close` (case-insensitive).:

```julia
plot(TimeSeries.Candlestick(MarketData.ohlc))
Expand Down
34 changes: 29 additions & 5 deletions src/plotrecipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
end
end

# FIXME: refine Candlestick as a subtype of AbstractTimeSeries
# it requires the Base.view supports. (#419)
"""
Candlestick(ta::TimeArray)

# Argument

- There are four required columns from `ta::TimeArray`: `open`, `high`, `low` and
`close`. The column names is case-insensitive.

# Examples

```julia-repl
julia> using MarketData

julia> TimeSeries.Candlestick(ohlcv)
```
"""
mutable struct Candlestick{D <: TimeType}
time::Vector{D}
open::AbstractVector
Expand All @@ -24,9 +42,15 @@ end
Candlestick(ta::TimeArray) = Candlestick(extract_ohlc(ta)...)

function extract_ohlc(ta::TimeArray)
indices = [find(x->lowercase(x) == name, colnames(ta)) for name in ["open", "high", "low", "close"]]
minimum(length.(indices)) < 1 && error("The time array did not have variables named open, high, low and close")
(timestamp(ta), [values(ta)[:,i] for i in 1:4]...)
C = ["open", "high", "low", "close"]
cols = colnames(ta)
cols′ = lowercase.(string.(colnames(ta)))
V = map(C) do x
i = findfirst(isequal(x), cols′)
i ≡ nothing && throw(ArgumentError("the TimeArray did not have column `$x`"))
values(ta[cols[i]])
end
(timestamp(ta), V...)
end

function HeikinAshi!(cs::Candlestick) # some values here are made too high!
Expand Down Expand Up @@ -93,10 +117,10 @@ end


for att in attributes
inds = Vector{Int}(length(cs.close))
inds = similar(cs.close, Int)
inds[1] = att[:close_open](cs.close[1], cs.open[1]) & att[:close_prev](cs.close[1], cs.close[1])
@. inds[2:end] = att[:close_open](cs.close[2:end], cs.open[2:end]) & att[:close_prev]($diff(cs.close), 0)
inds = find(inds)
inds = findall(Bool.(inds))

if length(inds) > 0
@series begin
Expand Down
22 changes: 22 additions & 0 deletions test/plotrecipes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Test

using MarketData
using TimeSeries


@testset "plotrecipes" begin


@testset "the Candlestick constructor works" begin
candlestick = TimeSeries.Candlestick(ohlcv)
@test length(candlestick.time) == length(ohlcv)

candlestick = TimeSeries.Candlestick(ohlc)
@test length(candlestick.time) == length(ohlc)

@test_throws ArgumentError TimeSeries.Candlestick(cl)
@test_throws ArgumentError TimeSeries.Candlestick(op)
end


end # @testset "plotrecipes
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tests = [
"timeseriesrc",
"basemisc",
"tables",
"plotrecipes",
]


Expand Down