/ Instances / src / constructors.jl
constructors.jl
 1  using Exchanges: market_limits, market_precision, market_fees
 2  
 3  @doc """ Creates an AssetInstance.
 4  
 5  $(TYPEDSIGNATURES)
 6  
 7  This function creates an AssetInstance with the specified asset (`a`), data, exchange (`exc`), margin, and an optional minimum amount (`min_amount`). If no minimum amount is provided, it defaults to 1e-15.
 8  
 9  """
10  function Instances.AssetInstance(a; data, exc, margin, min_amount=1e-15)
11      precision = market_precision(a.raw, exc)
12      limits = market_limits(a.raw, exc; default_amount=(min=min_amount, max=Inf), precision)
13      fees = market_fees(a.raw, exc)
14      AssetInstance(a, data, exc, margin; limits, precision, fees)
15  end
16  @doc """ Creates an AssetInstance from strings.
17  
18  $(TYPEDSIGNATURES)
19  
20  This function creates an AssetInstance using the provided strings for the asset (`s`), data type (`t`), exchange (`e`), and margin type (`m`).
21  
22  """
23  function Instances.AssetInstance(
24      s::S, t::S, e::S, m::S; sandbox::Bool, params=nothing, account=""
25  ) where {S<:AbstractString}
26      a = parse(AbstractAsset, s)
27      tf = convert(TimeFrame, t)
28      exc = getexchange!(Symbol(e), params; sandbox, account)
29      margin = if m == "isolated"
30          Isolated()
31      elseif m == "cross"
32          Cross()
33      else
34          NoMargin()
35      end
36      data = Dict(tf => load(zi, exc.name, a.raw, t))
37      AssetInstance(a, data, exc, margin)
38  end
39  
40  function Instances.AssetInstance{AA,EID,MM}(sym; sandbox, params=nothing, account="") where {AA,EID,MM}
41      AssetInstance(
42          parse(AbstractAsset, sym);
43          data=SortedDict{TimeFrame,DataFrame}(),
44          exc=getexchange!(EID(), params; sandbox, account),
45          margin=MM(),
46      )
47  end
48  
49  function default_asset_df(ai::AssetInstance)
50      df = DataFrame()
51      metadata!(df, "asset_instance", ai; style=:note)
52      return df
53  end