80static void bar(
int val,
int scale = 1)
83 for (
int i = 0; i <
w; ++i)
117 cout <<
"============================================================\n"
118 <<
" SCENARIO 1: Order Book Depth (Fenwick_Tree + find_kth)\n"
119 <<
"============================================================\n\n";
121 const size_t TICKS = 20;
125 cout <<
"Limit SELL orders arrive:\n";
127 struct Order {
size_t tick;
int shares;
const char *label; };
129 { 0, 120,
"$100.00"},
131 { 3, 200,
"$100.03"},
133 { 7, 300,
"$100.07"},
134 {10, 150,
"$100.10"},
135 {15, 400,
"$100.15"},
138 for (
auto & o : orders)
140 ask_book.
update(o.tick, o.shares);
141 cout <<
" " << setw(7) << o.label
142 <<
" +" << setw(4) << o.shares <<
" shares\n";
145 cout <<
"\nOrder book (ask side):\n\n";
146 cout <<
" Tick Price Volume Cumulative\n"
147 <<
" ---- -------- ------ ----------\n";
149 for (
size_t t = 0; t < TICKS; ++t)
151 int vol = ask_book.
get(t);
154 int cum = ask_book.
prefix(t);
155 cout <<
" " << setw(4) << t
156 <<
" $" << fixed << setprecision(2) << (100.0 + t * 0.01)
157 <<
" " << setw(6) << vol
158 <<
" " << setw(10) << cum <<
" ";
164 int buy_sizes[] = {100, 250, 500, 1000, 1500};
166 cout <<
"\nMarket BUY order fill simulation:\n\n";
167 cout <<
" Order Size Worst Tick Worst Price\n"
168 <<
" ---------- ---------- -----------\n";
170 for (
int sz : buy_sizes)
172 size_t worst_tick = ask_book.
find_kth(sz);
173 if (worst_tick >= TICKS)
174 cout <<
" " << setw(10) << sz <<
" INSUFFICIENT LIQUIDITY\n";
176 cout <<
" " << setw(10) << sz
177 <<
" " << setw(10) << worst_tick
178 <<
" $" << fixed << setprecision(2)
179 << (100.0 + worst_tick * 0.01) <<
"\n";
183 cout <<
"\n>> Cancel 150 shares at tick 3 ($100.03)\n";
186 cout <<
">> New fill for 250 shares: ";
188 cout <<
"worst price = $" << fixed << setprecision(2)
189 << (100.0 +
w * 0.01)
190 <<
" (tick " <<
w <<
")\n";
192 int total_liquidity = ask_book.
prefix(TICKS - 1);
193 cout <<
"\nTotal ask liquidity: " << total_liquidity <<
" shares\n";
211 cout <<
"\n\n============================================================\n"
212 <<
" SCENARIO 2: Intraday P&L Dashboard (Gen_Fenwick_Tree)\n"
213 <<
"============================================================\n\n";
215 const size_t MINUTES = 390;
219 struct Trade {
size_t minute;
double pnl;
const char *event; };
221 { 0, 1200.0,
"Open: initial scalp profit" },
222 { 5, -300.0,
"Stop-loss hit on AAPL" },
223 { 30, 4500.0,
"NVDA earnings beat — long pays off" },
224 { 31, 2200.0,
"Follow-through momentum" },
225 { 60, -800.0,
"Mean reversion loss" },
226 { 120, -1500.0,
"Lunch hour chop" },
227 { 180, 3000.0,
"Afternoon trend resumes" },
228 { 270, 8000.0,
"Fed holds rates — massive rally" },
229 { 271, 5000.0,
"Fed follow-through" },
230 { 330, -2000.0,
"Profit taking" },
231 { 389, 1500.0,
"MOC imbalance capture" },
234 auto min_to_time = [](
size_t m) ->
string
236 int h = 9 + (int)(
m + 30) / 60;
237 int mm = ((int)
m + 30) % 60;
239 snprintf(buf,
sizeof(buf),
"%02d:%02d",
h, mm);
243 cout <<
"Trades booked:\n\n";
244 cout <<
" Time Minute P&L Event\n"
245 <<
" ----- ------ ---------- ----------------------------\n";
247 for (
auto & t : trades)
249 pnl.
update(t.minute, t.pnl);
250 cout <<
" " << min_to_time(t.minute)
251 <<
" " << setw(6) << t.minute
252 <<
" " << setw(10) << fixed << setprecision(2) << t.pnl
253 <<
" " << t.event <<
"\n";
257 cout <<
"\nDashboard queries:\n\n";
259 double open_to_lunch = pnl.
prefix(179);
260 double fed_to_close = pnl.
query(270, 389);
261 double total_day = pnl.
prefix(389);
262 double morning = pnl.
query(0, 119);
263 double afternoon = pnl.
query(120, 389);
265 cout <<
" Open to lunch (09:30-12:30): $" << setw(10) << open_to_lunch <<
"\n"
266 <<
" Fed to close (14:00-16:00): $" << setw(10) << fed_to_close <<
"\n"
267 <<
" Morning session (09:30-11:30): $" << setw(10) << morning <<
"\n"
268 <<
" Afternoon (11:30-16:00): $" << setw(10) << afternoon <<
"\n"
269 <<
" ----------------------------------------\n"
270 <<
" Total day P&L: $" << setw(10) << total_day <<
"\n";
273 cout <<
"\n>> Correction: NVDA trade at 10:00 re-priced from "
274 <<
"$4500 to $4000\n";
276 cout <<
" Adjusted total day P&L: $"
277 << fixed << setprecision(2) << pnl.
prefix(389) <<
"\n";
301 cout <<
"\n\n============================================================\n"
302 <<
" SCENARIO 3: Betting Exchange Promotions (Range_Fenwick_Tree)\n"
303 <<
"============================================================\n\n";
305 const size_t DAYS = 14;
308 struct Promo {
size_t from;
size_t to;
int bonus;
const char *name; };
310 { 0, 6, 50,
"Welcome Week" },
311 { 3, 9, 30,
"Midweek Boost" },
312 { 5, 5, 100,
"Super Saturday" },
313 { 0, 13, 10,
"Loyalty Baseline" },
316 cout <<
"Promotions applied:\n\n";
317 cout <<
" Promotion Days Bonus/day\n"
318 <<
" ---------------- --------- ---------\n";
320 for (
auto & p : promos)
322 payouts.
update(p.from, p.to, p.bonus);
323 cout <<
" " << setw(16) << left << p.name << right
324 <<
" " << setw(2) << p.from <<
" - " << setw(2) << p.to
325 <<
" " << setw(5) << p.bonus <<
"c\n";
329 const char *day_names[] = {
330 "Mon W1",
"Tue W1",
"Wed W1",
"Thu W1",
"Fri W1",
"Sat W1",
"Sun W1",
331 "Mon W2",
"Tue W2",
"Wed W2",
"Thu W2",
"Fri W2",
"Sat W2",
"Sun W2"
334 cout <<
"\nDaily payout per user:\n\n"
335 <<
" Day Name Cents Breakdown\n"
336 <<
" --- ------- ----- ---------\n";
338 for (
size_t d = 0; d < DAYS; ++d)
340 int cents = payouts.
get(d);
341 cout <<
" " << setw(3) << d
342 <<
" " << day_names[d]
343 <<
" " << setw(5) << cents <<
" ";
349 cout <<
"\nAccounting queries:\n\n";
351 int week1 = payouts.
query(0, 6);
352 int week2 = payouts.
query(7, 13);
353 int total = payouts.
prefix(13);
354 int peak_weekend = payouts.
query(4, 6);
356 cout <<
" Week 1 total (days 0-6): " << setw(5) << week1 <<
"c\n"
357 <<
" Week 2 total (days 7-13): " << setw(5) << week2 <<
"c\n"
358 <<
" Peak weekend (Fri-Sun W1): " << setw(5) << peak_weekend <<
"c\n"
359 <<
" ----------------------------------\n"
360 <<
" Grand total (14 days): " << setw(5) << total <<
"c\n";
363 cout <<
"\n>> Retroactive adjustment: 'Apology Bonus' +20c on days 10-13\n";
364 payouts.
update(10, 13, 20);
366 cout <<
" Adjusted week 2 total: " << payouts.
query(7, 13) <<
"c\n"
367 <<
" Adjusted grand total: " << payouts.
prefix(13) <<
"c\n";
371 cout <<
"\n>> With " << users <<
" users:\n"
372 <<
" Total 14-day cost: $"
373 << fixed << setprecision(2)
374 << (payouts.
prefix(13) * users / 100.0) <<
"\n";
Fenwick tree over an arbitrary abelian group.
T query(const size_t l, const size_t r) const
Range query: Plus(a[l], a[l+1], ..., a[r]).
T prefix(size_t i) const
Prefix query: Plus(a[0], a[1], ..., a[i]).
void update(size_t i, const T &delta)
Point update: a[i] = Plus(a[i], delta).
T get(const size_t i) const
Retrieve the logical value a[i].
Fenwick tree supporting range updates and range queries.
void update(size_t l, const size_t r, const T &delta)
Range update: add delta to every a[i] with l <= i <= r.
T get(const size_t i) const
Retrieve the logical value a[i].
T prefix(const size_t i) const
Prefix query: sum of a[0..i].
T query(const size_t l, const size_t r) const
Range query: sum of a[l..r].
static void scenario_pnl_dashboard()
static void scenario_order_book()
static void bar(int val, int scale=1)
static void scenario_betting_promos()
Main namespace for Aleph-w library functions.
Fenwick tree for arithmetic types with find_kth support.
size_t find_kth(T k) const
Find the smallest 0-based index i such that prefix(i) >= k.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Fenwick tree (Binary Indexed Tree) for prefix queries.