Skip to content

Commit a778253

Browse files
committed
HitBTC: added Trade stream (websocket)
- DRY trade stream parsing in Gemini
1 parent 0e3b4f7 commit a778253

File tree

3 files changed

+122
-7
lines changed

3 files changed

+122
-7
lines changed

ExchangeSharp/API/Exchanges/Gemini/ExchangeGeminiAPI.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,19 +310,15 @@ protected override IWebSocket OnGetTradesWebSocket(Func<KeyValuePair<string, Exc
310310
var tradesToken = token["trades"];
311311
if (tradesToken != null) foreach (var tradeToken in tradesToken)
312312
{
313-
var trade = tradeToken.ParseTrade(amountKey: "quantity", priceKey: "price",
314-
typeKey: "side", timestampKey: "timestamp",
315-
TimestampType.UnixMilliseconds, idKey: "event_id");
313+
var trade = parseTrade(tradeToken);
316314
trade.Flags |= ExchangeTradeFlags.IsFromSnapshot;
317315
await callback(new KeyValuePair<string, ExchangeTrade>(marketSymbol, trade));
318316
}
319317
}
320318
else if (token["type"].ToStringInvariant() == "trade")
321319
{
322320
string marketSymbol = token["symbol"].ToStringInvariant();
323-
var trade = token.ParseTrade(amountKey: "quantity", priceKey: "price",
324-
typeKey: "side", timestampKey: "timestamp",
325-
TimestampType.UnixMilliseconds, idKey: "event_id");
321+
var trade = parseTrade(token);
326322
await callback(new KeyValuePair<string, ExchangeTrade>(marketSymbol, trade));
327323
}
328324
}, connectCallback: async (_socket) =>
@@ -331,6 +327,10 @@ protected override IWebSocket OnGetTradesWebSocket(Func<KeyValuePair<string, Exc
331327
await _socket.SendMessageAsync(new {
332328
type = "subscribe", subscriptions = new[] { new { name = "l2", symbols = marketSymbols } } });
333329
});
330+
ExchangeTrade parseTrade(JToken token) => token.ParseTrade(
331+
amountKey: "quantity", priceKey: "price",
332+
typeKey: "side", timestampKey: "timestamp",
333+
TimestampType.UnixMilliseconds, idKey: "event_id");
334334
}
335335
}
336336

ExchangeSharp/API/Exchanges/Hitbtc/ExchangeHitbtcAPI.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,121 @@ protected override async Task<ExchangeWithdrawalResponse> OnWithdrawAsync(Exchan
440440

441441
// working on it. Hitbtc has extensive support for sockets, including trading
442442

443+
protected override IWebSocket OnGetTradesWebSocket(Func<KeyValuePair<string, ExchangeTrade>, Task> callback, params string[] marketSymbols)
444+
{
445+
if (marketSymbols == null || marketSymbols.Length == 0)
446+
{
447+
marketSymbols = GetMarketSymbolsAsync().Sync().ToArray();
448+
}
449+
return ConnectWebSocket(null, messageCallback: async (_socket, msg) =>
450+
{
451+
JToken token = JToken.Parse(msg.ToStringFromUTF8());
452+
if (token["error"] != null)
453+
{ /* {
454+
"jsonrpc": "2.0",
455+
"error": {
456+
"code": 2001,
457+
"message": "Symbol not found",
458+
"description": "Try get /api/2/public/symbol, to get list of all available symbols."
459+
},
460+
"id": 123
461+
} */
462+
Logger.Info(token["error"]["code"].ToStringInvariant() + ", "
463+
+ token["error"]["message"].ToStringInvariant() + ", "
464+
+ token["error"]["description"].ToStringInvariant());
465+
}
466+
else if (token["method"].ToStringInvariant() == "snapshotTrades")
467+
{ /* snapshot: {
468+
"jsonrpc": "2.0",
469+
"method": "snapshotTrades",
470+
"params": {
471+
"data": [
472+
{
473+
"id": 54469456,
474+
"price": "0.054656",
475+
"quantity": "0.057",
476+
"side": "buy",
477+
"timestamp": "2017-10-19T16:33:42.821Z"
478+
},
479+
{
480+
"id": 54469497,
481+
"price": "0.054656",
482+
"quantity": "0.092",
483+
"side": "buy",
484+
"timestamp": "2017-10-19T16:33:48.754Z"
485+
},
486+
{
487+
"id": 54469697,
488+
"price": "0.054669",
489+
"quantity": "0.002",
490+
"side": "buy",
491+
"timestamp": "2017-10-19T16:34:13.288Z"
492+
}
493+
],
494+
"symbol": "ETHBTC"
495+
}
496+
} */
497+
token = token["params"];
498+
string marketSymbol = token["symbol"].ToStringInvariant();
499+
foreach (var tradesToken in token["data"])
500+
{
501+
var trade = parseTrade(tradesToken);
502+
trade.Flags |= ExchangeTradeFlags.IsFromSnapshot;
503+
await callback(new KeyValuePair<string, ExchangeTrade>(marketSymbol, trade));
504+
}
505+
}
506+
else if (token["method"].ToStringInvariant() == "updateTrades")
507+
{ /* {
508+
"jsonrpc": "2.0",
509+
"method": "updateTrades",
510+
"params": {
511+
"data": [
512+
{
513+
"id": 54469813,
514+
"price": "0.054670",
515+
"quantity": "0.183",
516+
"side": "buy",
517+
"timestamp": "2017-10-19T16:34:25.041Z"
518+
}
519+
],
520+
"symbol": "ETHBTC"
521+
}
522+
} */
523+
token = token["params"];
524+
string marketSymbol = token["symbol"].ToStringInvariant();
525+
foreach (var tradesToken in token["data"])
526+
{
527+
var trade = parseTrade(tradesToken);
528+
await callback(new KeyValuePair<string, ExchangeTrade>(marketSymbol, trade));
529+
}
530+
}
531+
}, connectCallback: async (_socket) =>
532+
{ /* {
533+
"method": "subscribeTrades",
534+
"params": {
535+
"symbol": "ETHBTC",
536+
"limit": 100
537+
},
538+
"id": 123
539+
} */
540+
foreach (var marketSymbol in marketSymbols)
541+
{
542+
await _socket.SendMessageAsync(new
543+
{
544+
method = "subscribeTrades",
545+
@params = new {
546+
symbol = marketSymbol,
547+
limit = 10,
548+
},
549+
id = CryptoUtility.UtcNow.Ticks // just need a unique number for client ID
550+
});
551+
}
552+
});
553+
ExchangeTrade parseTrade(JToken token) => token.ParseTrade(amountKey: "quantity",
554+
priceKey: "price", typeKey: "side", timestampKey: "timestamp",
555+
timestampType: TimestampType.Iso8601, idKey: "id");
556+
}
557+
443558
#endregion
444559

445560
#region Hitbtc Public Functions outside the ExchangeAPI

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The following cryptocurrency exchanges are supported:
2929
| Coinbase | x | x | T R |
3030
| Cryptopia | x | x | |
3131
| Gemini | x | x | R |
32-
| Hitbtc | x | x | |
32+
| HitBTC | x | x | R |
3333
| Huobi | x | x | R B |
3434
| Kraken | x | x | R |
3535
| KuCoin | x | x | T R |

0 commit comments

Comments
 (0)