Skip to content

Commit 239be68

Browse files
authored
KuCoin: fix GetRecentTradesAsync() and GetHistoricalTradesAsync() (#513)
- changed endpoint for GetRecentTradesAsync() - changed TS parsing to UnixNanoseconds for both - DRY both methods - fixes #454
1 parent 4973501 commit 239be68

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

src/ExchangeSharp/API/Exchanges/KuCoin/ExchangeKuCoinAPI.cs

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -209,40 +209,64 @@ protected override async Task<IEnumerable<KeyValuePair<string, ExchangeTicker>>>
209209
}
210210

211211
protected override async Task<IEnumerable<ExchangeTrade>> OnGetRecentTradesAsync(string marketSymbol, int? limit = null)
212-
{
213-
List<ExchangeTrade> trades = new List<ExchangeTrade>();
214-
// [0]-Timestamp [1]-OrderType [2]-Price [3]-Amount [4]-Volume
215-
// [[1506037604000,"SELL",5210,48600633397,2532093],... ]
216-
JToken token = await MakeJsonRequestAsync<JToken>("/orders?status=active&symbol=" + marketSymbol, payload: await GetNoncePayloadAsync());
217-
foreach (JToken trade in token)
218-
{
219-
trades.Add(trade.ParseTrade("size", "price", "side", "time", TimestampType.UnixMilliseconds, idKey: "tradeId"));
212+
{
213+
List<ExchangeTrade> trades = await fetchTradeHistory(marketSymbol: marketSymbol, startDate: null, limit: limit);
214+
return trades.AsEnumerable().Reverse(); //descending - ie from newest to oldest trades
215+
}
220216

217+
protected override async Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback,
218+
string marketSymbol, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
219+
{
220+
if (endDate != null)
221+
{
222+
throw new APIException("KuCoin does not allow specifying endDate");
221223
}
222-
return trades;
223-
}
224+
List<ExchangeTrade> trades = await fetchTradeHistory(marketSymbol: marketSymbol, startDate: startDate, limit: limit);
225+
callback?.Invoke(trades);
226+
}
224227

225-
protected override async Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
226-
{
227-
List<ExchangeTrade> trades = new List<ExchangeTrade>();
228-
JToken token = await MakeJsonRequestAsync<JToken>("/market/histories?symbol=" + marketSymbol + (startDate == null ? string.Empty : "&since=" + startDate.Value.UnixTimestampFromDateTimeMilliseconds()));
229-
foreach (JObject trade in token)
230-
{
231-
trades.Add(trade.ParseTrade("size", "price", "side", "time", TimestampType.UnixMilliseconds, idKey: "tradeId"));
232-
}
233-
var rc = callback?.Invoke(trades);
234-
}
228+
async Task<List<ExchangeTrade>> fetchTradeHistory(string marketSymbol, DateTime? startDate, int? limit)
229+
{
230+
if (limit != null && limit != 100)
231+
{
232+
throw new ArgumentException("limit is always 100 in KuCoin");
233+
}
234+
List<ExchangeTrade> trades = new List<ExchangeTrade>();
235+
JToken token = await MakeJsonRequestAsync<JToken>(
236+
"/market/histories?symbol=" + marketSymbol +
237+
(startDate == null ? string.Empty : "&since=" + startDate.Value.UnixTimestampFromDateTimeMilliseconds()));
238+
/* {[ {
239+
"sequence": "1568570510897",
240+
"side": "buy",
241+
"size": "0.0025824",
242+
"price": "168.48",
243+
"time": 1579661286138826064
244+
},
245+
{
246+
"sequence": "1568570510943",
247+
"side": "buy",
248+
"size": "0.009223",
249+
"price": "168.48",
250+
"time": 1579661286980037641
251+
}, ... ]} */
252+
foreach (JObject trade in token)
253+
{
254+
trades.Add(trade.ParseTrade(amountKey: "size", priceKey: "price", typeKey: "side",
255+
timestampKey: "time", timestampType: TimestampType.UnixNanoseconds, idKey: "sequence"));
256+
}
257+
return trades;
258+
}
235259

236-
/// <summary>
237-
/// This is a private call on Kucoin and therefore requires an API Key + API Secret. Calling this without authorization will cause an exception
238-
/// </summary>
239-
/// <param name="marketSymbol"></param>
240-
/// <param name="periodSeconds"></param>
241-
/// <param name="startDate"></param>
242-
/// <param name="endDate"></param>
243-
/// <param name="limit"></param>
244-
/// <returns></returns>
245-
protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(string marketSymbol, int periodSeconds, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
260+
/// <summary>
261+
/// This is a private call on Kucoin and therefore requires an API Key + API Secret. Calling this without authorization will cause an exception
262+
/// </summary>
263+
/// <param name="marketSymbol"></param>
264+
/// <param name="periodSeconds"></param>
265+
/// <param name="startDate"></param>
266+
/// <param name="endDate"></param>
267+
/// <param name="limit"></param>
268+
/// <returns></returns>
269+
protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(string marketSymbol, int periodSeconds, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
246270
{
247271
List<MarketCandle> candles = new List<MarketCandle>();
248272

0 commit comments

Comments
 (0)