Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bounded_cycle_index_example.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
68# include <cmath>
69# include <iostream>
70# include <limits>
71# include <string>
72
73# include <gmpxx.h>
74
75# include <Bounded_Cycle_Index.H>
76# include <tpl_array.H>
77# include <tpl_const_graph_view.H>
78# include <tpl_graph.H>
79
80using namespace std;
81using namespace Aleph;
82
83namespace
84{
85 struct Trade
86 {
87 string name; // the market and the side, which identify the trade
88 mpq_class rate; // exact, fees included
89 };
90
92 using View = Const_Graph_View<Market>;
93 using Index = Bounded_Cycle_Index<View>;
94
95 // A rate from its digits. GMP keeps the fraction as it is written, and
96 // its arithmetic needs it in lowest terms
97 mpq_class exact_rate(const char *fraction)
98 {
99 mpq_class rate(fraction);
100 rate.canonicalize();
101 return rate;
102 }
103
104 // The weight of a trade: a profitable cycle has a negative sum
105 double weight_of(const mpq_class & rate) { return -log(rate.get_d()); }
106
107 // The margin of the bound. The sum of at most 4 weights below 64 is
108 // within sum_error_bound() of the sum of those weights, and each weight
109 // is within a few units in its last place of the exact -log(rate): one
110 // part in a thousand millions covers both with much room.
111 constexpr double Margin = 1e-9;
112 static_assert(Index::sum_error_bound(4, 64) < Margin);
113
114 // The exact rule: the product of the rates of the cycle exceeds one
115 bool is_profitable(const Index & index, const size_t position)
116 {
117 mpq_class product = 1;
118 for (const auto number : index.cycle_arcs(position))
119 product *= index.arc(number)->get_info().rate;
120 return product > 1;
121 }
122
123 string path_of(const View & market, const Index & index, const size_t position)
124 {
125 string ret;
126 for (const auto number : index.cycle_arcs(position))
127 ret += market.get_src_node(index.arc(number))->get_info() + " -> ";
128 return ret + market.get_src_node(index.arc(index.cycle_arcs(position).front()))->get_info();
129 }
130
131 // What to do with a cycle that the index hands: decide it exactly
132 struct Decide
133 {
134 const View & market;
135 const Index & index;
136 size_t candidates = 0;
137 size_t profitable = 0;
138
139 void operator () (const size_t position, const double sum)
140 {
141 ++candidates;
142 const bool yes = is_profitable(index, position);
143 profitable += yes;
144 cout << " " << path_of(market, index, position) << ": sum " << sum
145 << (yes ? ", profitable" : ", not profitable") << '\n';
146 }
147 };
148}
149
150int main()
151{
152 Market market;
153 const auto usd = market.insert_node("USD");
154 const auto eur = market.insert_node("EUR");
155 const auto gbp = market.insert_node("GBP");
156 const auto jpy = market.insert_node("JPY");
157
158 // Each market has two trades; the rates are decimals, taken exactly
159 const auto trade = [&market] (Market::Node *from, Market::Node *to, const string & name,
160 const char *rate)
161 {
162 return market.insert_arc(from, to, Trade{name, exact_rate(rate)});
163 };
164 const auto eurusd_sell_arc = trade(eur, usd, "EURUSD sell", "10850/10000");
165 const auto eurusd_buy_arc = trade(usd, eur, "EURUSD buy", "10000/10852");
166 trade(gbp, usd, "GBPUSD sell", "12700/10000");
167 trade(usd, gbp, "GBPUSD buy", "10000/12703");
168 const auto eurgbp_sell_arc = trade(eur, gbp, "EURGBP sell", "8540/10000");
169 trade(gbp, eur, "EURGBP buy", "10000/8543");
170 trade(usd, jpy, "USDJPY sell", "15000/100");
171 trade(jpy, usd, "USDJPY buy", "100/15003");
172 trade(eur, jpy, "EURJPY sell", "16270/100");
173 trade(jpy, eur, "EURJPY buy", "100/16275");
174
175 // The index reads the market through a view: it cannot change it. The
176 // arcs take their numbers in the order of their names, whatever the
177 // order in which they were inserted.
178 const View view(market);
179 Index index(view, {2, 3, 4}, [] (const Market::Arc *a, const Market::Arc *b)
180 {
181 return a->get_info().name < b->get_info().name;
182 });
183
184 cout << "Arcs: " << index.num_arcs() << "; cycles: " << index.num_cycles() << " ("
185 << index.num_cycles(2) << " of 2 arcs, " << index.num_cycles(3) << " of 3, "
186 << index.num_cycles(4) << " of 4)\n";
187 const size_t eurusd_sell = index.arc_number(eurusd_sell_arc);
188 const size_t eurusd_buy = index.arc_number(eurusd_buy_arc);
189 cout << "Arc " << eurusd_sell << " is \"" << index.arc(eurusd_sell)->get_info().name
190 << "\", in " << index.cycles_of(eurusd_sell).size() << " cycles\n\n";
191
192 // To start: every weight, and then the whole catalog. One cycle has a
193 // product of exactly one: its sum is rounded, not zero, the margin
194 // hands it and the exact rule says that it is not profitable.
195 for (size_t a = 0; a < index.num_arcs(); ++a)
196 index.set_weight(a, weight_of(index.arc(a)->get_info().rate));
197 Decide decide{view, index};
198 cout << "With the first quotes:\n";
199 const size_t all = index.for_each_cycle_below(Margin, 4, decide);
200 cout << " " << all << " cycles looked at, " << decide.candidates << " candidates, "
201 << decide.profitable << " profitable\n\n";
202
203 // A quote of EURUSD changes its two trades. Both are written before
204 // any cycle is looked at, and each cycle is handed once.
205 const auto quote = [&] (const char *bid, const char *ask_inverse)
206 {
207 eurusd_sell_arc->get_info().rate = exact_rate(bid); // the owner of the market writes it
208 eurusd_buy_arc->get_info().rate = exact_rate(ask_inverse);
209 index.set_weight(eurusd_sell, weight_of(eurusd_sell_arc->get_info().rate));
210 index.set_weight(eurusd_buy, weight_of(eurusd_buy_arc->get_info().rate));
211 decide.candidates = decide.profitable = 0;
212 const size_t seen = index.for_each_cycle_below({eurusd_sell, eurusd_buy}, Margin, 4, decide);
213 cout << " " << seen << " cycles looked at, " << decide.candidates << " candidates, "
214 << decide.profitable << " profitable\n\n";
215 };
216 cout << "EURUSD rises to 1.0880 / 1.0882:\n";
217 quote("10880/10000", "10000/10882");
218
219 cout << "EURUSD falls back to 1.0850 / 1.0852:\n";
220 quote("10850/10000", "10000/10852");
221
222 // The change of one arc: update() writes it and looks at its cycles
223 const size_t eurgbp_sell = index.arc_number(eurgbp_sell_arc);
224 eurgbp_sell_arc->get_info().rate = exact_rate("8600/10000");
225 cout << "EURGBP sell at 0.8600:\n";
226 decide.candidates = decide.profitable = 0;
227 const uint64_t before = index.clock();
228 const size_t seen = index.update(eurgbp_sell, weight_of(eurgbp_sell_arc->get_info().rate),
229 Margin, 4, decide);
230 cout << " " << seen << " cycles looked at, " << decide.candidates << " candidates, "
231 << decide.profitable << " profitable\n\n";
232
233 // What was computed before that change is old for the cycles of the arc
234 size_t stale = 0;
235 for (size_t position = 0; position < index.num_cycles(); ++position)
236 stale += index.is_stale(position, before);
237 cout << stale << " cycles are stale since the clock was " << before << ": those of arc "
238 << eurgbp_sell << ", which are " << index.cycles_of(eurgbp_sell).size() << "\n\n";
239
240 // A market that stops: its trades cannot be used, and no cycle through
241 // them is a candidate
242 index.set_weight(eurgbp_sell, numeric_limits<double>::infinity());
243 decide.candidates = decide.profitable = 0;
244 index.for_each_cycle_below(Margin, 4, decide);
245 cout << "Without EURGBP sell: " << decide.candidates << " candidates\n";
246
247 return 0;
248}
Index of the simple cycles of bounded length of a digraph, with a weight for each arc and a filter by...
Index of the simple cycles of bounded length of a digraph.
Strict read-only view of a graph.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4141
Main namespace for Aleph-w library functions.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
STL namespace.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Dynamic array container with automatic resizing.
Strict read-only access to a graph: Const_Graph_View and the read-only iterators on the nodes,...
Generic graph and digraph implementations.