Skip to content

Commit 6e26842

Browse files
committed
Bug fix gemini web socket tickers
1 parent 2d13b9f commit 6e26842

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

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

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -392,30 +392,35 @@ protected override async Task<IWebSocket> OnGetTickersWebSocketAsync(Action<IRea
392392
// fetch the last bid/ask/last prices
393393
if (token["changes"] is JArray changesToken)
394394
{
395-
if (changesToken.FirstOrDefault(t => t[0].ToStringInvariant().Equals("buy", StringComparison.OrdinalIgnoreCase)) is JArray buyToken)
396-
{
397-
decimal bidPrice = buyToken[1].ConvertInvariant<decimal>();
398-
ticker.Bid = bidPrice;
399-
}
400-
401-
if (changesToken.FirstOrDefault(t => t[0].ToStringInvariant().Equals("sell", StringComparison.OrdinalIgnoreCase)) is JArray sellToken)
402-
{
403-
decimal askPrice = sellToken[1].ConvertInvariant<decimal>();
404-
ticker.Ask = askPrice;
405-
}
406-
407395
if (token["trades"] is JArray tradesToken)
408396
{
409-
JToken lastTrade = tradesToken.FirstOrDefault();
410-
if (lastTrade != null)
397+
JToken lastSell = tradesToken.FirstOrDefault(t => t["side"].ToStringInvariant().Equals("sell", StringComparison.OrdinalIgnoreCase));
398+
if (lastSell != null)
411399
{
412-
decimal lastTradePrice = lastTrade["price"].ConvertInvariant<decimal>();
413-
ticker.Last = lastTradePrice;
400+
decimal lastTradePrice = lastSell["price"].ConvertInvariant<decimal>();
401+
ticker.Last = ticker.Bid = lastTradePrice;
402+
if (ticker.Bid > ticker.Ask)
403+
{
404+
// out of sync, reset ask
405+
ticker.Ask = 0m;
406+
}
407+
}
408+
JToken lastBuy = tradesToken.FirstOrDefault(t => t["side"].ToStringInvariant().Equals("buy", StringComparison.OrdinalIgnoreCase));
409+
if (lastBuy != null)
410+
{
411+
decimal lastTradePrice = lastBuy["price"].ConvertInvariant<decimal>();
412+
ticker.Ask = lastTradePrice;
413+
if (ticker.Ask < ticker.Bid)
414+
{
415+
// out of sync, reset bid
416+
ticker.Bid = 0m;
417+
}
414418
}
415419
}
416420

417-
// see if we have volume yet
418-
if (volumeDict.TryGetValue(marketSymbol, out decimal tickerVolume))
421+
// if we are fully populated...
422+
if (ticker.Bid > 0m && ticker.Ask > 0m &&
423+
volumeDict.TryGetValue(marketSymbol, out decimal tickerVolume))
419424
{
420425
ticker.Volume.BaseCurrencyVolume = tickerVolume;
421426
ticker.Volume.QuoteCurrencyVolume = tickerVolume * ticker.Last;
@@ -513,7 +518,8 @@ protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValu
513518
{
514519
JToken token = JToken.Parse(msg.ToStringFromUTF8());
515520
if (token["result"].ToStringInvariant() == "error")
516-
{ // {{ "result": "error", "reason": "InvalidJson"}}
521+
{
522+
// {{ "result": "error", "reason": "InvalidJson"}}
517523
Logger.Info(token["reason"].ToStringInvariant());
518524
}
519525
else if (token["type"].ToStringInvariant() == "l2_updates")
@@ -536,8 +542,18 @@ protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValu
536542
}, connectCallback: async (_socket) =>
537543
{
538544
//{ "type": "subscribe","subscriptions":[{ "name":"l2","symbols":["BTCUSD","ETHUSD","ETHBTC"]}]}
539-
await _socket.SendMessageAsync(new {
540-
type = "subscribe", subscriptions = new[] { new { name = "l2", symbols = marketSymbols } } });
545+
await _socket.SendMessageAsync(new
546+
{
547+
type = "subscribe",
548+
subscriptions = new[]
549+
{
550+
new
551+
{
552+
name = "l2",
553+
symbols = marketSymbols
554+
}
555+
}
556+
});
541557
});
542558
}
543559

0 commit comments

Comments
 (0)