API Reference
CcyConv.AbstractPrice
— TypeAbstractPrice
An abstract type representing price of a currency pair.
For types using this interface the following methods must be defined:
CcyConv.AbstractCtx
— TypeAbstractCtx
An abstract type representing workspace context.
Price
CcyConv.Price
— TypePrice <: AbstractPrice
A type representing the price of currency pair.
Fields
from_asset::String
: Base currency name.to_asset::String
: Quote currency name.price::Float64
: The currency pair price.
CcyConv.from_asset
— Functionfrom_asset(x::AbstractPrice) -> String
Returns the name of the base currency of x
.
CcyConv.to_asset
— Functionto_asset(x::AbstractPrice) -> String
Returns the name of the quote currency of x
.
CcyConv.price
— Functionprice(x::AbstractPrice) -> Float64
Returns price of the currency pair x
.
price(ctx::AbstractCtx, x::AbstractPrice) -> Float64
Advanced function for getting the price of a currency pair x
that can take into account the context of the ctx
.
This function is called when searching for a currency conversion path and can be overloaded to achieve advanced functionality using context (for example, caching the requested data for subsequent requests). See context guide.
FXGraph
CcyConv.FXGraph
— TypeFXGraph
This type describes a weighted directed graph in which:
- The nodes are currencies.
- The edges between such nodes represent a currency pair.
- The direction of the edge determines the base and quote currency.
- The weight of the edge is determined by the conversion price of the currency pair.
Fields
edge_nodes::Dict{NTuple{2,UInt64},Vector{AbstractPrice}}
: Dictionary containing information about conversion prices between nodes.edge_encode::Dict{String,UInt64}
: A dictionary containing the names of currencies as keys and their identification numbers as values.graph::Graphs.SimpleGraph{Int64}
: A graph containing basic information about vertices and edges.
CcyConv.conv_ccys
— Functionconv_ccys(fx::FXGraph) -> Vector{String}
Returns the names of all currencies stored in the graph fx
.
Base.push!
— FunctionBase.push!(fx::FXGraph, node::AbstractPrice)
Adds a new edge to the graph fx
corresponding to the currency pair node
.
Base.append!
— FunctionBase.append!(fx::FXGraph, nodes::Vector{AbstractPrice})
Does the same as push!
but can pass several currency pairs nodes
at once.
ConvRate
CcyConv.ConvRate
— TypeConvRate
An object describing the price and conversion path between two currencies.
Fields
from_asset::String
: The name of an initial curreny.to_asset::String
: The name of a target currency.conv::Float64
: Total currency conversion price.chain::Vector{AbstractPrice}
: Chain of currency pairs involved in conversion.
CcyConv.conv_value
— Functionconv_value(x::ConvRate) -> Float64
Returns the convert rate of x
.
CcyConv.conv_safe_value
— Functionconv_safe_value(x::ConvRate) -> Float64
Asserts that the conversion rate of x
is not a NaN
value and then returns it. Otherwise throws an AssertionError
.
CcyConv.conv_chain
— Functionconv_chain(x::ConvRate) -> Vector{AbstractPrice}
Returns the path chain of x
.
Pathfinding
Available pathfinding algorithms:
a_star_alg
: The most basic algorithm for finding the shortest path between two currencies.
CcyConv.conv_a_star
— Functionconv_a_star(fx::FXGraph, from_asset::String, to_asset::String) -> ConvRate
Uses an A*
search algorithm to find the shortest path between from_asset
and to_asset
currencies in graph fx
.
Examples
julia> crypto = FXGraph();
julia> append!(
crypto,
[
Price("ADA", "USDT", 0.4037234),
Price("USDT", "BTC", 0.0000237),
Price("BTC", "ETH", 18.808910),
Price("ETH", "ALGO", 14735.460),
],
);
julia> conv = conv_a_star(crypto, "ADA", "BTC");
julia> conv_value(conv)
0.000009582698067
julia> conv_chain(conv)
2-element Vector{CcyConv.AbstractPrice}:
Price("ADA", "USDT", 0.4037234)
Price("USDT", "BTC", 0.0000237)
CcyConv.FXGraph
— Method(fx::FXGraph)(ctx::AbstractCtx, path_alg::Function, from_asset::String, to_asset::String) -> ConvRate
Applies algorithm path_alg
to find a path on graph fx
between base currency from_asset
and target currency to_asset
using context ctx
.
This method is low-level and is required when using a custom context.