#include "maba_instant/pipeline.hpp" #include #include #include #include #include #include #include #include #include #include enum class OrderSide { Buy = 0, Sell = 1 }; enum class OrderType { Limit = 0, Market = 1, Cancel = 2 }; struct Order { uint64_t id; int agent_id; OrderSide side; OrderType type; double price; int qty; uint64_t timestamp_us; uint64_t cancel_target_id = 0; }; struct Execution { uint64_t maker_id; uint64_t taker_id; int maker_agent; int taker_agent; double price; int qty; uint64_t timestamp_us; }; struct AgentStats { std::string name; double latency_us; double cash = 100000.0; int inventory = 0; double realized_pnl = 0.0; double maker_volume = 0.0; double taker_volume = 0.0; int maker_fills = 0; int taker_fills = 0; int orders_placed = 0; int cancels_placed = 0; int cancels_succeeded = 0; int adverse_fills = 0; double adverse_loss = 0.0; double peak_equity = 100000.0; double max_drawdown = 0.0; std::vector equity_curve; AgentStats(std::string n, double l) : name(std::move(n)), latency_us(l) {} double equity(double mid_price) const { return cash + inventory * mid_price; } void record_equity(double mid_price) { double eq = equity(mid_price); equity_curve.push_back(eq); if (eq > peak_equity) peak_equity = eq; double dd = peak_equity - eq; if (dd > max_drawdown) max_drawdown = dd; } double sharpe_ratio() const { if (equity_curve.size() < 2) return 0.0; std::vector ret; ret.reserve(equity_curve.size() - 1); for (size_t i = 1; i < equity_curve.size(); ++i) { double r = (equity_curve[i] - equity_curve[i - 1]) / equity_curve[i - 1]; ret.push_back(r); } double mean = 0.0; for (double r : ret) mean += r; mean /= ret.size(); double var = 0.0; for (double r : ret) var += (r - mean) * (r - mean); var /= (ret.size() > 1 ? ret.size() - 1 : 1); double std = std::sqrt(var); if (std < 1e-9) return 0.0; return (mean / std) * std::sqrt(252.0 * 86400.0 * 10.0); } }; class RealisticOrderBook { public: double reference_price = 100.00; std::map, std::greater> bids; std::map, std::less> asks; std::deque wire_inbox; uint64_t order_counter = 1; void post_external_liquidity() { for (int i = 1; i <= 8; ++i) { double bp = std::round((reference_price - i * 0.05) * 100.0) / 100.0; double ap = std::round((reference_price + i * 0.05) * 100.0) / 100.0; if (bids[bp].empty()) { Order b{order_counter++, -1, OrderSide::Buy, OrderType::Limit, bp, 50, 0, 0}; bids[bp].push_back(b); } if (asks[ap].empty()) { Order a{order_counter++, -1, OrderSide::Sell, OrderType::Limit, ap, 50, 0, 0}; asks[ap].push_back(a); } } } double best_bid() const { if (bids.empty()) return reference_price - 0.05; return bids.begin()->first; } double best_ask() const { if (asks.empty()) return reference_price + 0.05; return asks.begin()->first; } double mid_price() const { return 0.5 * (best_bid() + best_ask()); } void submit(const Order& ord, uint64_t arrival_time_us) { Order o = ord; o.timestamp_us = arrival_time_us; wire_inbox.push_back(o); } void cancel_order(uint64_t target_id, int agent_id, uint64_t arrival_time_us) { Order c; c.id = order_counter++; c.agent_id = agent_id; c.type = OrderType::Cancel; c.cancel_target_id = target_id; c.timestamp_us = arrival_time_us; wire_inbox.push_back(c); } std::vector step(uint64_t current_time_us, std::vector& agents) { std::vector executions; std::sort(wire_inbox.begin(), wire_inbox.end(), [](const Order& a, const Order& b) { return a.timestamp_us < b.timestamp_us; }); while (!wire_inbox.empty() && wire_inbox.front().timestamp_us <= current_time_us) { Order ord = wire_inbox.front(); wire_inbox.pop_front(); if (ord.type == OrderType::Cancel) { bool found = false; for (auto it = bids.begin(); it != bids.end() && !found; ++it) { for (auto qit = it->second.begin(); qit != it->second.end(); ++qit) { if (qit->id == ord.cancel_target_id && qit->agent_id == ord.agent_id) { it->second.erase(qit); found = true; if (ord.agent_id >= 0 && ord.agent_id < static_cast(agents.size())) { agents[ord.agent_id].cancels_succeeded++; } break; } } } for (auto it = asks.begin(); it != asks.end() && !found; ++it) { for (auto qit = it->second.begin(); qit != it->second.end(); ++qit) { if (qit->id == ord.cancel_target_id && qit->agent_id == ord.agent_id) { it->second.erase(qit); found = true; if (ord.agent_id >= 0 && ord.agent_id < static_cast(agents.size())) { agents[ord.agent_id].cancels_succeeded++; } break; } } } continue; } if (ord.type == OrderType::Market) { if (ord.side == OrderSide::Buy) { while (ord.qty > 0 && !asks.empty()) { auto best_it = asks.begin(); double fill_price = best_it->first; auto& queue = best_it->second; while (ord.qty > 0 && !queue.empty()) { auto& maker = queue.front(); int fill = std::min(ord.qty, maker.qty); ord.qty -= fill; maker.qty -= fill; Execution ex{maker.id, ord.id, maker.agent_id, ord.agent_id, fill_price, fill, current_time_us}; executions.push_back(ex); if (maker.agent_id >= 0 && maker.agent_id < static_cast(agents.size())) { auto& mag = agents[maker.agent_id]; mag.inventory -= fill; mag.cash += fill * fill_price * (1.0 + 0.0001); mag.maker_volume += fill * fill_price; mag.maker_fills++; if (fill_price < reference_price) { mag.adverse_fills++; mag.adverse_loss += fill * (reference_price - fill_price); } } if (ord.agent_id >= 0 && ord.agent_id < static_cast(agents.size())) { auto& tag = agents[ord.agent_id]; tag.inventory += fill; tag.cash -= fill * fill_price * (1.0 + 0.0003); tag.taker_volume += fill * fill_price; tag.taker_fills++; } if (maker.qty <= 0) queue.pop_front(); } if (queue.empty()) asks.erase(best_it); } } else { while (ord.qty > 0 && !bids.empty()) { auto best_it = bids.begin(); double fill_price = best_it->first; auto& queue = best_it->second; while (ord.qty > 0 && !queue.empty()) { auto& maker = queue.front(); int fill = std::min(ord.qty, maker.qty); ord.qty -= fill; maker.qty -= fill; Execution ex{maker.id, ord.id, maker.agent_id, ord.agent_id, fill_price, fill, current_time_us}; executions.push_back(ex); if (maker.agent_id >= 0 && maker.agent_id < static_cast(agents.size())) { auto& mag = agents[maker.agent_id]; mag.inventory += fill; mag.cash -= fill * fill_price * (1.0 - 0.0001); mag.maker_volume += fill * fill_price; mag.maker_fills++; if (fill_price > reference_price) { mag.adverse_fills++; mag.adverse_loss += fill * (fill_price - reference_price); } } if (ord.agent_id >= 0 && ord.agent_id < static_cast(agents.size())) { auto& tag = agents[ord.agent_id]; tag.inventory -= fill; tag.cash += fill * fill_price * (1.0 - 0.0003); tag.taker_volume += fill * fill_price; tag.taker_fills++; } if (maker.qty <= 0) queue.pop_front(); } if (queue.empty()) bids.erase(best_it); } } continue; } if (ord.type == OrderType::Limit) { if (ord.side == OrderSide::Buy) { bids[ord.price].push_back(ord); } else { asks[ord.price].push_back(ord); } } } post_external_liquidity(); return executions; } }; int main() { std::vector agents = { {"maba-instant v1", 0.9}, {"RWKV-v6", 12.0}, {"Mamba-3 SSM", 20.0}, {"LLaMA-3 AR", 25000.0}, {"Discrete Diffusion", 26000.0} }; RealisticOrderBook book; book.post_external_liquidity(); std::mt19937_64 rng(1337); std::normal_distribution brownian(0.0, 0.04); std::uniform_real_distribution unif(0.0, 1.0); std::uniform_int_distribution flow_qty(5, 25); uint64_t current_time_us = 0; const uint64_t total_events = 30000; const uint64_t step_dt_us = 250; std::vector> active_agent_orders(agents.size()); std::vector next_agent_act_us(agents.size(), 0); for (uint64_t ev = 0; ev < total_events; ++ev) { current_time_us += step_dt_us; double old_ref = book.reference_price; double drift = 0.0005 * (100.0 - book.reference_price) + brownian(rng); book.reference_price = std::round((book.reference_price + drift) * 100.0) / 100.0; if (book.reference_price < 80.0) book.reference_price = 80.0; if (book.reference_price > 120.0) book.reference_price = 120.0; if (book.reference_price > old_ref + 0.01) { Order arb; arb.id = book.order_counter++; arb.agent_id = -1; arb.type = OrderType::Market; arb.qty = 50; arb.side = OrderSide::Buy; arb.price = 0.0; book.submit(arb, current_time_us + 5); } else if (book.reference_price < old_ref - 0.01) { Order arb; arb.id = book.order_counter++; arb.agent_id = -1; arb.type = OrderType::Market; arb.qty = 50; arb.side = OrderSide::Sell; arb.price = 0.0; book.submit(arb, current_time_us + 5); } if (unif(rng) < 0.25) { Order taker_flow; taker_flow.id = book.order_counter++; taker_flow.agent_id = -1; taker_flow.type = OrderType::Market; taker_flow.qty = flow_qty(rng); taker_flow.side = (drift > 0.0) ? OrderSide::Buy : OrderSide::Sell; taker_flow.price = 0.0; book.submit(taker_flow, current_time_us); } for (size_t i = 0; i < agents.size(); ++i) { auto& ag = agents[i]; if (current_time_us < next_agent_act_us[i]) continue; next_agent_act_us[i] = current_time_us + static_cast(ag.latency_us); uint64_t arrival = current_time_us + static_cast(ag.latency_us); for (uint64_t oid : active_agent_orders[i]) { book.cancel_order(oid, static_cast(i), arrival); ag.cancels_placed++; } active_agent_orders[i].clear(); double reservation = book.mid_price() - 0.001 * ag.inventory; double spread_half = (ag.latency_us > 1000.0) ? 0.03 : 0.02; double my_bid = std::round((reservation - spread_half) * 100.0) / 100.0; double my_ask = std::round((reservation + spread_half) * 100.0) / 100.0; Order b{book.order_counter++, static_cast(i), OrderSide::Buy, OrderType::Limit, my_bid, 10, arrival, 0}; Order a{book.order_counter++, static_cast(i), OrderSide::Sell, OrderType::Limit, my_ask, 10, arrival, 0}; book.submit(b, arrival); book.submit(a, arrival); active_agent_orders[i].push_back(b.id); active_agent_orders[i].push_back(a.id); ag.orders_placed += 2; if (ag.latency_us < 50.0 && std::abs(drift) > 0.03 && unif(rng) < 0.25) { OrderSide side = (drift > 0.0) ? OrderSide::Buy : OrderSide::Sell; Order snipe{book.order_counter++, static_cast(i), side, OrderType::Market, 0.0, 10, arrival, 0}; book.submit(snipe, arrival); ag.orders_placed++; } } book.step(current_time_us, agents); if (ev % 10 == 0) { for (auto& ag : agents) { ag.record_equity(book.mid_price()); } } } double final_mid = book.mid_price(); std::cout << std::fixed << std::setprecision(2); for (const auto& ag : agents) { double eq = ag.equity(final_mid); double net = eq - 100000.0; std::cout << "agent=" << ag.name << " latency_us=" << ag.latency_us << " net_pnl=" << net << " inventory=" << ag.inventory << " maker_fills=" << ag.maker_fills << " taker_fills=" << ag.taker_fills << " cancels_placed=" << ag.cancels_placed << " cancels_done=" << ag.cancels_succeeded << " adverse_fills=" << ag.adverse_fills << " adverse_loss=" << ag.adverse_loss << " sharpe=" << std::setprecision(3) << ag.sharpe_ratio() << " max_dd=" << std::setprecision(2) << ag.max_drawdown << "\n"; } return 0; }