Skip to content

fix BinanceGroupCommon #677

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

Merged
merged 6 commits into from
Oct 10, 2021
Merged
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
54 changes: 24 additions & 30 deletions src/ExchangeSharp/API/Exchanges/BinanceGroup/BinanceGroupCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,17 @@ protected internal override async Task<IEnumerable<ExchangeMarket>> OnGetMarketS

protected override async Task<ExchangeTicker> OnGetTickerAsync(string marketSymbol)
{
JToken obj = await MakeJsonRequestAsync<JToken>("/ticker/24hr?symbol=" + marketSymbol);
JToken obj = await MakeJsonRequestAsync<JToken>("/ticker/24hr?symbol=" + marketSymbol, BaseUrlApi);
return await ParseTickerAsync(marketSymbol, obj);
}

protected override async Task<IEnumerable<KeyValuePair<string, ExchangeTicker>>> OnGetTickersAsync()
{
List<KeyValuePair<string, ExchangeTicker>> tickers = new List<KeyValuePair<string, ExchangeTicker>>();
string marketSymbol;
JToken obj = await MakeJsonRequestAsync<JToken>("/ticker/24hr");
JToken obj = await MakeJsonRequestAsync<JToken>("/ticker/24hr", BaseUrlApi);
foreach (JToken child in obj)
{
marketSymbol = child["symbol"].ToStringInvariant();
string marketSymbol = child["symbol"].ToStringInvariant();
tickers.Add(new KeyValuePair<string, ExchangeTicker>(marketSymbol, await ParseTickerAsync(marketSymbol, child)));
}
return tickers;
Expand Down Expand Up @@ -308,7 +307,7 @@ protected override async Task<IWebSocket> OnGetDeltaOrderBookWebSocketAsync(Acti

protected override async Task<ExchangeOrderBook> OnGetOrderBookAsync(string marketSymbol, int maxCount = 100)
{
JToken obj = await MakeJsonRequestAsync<JToken>("/depth?symbol=" + marketSymbol + "&limit=" + maxCount);
JToken obj = await MakeJsonRequestAsync<JToken>($"/depth?symbol={marketSymbol}&limit={maxCount}", BaseUrlApi);
return ExchangeAPIExtensions.ParseOrderBookFromJTokenArrays(obj, sequence: "lastUpdateId", maxCount: maxCount);
}

Expand Down Expand Up @@ -348,21 +347,17 @@ protected override async Task OnGetHistoricalTradesAsync(Func<IEnumerable<Exchan

protected override async Task<IEnumerable<ExchangeTrade>> OnGetRecentTradesAsync(string marketSymbol, int? limit = null)
{
List<ExchangeTrade> trades = new List<ExchangeTrade>();
//var maxRequestLimit = 1000; //hard coded for now, should add limit as an argument
//https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#compressedaggregate-trades-list
int maxRequestLimit = (limit == null || limit < 1 || limit > 1000) ? 1000 : (int)limit;

JToken obj = await MakeJsonRequestAsync<JToken>($"/aggTrades?symbol={marketSymbol}&limit={maxRequestLimit}");
//JToken obj = await MakeJsonRequestAsync<JToken>("/public/trades/" + marketSymbol + "?limit=" + maxRequestLimit + "?sort=DESC");
if(obj.HasValues) { //
foreach(JToken token in obj) {
var trade = token.ParseTrade("q", "p", "m", "T", TimestampType.UnixMilliseconds, "a", "false");
trades.Add(trade);
}
//https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md
var trades = new List<ExchangeTrade>();
var maxRequestLimit = (limit == null || limit < 1 || limit > 1000) ? 1000 : (int)limit;

JToken obj = await MakeJsonRequestAsync<JToken>($"/aggTrades?symbol={marketSymbol}&limit={maxRequestLimit}", BaseUrlApi);
if (obj.HasValues)
{
trades.AddRange(obj.Select(token =>
token.ParseTrade("q", "p", "m", "T", TimestampType.UnixMilliseconds, "a", "false")));
}
return trades.AsEnumerable().Reverse(); //Descending order (ie newest trades first)
//return trades;
}

public async Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, long startId, long? endId = null)
Expand Down Expand Up @@ -462,9 +457,8 @@ public async Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bo
}




protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(string marketSymbol, int periodSeconds, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(string marketSymbol,
int periodSeconds, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
{
/* [
[
Expand All @@ -482,25 +476,25 @@ protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(strin
"17928899.62484339" // Can be ignored
]] */

List<MarketCandle> candles = new List<MarketCandle>();
string url = "/klines?symbol=" + marketSymbol;
if (startDate != null)
{
url += "&startTime=" + (long)startDate.Value.UnixTimestampFromDateTimeMilliseconds();
url += "&endTime=" + ((endDate == null ? long.MaxValue : (long)endDate.Value.UnixTimestampFromDateTimeMilliseconds())).ToStringInvariant();
url += "&endTime=" +
((endDate == null ? long.MaxValue : (long)endDate.Value.UnixTimestampFromDateTimeMilliseconds()))
.ToStringInvariant();
}

if (limit != null)
{
url += "&limit=" + (limit.Value.ToStringInvariant());
}

url += "&interval=" + PeriodSecondsToString(periodSeconds);
JToken obj = await MakeJsonRequestAsync<JToken>(url);
foreach (JToken token in obj)
{
candles.Add(this.ParseCandle(token, marketSymbol, periodSeconds, 1, 2, 3, 4, 0, TimestampType.UnixMilliseconds, 5, 7));
}
JToken obj = await MakeJsonRequestAsync<JToken>(url, BaseUrlApi);

return candles;
return obj.Select(token => this.ParseCandle(token, marketSymbol, periodSeconds, 1, 2, 3, 4, 0,
TimestampType.UnixMilliseconds, 5, 7)).ToList();
}

protected override async Task<Dictionary<string, decimal>> OnGetAmountsAsync()
Expand Down Expand Up @@ -582,7 +576,7 @@ protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string
throw new InvalidOperationException("Binance single order details request requires symbol");
}
payload["symbol"] = marketSymbol!;

if (isClientOrderId) // Either orderId or origClientOrderId must be sent.
payload["origClientOrderId"] = orderId;
else
Expand Down