-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
202 lines (168 loc) · 7.47 KB
/
main.cpp
File metadata and controls
202 lines (168 loc) · 7.47 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
#include "delta_exchange/api/product/product.hpp"
#include "market_state/market_state.hpp"
#include "processes/feed.hpp"
#include "processes/oms.hpp"
#include "processes/strategy.hpp"
#include "ipc/shm.hpp"
#include "config/env.hpp"
#include "latency/clock.hpp"
#include <sys/wait.h>
#include <signal.h>
#include <atomic>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <csignal>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#if defined(__linux__)
#include <sys/mman.h>
#endif
// Pin all heap + stack pages into RAM and prevent any future allocations from
// being swapped or demand-paged. Hot paths get a 200µs-1ms page-fault tail
// otherwise — invisible at p50, dominant at p99.9. EPERM on macOS / non-root
// is logged and ignored (best-effort; the rest of the run is still meaningful).
static void try_mlockall() {
#if defined(__linux__)
if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
std::fprintf(stderr, "[main] WARN mlockall failed: %s "
"(need CAP_IPC_LOCK / root, or raise RLIMIT_MEMLOCK)\n",
std::strerror(errno));
} else {
std::fprintf(stderr, "[main] mlockall(MCL_CURRENT|MCL_FUTURE) OK\n");
}
#else
std::fprintf(stderr, "[main] mlockall skipped (non-Linux)\n");
#endif
}
// Signal handlers only receive int sig — store PIDs in process-global state.
static std::vector<pid_t> g_child_pids;
static void on_shutdown_signal(int sig)
{
(void)sig;
for (pid_t pid : g_child_pids) {
if (pid > 0)
kill(pid, SIGTERM);
}
}
static void print_product(const Product& p) {
std::cout << "┌─ Product ─────────────────────────\n"
<< "│ symbol: " << p.symbol << "\n"
<< "│ index_symbol: " << p.index_symbol << "\n"
<< "│ exchange_id: " << p.exchange_id << "\n"
<< "│ contract_type: " << (p.contract_type == Product::ContractType::Perpetual ? "perpetual" :
p.contract_type == Product::ContractType::Futures ? "futures" : "options") << "\n"
<< "│ tick_size: " << p.tick_size << "\n"
<< "│ contract_value: " << p.contract_value << "\n"
<< "│ impact_size: " << p.impact_size << "\n"
<< "│ pos_size_limit: " << p.position_size_limit << "\n"
<< "│ taker_fee: " << p.taker_commission_rate << "\n"
<< "│ maker_fee: " << p.maker_commission_rate << "\n"
<< "│ initial_margin: " << p.initial_margin << "\n"
<< "│ maint_margin: " << p.maintenance_margin << "\n"
<< "│ default_leverage: " << p.default_leverage << "\n"
<< "│ price_band: " << p.price_band << "%\n"
<< "└───────────────────────────────────\n";
}
int main(int argc, char** argv)
{
// Load .env from cwd (no error if missing — real shell env still works).
// Shell env wins over .env values (overwrite=0 inside the loader).
env::load_file(".env");
// Pin pages BEFORE fork so children inherit the locked state. Doing it
// post-fork would force each child to lock its own COW pages individually.
try_mlockall();
// Calibrate the latency clock ONCE in the parent, BEFORE fork(). Each
// child inherits the calibrated ns_per_cycle via COW; otherwise every
// Span records 0 ns and percentiles flatline at 1. (See clock.hpp.)
latency::calibrate();
const std::string delta_rest_host = env::get_or("DELTA_REST_HOST", "api.india.delta.exchange");
const std::string delta_ws_public_host = env::get_or("DELTA_WS_PUBLIC_HOST", "public-socket.india.delta.exchange");
const std::string delta_ws_private_host = env::get_or("DELTA_WS_PRIVATE_HOST", "socket.india.delta.exchange");
ProductTable products;
std::vector<std::string> allowed_products {"BTCUSD", "ETHUSD", "SOLUSD"};
DeltaRestClient rest(delta_rest_host.c_str(), "", "", products);
rest.connect();
for (const std::string& sym : allowed_products) {
try {
products.add(fetch_product(rest, sym));
print_product(products[products.count - 1]);
} catch (const std::exception& e) {
std::cerr << "[init] failed to fetch " << sym << ": " << e.what()
<< "\n";
return 1;
}
}
ProductGroup btc_group {"BTC", {products.idfromSymbol("BTCUSD")}};
// ProductGroup sol_group {"SOL", {products.idfromSymbol("SOLUSD")}};
// ProductGroup eth_group {"ETH", {products.idfromSymbol("ETHUSD")}};
auto shm_trading_bot = ShmOwner<SharedState>::create("/trading_bot_state");
SharedState* state = shm_trading_bot.get();
FeedConfig feed_cfg_ {
.host = delta_ws_public_host,
.port = 443,
.path = "/",
};
latency::InfluxWriter::Config influx_config {
.host = env::get_or ("INFLUX_HOST", "127.0.0.1"),
.port = env::get_int_or("INFLUX_PORT", 8086),
.org = env::require ("INFLUX_ORG"),
.bucket = env::require ("INFLUX_BUCKET"),
.token = env::require ("INFLUX_TOKEN"),
};
FeedProcess<DeltaWebsocketClient> btc_feed {
products, btc_group, state, feed_cfg_,
influx_config, core::Venue::Delta,
};
// FeedProcess<DeltaWebsocketClient> eth_feed {products, eth_group, state, cfg};
// ── OMS (private session) ─────────────────────────────────────────────
// Credentials come from .env via DELTA_API_KEY / DELTA_API_SECRET.
// Host is testnet or prod depending on DELTA_WS_PRIVATE_HOST.
OMSConfig oms_cfg_ {
.ws_host = delta_ws_private_host,
.rest_host = delta_rest_host,
.port = 443,
.path = "/",
.api_key = env::require("DELTA_API_KEY"),
.api_secret = env::require("DELTA_API_SECRET"),
};
OmsProcess<DeltaOMSWebsocketClient, DeltaRestClient> oms_delta {products, btc_group, influx_config, oms_cfg_, state, ExecMode::Shadow, core::Venue::Delta};
// ── Strategy (reads market_state SHM, emits ExecutionIntents) ─────────────
Strategy strategy_delta {products, btc_group, influx_config, state, core::Venue::Delta};
std::vector<FeedProcess<DeltaWebsocketClient>*> feeds;
feeds.push_back(&btc_feed);
// feeds.push_back(ð_feed);
// feeds.push_back(&sol_feed);
// FeedProcess<DeltaWebsocketClient> sol_feed {products, sol_group, state, cfg};
for(auto* feed : feeds) {
pid_t pid = fork();
if(pid == 0) {
feed->start();
return 0;
}
g_child_pids.push_back(pid);
}
pid_t pid = fork();
if(pid == 0) {
oms_delta.start();
return 0;
}
g_child_pids.push_back(pid);
pid_t strat_pid = fork();
if(strat_pid == 0) {
strategy_delta.start();
return 0;
}
g_child_pids.push_back(strat_pid);
std::signal(SIGINT, on_shutdown_signal);
std::signal(SIGTERM, on_shutdown_signal);
for (size_t i = 0; i < g_child_pids.size(); ++i) {
int status;
(void)waitpid(-1, &status, 0);
}
std::cerr << "[delta] exit\n";
return 0;
}