-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL32.cpp
More file actions
469 lines (395 loc) · 16.6 KB
/
Copy pathL32.cpp
File metadata and controls
469 lines (395 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
// ============================================================
// L32: File I/O and Serialization
// ============================================================
// WHAT: Reading and writing files with <fstream>: text and
// binary modes, seeking, and memory-mapped files.
// WHY (TRADING): Every trading system needs file I/O for:
// - Trade logs (every fill, rejection, and risk event)
// - Config loading at startup (symbols, risk limits, params)
// - Market data replay (reading tick files for backtesting)
// - Binary serialization (writing order books to disk fast)
// Binary I/O is critical: writing 1M ticks as binary is ~10x
// faster than writing them as text because no formatting cost.
// PHASE: Modern C++
// ============================================================
/*
CONCEPT OVERVIEW:
TEXT vs BINARY MODE:
Text mode: numbers stored as strings ("182.50\n") — human readable, slow
Binary mode: numbers stored as raw bytes (8 bytes for double) — fast, compact
Open with std::ios::binary to get binary mode.
Rule: logs are text (grep-able); market data files are binary (fast replay).
FSTREAM CLASSES:
std::ifstream — read from file (input)
std::ofstream — write to file (output)
std::fstream — read and write
Open modes (combine with |):
std::ios::in — read
std::ios::out — write (truncates existing file)
std::ios::app — append (don't truncate)
std::ios::binary — binary mode
std::ios::trunc — explicitly truncate on open
std::ios::ate — start at end of file
CHECKING SUCCESS:
if (!file) { ... } — stream is in bad state (failed to open, etc.)
file.good() — true if all OK
file.fail() — true if last operation failed
file.eof() — true if end of file
file.is_open() — true if currently open
Always check after open AND after each critical read.
POSITIONING (SEEK):
file.seekg(offset, whence) — seek read head (g = get)
file.seekp(offset, whence) — seek write head (p = put)
file.tellg() — current read position (returns std::streampos)
file.tellp() — current write position
whence:
std::ios::beg — from beginning
std::ios::cur — from current position
std::ios::end — from end of file
BINARY READ/WRITE:
file.write(reinterpret_cast<const char*>(&val), sizeof(val)) — write T as raw bytes
file.read (reinterpret_cast<char*>(&val), sizeof(val)) — read raw bytes into T
MEMORY-MAPPED FILES (mmap — Linux):
OS maps the file into virtual address space.
Reading: just read from the pointer — OS fetches pages lazily.
Writing: write to the pointer — OS flushes to disk (msync or on close).
LATENCY: For hot-path tick replay, mmap avoids read() syscall overhead.
On Windows: CreateFileMapping / MapViewOfFile (similar concept).
For cross-platform code: use a simple binary file with ifstream.
TRADING USE CASE:
// Write trade log (text, append mode):
std::ofstream log("trades.csv", std::ios::app);
log << timestamp_ns << "," << symbol << "," << price << "\n";
// Write tick data (binary):
std::ofstream ticks("data.bin", std::ios::binary);
for (const Tick& t : buffer) {
ticks.write(reinterpret_cast<const char*>(&t), sizeof(Tick));
}
// Read tick data (binary replay):
Tick t;
while (file.read(reinterpret_cast<char*>(&t), sizeof(Tick))) {
strategy.on_tick(t);
}
COMMON MISTAKES:
- Not checking if the file opened successfully before reading/writing
- Forgetting std::ios::binary — text mode inserts \r\n on Windows, corrupts binary data
- Using float/double in binary serialization that must be read on different hardware
(same endianness, same sizeof — usually fine on x86, but verify)
- Reading structs with pointers from disk — the pointers are meaningless after reload
- Flushing every line with endl — use "\n" and flush explicitly when needed
- Opening a log with ios::out and forgetting ios::app — you'll truncate your log!
*/
#include <iostream>
#include <fstream>
#include <sstream> // std::ostringstream for in-memory formatting
#include <string>
#include <vector>
#include <cstdint>
#include <cstring> // memset
#include <chrono> // for fake timestamps
// ============================================================
// TYPES
// ============================================================
// POD (Plain Old Data) struct — safe to binary serialize
// All fields are fixed-width, no pointers, no virtual functions
struct Tick {
uint64_t timestamp_ns;
int64_t bid_price; // ticks (int64 * 10000 = cents * 100)
int64_t ask_price;
int32_t bid_qty;
int32_t ask_qty;
char symbol[8]; // fixed-size char array (NOT std::string — no pointer)
};
// Fill record for trade log
struct Fill {
uint64_t timestamp_ns;
int64_t price; // ticks
int32_t qty;
bool is_buy;
char symbol[8];
uint64_t order_id;
};
// ============================================================
// TRADE LOG — text append, CSV format
// ============================================================
class TradeLog {
public:
explicit TradeLog(const std::string& path) {
// ios::app — append to existing file, don't truncate
file_.open(path, std::ios::out | std::ios::app);
if (!file_) {
throw std::runtime_error("Cannot open trade log: " + path);
}
// Write header only if file was just created (file is at position 0)
if (file_.tellp() == std::streampos(0)) {
file_ << "timestamp_ns,symbol,price,qty,side,order_id\n";
}
}
void record(const Fill& f) {
// Use "\n" not std::endl — endl flushes, which is slow in the hot path
file_ << f.timestamp_ns << ","
<< f.symbol << ","
<< f.price / 10000.0 << ","
<< f.qty << ","
<< (f.is_buy ? "BUY" : "SELL") << ","
<< f.order_id << "\n";
}
// Explicit flush when we want to guarantee disk write
void flush() { file_.flush(); }
~TradeLog() {
if (file_.is_open()) {
file_.flush();
file_.close();
}
}
private:
std::ofstream file_;
};
// ============================================================
// BINARY TICK FILE — write and read back
// ============================================================
class TickFileWriter {
public:
explicit TickFileWriter(const std::string& path) {
// ios::binary — raw bytes, no newline translation
// ios::trunc — start fresh each time
file_.open(path, std::ios::out | std::ios::binary | std::ios::trunc);
if (!file_) {
throw std::runtime_error("Cannot open tick file for writing: " + path);
}
}
void write(const Tick& t) {
// reinterpret_cast<const char*>(&t) — treat the struct as a byte array
file_.write(reinterpret_cast<const char*>(&t), sizeof(Tick));
++written_;
}
void write_batch(const std::vector<Tick>& ticks) {
if (ticks.empty()) return;
// Write entire vector as contiguous bytes — single syscall
file_.write(reinterpret_cast<const char*>(ticks.data()),
static_cast<std::streamsize>(ticks.size() * sizeof(Tick)));
written_ += static_cast<int>(ticks.size());
}
int ticks_written() const { return written_; }
~TickFileWriter() {
if (file_.is_open()) file_.close();
}
private:
std::ofstream file_;
int written_ = 0;
};
class TickFileReader {
public:
explicit TickFileReader(const std::string& path) {
file_.open(path, std::ios::in | std::ios::binary);
if (!file_) {
throw std::runtime_error("Cannot open tick file: " + path);
}
// Determine file size using seek
file_.seekg(0, std::ios::end);
auto file_size = file_.tellg();
file_.seekg(0, std::ios::beg);
tick_count_ = static_cast<int>(file_size / sizeof(Tick));
}
bool read_next(Tick& t) {
return static_cast<bool>(
file_.read(reinterpret_cast<char*>(&t), sizeof(Tick)));
}
// Read all ticks into a vector at once
std::vector<Tick> read_all() {
file_.seekg(0, std::ios::beg);
std::vector<Tick> ticks(tick_count_);
file_.read(reinterpret_cast<char*>(ticks.data()),
static_cast<std::streamsize>(tick_count_ * sizeof(Tick)));
return ticks;
}
int tick_count() const { return tick_count_; }
private:
std::ifstream file_;
int tick_count_ = 0;
};
// ============================================================
// CONFIG READER — text file, key=value format
// ============================================================
struct Config {
std::string exchange_host;
int port = 9000;
int32_t max_position = 1000;
double max_loss = 50000.0;
};
Config read_config(const std::string& path) {
std::ifstream file(path);
if (!file) {
throw std::runtime_error("Config file not found: " + path);
}
Config cfg;
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') continue; // skip comments and blanks
auto eq = line.find('=');
if (eq == std::string::npos) continue;
std::string key = line.substr(0, eq);
std::string value = line.substr(eq + 1);
if (key == "exchange_host") { cfg.exchange_host = value; }
else if (key == "port") { cfg.port = std::stoi(value); }
else if (key == "max_position") { cfg.max_position = std::stoi(value); }
else if (key == "max_loss") { cfg.max_loss = std::stod(value); }
}
return cfg;
}
// ============================================================
// MAIN
// ============================================================
int main() {
const std::string temp_dir = ""; // write to current directory for demo
// -------------------------------------------------------
// TEXT FILE — trade log
// -------------------------------------------------------
std::cout << "=== Text file: trade log ===\n";
{
const std::string log_path = "trades_demo.csv";
try {
TradeLog log(log_path);
Fill fills[] = {
{1000000000ULL, 1825500, 100, true, "AAPL\0\0\0", 1001},
{1000000100ULL, 1825500, 50, false, "AAPL\0\0\0", 1002},
{1000000200ULL, 1826000, 75, false, "AAPL\0\0\0", 1003},
};
for (const auto& f : fills) {
log.record(f);
std::cout << " Logged fill: " << f.symbol
<< " $" << f.price / 10000.0 << "\n";
}
log.flush();
std::cout << " Trade log written: " << log_path << "\n";
}
catch (const std::exception& e) {
std::cout << " [Log error] " << e.what() << "\n";
}
}
// -------------------------------------------------------
// BINARY FILE — tick data
// -------------------------------------------------------
std::cout << "\n=== Binary file: tick data ===\n";
{
const std::string tick_path = "ticks_demo.bin";
// Write ticks
{
TickFileWriter writer(tick_path);
std::vector<Tick> batch;
for (int i = 0; i < 5; ++i) {
Tick t{};
t.timestamp_ns = 1000000000ULL + i * 100;
t.bid_price = 1825000 + i * 100;
t.ask_price = 1825100 + i * 100;
t.bid_qty = 200;
t.ask_qty = 150;
std::memset(t.symbol, 0, sizeof(t.symbol));
std::memcpy(t.symbol, "AAPL", 4);
batch.push_back(t);
}
writer.write_batch(batch);
std::cout << " Wrote " << writer.ticks_written() << " ticks to " << tick_path
<< " (" << writer.ticks_written() * sizeof(Tick) << " bytes)\n";
}
// Read back
{
TickFileReader reader(tick_path);
std::cout << " File contains " << reader.tick_count() << " ticks\n";
auto ticks = reader.read_all();
std::cout << " Replaying:\n";
for (const auto& t : ticks) {
std::cout << " ts=" << t.timestamp_ns
<< " bid=$" << t.bid_price / 10000.0
<< " ask=$" << t.ask_price / 10000.0 << "\n";
}
}
}
// -------------------------------------------------------
// TEXT FILE — config read
// -------------------------------------------------------
std::cout << "\n=== Text file: config ===\n";
{
// Write a sample config file
const std::string cfg_path = "trading_demo.conf";
{
std::ofstream f(cfg_path);
f << "# Trading system config\n"
<< "exchange_host=10.0.0.1\n"
<< "port=4001\n"
<< "max_position=500\n"
<< "max_loss=25000.0\n";
}
try {
Config cfg = read_config(cfg_path);
std::cout << " exchange_host: " << cfg.exchange_host << "\n";
std::cout << " port: " << cfg.port << "\n";
std::cout << " max_position: " << cfg.max_position << "\n";
std::cout << " max_loss: $" << cfg.max_loss << "\n";
}
catch (const std::exception& e) {
std::cout << " Config error: " << e.what() << "\n";
}
}
// -------------------------------------------------------
// SEEK — jump to specific tick in binary file
// -------------------------------------------------------
std::cout << "\n=== Seeking in binary file ===\n";
{
const std::string tick_path = "ticks_demo.bin";
std::ifstream file(tick_path, std::ios::in | std::ios::binary);
if (file) {
// Jump directly to tick #3 (0-indexed)
int target_tick = 3;
file.seekg(target_tick * sizeof(Tick), std::ios::beg);
Tick t{};
if (file.read(reinterpret_cast<char*>(&t), sizeof(Tick))) {
std::cout << " Tick #" << target_tick
<< ": ts=" << t.timestamp_ns
<< " bid=$" << t.bid_price / 10000.0 << "\n";
}
// Get file size via seek to end
file.seekg(0, std::ios::end);
std::streampos file_size = file.tellg();
std::cout << " File size: " << static_cast<int64_t>(file_size)
<< " bytes (" << static_cast<int64_t>(file_size) / sizeof(Tick)
<< " ticks)\n";
}
}
// -------------------------------------------------------
// IN-MEMORY FORMATTING: ostringstream
// -------------------------------------------------------
std::cout << "\n=== ostringstream: build string before writing ===\n";
{
// When you want to format a string without writing to disk yet
// (e.g., to send over the network or pass to a logger queue)
std::ostringstream oss;
oss << "FIX|35=D|49=CLIENT|56=EXCHANGE|"
<< "55=AAPL|38=100|44=182.50|54=1|";
std::string fix_msg = oss.str();
std::cout << " FIX message: " << fix_msg << "\n";
std::cout << " Length: " << fix_msg.size() << " bytes\n";
}
return 0;
/*
TRADING CONTEXT EXAMPLE:
Binary tick replay for backtesting — reads at memory bus speed:
// At startup: memory-map the entire tick file
// (on Linux; Windows uses MapViewOfFile)
int fd = open("market_data_2024.bin", O_RDONLY);
fstat(fd, &sb);
const Tick* ticks = static_cast<const Tick*>(
mmap(nullptr, sb.st_size, PROT_READ, MAP_SHARED, fd, 0));
int tick_count = sb.st_size / sizeof(Tick);
// Replay: no syscalls per tick — OS handles paging automatically
for (int i = 0; i < tick_count; ++i) {
strategy.on_tick(ticks[i]); // reads directly from mapped memory
}
munmap((void*)ticks, sb.st_size);
close(fd);
// RESULT: replaying 1 billion ticks ≈ 30 seconds
// vs fstream read loop: ≈ 4 minutes
// For Windows cross-platform use, stick with std::fstream binary
// and call read_all() at startup — load into RAM once, replay from vector.
*/
}