Skip to content

fixed GetHistoricalTradesAsync/GetRecentTradesAsync() issues #511

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 1 commit into from
Jan 22, 2020
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
99 changes: 88 additions & 11 deletions src/ExchangeSharp/API/Exchanges/BinanceGroup/BinanceGroupCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected override async Task<ExchangeOrderBook> OnGetOrderBookAsync(string mark
return ExchangeAPIExtensions.ParseOrderBookFromJTokenArrays(obj, sequence: "lastUpdateId", maxCount: maxCount);
}

protected override async Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, DateTime? startDate = null, DateTime? endDate = null)
protected override async Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
{
/* [ {
"a": 26129, // Aggregate tradeId
Expand All @@ -345,17 +345,44 @@ protected override async Task OnGetHistoricalTradesAsync(Func<IEnumerable<Exchan
"M": true // Was the trade the best price match?
} ] */

ExchangeHistoricalTradeHelper state = new ExchangeHistoricalTradeHelper(this)
//if(startDate == null && endDate == null) {
// await OnGetRecentTradesAsync(marketSymbol, limit);
//}
//else {
//System.Windows.Forms.MessageBox.Show("Duplicate MessageId " + MessageId, "HMMMM RETURN?");


ExchangeHistoricalTradeHelper state = new ExchangeHistoricalTradeHelper(this)
{
Callback = callback,
EndDate = endDate,
ParseFunction = (JToken token) => token.ParseTrade("q", "p", "m", "T", TimestampType.UnixMilliseconds, "a", "false"),
StartDate = startDate,
MarketSymbol = marketSymbol,
TimestampFunction = (DateTime dt) => ((long)CryptoUtility.UnixTimestampFromDateTimeMilliseconds(dt)).ToStringInvariant(),
Url = "/aggTrades?symbol=[marketSymbol]&startTime={0}&endTime={1}",
};
await state.ProcessHistoricalTrades();
Callback = callback,
EndDate = endDate,
ParseFunction = (JToken token) => token.ParseTrade("q", "p", "m", "T", TimestampType.UnixMilliseconds, "a", "false"),
StartDate = startDate,
MarketSymbol = marketSymbol,
TimestampFunction = (DateTime dt) => ((long)CryptoUtility.UnixTimestampFromDateTimeMilliseconds(dt)).ToStringInvariant(),
Url = "/aggTrades?symbol=[marketSymbol]&startTime={0}&endTime={1}",
};
await state.ProcessHistoricalTrades();
//}
}

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);
}
}
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 @@ -407,6 +434,56 @@ public async Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bo
} while (callback(trades) && trades.Count > 0);
}

public async Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, int limit = 100)
{
/* [ {
"a": 26129, // Aggregate tradeId
"p": "0.01633102", // Price
"q": "4.70443515", // Quantity
"f": 27781, // First tradeId
"l": 27781, // Last tradeId
"T": 1498793709153, // Timestamp
"m": true, // Was the buyer the maker?
"M": true // Was the trade the best price match?
} ] */

// TODO : Refactor into a common layer once more Exchanges implement this pattern
// https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#compressedaggregate-trades-list
if(limit > 1000) limit = 1000; //Binance max = 1000
var maxRequestLimit = 1000;
var trades = new List<ExchangeTrade>();
var processedIds = new HashSet<long>();
marketSymbol = NormalizeMarketSymbol(marketSymbol);

do {
//if(fromId > endId)
// break;

trades.Clear();
//var limit = Math.Min(endId - fromId ?? maxRequestLimit, maxRequestLimit);
var obj = await MakeJsonRequestAsync<JToken>($"/aggTrades?symbol={marketSymbol}&limit={limit}");

foreach(var token in obj) {
var trade = token.ParseTrade("q", "p", "m", "T", TimestampType.UnixMilliseconds, "a", "false");
//long tradeId = (long)trade.Id.ConvertInvariant<ulong>();
//if(tradeId < fromId)
// continue;
//if(tradeId > endId)
// continue;
//if(!processedIds.Add(tradeId))
// continue;

trades.Add(trade);
//fromId = tradeId;
}

//fromId++;
} while(callback(trades) && trades.Count > 0);
}




protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(string marketSymbol, int periodSeconds, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
{
/* [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(strin

protected override async Task<Dictionary<string, decimal>> OnGetAmountsAsync() => await OnGetAmountsAsyncCore("onhand_amount");

protected override Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, DateTime? startDate = null, DateTime? endDate = null)
protected override Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
{
throw new NotImplementedException();
}
Expand Down
Loading