-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
405 lines (342 loc) · 13.2 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class ClientDetails
{
public:
string orderID;
string clientOrderID;
string instrument;
int side;
int quantity;
double price;
ClientDetails *previous;
// Constructor
ClientDetails(string oID, string cID, string inst, int s, int qty, double pr)
{
orderID = oID;
clientOrderID = cID;
instrument = inst;
side = s;
quantity = qty;
price = pr;
previous = nullptr;
}
};
class ExecutionRepDetails
{
public:
string orderID;
string clientOrderID;
string instrument;
int side;
string executionStatus;
int quantity;
double price;
ExecutionRepDetails *next;
ExecutionRepDetails(string oID, string cID, string inst, int s, string status, int qty, double pr)
{
orderID = oID;
clientOrderID = cID;
instrument = inst;
side = s;
executionStatus = status;
quantity = qty;
price = pr;
next = nullptr;
}
};
ExecutionRepDetails *head = nullptr;
void addToExecutionRepDetailsLinkedList(ExecutionRepDetails *&head, ExecutionRepDetails *newNode)
{
if (head == nullptr)
{
head = newNode;
}
else
{
ExecutionRepDetails *temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
}
temp->next = newNode;
newNode->next = nullptr;
}
}
void writeToCSV(const std::string &filename, const ExecutionRepDetails *head)
{
std::ofstream file(filename); // Open the file
if (!file.is_open()) // Check if the file was opened successfully
{
std::cout << "Failed to open the file: " << filename << std::endl;
return;
}
// Write the CSV header
file << "Order ID,Client Order ID,Instrument,Side,Execution Status,Quantity,Price\n";
const ExecutionRepDetails *temp2 = head;
while (temp2 != nullptr)
{
file << temp2->orderID << ","
<< temp2->clientOrderID << ","
<< temp2->instrument << ","
<< temp2->side << ","
<< temp2->executionStatus << ","
<< temp2->quantity << ","
<< temp2->price << "\n";
temp2 = temp2->next;
}
file.close(); // Close the file
std::cout << "Data has been written to the file: " << filename << std::endl;
}
// Function to free the memory of the ExecutionRepDetails linked list
void freeLinkedList(ExecutionRepDetails *&head)
{
ExecutionRepDetails *temp2 = head;
while (temp2 != nullptr)
{
ExecutionRepDetails *current = temp2;
temp2 = temp2->next;
delete current;
}
head = nullptr;
}
// Stack class
class Stack
{
private:
ClientDetails *top;
public:
// Constructor
Stack()
{
top = nullptr;
}
// Push a new element to the top of the stack
void push(string oID, string cID, string inst, int s, int qty, double pr)
{
ClientDetails *newNode = new ClientDetails(oID, cID, inst, s, qty, pr);
newNode->previous = top;
top = newNode;
cout << "Client order ID " << cID << " pushed to the stack." << endl;
}
// Pop the top element from the stack
void pop()
{
if (top == nullptr)
{
cout << "The stack is empty." << endl;
return;
}
ClientDetails *temp = top;
top = top->previous;
string poppedOrderID = temp->clientOrderID;
delete temp;
cout << "Client order ID " << poppedOrderID << " popped from the stack." << endl;
}
// Remove an element from the stack based on client order ID, instrument, and quantity
void remove(string cID, string inst, int qty)
{
if (top == nullptr)
{
cout << "The stack is empty." << endl;
return;
}
ClientDetails *current = top;
ClientDetails *previous = nullptr;
while (current != nullptr)
{
if (current->clientOrderID == cID && current->instrument == inst && current->quantity == qty)
{
if (previous == nullptr)
{
top = current->previous;
}
else
{
previous->previous = current->previous;
}
cout << "Client order ID " << cID << " removed from the stack." << endl;
delete current;
return;
}
previous = current;
current = current->previous;
}
cout << "Element not found in the stack." << endl;
}
// Search for an element in the stack based on instrument and price
ClientDetails *search(string inst, double pr)
{
ClientDetails *current = top;
while (current != nullptr)
{
if (current->instrument == inst && current->price == pr)
{
return current;
}
current = current->previous;
}
return nullptr; // Element not found
}
// Print the elements of the stack
void printStack()
{
if (top == nullptr)
{
cout << "The stack is empty." << endl;
return;
}
cout << "Elements in the stack:" << endl;
ClientDetails *current = top;
while (current != nullptr)
{
cout << "Client Order ID: " << current->clientOrderID << endl;
cout << "Instrument: " << current->instrument << endl;
cout << "Side: " << current->side << endl;
cout << "Quantity: " << current->quantity << endl;
cout << "Price: " << current->price << endl;
cout << endl;
current = current->previous;
}
}
};
void ExchangeCalc()
{
Stack buyer;
Stack seller;
ifstream ip("order.csv");
if (!ip.is_open())
cout << "ERROR: File Open" << '\n';
int orderCount = 1;
while (ip.good())
{
string clientOrderID;
string instrument;
string sSide;
string sPrice;
string sQuantity;
getline(ip, clientOrderID, ',');
getline(ip, instrument, ',');
getline(ip, sSide, ',');
getline(ip, sQuantity, ',');
getline(ip, sPrice, '\n');
int side = 0;
int quantity = 0;
double price = 0.0;
try
{
side = stoi(sSide);
quantity = stoi(sQuantity);
price = stod(sPrice);
}
catch (const std::invalid_argument &e)
{
std::cerr << "Invalid price value: " << sPrice << std::endl;
// Handle the error, such as skipping the current line or exiting the function
continue; // Skip to the next iteration
}
string orderID = "ord" + to_string(orderCount);
orderCount++;
if (side == 1)
{
buyer.push(orderID, clientOrderID, instrument, side, quantity, price);
ClientDetails *temp = seller.search(instrument, price);
while (temp != nullptr && buyer.search(instrument, price)->quantity > 0)
{
if (temp != nullptr && instrument == temp->instrument && quantity == temp->quantity && price >= temp->price)
{
ExecutionRepDetails *buyerExecution = new ExecutionRepDetails(orderID, clientOrderID, instrument, side, "Fill", quantity, price);
ExecutionRepDetails *sellerExecution = new ExecutionRepDetails(temp->orderID, temp->clientOrderID, instrument, temp->side, "Fill", quantity, price);
addToExecutionRepDetailsLinkedList(head, buyerExecution);
addToExecutionRepDetailsLinkedList(head, sellerExecution);
buyer.pop();
seller.remove(temp->clientOrderID, temp->instrument, temp->quantity);
cout << "Buyer and seller orders filled completely." << endl;
}
// Check if the top buyer's quantity is less than the seller's quantity
else if (temp != nullptr && instrument == temp->instrument && quantity < temp->quantity && price >= temp->price)
{
ExecutionRepDetails *buyerExecution = new ExecutionRepDetails(orderID, clientOrderID, instrument, side, "Fill", quantity, price);
ExecutionRepDetails *sellerExecution = new ExecutionRepDetails(temp->orderID, temp->clientOrderID, instrument, temp->side, "Pfill", quantity, price);
addToExecutionRepDetailsLinkedList(head, buyerExecution);
addToExecutionRepDetailsLinkedList(head, sellerExecution);
temp->quantity -= quantity;
buyer.pop();
cout << "Buyer order partially filled." << endl;
}
else if (temp != nullptr && instrument == temp->instrument && quantity > temp->quantity && price >= temp->price)
{
ExecutionRepDetails *buyerExecution = new ExecutionRepDetails(orderID, clientOrderID, instrument, side, "Pfill", quantity, price);
ExecutionRepDetails *sellerExecution = new ExecutionRepDetails(temp->orderID, temp->clientOrderID, instrument, temp->side, "Fill", quantity, price);
seller.remove(temp->clientOrderID, temp->instrument, temp->quantity);
buyer.search(instrument, price)->quantity -= temp->quantity;
addToExecutionRepDetailsLinkedList(head, buyerExecution);
addToExecutionRepDetailsLinkedList(head, sellerExecution);
}
else
{
ExecutionRepDetails *buyerExecution = new ExecutionRepDetails(orderID, clientOrderID, instrument, side, "New", quantity, price);
addToExecutionRepDetailsLinkedList(head, buyerExecution);
}
}
}
else if (side == 2)
{
seller.push(orderID, clientOrderID, instrument, side, quantity, price);
ClientDetails *temp = buyer.search(instrument, price);
while (temp != nullptr && seller.search(instrument, price)->quantity > 0)
{
if (temp != nullptr && instrument == temp->instrument && quantity == temp->quantity && price <= temp->price)
{
ExecutionRepDetails *sellerExecution = new ExecutionRepDetails(orderID, clientOrderID, instrument, side, "Fill", quantity, price);
ExecutionRepDetails *buyerExecution = new ExecutionRepDetails(temp->orderID, temp->clientOrderID, instrument, temp->side, "Fill", quantity, price);
addToExecutionRepDetailsLinkedList(head, sellerExecution);
addToExecutionRepDetailsLinkedList(head, buyerExecution);
buyer.pop();
seller.remove(temp->clientOrderID, temp->instrument, temp->quantity && price <= temp->price);
cout << "Buyer and seller orders filled completely." << endl;
}
// Check if the top buyer's quantity is less than the seller's quantity
else if (temp != nullptr && instrument == temp->instrument && quantity < temp->quantity && price <= temp->price)
{
ExecutionRepDetails *sellerExecution = new ExecutionRepDetails(orderID, clientOrderID, instrument, side, "Fill", quantity, price);
ExecutionRepDetails *buyerExecution = new ExecutionRepDetails(temp->orderID, temp->clientOrderID, instrument, temp->side, "Pfill", quantity, price);
addToExecutionRepDetailsLinkedList(head, sellerExecution);
addToExecutionRepDetailsLinkedList(head, buyerExecution);
temp->quantity -= quantity;
buyer.pop();
cout << "Seller order partially filled." << endl;
}
else if (temp != nullptr && instrument == temp->instrument && quantity > temp->quantity)
{
ExecutionRepDetails *sellerExecution = new ExecutionRepDetails(orderID, clientOrderID, instrument, side, "Pfill", quantity, price);
ExecutionRepDetails *buyerExecution = new ExecutionRepDetails(temp->orderID, temp->clientOrderID, instrument, temp->side, "Fill", quantity, price);
seller.remove(temp->clientOrderID, temp->instrument, temp->quantity);
buyer.search(instrument, price)->quantity -= temp->quantity;
addToExecutionRepDetailsLinkedList(head, sellerExecution);
addToExecutionRepDetailsLinkedList(head, buyerExecution);
}
else
{
ExecutionRepDetails *sellerExecution = new ExecutionRepDetails(orderID, clientOrderID, instrument, side, "New", quantity, price);
addToExecutionRepDetailsLinkedList(head, sellerExecution);
}
}
}
}
string fileName = "execution_rep.csv";
writeToCSV(fileName, head);
freeLinkedList(head);
buyer.printStack();
seller.printStack();
}
// Example usage
int main()
{
ExchangeCalc();
return 0;
}