-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_data_api.cpp
More file actions
269 lines (248 loc) · 11.5 KB
/
04_data_api.cpp
File metadata and controls
269 lines (248 loc) · 11.5 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
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
// Example 04: Data API — positions, trades, activity, holders, leaderboard.
//
// The Data API (data-api.polymarket.com) provides historical and aggregated data.
// All endpoints are read-only and require no authentication.
//
// Topics:
// health
// leaderboard / builder_leaderboard / builder_volume
// traded / value / positions / closed_positions
// trades / activity
// open_interest / holders / live_volume
//
// Run: ./04_data_api
#include <polymarket/data/data_client.hpp>
#include <algorithm>
#include <cstdio>
using namespace polymarket;
int main() {
DataClient data;
// ── 1. Health ─────────────────────────────────────────────────────────────
printf("=== Health ===\n");
{
auto h = data.health();
if (h) printf(" %s\n", h->c_str());
}
// ── 2. Leaderboard ────────────────────────────────────────────────────────
// All-time top traders ranked by PnL. Includes display name / X handle.
printf("\n=== Leaderboard (top 5) ===\n");
std::string top_wallet;
{
auto result = data.leaderboard(5);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& e : *result) {
const char* name = e.userName ? e.userName->c_str()
: e.xUsername ? e.xUsername->c_str()
: "anonymous";
printf(" #%-3s %-22s vol=$%-12.0f pnl=$%.0f\n",
e.rank.c_str(), name, e.vol, e.pnl);
}
if (!result->empty()) top_wallet = result->at(0).proxyWallet;
}
}
// ── 3. Builder leaderboard ────────────────────────────────────────────────
// Volume generated by CLOB API integrators.
printf("\n=== Builder Leaderboard (top 3) ===\n");
std::string top_builder;
{
auto result = data.builder_leaderboard(3);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& e : *result) {
printf(" #%-3s %-30s vol=$%.0f\n",
e.rank.c_str(),
e.name ? e.name->c_str() : e.builder.substr(0, 30).c_str(),
e.volume);
}
if (!result->empty()) top_builder = result->at(0).builder;
}
}
if (top_wallet.empty()) {
printf("Leaderboard empty, stopping.\n");
return 1;
}
printf("\n=== Top Trader: %.20s... ===\n", top_wallet.c_str());
// ── 4. Markets traded count ───────────────────────────────────────────────
{
auto t = data.traded(top_wallet);
if (t) printf(" Markets traded: %d\n", t->traded);
}
// ── 5. Portfolio value ────────────────────────────────────────────────────
// Current mark-to-market value of open positions.
printf("\n=== Portfolio Value ===\n");
{
auto result = data.value(top_wallet);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& v : *result)
printf(" $%.2f\n", v.value);
}
}
// ── 6. Open positions ─────────────────────────────────────────────────────
// Active holdings: entry price, current price, unrealized + realized PnL.
printf("\n=== Open Positions (top 5) ===\n");
std::string first_condition_id;
std::string first_event_id;
{
auto result = data.positions(top_wallet, {}, 5);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
printf(" Count: %zu\n", result->size());
for (auto& p : *result) {
printf(" %-4s size=%-9.2f avg=%.4f→%.4f cashPnl=%+.2f realPnl=%+.2f",
p.outcome ? p.outcome->c_str() : "?",
p.size, p.avgPrice, p.curPrice, p.cashPnl, p.realizedPnl);
if (p.negativeRisk) printf(" [neg_risk]");
if (p.redeemable) printf(" [redeemable]");
printf("\n");
if (p.title) printf(" %s\n", p.title->c_str());
if (first_condition_id.empty()) {
first_condition_id = p.conditionId;
if (p.eventId) first_event_id = *p.eventId;
}
}
}
}
// ── 7. Closed positions ───────────────────────────────────────────────────
// Fully exited or resolved positions with realized PnL.
printf("\n=== Closed Positions (last 3) ===\n");
{
auto result = data.closed_positions(top_wallet, 3);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& p : *result) {
printf(" %-4s avg=%.4f realPnl=%+.2f t=%lld\n",
p.outcome ? p.outcome->c_str() : "?",
p.avgPrice, p.realizedPnl, (long long)p.timestamp);
if (p.title) printf(" %s\n", p.title->c_str());
}
}
}
// ── 8. User trades ────────────────────────────────────────────────────────
// Individual trade history. Filter by condition_id or asset as needed.
printf("\n=== User Trades (last 5) ===\n");
{
auto result = data.trades(top_wallet, {}, {}, 5);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& t : *result) {
printf(" %-5s size=%-9.2f @%.4f t=%lld",
t.side.c_str(), t.size, t.price, (long long)t.timestamp);
if (t.outcome) printf(" %s", t.outcome->c_str());
printf("\n");
if (t.title) printf(" %s\n", t.title->c_str());
}
}
}
// ── 9. Activity feed ──────────────────────────────────────────────────────
// All on-chain events: trades, deposits, withdrawals, redeems.
printf("\n=== Activity (last 5) ===\n");
{
auto result = data.activity(top_wallet, 5);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& a : *result) {
printf(" %-12s size=%.2f usdc=%.2f t=%lld",
a.type.c_str(), a.size, a.usdcSize, (long long)a.timestamp);
if (a.price) printf(" @%.4f", *a.price);
if (a.side) printf(" %s", a.side->c_str());
printf("\n");
if (a.title) printf(" %s\n", a.title->c_str());
}
}
}
// ── 10. Market-level data from a position's condition_id ─────────────────
if (!first_condition_id.empty()) {
// Open interest — total outstanding tokens per outcome
printf("\n=== Open Interest ===\n");
{
auto result = data.open_interest(first_condition_id);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& oi : *result)
printf(" %.20s... oi=$%.0f\n", oi.market.c_str(), oi.value);
}
}
// Token holders — top 5 holders of the Yes token (outcome_index=0)
// Use outcome_index=1 for the No token.
printf("\n=== Holders (Yes token, top 5) ===\n");
{
auto result = data.holders(first_condition_id, /*outcome_index=*/0, 5);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& th : *result) {
printf(" token: %.20s...\n", th.token.c_str());
for (auto& h : th.holders) {
const char* name = h.name ? h.name->c_str()
: h.pseudonym ? h.pseudonym->c_str()
: "anon";
printf(" %-20s amount=%.2f\n", name, h.amount);
}
}
}
}
}
// ── 11. Live volume for an event ──────────────────────────────────────────
// Rolling live trading volume broken down by market within an event.
// event_id comes from positions[].eventId or the Gamma API.
if (!first_event_id.empty()) {
printf("\n=== Live Volume (event from position) ===\n");
{
auto result = data.live_volume(first_event_id);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& lv : *result) {
printf(" total=$%.0f\n", lv.total);
for (auto& mv : lv.markets)
printf(" %.20s... $%.0f\n", mv.market.c_str(), mv.value);
}
}
}
}
// ── 12. Global recent trades (no filter) ─────────────────────────────────
// Most recent trades across all markets and all wallets.
printf("\n=== Global Recent Trades (last 5) ===\n");
{
auto result = data.trades({}, {}, {}, 5);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
for (auto& t : *result) {
printf(" %.10s... %-5s size=%-8.2f @%.4f\n",
t.proxyWallet.c_str(), t.side.c_str(), t.size, t.price);
if (t.title) printf(" %s\n", t.title->c_str());
}
}
}
// ── 13. Builder volume by day ─────────────────────────────────────────────
// Daily volume for the top builder. Without start/end the API returns full
// history — can be large. Use start/end to narrow the range:
// data.builder_volume(builder, "2024-01-01", "2024-01-31")
if (!top_builder.empty()) {
printf("\n=== Builder Volume (last 5 days) ===\n");
{
auto result = data.builder_volume(top_builder);
if (!result) {
printf(" Error: %s\n", result.error().message().c_str());
} else {
int start = std::max(0, (int)result->size() - 5);
for (int i = start; i < (int)result->size(); ++i)
printf(" %s $%.0f\n",
result->at(i).date.c_str(), result->at(i).volume);
}
}
}
printf("\nDone.\n");
return 0;
}