/ ExchangeTypes / src / exchangeid.jl
exchangeid.jl
 1  @doc "All possible exchanges that can be instantiated by ccxt."
 2  const exchangeIds = Symbol[]
 3  
 4  @doc """A structure for handling Exchange IDs in CCXT.
 5  
 6  $(FIELDS)
 7  
 8  This structure is used to manage Exchange IDs in the CCXT library. It contains methods for creating an Exchange ID from a symbol, a Python object, or directly from a symbol type. It ensures that the symbol is in the list of valid exchange IDs.
 9  """
10  struct ExchangeID{I}
11      function ExchangeID(sym::Symbol=Symbol())
12          sym == Symbol() && return new{sym}()
13          if isempty(exchangeIds)
14              prev = Set{Symbol}()
15              for name in ccxt_exchange_names()
16                  id = Symbol(name)
17                  if id ∉ prev
18                      push!(exchangeIds, id)
19                      push!(prev, id)
20                  end
21              end
22              @assert sym ∈ exchangeIds
23          else
24              @assert sym ∈ exchangeIds
25          end
26          new{sym}()
27      end
28      function ExchangeID(py::Py)
29          s = if pyisnone(py)
30              ""
31          else
32              (pyhasattr(py, "__name__") ? py.__name__ : py.__class__.__name__)
33          end
34          ExchangeID(pyconvert(Symbol, s))
35      end
36      function ExchangeID{sym}() where {sym}
37          ExchangeID(sym)
38      end
39  end
40  
41  const EIDType = Type{<:ExchangeID}
42  Base.getproperty(::T, ::Symbol) where {T<:ExchangeID} = T.parameters[1]
43  Base.nameof(::ExchangeID{T}) where {T} = T
44  Base.show(io::IO, id::ExchangeID) = begin
45      write(io, "ExchangeID(:")
46      write(io, id.sym)
47      write(io, ")")
48  end
49  Base.convert(::Type{<:AbstractString}, id::ExchangeID) = string(id.sym)
50  Base.convert(::Type{Symbol}, id::ExchangeID) = id.sym
51  Base.Symbol(::Type{<:ExchangeID{T}}) where {T} = T
52  Base.Symbol(id::ExchangeID) = id.sym
53  Base.string(id::ExchangeID) = string(id.sym)
54  function Base.display(
55      ids::T
56  ) where {T<:Union{AbstractVector{ExchangeID},AbstractSet{ExchangeID}}}
57      s = String[]
58      for id in ids
59          push!(s, string(id.sym))
60      end
61      Base.display(s)
62  end
63  Base.Broadcast.broadcastable(q::ExchangeID) = Ref(q)
64  import Base.==
65  ==(id::ExchangeID, s::Symbol) = Base.isequal(nameof(id), s)
66  
67  @doc "Create an ExchangeID instance from a symbol."
68  exchangeid(sym::Symbol) = ExchangeID(sym)
69  @doc "Return the given ExchangeID instance."
70  exchangeid(id::ExchangeID) = id
71  @doc "Union type of many exchange ids (from `Symbol` arguments)"
72  eids(ids...) = Union{((ExchangeID{i}) for i in ids)...}