-
Notifications
You must be signed in to change notification settings - Fork 2
/
Printing.h
65 lines (58 loc) · 2.36 KB
/
Printing.h
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
#pragma once
namespace wwhrt {
// This file should not be modified.
class EventPrinter : public Subscriber {
public:
void onAdd(const CryptoAdd& add) final {
std::cout << add.seqNum << " " << add.symbol << " add " << add.orderId
<< ": " << (add.side == Side::Bid ? "buy " : "sell ")
<< add.size << " @ " << add.price << "\n";
}
void onUpdate(const CryptoUpdate& update) final {
std::cout << update.seqNum << " " << update.symbol << " update "
<< update.orderId << ": "
<< (update.side == Side::Bid ? "buy " : "sell ")
<< update.size << " @ " << update.price << "\n";
}
void onDelete(const CryptoDelete& delete_) final {
std::cout << delete_.seqNum << " " << delete_.symbol << " delete "
<< delete_.orderId << "\n";
}
};
class BookPrinter : public Subscriber {
public:
BookPrinter(BookBuilder* bb) : bookBuilder(bb) {}
void onAdd(const CryptoAdd& add) final { onAny(add.symbol, add.seqNum); }
virtual void onUpdate(const CryptoUpdate& update) final {
onAny(update.symbol, update.seqNum);
}
virtual void onDelete(const CryptoDelete& delete_) final {
onAny(delete_.symbol, delete_.seqNum);
}
void onAny(const Symbol &symbol, const uint32_t &seqNum) {
std::vector<BookBuilder::Order, Allocator<BookBuilder::Order>> bestBids =
bookBuilder->getBestBids(symbol);
std::vector<BookBuilder::Order, Allocator<BookBuilder::Order>> bestOffers =
bookBuilder->getBestOffers(symbol);
double bidSize = 0.0, askSize = 0.0;
for (auto& order : bestBids) {
bidSize += order.size;
}
for (auto& order : bestOffers) {
askSize += order.size;
}
std::cout << seqNum << " " << symbol << " best bid/ask: " << bidSize
<< " @ "
<< (bestBids.empty()
? std::numeric_limits<double>::quiet_NaN()
: bestBids[0].price)
<< " / " << askSize << " @ "
<< (bestOffers.empty()
? std::numeric_limits<double>::quiet_NaN()
: bestOffers[0].price)
<< "\n";
}
private:
BookBuilder* bookBuilder;
};
} // namespace wwhrt