-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPublicConnection.cs
More file actions
39 lines (30 loc) · 1.86 KB
/
Copy pathPublicConnection.cs
File metadata and controls
39 lines (30 loc) · 1.86 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
using System;
using System.Threading;
using System.Threading.Tasks;
using WhalesSecret.TradeScriptLib.API.TradingV1;
using WhalesSecret.TradeScriptLib.Entities;
namespace WhalesSecret.ScriptApiLib.Samples.BasicSamples.Connections;
/// <summary>
/// Sample that demonstrates how to connect to an exchange market using a public connection. Public connections are commonly used for accessing publicly available information,
/// such as candlesticks, order books, or tickers.
/// </summary>
public class PublicConnection : IScriptApiSample
{
/// <inheritdoc/>
public async Task RunSampleAsync(ExchangeMarket exchangeMarket)
{
using CancellationTokenSource timeoutCts = new(TimeSpan.FromMinutes(2));
await using ScriptApi scriptApi = await ScriptApi.CreateAsync(timeoutCts.Token).ConfigureAwait(false);
Console.WriteLine($"Connect to {exchangeMarket} exchange with a public connection.");
// Market-data connection type is the only connection type that does not need exchange API credentials.
ConnectionOptions connectionOptions = new(connectionType: ConnectionType.MarketData);
await using ITradeApiClient tradeClient = await scriptApi.ConnectAsync(exchangeMarket, connectionOptions).ConfigureAwait(false);
Console.WriteLine($"Public connection to {exchangeMarket} has been established successfully.");
// As the connection is established, we can use the connected client to, for example, query the time of the exchange.
DateTime utcExchangeTime = tradeClient.GetExchangeUtcDateTime();
TimeSpan diff = utcExchangeTime - DateTime.UtcNow;
Console.WriteLine($"Current UTC time of the {exchangeMarket} exchange is {utcExchangeTime}. The difference between the exchange time and the local time is {
diff}.");
Console.WriteLine("Disposing trade API client and script API.");
}
}