-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path03-websocket.cs
More file actions
46 lines (38 loc) · 1.36 KB
/
Copy path03-websocket.cs
File metadata and controls
46 lines (38 loc) · 1.36 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
// 03-websocket.cs
//
// Demonstrates: BitMart public websocket subscriptions.
//
// Setup: dotnet add package BitMart.Net
using BitMart.Net.Clients;
using BitMart.Net.Enums;
var socketClient = new BitMartSocketClient();
var spotTickerSubscription = await socketClient.SpotApi.SubscribeToTickerUpdatesAsync(
"BTC_USDT",
update =>
{
Console.WriteLine($"Spot BTC_USDT ticker: {update.Data.LastPrice}");
});
if (!spotTickerSubscription.Success)
{
Console.WriteLine($"Spot ticker subscription failed: {spotTickerSubscription.Error}");
return;
}
var futuresKlineSubscription = await socketClient.UsdFuturesApi.SubscribeToKlineUpdatesAsync(
"ETHUSDT",
FuturesStreamKlineInterval.OneMinute,
update =>
{
var candle = update.Data.Klines.FirstOrDefault();
if (candle != null)
Console.WriteLine($"Futures ETHUSDT 1m candle: open={candle.OpenPrice}, close={candle.ClosePrice}");
});
if (!futuresKlineSubscription.Success)
{
Console.WriteLine($"Futures kline subscription failed: {futuresKlineSubscription.Error}");
await socketClient.UnsubscribeAsync(spotTickerSubscription.Data);
return;
}
Console.WriteLine("Listening. Press Enter to unsubscribe.");
Console.ReadLine();
await socketClient.UnsubscribeAsync(spotTickerSubscription.Data);
await socketClient.UnsubscribeAsync(futuresKlineSubscription.Data);