Repository navigation
Expand file tree
/
Copy pathhft_engine.cpp
More file actions
519 lines (457 loc) Β· 16.6 KB
/
Copy pathhft_engine.cpp
File metadata and controls
519 lines (457 loc) Β· 16.6 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include <algorithm>
#include <atomic>
#include <array>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <pthread.h>
#include <sched.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
// Platform Compatibility
#if defined(__x86_64__) || defined(_M_X64)
#include <immintrin.h>
#include <x86intrin.h>
#elif defined(__aarch64__)
#include <time.h>
#endif
// ββ Include our new modules ββββββββββββββββββββββββββββββββββ
#include "include/types.hpp"
#include "include/spsc_queue.hpp"
#include "include/indicators.hpp"
#include "include/strategy.hpp"
#include "include/feed.hpp"
#include "src/strategies/my_strategy.hpp"
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// SECTION 1 β Platform Utilities
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
static uint64_t rdtsc_hz = 0;
#if defined(__x86_64__) || defined(_M_X64)
static void calibrate_tsc()
{
struct timespec t1, t2;
clock_gettime(CLOCK_MONOTONIC_RAW, &t1);
uint64_t c1 = __rdtsc();
usleep(10000);
uint64_t c2 = __rdtsc();
clock_gettime(CLOCK_MONOTONIC_RAW, &t2);
uint64_t ns = (t2.tv_sec - t1.tv_sec) * 1'000'000'000ULL + (t2.tv_nsec - t1.tv_nsec);
rdtsc_hz = (c2 - c1) * 1'000'000'000ULL / ns;
}
inline uint64_t now_ns() { return __rdtsc() * 1'000'000'000ULL / rdtsc_hz; }
#else
static void calibrate_tsc() { rdtsc_hz = 1; }
inline uint64_t now_ns()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1'000'000'000ULL + ts.tv_nsec;
}
#endif
static void pin_thread(int core)
{
#ifdef __linux__
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
#else
(void)core; // Ignored on MacOS
#endif
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// SECTION 2 β Engine Utilities
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
constexpr int BOOK_LEVELS = 64;
constexpr int64_t TICK_SIZE = 10000;
struct Level
{
int64_t price;
uint64_t qty;
};
class alignas(CACHE_LINE) OrderBook
{
public:
void reset(int64_t mid) noexcept
{
mid_price_ = mid;
for (int i = 0; i < BOOK_LEVELS; i++)
{
bids_[i] = {mid - (i + 1) * TICK_SIZE, 0};
asks_[i] = {mid + (i + 1) * TICK_SIZE, 0};
}
}
void update_bid(int64_t price, uint64_t qty) noexcept
{
int idx = (int)((mid_price_ - price) / TICK_SIZE) - 1;
if (idx >= 0 && idx < BOOK_LEVELS)
{
bids_[idx].qty = qty;
bids_[idx].price = price;
}
}
void update_ask(int64_t price, uint64_t qty) noexcept
{
int idx = (int)((price - mid_price_) / TICK_SIZE) - 1;
if (idx >= 0 && idx < BOOK_LEVELS)
{
asks_[idx].qty = qty;
asks_[idx].price = price;
}
}
private:
int64_t mid_price_{0};
Level bids_[BOOK_LEVELS];
Level asks_[BOOK_LEVELS];
};
constexpr int LAT_SAMPLES = 1024;
class LatencyTracker
{
public:
void record(uint64_t t0, uint64_t t1) noexcept
{
uint64_t lat = t1 - t0;
uint32_t idx = head_ & (LAT_SAMPLES - 1);
sum_ = sum_ - samples_[idx] + lat;
samples_[idx] = lat;
head_++;
if (lat < min_)
min_ = lat;
if (lat > max_)
max_ = lat;
if (lat < 250) bins_[0]++;
else if (lat < 500) bins_[1]++;
else if (lat < 1000) bins_[2]++;
else if (lat < 2000) bins_[3]++;
else if (lat < 5000) bins_[4]++;
else bins_[5]++;
}
uint64_t avg_ns() const noexcept { return head_ == 0 ? 0 : sum_ / std::min(head_, (uint64_t)LAT_SAMPLES); }
uint64_t min_ns() const noexcept { return min_; }
uint64_t p99_ns() const noexcept
{
uint64_t buf[LAT_SAMPLES];
int n = (int)std::min(head_, (uint64_t)LAT_SAMPLES);
std::memcpy(buf, samples_, n * sizeof(uint64_t));
std::nth_element(buf, buf + n * 99 / 100, buf + n);
return buf[n * 99 / 100];
}
void print_histogram() const noexcept
{
printf("\n=== Tick-to-Signal Latency Histogram ===\n");
printf(" < 250 ns : %llu\n", (unsigned long long)bins_[0]);
printf(" 250-500 ns : %llu\n", (unsigned long long)bins_[1]);
printf(" 500-1000 ns : %llu\n", (unsigned long long)bins_[2]);
printf(" 1-2 us : %llu\n", (unsigned long long)bins_[3]);
printf(" 2-5 us : %llu\n", (unsigned long long)bins_[4]);
printf(" > 5 us : %llu\n", (unsigned long long)bins_[5]);
printf("========================================\n");
}
private:
uint64_t samples_[LAT_SAMPLES]{};
uint64_t head_{0}, sum_{0};
uint64_t min_{UINT64_MAX}, max_{0};
uint64_t bins_[6]{0};
};
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// SECTION 3 β ZMQ Telemetry Layer
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
struct TelemetryMsg
{
enum Kind : uint8_t
{
TICK,
BAR,
SIGNAL,
STATS
} kind;
union
{
struct
{
int64_t price, bid, ask;
uint64_t ts_ns;
} tick;
struct
{
int64_t o, h, l, c;
uint32_t vol;
uint64_t ts;
} bar;
struct
{
SignalType type;
uint8_t strength;
int64_t entry, sl, tp;
uint64_t lag_ns;
} sig;
struct
{
uint64_t tick_count, signal_count;
uint64_t lat_avg, lat_min, lat_p99;
int64_t pnl;
uint64_t trades, wins;
} stats;
};
};
using TelQueue = SPSCQueue<TelemetryMsg, 2048>;
constexpr size_t SHM_SIZE = 1024 * 1024 * 16; // 16 MB
struct MmapPublisher
{
int fd;
uint8_t* base;
std::atomic<uint64_t>* head_ptr;
uint8_t* buffer;
size_t buffer_size;
bool init(const char *endpoint = "/tmp/hft_telemetry.mmap")
{
fd = open(endpoint, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd < 0) return false;
if (ftruncate(fd, SHM_SIZE) != 0) return false;
base = (uint8_t*)mmap(nullptr, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (base == MAP_FAILED) return false;
head_ptr = new (base) std::atomic<uint64_t>();
head_ptr->store(0, std::memory_order_relaxed);
buffer = base + 8; // 8 bytes for head
buffer_size = SHM_SIZE - 8;
return true;
}
void publish(const void *data, size_t len)
{
uint64_t h = head_ptr->load(std::memory_order_relaxed);
uint32_t total_len = 2 + len;
uint64_t offset = h % buffer_size;
if (offset + total_len > buffer_size) {
uint32_t pad = buffer_size - offset;
if (pad >= 2) {
uint16_t zero = 0;
std::memcpy(buffer + offset, &zero, 2);
}
h += pad;
offset = 0;
}
uint16_t frame_len = len;
std::memcpy(buffer + offset, &frame_len, 2);
std::memcpy(buffer + offset + 2, data, len);
head_ptr->store(h + total_len, std::memory_order_release);
}
};
#pragma pack(push, 1)
struct BinTick { uint8_t kind; int64_t price, bid, ask; uint64_t ts_ns; };
struct BinBar { uint8_t kind; int64_t o, h, l, c; uint32_t vol; uint64_t ts; };
struct BinSignal { uint8_t kind; uint8_t type, strength; int64_t entry, sl, tp; uint64_t lag_ns; };
struct BinStats { uint8_t kind; uint64_t t_count, s_count, l_avg, l_min, l_p99; int64_t pnl; uint64_t trades, wins; };
#pragma pack(pop)
static void telemetry_thread_fn(TelQueue *q, MmapPublisher *pub)
{
TelemetryMsg msg;
while (true)
{
if (!q->pop(msg))
{
usleep(100);
continue;
}
if (msg.kind == TelemetryMsg::TICK)
{
BinTick b{0, msg.tick.price, msg.tick.bid, msg.tick.ask, msg.tick.ts_ns};
pub->publish(&b, sizeof(b));
}
else if (msg.kind == TelemetryMsg::BAR)
{
BinBar b{1, msg.bar.o, msg.bar.h, msg.bar.l, msg.bar.c, msg.bar.vol, msg.bar.ts};
pub->publish(&b, sizeof(b));
}
else if (msg.kind == TelemetryMsg::SIGNAL)
{
BinSignal b{2, (uint8_t)msg.sig.type, msg.sig.strength, msg.sig.entry, msg.sig.sl, msg.sig.tp, msg.sig.lag_ns};
pub->publish(&b, sizeof(b));
}
else if (msg.kind == TelemetryMsg::STATS)
{
BinStats b{3, msg.stats.tick_count, msg.stats.signal_count, msg.stats.lat_avg, msg.stats.lat_min, msg.stats.lat_p99, msg.stats.pnl, msg.stats.trades, msg.stats.wins};
pub->publish(&b, sizeof(b));
}
}
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Execution Simulator
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class ExecutionSimulator {
int64_t position_qty{0};
int64_t entry_price{0};
int64_t stop_loss{0};
int64_t take_profit{0};
int64_t realized_pnl{0};
uint64_t total_trades{0};
uint64_t winning_trades{0};
public:
void on_tick(const Tick& tick) {
if (position_qty > 0) {
if (tick.bid <= stop_loss || tick.bid >= take_profit) {
close_position(tick.bid);
}
} else if (position_qty < 0) {
if (tick.ask >= stop_loss || tick.ask <= take_profit) {
close_position(tick.ask);
}
}
}
void on_signal(const Signal& sig, const Tick& tick) {
if (position_qty != 0) return;
bool is_long = (sig.type == SignalType::LONG_EMA_CROSS || sig.type == SignalType::LONG_MOMENTUM || sig.type == SignalType::LONG_RSI_OVERSOLD || sig.type == SignalType::LONG_VWAP_RECLAIM);
position_qty = is_long ? 1 : -1;
entry_price = is_long ? tick.ask : tick.bid;
stop_loss = sig.stop_loss;
take_profit = sig.take_profit;
}
private:
void close_position(int64_t exit_price) {
int64_t pnl = position_qty > 0 ? (exit_price - entry_price) : (entry_price - exit_price);
realized_pnl += pnl;
total_trades++;
if (pnl > 0) winning_trades++;
position_qty = 0;
entry_price = 0;
stop_loss = 0;
take_profit = 0;
}
public:
int64_t get_pnl() const { return realized_pnl; }
uint64_t get_trades() const { return total_trades; }
uint64_t get_wins() const { return winning_trades; }
};
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// SECTION 4 β Threads & Main
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
using TickQueue = SPSCQueue<Tick, 4096>;
using SignalQueue = SPSCQueue<Signal, 256>;
struct Engine
{
TickQueue tick_q;
SignalQueue signal_q;
TelQueue tel_q;
BarBuilder bar_builder;
IndicatorEngine indicators;
OrderBook order_book;
LatencyTracker latency;
ExecutionSimulator exec_sim;
MmapPublisher mmap_pub;
std::atomic<bool> running{true};
std::atomic<uint64_t> tick_count{0};
std::atomic<uint64_t> signal_count{0};
};
static void signal_thread(Engine *eng)
{
pin_thread(3);
Tick tick;
Bar bar;
Signal sig;
uint64_t stats_counter = 0;
// Load dynamic risk config and init your custom strategy
RiskConfig config;
config.min_signal_strength = 75; // Lower to get more trades!
SwingMomentumStrategy my_strategy(config);
while (eng->running.load(std::memory_order_relaxed))
{
if (!eng->tick_q.pop(tick))
{
#if defined(__x86_64__) || defined(_M_X64)
_mm_pause();
#else
asm volatile("yield" ::: "memory");
#endif
continue;
}
uint64_t t0 = now_ns();
if (tick.ts_ns == 0) tick.ts_ns = now_ns();
eng->tick_count.fetch_add(1, std::memory_order_relaxed);
eng->exec_sim.on_tick(tick);
eng->order_book.update_bid(tick.bid, (uint64_t)tick.last_size);
eng->order_book.update_ask(tick.ask, (uint64_t)tick.last_size);
TelemetryMsg tm;
tm.kind = TelemetryMsg::TICK;
tm.tick = {tick.price, tick.bid, tick.ask, tick.ts_ns};
eng->tel_q.push(tm);
if (eng->bar_builder.on_tick(tick, bar))
{
eng->indicators.update(bar);
TelemetryMsg bm;
bm.kind = TelemetryMsg::BAR;
bm.bar = {bar.open, bar.high, bar.low, bar.close, bar.volume, bar.ts_close_ns};
eng->tel_q.push(bm);
// ---> Evaluate Custom Strategy from src/strategies/my_strategy.hpp <---
if (my_strategy.evaluate(bar, eng->indicators, sig))
{
sig.tick_ts_ns = bar.ts_close_ns;
sig.ts_ns = now_ns();
eng->exec_sim.on_signal(sig, tick);
while (!eng->signal_q.push(sig)) { /* spin */ }
eng->signal_count.fetch_add(1, std::memory_order_relaxed);
TelemetryMsg sm;
sm.kind = TelemetryMsg::SIGNAL;
sm.sig = {sig.type, sig.strength, sig.entry_price, sig.stop_loss, sig.take_profit, sig.ts_ns - sig.tick_ts_ns};
eng->tel_q.push(sm);
}
}
eng->latency.record(t0, now_ns());
if (++stats_counter % 50000 == 0)
{
eng->latency.print_histogram();
TelemetryMsg stm;
stm.kind = TelemetryMsg::STATS;
stm.stats = {eng->tick_count.load(), eng->signal_count.load(), eng->latency.avg_ns(), eng->latency.min_ns(), eng->latency.p99_ns(), eng->exec_sim.get_pnl(), eng->exec_sim.get_trades(), eng->exec_sim.get_wins()};
eng->tel_q.push(stm);
}
}
}
static void feed_thread(Engine *eng, FeedHandler* feed)
{
pin_thread(2);
feed->start(eng->tick_q, eng->running);
}
int main(int argc, char* argv[])
{
calibrate_tsc();
printf("[HFT] TSC frequency: %.3f GHz\n", rdtsc_hz / 1e9);
Engine eng;
if (!eng.mmap_pub.init())
{
fprintf(stderr, "[HFT] Mmap bind failed β check permissions for /tmp\n");
return 1;
}
printf("[HFT] Mmap telemetry β /tmp/hft_telemetry.mmap\n");
FeedHandler* feed = nullptr;
if (argc > 1) {
feed = new CsvReplayFeed(argv[1], 10);
printf("[HFT] Replaying CSV Feed: %s\n", argv[1]);
} else {
feed = new RandomFeed();
printf("[HFT] Running Random Feed\n");
}
pthread_t feed_tid, signal_tid, tel_tid;
pthread_create(&feed_tid, nullptr, [](void *a) -> void *
{
auto p = (std::pair<Engine*, FeedHandler*>*)a;
feed_thread(p->first, p->second);
return nullptr;
}, new std::pair<Engine*, FeedHandler*>(&eng, feed));
pthread_create(&signal_tid, nullptr, [](void *a) -> void *
{ signal_thread((Engine*)a); return nullptr; }, &eng);
pthread_create(&tel_tid, nullptr, [](void *a) -> void *
{
auto* p = (std::pair<TelQueue*, MmapPublisher*>*)a;
telemetry_thread_fn(p->first, p->second);
return nullptr; }, new std::pair<TelQueue *, MmapPublisher *>(&eng.tel_q, &eng.mmap_pub));
printf("[HFT] Engine running! Launch Python Dashboard now.\n");
for (;;)
{
sleep(1);
} // Main thread waits forever
return 0;
}