/ LiveMode / src / balance / fetch.jl
fetch.jl
 1  using .Lang: @lget!, splitkws
 2  using .ExchangeTypes
 3  using .Exchanges: issandbox, current_account
 4  using .Misc.TimeToLive
 5  using .Misc: LittleDict, DFT, ZERO
 6  using .Python: pytofloat, Py, @pystr, @pyconst, PyDict, pyfetch, pyconvert
 7  using .Instances: bc, qc
 8  import .st: current_total, MarginStrategy, NoMarginStrategy
 9  
10  _exc_balance_func(exc::Exchange) = first(exc, :fetchBalanceWs, :fetchBalance)
11  
12  @doc """ Fetches the balance for a given live strategy.
13  
14  $(TYPEDSIGNATURES)
15  
16  The function fetches the balance by calling the `_fetch_balance` function with the exchange associated with the live strategy and any additional arguments.
17  The balance is fetched from the exchange's API.
18  """
19  function fetch_balance(s::LiveStrategy, args...; type=balance_type(s), kwargs...)
20      qc = nameof(s.cash)
21      syms = st.assets(s)
22      _fetch_balance(exchange(s), qc, syms, args...; type, kwargs...)
23  end
24  
25  function _fetch_balance(exc::Exchange, args...; timeout=gettimeout(exc), kwargs...)
26      _execfunc_timeout(_exc_balance_func(exc), args...; timeout, kwargs...)
27  end
28  
29  _fetch_balance(exc::Exchange, qc; kwargs...) = _fetch_balance(exc, qc, (); kwargs...)