Skip to content

Implemented OnGetCandlesWebSocketAsync for BinanceGroup #690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/ExchangeSharp/API/Exchanges/BinanceGroup/BinanceGroupCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,36 @@ protected override async Task<IWebSocket> OnUserDataWebSocketAsync(Action<object
});
}

protected override async Task<IWebSocket> OnGetCandlesWebSocketAsync(Func<MarketCandle, Task> callbackAsync, int periodSeconds, params string[] marketSymbols)
{
//Candlesticks are received each 2s. Miniticker method is faster (1s), but you can't aggregate by period.
//https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#klinecandlestick-streams

if (marketSymbols == null || marketSymbols.Length == 0)
throw new ArgumentNullException(nameof(marketSymbols));

//only one symbol per connection
var symbol = marketSymbols[0].ToStringLowerInvariant();

var period = PeriodSecondsToString(periodSeconds);

return await ConnectPublicWebSocketAsync($"/stream?streams={symbol}@kline_{period}", async (_socket, msg) =>
{
JToken token = JToken.Parse(msg.ToStringFromUTF8());

if (token != null && token["data"] != null && token["data"]["k"] != null)
{
var candle = this.ParseCandle(token["data"]["k"], symbol, periodSeconds, "o", "h", "l", "c", "t",
TimestampType.UnixMilliseconds, "v", "q");

if (candle != null)
{
callbackAsync(candle);
}
}
});
}

public async Task<string> GetListenKeyAsync()
{
JToken response = await MakeJsonRequestAsync<JToken>("/userDataStream", BaseUrlApi, null, "POST");
Expand Down