-
Notifications
You must be signed in to change notification settings - Fork 2
/
Validity.jl
74 lines (52 loc) · 1.9 KB
/
Validity.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
validate_symbol(symbol::AbstractString)
Validates a Symbol (Ticker). Returns `true` if the ticker is valid and `false` if the ticker is not valid.
# Arguments
* smybol`::String` is a ticker (e.g. AAPL for Apple Computers, or ^GSPC for the S&P500)
# How it works
Checks if the HTTP request works (status 200) or whether the request errors (common status in this case: 404)
"""
function validate_symbol(symbol::AbstractString)
res = try
q = Dict("range"=>"1d", "interval"=>"1d")
res = HTTP.get("https://query2.finance.yahoo.com/v8/finance/chart/$(symbol)", query = q, proxy=_PROXY_SETTINGS[:proxy],headers=_PROXY_SETTINGS[:auth]).status
catch e
res = e.status
end #end try
return isequal(res,200) ? true : false
end #end validate_symbol
"""
get_valid_symbols(symbol::AbstractString)
Takes a symbol. If the symbol is valid it returns the symbol in a vector if not it returns and empy vector.
# Arguments
* smybol`::AbstractString` is a ticker (e.g. AAPL for Apple Computers, or ^GSPC for the S&P500)
# Examples
```julia-repl
julia> get_valid_symbols("AAPL")
1-element Vector{String}:
"AAPL"
julia> get_valid_symbols("asdfs")
String[]
```
"""
function get_valid_symbols(symbol::AbstractString)
valid = validate_symbol(symbol)
return valid ? [symbol] : String[]
end #end get_valid_symbols
"""
get_valid_symbols(symbol::AbstractVector{<:AbstractString})
Takes a `AbstractVector` of symbols and returns only the valid ones.
# Arguments
* smybol`::AbstractVector{<:AbstractString}` is a vector of tickers (e.g. AAPL for Apple Computers, or ^GSPC for the S&P500)
# Examples
```julia-repl
julia> get_valid_symbols("AAPL","AMD","asdfs")
2-element Vector{String}:
"AAPL"
"AMD"
```
"""
function get_valid_symbols(symbol::AbstractVector{<:AbstractString})
idx = collect(validate_symbol.(symbol))
return symbol[idx]
end #end get_valid_symbols