-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVwap.H
196 lines (168 loc) · 6.41 KB
/
Vwap.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
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
#pragma once
#include <cstdint>
#include <vector>
#include <unordered_map>
#include <string>
#include <array>
namespace trex{
using OrderId = uint64_t;
using LocateId = uint16_t;
using price_t = uint32_t;
using qty_t = uint32_t;
//constants for printing each trading hour
static const uint64_t EPSILON1 = 10000000000LL;
//hour in nanosecond
static const uint64_t HOUR = 3600000000000LL;
uint64_t CURRTIME = 0;
// 3 AM
uint64_t LASTPRINTED = HOUR * 3;
// bool indicating end of market hours.
bool END = false;
// using compiler lib __builtin_ functions for network order->little endian
uint64_t reinterpret8bytes(const char *w){
return __builtin_bswap64(*(reinterpret_cast<uint64_t*>(const_cast<char*>(&w[0]))));
}
uint64_t reinterpret6bytes(const char *w){
return (__builtin_bswap64(*reinterpret_cast<uint64_t*>(const_cast<char*>(&w[0]))) & (((1L << 48) - 1) << 16)) >> 16;
}
uint32_t reinterpret4bytes(const char *w){
return __builtin_bswap32(*(reinterpret_cast<uint32_t*>(const_cast<char*>(&w[0]))));
}
uint16_t reinterpret2bytes(const char *w){
return __builtin_bswap16(*(reinterpret_cast<uint16_t*>(const_cast<char*>(&w[0]))));
}
// Order structure holding price, qty and locate id of security.
// locateId is in the range(1, 1<<16)
struct Order{
price_t _price;
qty_t _qty;
LocateId _id;
Order(price_t price, qty_t qty, LocateId id) : _price(price), _qty(qty), _id(id){}
};
// indexed by LocateId
std::array<std::vector<Order>, (1 << 16)> attributedBuyOrders;
std::array<std::string, (1 << 16) > stockSymbols;
// Initial thought of using vector for allBuyOrders indexed by OrderId
// as the OrderIds are almost sequential (for lookups (spatial locality))
// Analysis: min max of keys in this data structure can be from
// (1000, 150,000,000) with only about 700,000 elements actually inside.
// Need a hash map with no speed loss with sequential keys.
// Google dense_hash_map fairs poorly on sequential keys.
// using gcc hash map for now.
std::unordered_map<OrderId, Order> allBuyOrders;
// CURRTIME is tracking time using messages and printing at
// each hour till market closes.
void systemMsg(const char* msg_start){
CURRTIME = reinterpret6bytes(msg_start + 5);
if(msg_start[11] == 'E')
END = true;
}
// populating locatedId -> stock symbol string mappings
void stockRelatedMsg(const char* msg_start){
LocateId locateId = reinterpret2bytes(msg_start + 1);
std::string symbol(msg_start + 11, 8);
stockSymbols[locateId] = symbol;
attributedBuyOrders[locateId] = std::vector<Order>();
attributedBuyOrders[locateId].reserve(1 << 5);
}
// only tracking 'B' orders
void addOrder(const char* msg_start){
LocateId locateId = reinterpret2bytes(msg_start + 1);
CURRTIME = reinterpret6bytes(msg_start + 5);
OrderId orderId = reinterpret8bytes(msg_start + 11);
if(msg_start[19] == 'B'){
allBuyOrders.emplace(orderId, Order(
reinterpret4bytes(msg_start + 32), // price
reinterpret4bytes(msg_start + 20), // qty
locateId));
}
}
// wait for static_if or implement own for code reduction
// orderExec and orderExecPrice are same
void orderExec(const char* msg_start){
LocateId locateId = reinterpret2bytes(msg_start + 1);
CURRTIME = reinterpret6bytes(msg_start + 5);
OrderId orderId = reinterpret8bytes(msg_start + 11);
qty_t qty = reinterpret4bytes(msg_start + 19);
auto pair = allBuyOrders.find(orderId);
if(pair == allBuyOrders.end())
return;
price_t price = pair->second._price;
if(qty == pair->second._qty){
allBuyOrders.erase(pair);
}else{
pair->second._qty -= qty;
}
attributedBuyOrders[locateId].push_back(Order(price, qty, locateId));
}
void orderExecPrice(const char* msg_start){
LocateId locateId = reinterpret2bytes(msg_start + 1);
CURRTIME = reinterpret6bytes(msg_start + 5);
OrderId orderId = reinterpret8bytes(msg_start + 11);
qty_t qty = reinterpret4bytes(msg_start + 19);
auto pair = allBuyOrders.find(orderId);
price_t price = reinterpret4bytes(msg_start + 32);
if(pair == allBuyOrders.end())
return ;
// non-printable to be handled at cross??
//if(msg_start[31] == 'N')
// return;
if(qty == pair->second._qty){
allBuyOrders.erase(pair);
}else{
pair->second._qty -= qty;
}
attributedBuyOrders[locateId].push_back(Order(price, qty, locateId));
}
void orderCancel(const char* msg_start){
//CURRTIME = reinterpret6bytes(msg_start + 5);
OrderId orderId = reinterpret8bytes(msg_start + 11);
qty_t cancelledQty = reinterpret4bytes(msg_start + 19);
auto pair = allBuyOrders.find(orderId);
if(pair == allBuyOrders.end())
return ;
pair->second._qty -= cancelledQty;
if(pair->second._qty == 0)
allBuyOrders.erase(pair);
}
void orderDelete(const char* msg_start){
//CURRTIME = reinterpret6bytes(msg_start + 5);
OrderId orderId = reinterpret8bytes(msg_start + 11);
allBuyOrders.erase(orderId);
}
void orderReplace(const char* msg_start){
LocateId locateId = reinterpret2bytes(msg_start + 1);
//CURRTIME = reinterpret6bytes(msg_start + 5);
OrderId oldId = reinterpret8bytes(msg_start + 11);
OrderId newId = reinterpret8bytes(msg_start + 19);
qty_t newQty = reinterpret4bytes(msg_start + 27);
price_t newPrice = reinterpret4bytes(msg_start + 31);
auto pair = allBuyOrders.find(oldId);
if(pair == allBuyOrders.end())
return;
allBuyOrders.erase(pair);
allBuyOrders.emplace(newId, Order(newPrice, newQty, locateId));
}
// [TODO]: clarify:
// "Since Trade Messages do not affect the book, however, they may be ignored by
// firms just looking to build and track the Nasdaq execution system display."
void nonCrossTrade(const char* msg_start){
/*LocateId locateId = reinterpret2bytes(msg_start + 1);
CURRTIME = reinterpret6bytes(msg_start + 5);
qty_t qty = reinterpret4bytes(msg_start + 20);
price_t price = reinterpret4bytes(msg_start + 32);
attributedBuyOrders[locateId].push_back(Order(price, qty, locateId));*/
}
// [TODO]: clarify
void crossTrade(const char* msg_start){
}
// [TODO]: clarify
// "If a firm is only using the ITCH feed to build a book, however, it may ignore
// these messages as they have no impact on the current book."
void brokenTrade(const char* msg_start){
}
std::string formatTime(uint64_t currTime){
int hour = currTime/ HOUR;
return std::to_string(hour);
}
};//trex