-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrade_execution.cpp
193 lines (178 loc) · 6.17 KB
/
trade_execution.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
#include "trade_execution.h"
#include "websocket_handler.h"
#include <iostream>
#include <stdexcept>
#include "latency_module.h"
std::atomic<int> TradeExecution::request_id{ 1 }; // Initialize static atomic counter
TradeExecution::TradeExecution(WebSocketHandler& websocket)
: websocket_(websocket) {}
TradeExecution::~TradeExecution() {
// Perform cleanup, such as clearing the subscribers
market_data_subscribers_.clear();
}
// Helper function to generate the next unique request ID
int TradeExecution::getNextRequestId() {
return request_id++;
}
// Method to handle incoming market data and notify subscribers
void TradeExecution::handleMarketData(const json& data) {
if (data.contains("symbol")) {
std::string symbol = data["symbol"];
if (market_data_subscribers_.count(symbol)) {
market_data_subscribers_[symbol](data); // Call the subscriber's callback
}
else {
std::cerr << "No subscribers for symbol: " << symbol << std::endl;
}
}
else {
std::cerr << "Invalid market data: Missing 'symbol'" << std::endl;
}
}
// Method called when new market data is received
void TradeExecution::onMarketDataReceived(const json& market_data) {
auto market_data_start = LatencyModule::start(); // Start the timer
handleMarketData(market_data);
LatencyModule::end(market_data_start, "Market Data Processing Latency"); // Measure latency
}
// Method to authenticate
json TradeExecution::authenticate(const std::string& client_id, const std::string& client_secret) {
try {
json auth_message = {
{"jsonrpc", "2.0"},
{"id", getNextRequestId()},
{"method", "public/auth"},
{"params", {
{"grant_type", "client_credentials"},
{"client_id", client_id},
{"client_secret", client_secret}
}}
};
websocket_.sendMessage(auth_message);
auto response = websocket_.readMessage();
if (!response.contains("result")) {
throw std::runtime_error("Authentication failed: " + response.dump());
}
return response["result"];
}
catch (const std::exception& e) {
std::cerr << "Error in authenticate: " << e.what() << std::endl;
throw; // Re-throw for higher-level handling
}
}
// Method to get available instruments
json TradeExecution::getInstruments(const std::string& currency, const std::string& kind, bool expired) {
try {
json request = {
{"jsonrpc", "2.0"},
{"id", getNextRequestId()},
{"method", "public/get_instruments"},
{"params", {{"currency", currency}, {"kind", kind}, {"expired", expired}}}
};
websocket_.sendMessage(request);
return websocket_.readMessage();
}
catch (const std::exception& e) {
std::cerr << "Error in getInstruments: " << e.what() << std::endl;
throw;
}
}
// Method to place a buy order
json TradeExecution::placeBuyOrder(const std::string& instrument_name, double amount, double price) {
try {
json buy_request = {
{"jsonrpc", "2.0"},
{"id", getNextRequestId()},
{"method", "private/buy"},
{"params", {
{"instrument_name", instrument_name},
{"amount", amount},
{"type", "limit"},
{"price", price}
}}
};
websocket_.sendMessage(buy_request);
return websocket_.readMessage();
}
catch (const std::exception& e) {
std::cerr << "Error in placeBuyOrder: " << e.what() << std::endl;
throw;
}
}
// Method to cancel an order
json TradeExecution::cancelOrder(const std::string& order_id) {
try {
json cancel_request = {
{"jsonrpc", "2.0"},
{"id", getNextRequestId()},
{"method", "private/cancel"},
{"params", {{"order_id", order_id}}}
};
websocket_.sendMessage(cancel_request);
return websocket_.readMessage();
}
catch (const std::exception& e) {
std::cerr << "Error in cancelOrder: " << e.what() << std::endl;
throw;
}
}
// Method to modify an order
json TradeExecution::modifyOrder(const std::string& order_id, double new_price, double new_amount) {
try {
json modify_request = {
{"jsonrpc", "2.0"},
{"id", getNextRequestId()},
{"method", "private/edit"},
{"params", {
{"order_id", order_id},
{"new_price", new_price},
{"new_amount", new_amount},
{"contracts", new_amount} // Add the 'contracts' parameter
}}
};
websocket_.sendMessage(modify_request);
return websocket_.readMessage();
}
catch (const std::exception& e) {
std::cerr << "Error in modifyOrder: " << e.what() << std::endl;
throw;
}
}
// Method to get the order book for a specific instrument
json TradeExecution::getOrderBook(const std::string& instrument_name) {
try {
json request = {
{"jsonrpc", "2.0"},
{"id", getNextRequestId()},
{"method", "public/get_order_book"},
{"params", {{"instrument_name", instrument_name}}}
};
websocket_.sendMessage(request);
return websocket_.readMessage();
}
catch (const std::exception& e) {
std::cerr << "Error in getOrderBook: " << e.what() << std::endl;
throw;
}
}
// Method to get current positions
json TradeExecution::getPositions() {
try {
json request = {
{"jsonrpc", "2.0"},
{"id", getNextRequestId()},
{"method", "private/get_positions"},
{"params", {}}
};
websocket_.sendMessage(request);
return websocket_.readMessage();
}
catch (const std::exception& e) {
std::cerr << "Error in getPositions: " << e.what() << std::endl;
throw;
}
}
// Add a subscriber for real-time market data updates
void TradeExecution::addMarketDataSubscriber(const std::string& symbol, std::function<void(const json&)> callback) {
market_data_subscribers_[symbol] = callback;
}