Text Generation
RWKV
English
maba-instant
maba
maba-instant-v1
architecture
custom-architecture
state-space-model
ssm
mamba
mamba-3
recurrent
dgda
decoupled-gated-delta-attention
gated-deltanet
linear-attention
linear-recurrence
mla
multi-head-latent-attention
deepseek
flow-matching
continuous-flow
non-autoregressive
speculative-decoding
low-latency
microsecond-latency
cpu-inference
avx-512
avx2
simd
cpp
c++
fast-exit
early-exit
rlcd
brier-score
calibration
hft
algorithmic-trading
order-book
real-time
on-device
edge-ai
efficient-inference
zero-kv-cache
arena-allocator
sub-quadratic
llm
nlp
Instructions to use AndrewThompson1233/maba-instant-v1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- RWKV
How to use AndrewThompson1233/maba-instant-v1 with RWKV:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
File size: 15,815 Bytes
9ceb2d7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | #include "maba_instant/pipeline.hpp"
#include <iostream>
#include <iomanip>
#include <vector>
#include <deque>
#include <chrono>
#include <cmath>
#include <random>
#include <algorithm>
#include <cstring>
#include <map>
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<double> 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<double> 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<double, std::deque<Order>, std::greater<double>> bids;
std::map<double, std::deque<Order>, std::less<double>> asks;
std::deque<Order> 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<Execution> step(uint64_t current_time_us, std::vector<AgentStats>& agents) {
std::vector<Execution> 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<int>(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<int>(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<int>(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<int>(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<int>(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<int>(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<AgentStats> 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<double> brownian(0.0, 0.04);
std::uniform_real_distribution<double> unif(0.0, 1.0);
std::uniform_int_distribution<int> 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<std::vector<uint64_t>> active_agent_orders(agents.size());
std::vector<uint64_t> 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<uint64_t>(ag.latency_us);
uint64_t arrival = current_time_us + static_cast<uint64_t>(ag.latency_us);
for (uint64_t oid : active_agent_orders[i]) {
book.cancel_order(oid, static_cast<int>(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<int>(i), OrderSide::Buy, OrderType::Limit, my_bid, 10, arrival, 0};
Order a{book.order_counter++, static_cast<int>(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<int>(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;
}
|