Skip to content

Commit 21135b0

Browse files
authored
Feature: Add support for ws-orderbook to Kraken and Gemini Exchanges (#588)
* Linting. Consistent tabs for code formatting. * Add support for ws-orderbooks to Kraken and Gemini.
1 parent a6d417e commit 21135b0

File tree

9 files changed

+2139
-1886
lines changed

9 files changed

+2139
-1886
lines changed

src/ExchangeSharp/API/Exchanges/Gemini/ExchangeGeminiAPI.cs

Lines changed: 317 additions & 312 deletions
Large diffs are not rendered by default.

src/ExchangeSharp/API/Exchanges/Kraken/ExchangeKrakenAPI.cs

Lines changed: 823 additions & 673 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using ExchangeSharp.API.Exchanges.Kraken.Models.Types;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Converters;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace ExchangeSharp.API.Exchanges.Kraken.Models.Request
9+
{
10+
internal class ChannelAction
11+
{
12+
[JsonConverter(typeof(StringEnumConverter))]
13+
[JsonProperty("event")]
14+
public ActionType Event { get; set; }
15+
16+
[JsonProperty("pair")]
17+
public List<string> Pairs { get; set; }
18+
19+
[JsonProperty("subscription")]
20+
public Subscription SubscriptionSettings { get; set; }
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
4+
using System.Text;
5+
6+
namespace ExchangeSharp.API.Exchanges.Kraken.Models.Types
7+
{
8+
internal enum ActionType
9+
{
10+
[EnumMember(Value = "subscribe")]
11+
Subscribe,
12+
13+
[EnumMember(Value = "unsubscribe")]
14+
Unsubscribe
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace ExchangeSharp.API.Exchanges.Kraken.Models.Types
7+
{
8+
internal class Subscription
9+
{
10+
[JsonProperty("name")]
11+
public string Name { get; set; }
12+
13+
[JsonProperty("depth")]
14+
public int Depth { get; set; }
15+
}
16+
}

src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs

Lines changed: 707 additions & 667 deletions
Large diffs are not rendered by default.

src/ExchangeSharp/API/Exchanges/_Base/IExchangeAPI.cs

Lines changed: 219 additions & 213 deletions
Large diffs are not rendered by default.

src/ExchangeSharpConsole/Options/BaseOption.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task CheckDebugger()
2626
return;
2727
}
2828

29-
using (var proc = Process.GetCurrentProcess())
29+
using(var proc = Process.GetCurrentProcess())
3030
{
3131
Console.Error.WriteLine($"Connect debugger to PID: {proc.Id}.");
3232
Console.Error.WriteLine("Waiting...");
@@ -166,17 +166,15 @@ protected async Task RunWebSocket(string exchangeName, Func<IExchangeAPI, Task<I
166166
}
167167
}
168168

169-
170169
/// <summary>
171170
/// Makes the app keep running after main thread has exited
172171
/// </summary>
173172
/// <param name="callback">A callback for when the user press CTRL-C or Q</param>
174-
protected static IDisposable KeepSessionAlive(Action callback = null)
175-
=> new ConsoleSessionKeeper(callback);
173+
protected static IDisposable KeepSessionAlive(Action callback = null) => new ConsoleSessionKeeper(callback);
176174

177-
protected static async Task<string[]> ValidateMarketSymbolsAsync(IExchangeAPI api, string[] marketSymbols)
175+
protected static async Task<string[]> ValidateMarketSymbolsAsync(IExchangeAPI api, string[] marketSymbols, bool isWebSocket = false)
178176
{
179-
var apiSymbols = (await api.GetMarketSymbolsAsync()).ToArray();
177+
var apiSymbols = (await api.GetMarketSymbolsAsync(isWebSocket)).ToArray();
180178

181179
if (marketSymbols is null || marketSymbols.Length == 0)
182180
{
@@ -209,8 +207,8 @@ string[] apiSymbols
209207
var validSymbols = string.Join(",", apiSymbols.OrderBy(s => s));
210208

211209
throw new ArgumentException(
212-
$"Symbol {marketSymbol} does not exist in API {api.Name}.\n" +
213-
$"Valid symbols: {validSymbols}"
210+
$"Symbol {marketSymbol} does not exist in API {api.Name}.\n"
211+
+ $"Valid symbols: {validSymbols}"
214212
);
215213
}
216214
}
@@ -230,10 +228,10 @@ protected void PrintOrderBook(ExchangeOrderBook orderBook)
230228

231229
for (var i = 0; i < length; i++)
232230
{
233-
var (_, ask) = orderBook.Asks.ElementAtOrDefault(i);
234-
var (_, bid) = orderBook.Bids.ElementAtOrDefault(i);
235-
Console.WriteLine($"{bid.Price,10} ({bid.Amount,9:N2}) | " +
236-
$"{ask.Price,10} ({ask.Amount,9:N})");
231+
var(_, ask) = orderBook.Asks.ElementAtOrDefault(i);
232+
var(_, bid) = orderBook.Bids.ElementAtOrDefault(i);
233+
Console.WriteLine($"{bid.Price,10} ({bid.Amount,9:N2}) | "
234+
+ $"{ask.Price,10} ({ask.Amount,9:N})");
237235
}
238236
}
239237
}

src/ExchangeSharpConsole/Options/WebSocketsOrderbookOption.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
namespace ExchangeSharpConsole.Options
1010
{
1111
[Verb("ws-orderbook", HelpText =
12-
"Connects to the given exchange websocket and keeps printing the first bid and ask prices and amounts for the given market symbols." +
13-
"If market symbol is not set then uses all.")]
12+
"Connects to the given exchange websocket and keeps printing the first bid and ask prices and amounts for the given market symbols."
13+
+ "If market symbol is not set then uses all.")]
1414
public class WebSocketsOrderbookOption : BaseOption, IOptionPerExchange, IOptionWithMultipleMarketSymbol
1515
{
1616
public override async Task RunCommand()
1717
{
1818
async Task<IWebSocket> GetWebSocket(IExchangeAPI api)
1919
{
20-
var symbols = await ValidateMarketSymbolsAsync(api, MarketSymbols.ToArray());
20+
var symbols = await ValidateMarketSymbolsAsync(api, MarketSymbols.ToArray(), true);
2121

2222
return await api.GetFullOrderBookWebSocketAsync(
2323
OrderBookCallback,
24-
symbols: symbols
24+
symbols : symbols
2525
);
2626
}
2727

@@ -30,13 +30,13 @@ async Task<IWebSocket> GetWebSocket(IExchangeAPI api)
3030

3131
private static void OrderBookCallback(ExchangeOrderBook msg)
3232
{
33-
var (_, bid) = msg.Bids.FirstOrDefault();
34-
var (_, ask) = msg.Asks.FirstOrDefault();
33+
var(_, bid) = msg.Bids.FirstOrDefault();
34+
var(_, ask) = msg.Asks.FirstOrDefault();
3535

3636
Console.WriteLine(
37-
$"[{msg.MarketSymbol,-8}:{msg.SequenceId,10}] " +
38-
$"{bid.Price,10} ({bid.Amount,9:N2}) | " +
39-
$"{ask.Price,10} ({ask.Amount,9:N})"
37+
$"[{msg.MarketSymbol,-8}:{msg.SequenceId,10}] "
38+
+ $"{bid.Price,10} ({bid.Amount,9:N2}) | "
39+
+ $"{ask.Price,10} ({ask.Amount,9:N})"
4040
);
4141
}
4242

0 commit comments

Comments
 (0)