Repository navigation
Expand file tree
/
Copy pathbasic.c
More file actions
92 lines (80 loc) · 2.83 KB
/
Copy pathbasic.c
File metadata and controls
92 lines (80 loc) · 2.83 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
/**
* A minimal walk-through of the C Book API.
*
* Build & run (from the repo root):
* cmake -S . -B build && cmake --build build && ./build/hftlob_example
*/
#include <math.h>
#include <stdio.h>
#include "hftlob.h"
static void
printTop(const Book *book){
Limit *bid = getBestBid(book);
Limit *ask = getBestAsk(book);
if(bid != NULL){
printf(" best bid: %.2f x %.0f (%d orders)\n",
bid->limitPrice, bid->size, bid->orderCount);
}
else{
printf(" best bid: -\n");
}
if(ask != NULL){
printf(" best ask: %.2f x %.0f (%d orders)\n",
ask->limitPrice, ask->size, ask->orderCount);
}
else{
printf(" best ask: -\n");
}
}
static void
printFill(const Order *resting, double shares, double price, void *ctx){
(void)ctx;
printf(" fill: %.0f @ %.2f against order %llu\n",
shares, price, (unsigned long long)resting->id);
}
int
main(void){
Book *book = createBook();
if(book == NULL){
return 1;
}
/* The order sequence from issue #17. */
printf("add 5879: sell 550 @ 26.00\n");
addOrderToBook(book, 5879, HFTLOB_SELL, 550, 26.0);
printTop(book);
printf("cancel 5879\n");
removeOrderFromBook(book, 5879);
printTop(book);
printf("add 5880: sell 80 @ 38.00, add 5881: sell 100 @ 22.70\n");
addOrderToBook(book, 5880, HFTLOB_SELL, 80, 38.0);
addOrderToBook(book, 5881, HFTLOB_SELL, 100, 22.7);
printTop(book);
/* Some bids, two at the same price: they queue first-in, first-out. */
printf("add bids: 1 x 50 @ 21.00, 2 x 20 @ 22.00, 3 x 30 @ 22.00\n");
addOrderToBook(book, 1, HFTLOB_BUY, 50, 21.0);
addOrderToBook(book, 2, HFTLOB_BUY, 20, 22.0);
addOrderToBook(book, 3, HFTLOB_BUY, 30, 22.0);
printTop(book);
printf(" oldest order at best bid: %llu\n",
(unsigned long long)getBestBid(book)->tailOrder->id);
/* A size change keeps queue position; a price change re-queues. */
printf("replace 2: 15 @ 22.00 (size only), then 3: 30 @ 22.50 (new price)\n");
replaceOrderInBook(book, 2, 15, 22.0);
replaceOrderInBook(book, 3, 30, 22.5);
printTop(book);
printf("volume resting at 22.00: %.0f\n",
getLimitFromBook(book, HFTLOB_BUY, 22.0)->size);
/* An incoming sell for 40, no lower than 22.00: takes 30 @ 22.50,
* then 10 of the 15 resting @ 22.00. */
printf("execute: sell 40, limit 22.00\n");
double filled = executeInBook(book, HFTLOB_SELL, 40, 22.0, printFill, NULL);
printf(" filled %.0f\n", filled);
printTop(book);
/* A market buy for 500 takes all asks; the rest is not rested. */
printf("execute: market buy 500\n");
filled = executeInBook(book, HFTLOB_BUY, 500, INFINITY, printFill, NULL);
printf(" filled %.0f\n", filled);
printTop(book);
freeBook(book);
return 0;
}