-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
OrderBookOption.cs
80 lines (66 loc) · 1.86 KB
/
OrderBookOption.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
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
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("orderbook", HelpText = "Prints the order book from an exchange.")]
public class OrderBookOption
: BaseOption,
IOptionPerExchange,
IOptionWithMultipleMarketSymbol,
IOptionWithMaximum,
IOptionWithKey
{
public override async Task RunCommand()
{
using var api = await GetExchangeInstanceAsync(ExchangeName);
if (
!string.IsNullOrWhiteSpace(KeyPath)
&& !KeyPath.Equals(Constants.DefaultKeyPath, StringComparison.Ordinal)
)
{
api.LoadAPIKeys(KeyPath);
}
var marketSymbols = MarketSymbols.ToArray();
var orderBooks = await GetOrderBooks(marketSymbols, api);
foreach (var (marketSymbol, orderBook) in orderBooks)
{
Console.WriteLine($"Order Book for market: {marketSymbol} {orderBook}");
PrintOrderBook(orderBook);
Console.WriteLine();
}
}
private async Task<IEnumerable<KeyValuePair<string, ExchangeOrderBook>>> GetOrderBooks(
string[] marketSymbols,
IExchangeAPI api
)
{
IEnumerable<KeyValuePair<string, ExchangeOrderBook>> orderBooks;
if (marketSymbols.Length == 0)
{
orderBooks = await api.GetOrderBooksAsync(Max);
}
else
{
var orderBooksList = await Task.WhenAll(
marketSymbols.Select(async ms =>
{
var orderBook = await api.GetOrderBookAsync(ms, Max);
orderBook.MarketSymbol ??= ms;
return orderBook;
})
);
orderBooks = orderBooksList.ToDictionary(k => k.MarketSymbol, v => v);
}
return orderBooks;
}
public string ExchangeName { get; set; }
public IEnumerable<string> MarketSymbols { get; set; }
public int Max { get; set; }
public string KeyPath { get; set; }
}
}