Skip to content

Commit

Permalink
Fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
eohne committed Aug 13, 2024
1 parent 7163741 commit 5806458
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "YFinance"
uuid = "e4b3b0a2-f9a3-42f3-aabb-32142cceaf77"
authors = ["Elias L Ohneberg"]
version = "0.1.6"
version = "0.1.7"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
4 changes: 4 additions & 0 deletions docs/src/VersionChanges.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
!!! info "v0.1.7"
## Bug Fix
* `get_prices`, `get_splits`, `get_dividends` now error more nicely when there is no data for the selected date range. ([#19](https://github.com/eohne/YFinance.jl/issues/19))

!!! info "v0.1.6"
## Improvements
* `get_prices` can now return dividends and splits ([#11](https://github.com/eohne/YFinance.jl/issues/11), [#18](https://github.com/eohne/YFinance.jl/issues/18))
Expand Down
93 changes: 69 additions & 24 deletions src/Prices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,24 @@ function get_prices(symbol::AbstractString; range::AbstractString="5d", interval
return OrderedCollections.OrderedDict()
end
elseif isequal(res.status, 400)
if throw_error
error("$(JSON3.read(res.response.body).finance.error.description).")
yahoo_error = JSON3.read(res.response.body)
if haskey(yahoo_error,:finance)
if throw_error
error("$(yahoo_error.finance.error.description).")
else
@warn "$(yahoo_error.finance.error.description). An empy OrderedCollections.OrderedDict was returned!"
return OrderedCollections.OrderedDict()
end
else
@warn "$(JSON3.read(res.response.body).finance.error.description). An empy OrderedCollections.OrderedDict was returned!"
return OrderedCollections.OrderedDict()
end
error_dates = unix2datetime.(parse.(Float64, [ match.match for match in eachmatch(r"(-)?[0-9]{1,}", yahoo_error.chart.error.description)]))
yahoo_error ="Data doesn't exist for startDate = $(error_dates[1]), endDate = $(error_dates[2])"
if throw_error
error("$yahoo_error for $symbol")
else
@warn "$yahoo_error for $symbol. An empy OrderedCollections.OrderedDict was returned!"
return OrderedCollections.OrderedDict()
end
end
end

res = JSON3.read(res.body).chart.result[1]
Expand Down Expand Up @@ -396,18 +408,36 @@ function get_splits(symbol::AbstractString;startdt="", enddt="",timeout = 10,thr
)
end
elseif isequal(res.status, 400)
if throw_error
error("$(JSON3.read(res.response.body).finance.error.description).")
yahoo_error = JSON3.read(res.response.body)
if haskey(yahoo_error,:finance)
if throw_error
error("$(yahoo_error.finance.error.description).")
else
@warn "$(yahoo_error.finance.error.description). An empy OrderedCollections.OrderedDict was returned!"
return OrderedDict(
"ticker" => symbol,
"timestamp" => DateTime[],
"numerator" => Int[],
"denominator" => Int[],
"ratio" => String[]
)
end
else
@warn "$(JSON3.read(res.response.body).finance.error.description). An empy OrderedCollections.OrderedDict was returned!"
return d = OrderedDict(
"ticker" => symbol,
"timestamp" => DateTime[],
"numerator" => Int[],
"denominator" => Int[],
"ratio" => String[]
)
end
error_dates = unix2datetime.(parse.(Float64, [ match.match for match in eachmatch(r"(-)?[0-9]{1,}", yahoo_error.chart.error.description)]))
yahoo_error ="Data doesn't exist for startDate = $(error_dates[1]), endDate = $(error_dates[2])"
if throw_error
error("$yahoo_error for $symbol")
else
@warn "$yahoo_error for $symbol. An empy OrderedCollections.OrderedDict was returned!"
return OrderedDict(
"ticker" => symbol,
"timestamp" => DateTime[],
"numerator" => Int[],
"denominator" => Int[],
"ratio" => String[]
)
end
end
end

res = JSON3.read(res.body).chart.result[1]
Expand Down Expand Up @@ -557,15 +587,30 @@ function get_dividends(symbol::AbstractString;startdt="", enddt="",timeout = 10,
"dividend" => Float64[])
end
elseif isequal(res.status, 400)
if throw_error
error("$(JSON3.read(res.response.body).finance.error.description).")
yahoo_error = JSON3.read(res.response.body)
if haskey(yahoo_error,:finance)
if throw_error
error("$(yahoo_error.finance.error.description).")
else
@warn "$(yahoo_error.finance.error.description). An empy OrderedCollections.OrderedDict was returned!"
return OrderedDict(
"ticker" => symbol,
"timestamp" => DateTime[],
"dividend" => Float64[])
end
else
@warn "$(JSON3.read(res.response.body).finance.error.description). An empy OrderedCollections.OrderedDict was returned!"
return d = OrderedDict(
"ticker" => symbol,
"timestamp" => DateTime[],
"dividend" => Float64[])
end
error_dates = unix2datetime.(parse.(Float64, [ match.match for match in eachmatch(r"(-)?[0-9]{1,}", yahoo_error.chart.error.description)]))
yahoo_error ="Data doesn't exist for startDate = $(error_dates[1]), endDate = $(error_dates[2])"
if throw_error
error("$yahoo_error for $symbol")
else
@warn "$yahoo_error for $symbol. An empy OrderedCollections.OrderedDict was returned!"
return OrderedDict(
"ticker" => symbol,
"timestamp" => DateTime[],
"dividend" => Float64[])
end
end
end

res = JSON3.read(res.body).chart.result[1]
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ using Test
ta=YFinance._date_to_unix(DateTime(200,1,1),DateTime(201,1,1))
@test ta==(-55855785600, -55824249600)

@test_throws ErrorException get_prices("aapl",startdt=Date(1800), enddt=Date(1900),throw_error=true)
ta= get_prices("aapl",startdt=Date(1800), enddt=Date(1900),throw_error=false)
@test isempty(ta)
@test_throws ErrorException get_prices("aapl",interval="1m", range="1mo")
@test_throws ErrorException get_prices("aapl",interval="1m",startdt="2000-01-01", enddt="2020-10-01")

Expand Down

0 comments on commit 5806458

Please sign in to comment.