-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples.zig
More file actions
207 lines (169 loc) · 7.57 KB
/
examples.zig
File metadata and controls
207 lines (169 loc) · 7.57 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
// Examples for CCXT-Zig Phase 2: Major Exchanges
//
// These examples demonstrate how to use the implemented exchanges.
// Note: For authenticated endpoints, you need to provide valid API credentials.
const std = @import("std");
const ccxt = @import("ccxt_zig");
// Example 1: Fetching markets from Binance (public endpoint)
pub fn fetchBinanceMarkets(allocator: std.mem.Allocator) !void {
var auth_config = ccxt.auth.AuthConfig{};
const binance = try ccxt.binance.create(allocator, auth_config);
defer binance.deinit();
const markets = try binance.fetchMarkets();
defer {
for (markets) |*market| market.deinit(allocator);
allocator.free(markets);
}
std.debug.print("Binance markets: {d} trading pairs\n", .{markets.len});
// Print first 5 markets
for (markets[0..@min(5, markets.len)]) |market| {
std.debug.print(" {s}: {s}/{s} (active: {})\n", .{
market.id, market.base, market.quote, market.active,
});
}
}
// Example 2: Fetching ticker from Kraken
pub fn fetchKrakenTicker(allocator: std.mem.Allocator) !void {
var auth_config = ccxt.auth.AuthConfig{};
const kraken = try ccxt.kraken.create(allocator, auth_config);
defer kraken.deinit();
// First fetch markets to get the correct symbol format
const markets = try kraken.fetchMarkets();
defer {
for (markets) |*market| market.deinit(allocator);
allocator.free(markets);
}
// Find BTC/USD market (Kraken uses XBT for Bitcoin)
const btc_usd = for (markets) |market| {
if (std.mem.eql(u8, market.base, "XBT") and std.mem.eql(u8, market.quote, "USD")) {
break market;
}
} else return error.SymbolNotFound;
const ticker = try kraken.fetchTicker(btc_usd.symbol);
defer ticker.deinit(allocator);
std.debug.print("Kraken {s} ticker:\n", .{btc_usd.symbol});
std.debug.print(" Last: ${d:.2}\n", .{ticker.last orelse 0});
std.debug.print(" High: ${d:.2}\n", .{ticker.high orelse 0});
std.debug.print(" Low: ${d:.2}\n", .{ticker.low orelse 0});
std.debug.print(" 24h Volume: {d:.2}\n", .{ticker.baseVolume orelse 0});
}
// Example 3: Fetching order book from Coinbase
pub fn fetchCoinbaseOrderBook(allocator: std.mem.Allocator) !void {
var auth_config = ccxt.auth.AuthConfig{};
const coinbase = try ccxt.coinbase.create(allocator, auth_config);
defer coinbase.deinit();
const orderbook = try coinbase.fetchOrderBook("BTC/USD", 20);
defer orderbook.deinit(allocator);
std.debug.print("Coinbase BTC/USD Order Book:\n", .{});
std.debug.print(" Top 3 Bids:\n", .{});
for (orderbook.bids[0..@min(3, orderbook.bids.len)]) |bid| {
std.debug.print(" ${d:.2} x {d:.6}\n", .{ bid.price, bid.amount });
}
std.debug.print(" Top 3 Asks:\n", .{});
for (orderbook.asks[0..@min(3, orderbook.asks.len)]) |ask| {
std.debug.print(" ${d:.2} x {d:.6}\n", .{ ask.price, ask.amount });
}
}
// Example 4: Fetching OHLCV data from Bybit
pub fn fetchBybitOHLCV(allocator: std.mem.Allocator) !void {
var auth_config = ccxt.auth.AuthConfig{};
const bybit = try ccxt.bybit.create(allocator, auth_config);
defer bybit.deinit();
const ohlcv_data = try bybit.fetchOHLCV("BTC/USDT", "1h", null, 24);
defer allocator.free(ohlcv_data);
std.debug.print("Bybit BTC/USDT Hourly Candles (last 24):\n", .{});
for (ohlcv_data) |candle| {
const dt = try std.fmt.allocPrint(allocator, "{d}", .{candle.timestamp});
defer allocator.free(dt);
std.debug.print(" {s}: O: ${d:.2} H: ${d:.2} L: ${d:.2} C: ${d:.2} V: {d:.4}\n", .{
dt, candle.open, candle.high, candle.low, candle.close, candle.volume,
});
}
}
// Example 5: Using the Exchange Registry
pub fn useExchangeRegistry(allocator: std.mem.Allocator) !void {
var registry = try ccxt.registry.createDefaultRegistry(allocator);
defer registry.deinit();
std.debug.print("Available exchanges:\n", .{});
const names = registry.getNames();
defer allocator.free(names);
for (names) |name| {
if (registry.get(name)) |entry| {
std.debug.print(" {s}: {s}\n", .{ entry.info.name, entry.info.description });
std.debug.print(" Spot: {}, Margin: {}, Futures: {}\n", .{
entry.info.spot_supported,
entry.info.margin_supported,
entry.info.futures_supported,
});
}
}
// Create a testnet Binance instance
if (registry.get("binance") != null) {
var auth_config = ccxt.auth.AuthConfig{
.apiKey = try allocator.dupe(u8, "your_api_key"),
.apiSecret = try allocator.dupe(u8, "your_api_secret"),
};
const binance = try ccxt.binance.createTestnet(allocator, auth_config);
defer binance.deinit();
}
}
// Example 6: Private endpoint with API keys (requires valid credentials)
pub fn fetchPrivateBalance(allocator: std.mem.Allocator) !void {
// NOTE: Replace with your actual API credentials
// AuthConfig ownership is transferred to the exchange instance (exchange.deinit frees it).
var okx_config = ccxt.auth.AuthConfig{
.apiKey = try allocator.dupe(u8, "your_api_key"),
.apiSecret = try allocator.dupe(u8, "your_api_secret"),
.passphrase = try allocator.dupe(u8, "your_passphrase"),
};
const okx = try ccxt.okx.create(allocator, okx_config);
defer okx.deinit();
// This would fetch your balance (requires valid credentials)
// const balance = try okx.fetchBalance();
// std.debug.print("OKX USDT balance: {d}\n", .{balance.free});
}
// Example 7: Creating an order (requires valid credentials)
pub fn createTestOrder(allocator: std.mem.Allocator) !void {
var auth_config = ccxt.auth.AuthConfig{
.apiKey = try allocator.dupe(u8, "your_api_key"),
.apiSecret = try allocator.dupe(u8, "your_api_secret"),
};
const gate = try ccxt.gate.create(allocator, auth_config);
defer gate.deinit();
// This would create a test limit order (requires valid credentials)
// const order = try gate.createOrder("BTC/USDT", "limit", "buy", 0.001, 50000.0, null);
// std.debug.print("Created order: {s}\n", .{order.id});
}
// Example 8: Hyperliquid DEX (Phase 3 Preview)
pub fn fetchHyperliquidMarkets(allocator: std.mem.Allocator) !void {
var auth_config = ccxt.auth.AuthConfig{};
const hyperliquid = try ccxt.hyperliquid.create(allocator, auth_config);
defer hyperliquid.deinit();
const markets = try hyperliquid.fetchMarkets();
defer {
for (markets) |*market| market.deinit(allocator);
allocator.free(markets);
}
std.debug.print("Hyperliquid markets: {d} perpetual contracts\n", .{markets.len});
// Print first 5 markets
for (markets[0..@min(5, markets.len)]) |market| {
std.debug.print(" {s}: {s}/{s} (perpetual: {})\n", .{
market.id, market.base, market.quote, market.perpetual,
});
}
}
// Main entry point for examples
pub fn main() !void {
const allocator = std.heap.page_allocator;
std.debug.print("CCXT-Zig Phase 2 Examples\n", .{});
std.debug.print("========================\n\n", .{});
// Run examples (uncomment to test)
// try fetchBinanceMarkets(allocator);
// try fetchKrakenTicker(allocator);
// try fetchCoinbaseOrderBook(allocator);
// try fetchBybitOHLCV(allocator);
// try useExchangeRegistry(allocator);
// try fetchHyperliquidMarkets(allocator); // Phase 3 Preview
std.debug.print("Examples ready to use.\n", .{});
std.debug.print("Uncomment the function calls in main() to run each example.\n", .{});
}