Skip to content

Commit 4cb94a2

Browse files
authored
Implemented OnGetCandlesWebSocketAsync for BinanceGroup (#692)
1 parent 2ef1f50 commit 4cb94a2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/ExchangeSharp/API/Exchanges/BinanceGroup/BinanceGroupCommon.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,38 @@ protected override async Task<IWebSocket> OnUserDataWebSocketAsync(Action<object
11351135
});
11361136
}
11371137

1138+
protected override async Task<IWebSocket> OnGetCandlesWebSocketAsync(Func<MarketCandle, Task> callbackAsync,
1139+
int periodSeconds, params string[] marketSymbols)
1140+
{
1141+
if (!marketSymbols.Any())
1142+
{
1143+
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
1144+
}
1145+
1146+
var period = PeriodSecondsToString(periodSeconds);
1147+
var payload = marketSymbols.Select(s => $"{s.ToLowerInvariant()}@kline_{period}");
1148+
var url = $"/stream?streams={string.Join("/", payload)}";
1149+
if (url.Length > 2048)
1150+
{
1151+
throw new InvalidOperationException(
1152+
$"URL length over the limit of 2048 characters. Consider splitting instruments in multiple connections.");
1153+
}
1154+
1155+
return await ConnectPublicWebSocketAsync(url, async (_socket, msg) =>
1156+
{
1157+
JToken token = JToken.Parse(msg.ToStringFromUTF8());
1158+
1159+
if (token?["data"]?["k"] != null && token["data"]["s"] != null)
1160+
{
1161+
var candle = this.ParseCandle(token["data"]["k"], token["data"]["s"].ToStringInvariant(),
1162+
periodSeconds, "o", "h", "l", "c", "t",
1163+
TimestampType.UnixMilliseconds, "v", "q");
1164+
1165+
await callbackAsync(candle);
1166+
}
1167+
});
1168+
}
1169+
11381170
public async Task<string> GetListenKeyAsync()
11391171
{
11401172
JToken response = await MakeJsonRequestAsync<JToken>("/userDataStream", BaseUrlApi, null, "POST");

0 commit comments

Comments
 (0)