/ Fetch / src / dispatch.jl
dispatch.jl
 1  # FIXME: `pairs` and `timeframe` should swap place to be consistent with `load_ohlcv` func
 2  function fetch_ohlcv(exc::Exchange, timeframe::TimeFrame, pairs::Iterable; kwargs...)
 3      fetch_ohlcv(exc, string(timeframe), pairs; kwargs...)
 4  end
 5  function fetch_ohlcv(exc, timeframe, pair::AbstractString; kwargs...)
 6      fetch_ohlcv(exc, string(timeframe), [pair]; kwargs...)
 7  end
 8  function fetch_ohlcv(exc, timeframe; qc=config.qc, kwargs...)
 9      pairs = tickers(exc, qc; as_vec=true)
10      fetch_ohlcv(exc, string(timeframe), pairs; kwargs...)
11  end
12  
13  @doc """Fetches OHLCV data for multiple exchanges on the same timeframe.
14  
15  $(TYPEDSIGNATURES)
16  
17  This function fetches OHLCV data for multiple exchanges over the same timeframe. It accepts:
18  - A vector of exchange instances `excs`.
19  - The desired timeframe `timeframe`.
20  
21  The function can run in parallel if `parallel` is set to true. If `wait_task` is set to true, the function will wait for all tasks to complete before returning.
22  
23  You can provide additional parameters using `kwargs`.
24  """
25  function fetch_ohlcv(
26      excs::Vector{Exchange},
27      timeframe;
28      account="",
29      sandbox=true,
30      parallel=false,
31      wait_task=false,
32      kwargs...,
33  )
34      # out_file = joinpath(DATA_PATH, "out.log")
35      # err_file = joinpath(DATA_PATH, "err.log")
36      # FIXME: find out how io redirection interacts with distributed
37      # t = redirect_stdio(; stdout=out_file, stderr=err_file) do
38      parallel && _instantiate_workers(:Planar; num=length(excs))
39      # NOTE: The python classes have to be instantiated inside the worker processes
40      if eltype(excs) === Symbol
41          e_pl = s -> (ex = getexchange!(s; sandbox, account); (ex, tickers(ex; as_vec=true)))
42      else
43          e_pl = s -> (getexchange!(Symbol(lowercase(s[1].name)); sandbox, account), s[2])
44      end
45      t = @parallel parallel for s in excs
46          ex, pl = e_pl(s)
47          fetch_ohlcv(ex, timeframe, pl; kwargs...)
48      end
49      # end
50      parallel && wait_task && wait(t)
51      t
52  end
53  
54  @doc """Prompts user for confirmation before fetching OHLCV data.
55  
56  $(TYPEDSIGNATURES)
57  
58  This function prompts the user for confirmation before fetching OHLCV data for the specified arguments `args` and keyword arguments `kwargs`. If the user inputs 'Y', 'y', or simply presses Enter, it proceeds with the `fetch_ohlcv` function. If any other input is given, the function returns `nothing`.
59  """
60  function fetch_ohlcv(::Val{:ask}, args...; kwargs...)
61      Base.display("fetch? Y/n")
62      ans = String(read(stdin, 1))
63      ans ∉ ("\n", "y", "Y") && return nothing
64      fetch_ohlcv(args...; qc=config.qc, zi, kwargs...)
65  end
66  
67  function fetch_candles(exc::Exchange, tf::TimeFrame, args...; kwargs...)
68      fetch_candles(exc, string(tf), args...; kwargs...)
69  end