Skip to content
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

[Poloniex] Add websocket candle feed #799

Merged
merged 4 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
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
104 changes: 59 additions & 45 deletions src/ExchangeSharp/API/Exchanges/Poloniex/ExchangePoloniexAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,51 +456,6 @@ protected override async Task<IEnumerable<KeyValuePair<string, ExchangeTicker>>>
return tickers;
}

protected override async Task<IWebSocket> OnGetTickersWebSocketAsync(
Action<IReadOnlyCollection<KeyValuePair<string, ExchangeTicker>>> callback,
params string[] symbols) =>
await ConnectWebsocketPublicAsync(
async (socket) => { await SubscribeToChannel(socket, "ticker", symbols); },
async (socket, symbol, sArray, token) =>
{
var tickers = new List<KeyValuePair<string, ExchangeTicker>>
{
new KeyValuePair<string, ExchangeTicker>(symbol,
await this.ParseTickerWebSocketAsync(symbol, token))
};
callback(tickers);
});

protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(
Func<KeyValuePair<string, ExchangeTrade>, Task> callback,
params string[] marketSymbols) =>
await ConnectWebsocketPublicAsync(
async (socket) => { await SubscribeToChannel(socket, "trades", marketSymbols); },
async (socket, symbol, sArray, token) =>
{
var trade = token.ParseTrade(amountKey: "quantity", priceKey: "price", typeKey: "takerSide",
timestampKey: "ts", TimestampType.UnixMilliseconds, idKey: "id");
await callback(new KeyValuePair<string, ExchangeTrade>(symbol, trade));
});

protected override async Task<IWebSocket> OnGetDeltaOrderBookWebSocketAsync(
Action<ExchangeOrderBook> callback,
int maxCount = 20,
params string[] marketSymbols)
{
return await ConnectWebsocketPublicAsync(
async (socket) =>
{
await SubscribeToOrderBookDepthChannel(socket, marketSymbols, maxCount);
}, (socket, symbol, sArray, token) =>
{
var book = token.ParseOrderBookFromJTokenArrays();
book.MarketSymbol = symbol;
callback(book);
return Task.CompletedTask;
});
}

protected override async Task<ExchangeOrderBook> OnGetOrderBookAsync(string marketSymbol, int maxCount = 100)
{
//https://api.poloniex.com/markets/{symbol}/orderBook?scale={scale}&limit={limit}
Expand Down Expand Up @@ -853,6 +808,65 @@ protected override async Task<IEnumerable<ExchangeTransaction>> OnGetDepositHist
return transactions;
}

protected override async Task<IWebSocket> OnGetTickersWebSocketAsync(
Action<IReadOnlyCollection<KeyValuePair<string, ExchangeTicker>>> callback,
params string[] symbols) =>
await ConnectWebsocketPublicAsync(
async (socket) => { await SubscribeToChannel(socket, "ticker", symbols); },
async (socket, symbol, sArray, token) =>
{
var tickers = new List<KeyValuePair<string, ExchangeTicker>>
{
new KeyValuePair<string, ExchangeTicker>(symbol,
await this.ParseTickerWebSocketAsync(symbol, token))
};
callback(tickers);
});

protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(
Func<KeyValuePair<string, ExchangeTrade>, Task> callback,
params string[] marketSymbols) =>
await ConnectWebsocketPublicAsync(
async (socket) => { await SubscribeToChannel(socket, "trades", marketSymbols); },
async (socket, symbol, sArray, token) =>
{
var trade = token.ParseTrade(amountKey: "quantity", priceKey: "price", typeKey: "takerSide",
timestampKey: "ts", TimestampType.UnixMilliseconds, idKey: "id");
await callback(new KeyValuePair<string, ExchangeTrade>(symbol, trade));
});

protected override async Task<IWebSocket> OnGetDeltaOrderBookWebSocketAsync(
Action<ExchangeOrderBook> callback,
int maxCount = 20,
params string[] marketSymbols) =>
await ConnectWebsocketPublicAsync(
async (socket) => { await SubscribeToOrderBookDepthChannel(socket, marketSymbols, maxCount); },
(socket, symbol, sArray, token) =>
{
var book = token.ParseOrderBookFromJTokenArrays();
book.MarketSymbol = symbol;
callback(book);
return Task.CompletedTask;
});

protected override async Task<IWebSocket> OnGetCandlesWebSocketAsync(
Func<MarketCandle, Task> callback,
int periodSeconds,
params string[] marketSymbols) =>
await ConnectWebsocketPublicAsync(
async (socket) =>
{
await SubscribeToChannel(socket,
$"candles_{CryptoUtility.SecondsToPeriodStringLongReverse(periodSeconds).ToLowerInvariant()}",
marketSymbols);
}, async (socket, symbol, sArray, token) =>
{
var candle = this.ParseCandle(token, symbol, periodSeconds, "open", "high", "low", "close",
"ts", TimestampType.UnixMilliseconds, "quantity", "amount", null, "tradeCount");

await callback(candle);
});

private static string ParseFeesCurrency(bool isBuy, string symbol)
{
string feesCurrency = null;
Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeSharp/Utility/CryptoUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ public static string SecondsToPeriodStringLong(int seconds)
}

/// <summary>
/// Convert seconds to a period string, i.e. SECOND_5, MINUTE_1, HOUR_2, DAY_3, WEEK_1week, MONTH_1, YEAR_1 etc.
/// Convert seconds to a period string, i.e. SECOND_5, MINUTE_1, HOUR_2, DAY_3, WEEK_1, MONTH_1, YEAR_1 etc.
/// </summary>
/// <param name="seconds">Seconds. Use 60 for minute, 3600 for hour, 3600*24 for day, 3600*24*30 for month.</param>
/// <returns>Period string</returns>
Expand Down