-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
WebSocketsOrderbookOption.cs
52 lines (45 loc) · 1.36 KB
/
WebSocketsOrderbookOption.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CommandLine;
using ExchangeSharp;
using ExchangeSharpConsole.Options.Interfaces;
namespace ExchangeSharpConsole.Options
{
[Verb(
"ws-orderbook",
HelpText = "Connects to the given exchange websocket and keeps printing the first bid and ask prices and amounts for the given market symbols.\n"
+ "If market symbol is not set then uses all."
)]
public class WebSocketsOrderbookOption
: BaseOption,
IOptionPerExchange,
IOptionWithMultipleMarketSymbol
{
public override async Task RunCommand()
{
async Task<IWebSocket> GetWebSocket(IExchangeAPI api)
{
var symbols = await ValidateMarketSymbolsAsync(api, MarketSymbols.ToArray(), true);
return await api.GetFullOrderBookWebSocketAsync(
OrderBookCallback,
symbols: symbols
);
}
await RunWebSocket(ExchangeName, GetWebSocket);
}
private static void OrderBookCallback(ExchangeOrderBook msg)
{
var (_, bid) = msg.Bids.FirstOrDefault();
var (_, ask) = msg.Asks.FirstOrDefault();
Console.WriteLine(
$"[{msg.MarketSymbol,-8}:{msg.SequenceId,10}] "
+ $"{bid.Price,10} ({bid.Amount,9:N2}) | "
+ $"{ask.Price,10} ({ask.Amount,9:N})"
);
}
public string ExchangeName { get; set; }
public IEnumerable<string> MarketSymbols { get; set; }
}
}