forked from rhinofi/coding_challenge_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDvfSimulator.h
More file actions
234 lines (190 loc) · 6.85 KB
/
DvfSimulator.h
File metadata and controls
234 lines (190 loc) · 6.85 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
// Starting point for Deversifi C++ coding challenge
// Please do not modify or distribute this file
// Requires C++ 17 features
#pragma once
#include <optional>
#include <time.h>
#include <iostream>
#include <string>
#include <vector>
#include <set>
class IDvfSimulator
{
public:
using OrderID = uint32_t;
// Simulates a call to https://api.deversifi.com/bfx/v2/book/tETHUSD/
// returning a structure representative of the JSON structure in the form
// [[price, volume], ...] with +ve volumes representing bids and -ve asks
using OrderBook = std::vector<std::pair<double, double>>;
virtual OrderBook GetOrderBook() noexcept = 0;
// Simulates a POST-only type call to https://api.deversifi.com/v1/trading/w/submitOrder
// returning an order ID if place was successful or a std::nullopt if not
// Note: +ve volumes represent a bid type and -ve ask type order
virtual std::optional<OrderID> PlaceOrder(double price, double amount) noexcept = 0;
// Simulates a POST-only type call to https://api.deversifi.com/v1/trading/w/cancelOrder
// indicating sucess / failure of cancel operation via bool return
virtual bool CancelOrder(OrderID oid) noexcept = 0;
};
class DvfSimulator : public IDvfSimulator
{
public:
virtual ~DvfSimulator() noexcept = default;
static IDvfSimulator* Create() noexcept
{
return static_cast<IDvfSimulator*>(new DvfSimulator());
}
virtual OrderBook GetOrderBook() noexcept override final
{
// Simulate a possible market movement since the last call
const bool noChange = GetRandom(1, 3) == 1;
if (!noChange)
{
static const auto maxShift = 20;
const double priceMovement = GetRandom(1, GetRandom(2, maxShift));
const bool moveLeft = GetRandom(1, 2) == 1 && m_bb > maxShift && m_ba > maxShift;
if (moveLeft)
{
m_bb -= priceMovement;
m_ba -= priceMovement;
}
else
{
m_bb += priceMovement;
m_ba += priceMovement;
}
std::cout << "BB " << m_bb << " BA " << m_ba << std::endl;
FillOrders();
}
OrderBook ob;
std::multiset<Order> placedBids(m_bids);
std::multiset<Order> placedAsks(m_asks);
static const auto ordersOnEachSide{ 10 };
for (uint32_t bids{ ordersOnEachSide }; bids > 0; --bids)
{
double price = m_bb - (bids * 5.0) + GetRandom(1, 4);
while (!placedBids.empty() && price > placedBids.begin()->price)
{
auto it = placedBids.begin();
ob.push_back({ it->price, it->amount });
placedBids.erase(placedBids.begin());
}
ob.push_back({ price, GetRandom(1, 10) * 0.25 });
}
for (uint32_t asks{ 0 }; asks < ordersOnEachSide; ++asks)
{
double price = m_ba + (asks * 5.0) + GetRandom(1, 4);
while (!placedAsks.empty() && price < placedAsks.begin()->price)
{
auto it = placedAsks.begin();
ob.push_back({ it->price, it->amount });
placedAsks.erase(placedAsks.begin());
}
ob.push_back({ price, GetRandom(1, 10) * -0.25 });
}
return ob;
}
virtual std::optional<OrderID> PlaceOrder(double price, double amount) noexcept override final
{
if ( (amount > 0.0 && price >= m_ba)
|| (amount < 0.0 && price <= m_bb))
{
// Assume order is place-only, since it would be filled it fails to place
std::cout
<< "Failed to place order id " << std::to_string(m_nextClientOid)
<< " @ " << price
<< " : " << amount
<< " " << (amount > 0.0 ? "BID" : "ASK")
<< " as this would result in an immediate fill!"
<< std::endl;
return {};
}
const bool isBid = amount > 0.0;
std::cout
<< "Placed order id " << std::to_string(m_nextClientOid)
<< " @ " << price
<< " : " << amount
<< " " << (isBid ? "BID" : "ASK") << std::endl;
if (isBid)
{
m_bids.insert(Order{ m_nextClientOid, price, amount });
}
else
{
m_asks.insert(Order{ m_nextClientOid, price, amount });
}
return m_nextClientOid++;
}
virtual bool CancelOrder(OrderID oid) noexcept override final
{
auto checkAndRemoveFn = [&oid](auto& col) -> bool
{
for (auto it = col.begin(); it != col.end(); ++it)
{
if (it->oid == oid)
{
std::cout
<< "Cancelled order id " << std::to_string(it->oid)
<< " @ " << it->price
<< " : " << it->amount
<< " " << (it->amount > 0 ? "BID" : "ASK") << std::endl;
col.erase(it);
return true;
}
}
return false;
};
if ( checkAndRemoveFn(m_bids)
|| checkAndRemoveFn(m_asks))
{
return true;
}
return false;
}
private:
DvfSimulator() noexcept
{
srand(static_cast<uint32_t>(time(nullptr)));
}
uint32_t GetRandom(uint32_t start, uint32_t end)
{
return rand() % (end - start + 1) + start;
}
void FillOrders()
{
while(!m_bids.empty() && m_bids.begin()->price > m_bb)
{
auto it = m_bids.begin();
std::cout
<< "Filled BID order id " << std::to_string(it->oid)
<< " @ " << it->price
<< " : " << it->amount
<< std::endl;
m_bids.erase(it);
}
while (!m_asks.empty() && m_asks.begin()->price < m_ba)
{
auto it = m_asks.begin();
std::cout
<< "Filled ASK order id " << std::to_string(it->oid)
<< " @ " << it->price
<< " : " << it->amount
<< std::endl;
m_asks.erase(it);
}
}
OrderID m_nextClientOid{ static_cast<OrderID>(time(nullptr)) };
double m_bb{ 200.0 };
double m_ba{ 210.0 };
struct Order
{
const OrderID oid;
const double price;
const double amount;
bool operator < (const Order& rhs) const
{
return price < rhs.price;
}
};
std::multiset<Order> m_bids;
std::multiset<Order> m_asks;
};