-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathderibit_trader.cpp
More file actions
193 lines (168 loc) · 7.74 KB
/
deribit_trader.cpp
File metadata and controls
193 lines (168 loc) · 7.74 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
#include "api_credentials.h"
#include "websocket_handler.h"
#include "trade_execution.h"
#include "latency_module.h"
#include <iostream>
#include <string>
#include <exception>
#include <memory>
#include <unordered_map>
#include <future>
#include <vector>
#include <thread>
#include <immintrin.h> // For SIMD Prepares for potential SIMD optimizations (not yet implemented in this code).
//SIMD can be used for parallel processing of repetitive tasks like order book analysis.
//CPU Optimization
void executeTrades() {
try {
// Initialize WebSocket connection
WebSocketHandler websocket("test.deribit.com", "443", "/ws/api/v2");
websocket.connect();
// Initialize trading operations
// std::unique_ptr has minimal overhead and is generally faster than (optimization)
// manual memory management with new and delete.
auto trade = std::make_unique<TradeExecution>(websocket);
// Authenticate
json auth_response = trade->authenticate(CLIENT_ID, CLIENT_SECRET);
std::cout << "Auth Response: " << auth_response.dump(4) << std::endl;
// Use unordered_map to cache order responses (optimization)
// Replace linear search structures with
// std::unordered_map for faster access
std::unordered_map<std::string, json> order_cache;
while (true) {
std::string instrument_name, order_id;
double amount, price;
// Display menu to the user
std::cout << "\n--- Trading Menu ---\n";
std::cout << "1. Place Order\n";
std::cout << "2. Cancel Order\n";
std::cout << "3. Modify Order\n";
std::cout << "4. Get Order Book\n";
std::cout << "5. View Current Positions\n";
std::cout << "6. Exit\n";
std::cout << "Enter your choice: ";
int choice;
std::cin >> choice;
auto loop_start = LatencyModule::start(); // Start the timer for end-to-end latency
if (choice == 6) {
std::cout << "Exiting trading application.\n";
break;
}
switch (choice) {
case 1: { // Place Order
std::cout << "Enter instrument name (e.g., BTC-PERPETUAL): ";
std::cin >> instrument_name;
std::cout << "Enter amount: ";
std::cin >> amount;
std::cout << "Enter price: ";
std::cin >> price;
try {
auto order_future = std::async(std::launch::async, [&]() {
auto order_start = LatencyModule::start();
json buy_response = trade->placeBuyOrder(instrument_name, amount, price);
LatencyModule::end(order_start, "Order Placement");
return buy_response;
});
//Optimization
// Wait for the order result asynchronously (non-blocking until we get the result)
//used std::async for the functions placeBuyOrder, cancelOrder, and modifyOrder to
// make these network requests asynchronous. This allows the application to continue
// running while waiting for responses from the server. This reduces the time spent
// in blocking I/O operations.
//The use of std::async enables parallel execution of tasks like placing and canceling
// orders. This means that the main thread can continue interacting with the user
// while the network operations are handled in the background.
json buy_response = order_future.get();
std::cout << "Order Response: " << buy_response.dump(4) << std::endl;
}
catch (const std::exception& e) {
std::cerr << "Error placing order: " << e.what() << std::endl;
}
break;
}
case 2: { // Cancel Order
std::cout << "Enter order ID to cancel: ";
std::cin >> order_id;
try {
auto cancel_future = std::async(std::launch::async, [&]() {
auto cancel_start = LatencyModule::start();
json cancel_response = trade->cancelOrder(order_id);
LatencyModule::end(cancel_start, "Cancel Order");
return cancel_response;
});
json cancel_response = cancel_future.get();
std::cout << "Cancel Response: " << cancel_response.dump(4) << std::endl;
}
catch (const std::exception& e) {
std::cerr << "Error cancelling order: " << e.what() << std::endl;
}
break;
}
case 3: { // Modify Order
std::cout << "Enter order ID to modify: ";
std::cin >> order_id;
std::cout << "Enter new price: ";
std::cin >> price;
std::cout << "Enter new amount: ";
std::cin >> amount;
try {
auto modify_future = std::async(std::launch::async, [&]() {
auto modify_start = LatencyModule::start();
json modify_response = trade->modifyOrder(order_id, price, amount);
LatencyModule::end(modify_start, "Modify Order");
return modify_response;
});
json modify_response = modify_future.get();
std::cout << "Modify Response: " << modify_response.dump(4) << std::endl;
}
catch (const std::exception& e) {
std::cerr << "Error modifying order: " << e.what() << std::endl;
}
break;
}
case 4: { // Get Order Book
std::cout << "Enter instrument name to view order book (e.g., BTC-PERPETUAL): ";
std::cin >> instrument_name;
try {
auto order_book_start = LatencyModule::start();
json order_book = trade->getOrderBook(instrument_name);
LatencyModule::end(order_book_start, "Order Book Fetch");
std::cout << "Order Book: " << order_book.dump(4) << std::endl;
}
catch (const std::exception& e) {
std::cerr << "Error fetching order book: " << e.what() << std::endl;
}
break;
}
case 5: { // View Current Positions
try {
json positions = trade->getPositions();
std::cout << "Current Positions: " << positions.dump(4) << std::endl;
}
catch (const std::exception& e) {
std::cerr << "Error fetching positions: " << e.what() << std::endl;
}
break;
}
default:
std::cout << "Invalid choice. Please try again.\n";
break;
}
LatencyModule::end(loop_start, "End-to-End Trading Loop Latency"); // Measure latency for the trading loop
}
// Close connection
websocket.close();
}
catch (const std::exception& e) {
std::cerr << "Error in executeTrades: " << e.what() << std::endl;
}
}
int main() {
try {
executeTrades();
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}