104 using Asset = Market::Node;
105 using Trade = Market::Arc;
108 constexpr double trading_fee = 0.001;
118 struct Log_Rate_Distance
120 using Distance_Type = double;
122 double fee = trading_fee;
130 double operator()(Trade * trade)
const
132 return -std::log(trade->get_info() * (1.0 - fee)) + margin;
146 double usdt_value(
const string & asset)
170 {
"BTC",
"USDT", 0.0},
171 {
"ETH",
"USDT", 0.0},
172 {
"ETH",
"BTC", 0.010},
173 {
"BNB",
"USDT", 0.0},
174 {
"SOL",
"USDT", 0.0},
176 {
"ADA",
"USDT", 0.0},
178 {
"ADA",
"BNB", 0.008},
185 void build_market(Market & market)
188 auto asset_of = [&](
const char * name) -> Asset *
190 const string key = name;
191 if (not assets.
has(key))
192 assets.
insert(key, market.insert_node(key));
193 return assets.
find(key);
196 for (
const Pair & p : pairs)
198 const double price = usdt_value(p.base) / usdt_value(p.quote) * (1.0 + p.distortion);
199 Asset * base = asset_of(p.base);
200 Asset *
quote = asset_of(p.quote);
201 market.insert_arc(quote, base, 1.0 / price);
202 market.insert_arc(base, quote, price);
228 assets.
for_each([&names](Asset * a) { names.
append(a->get_info()); });
232 for (
size_t i = 1; i < names.
size(); ++i)
233 if (names(i) < names(first))
237 for (
size_t i = 0; i < names.
size(); ++i)
238 op.route += names((first + i) % names.
size()) +
" -> ";
239 op.route += names(first);
240 op.trades = names.
size();
243 trades.
for_each([&growth](Trade * t) { growth *= t->get_info() * (1.0 - trading_fee); });
244 op.gain = growth - 1.0;
248 void print_opportunity(
const Opportunity & op)
250 cout <<
" " << op.route <<
" (" << op.trades <<
" trades, gain "
251 << showpos << fixed << setprecision(3) << op.gain * 100.0 <<
"%" << noshowpos <<
")\n";
256 size_t list_opportunities(
const Market & market,
const double margin,
const size_t max_cycles)
259 market, max_cycles, Negative_Cycle_Exclusion::All_Arcs,
260 Log_Rate_Distance{trading_fee, margin});
263 for (
auto it = cycles.get_it(); it.has_curr(); it.next_ne())
264 found.
append(describe(it.get_curr().cycle.nodes(), it.get_curr().cycle.arcs()));
267 for (
size_t i = 1; i < found.size(); ++i)
268 for (
size_t j = i; j > 0
and found(j).gain > found(j - 1).gain; --j)
269 swap(found(j), found(j - 1));
271 for (
size_t i = 0; i < found.size(); ++i)
272 print_opportunity(found(i));
276 void show_bounded(
const Market & market,
const size_t max_trades)
279 Log_Rate_Distance{trading_fee, 0.0});
280 cout <<
" at most " << max_trades <<
" trades: ";
281 if (not
r.is_negative())
283 cout <<
"no profitable cycle\n";
290 const Opportunity op = describe(
r.cycle_nodes,
r.cycle_arcs);
291 cout << op.route <<
" (gain " << showpos << fixed << setprecision(3) << op.gain * 100.0
292 <<
"%" << noshowpos <<
", ";
294 cout <<
"proven optimal";
296 cout <<
"within " << scientific << setprecision(0) <<
r.optimality_gap
297 <<
" of the best in log terms, a rigorous bound" << fixed;
306 build_market(market);
308 cout <<
"Crypto arbitrage reference example\n";
309 cout <<
"Market: " << market.get_num_nodes() <<
" assets, "
310 << market.get_num_arcs() / 2 <<
" pairs, fee " << fixed << setprecision(2)
311 << trading_fee * 100.0 <<
"% per trade\n";
313 cout <<
"\nOpportunities, no minimum margin:\n";
314 const size_t unconstrained = list_opportunities(market, 0.0, 5);
315 cout <<
"Opportunities found: " << unconstrained <<
"\n";
319 cout <<
"\nOpportunities with a minimum margin of 0.2% per trade:\n";
320 const size_t with_margin = list_opportunities(market, 0.002, 5);
321 cout <<
"Opportunities found: " << with_margin <<
"\n";
323 cout <<
"\nMost profitable cycle by total gain, bounded by its length:\n";
324 show_bounded(market, 2);
325 show_bounded(market, 3);
329 cout <<
"\nKarp minimum mean cycle (profit per trade, not total profit):\n";
330 const Opportunity mean_cycle = describe(karp.cycle_nodes, karp.cycle_arcs);
331 cout <<
" " << mean_cycle.route <<
" (mean log-weight " << fixed << setprecision(5)
332 <<
static_cast<double>(karp.minimum_mean) <<
" per trade)\n";
Minimum mean cycle in directed graphs (Karp algorithm).
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Generic directed graph (digraph) wrapper template.
Doubly-linked list (defined in tpl_dynList.H).
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
bool has(const Key &key) const noexcept(nothrow_search)
Return whether key is in the map.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
DynList< Negative_Cycle_Item< GT, typename Distance::Distance_Type > > find_disjoint_negative_cycles(const GT &g, const size_t max_cycles, const Negative_Cycle_Exclusion exclusion=Negative_Cycle_Exclusion::Min_Weight_Arc, Distance distance=Distance(), SA sa=SA())
Find up to max_cycles negative cycles, excluding arcs of each cycle found before searching for the ne...
Bounded_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_bounded(const GT &g, const size_t max_length, Distance distance=Distance(), SA sa=SA())
Most negative simple cycle of at most max_length arcs, ranked by total cost (not by mean cost as Karp...
Min_Mean_Cycle_Result< GT, typename Distance::Distance_Type > karp_minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA())
Compute minimum mean cycle by Karp's algorithm.
std::string quote(const std::string &s)
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
and
Check uniqueness with explicit hash + equality functors.
Arc of graph implemented with double-linked adjacency lists.
Dynamic array container with automatic resizing.
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.