Skip to content

Commit e702b3b

Browse files
authored
Fix KuCoin PeriodSecondsToString (#833)
* Fix Kucoin PeriodSecondsToString method Fix PeriodSecondsToString returning wrong daily and weekly periods. * Remove unnecessary assignment of startDate and endDate * Remove redundant else keywords in CryptoUtility.SecondsToPeriodStringLong * Remove redundant else keywords in CryptoUtility.SecondsToPeriodString * Remove unnecessary using directives in CryptoUtility
1 parent 6d246e9 commit e702b3b

File tree

2 files changed

+30
-55
lines changed

2 files changed

+30
-55
lines changed

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

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,7 @@ private ExchangeKuCoinAPI()
4444
WebSocketOrderBookType = WebSocketOrderBookType.FullBookFirstThenDeltas;
4545
}
4646

47-
public override string PeriodSecondsToString(int seconds)
48-
{
49-
switch (seconds)
50-
{
51-
case 60:
52-
return "1min";
53-
case 180:
54-
return "3min";
55-
case 300:
56-
return "5min";
57-
case 900:
58-
return "15min";
59-
case 1800:
60-
return "30min";
61-
case 3600:
62-
return "1hour";
63-
case 7200:
64-
return "2hour";
65-
case 14400:
66-
return "4hour";
67-
case 21600:
68-
return "6hour";
69-
case 28800:
70-
return "8hour";
71-
case 43200:
72-
return "12hour";
73-
case 86400:
74-
return "1D";
75-
case 604800:
76-
return "1W";
77-
default:
78-
throw new ArgumentException(
79-
$"{nameof(seconds)} must be 60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 28800, 43200, 86400, 604800"
80-
);
81-
}
82-
}
47+
public override string PeriodSecondsToString(int seconds) => CryptoUtility.SecondsToPeriodStringLong(seconds);
8348

8449
protected override JToken CheckJsonResponse(JToken result)
8550
{
@@ -401,22 +366,25 @@ protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(
401366
List<MarketCandle> candles = new List<MarketCandle>();
402367

403368
string periodString = PeriodSecondsToString(periodSeconds);
404-
endDate = endDate ?? CryptoUtility.UtcNow;
405-
startDate = startDate ?? CryptoUtility.UtcNow.AddDays(-1);
406-
407369
var payload = new Dictionary<string, object>
408370
{
409371
{ "symbol", marketSymbol },
410-
{ "type", periodString },
411-
{ "startAt", (long)startDate.Value.UnixTimestampFromDateTimeSeconds() }, // the nonce is milliseconds, this is seconds without decimal
412-
{ "endAt", (long)endDate.Value.UnixTimestampFromDateTimeSeconds() } // the nonce is milliseconds, this is seconds without decimal
372+
{ "type", periodString }
413373
};
414-
var addPayload = CryptoUtility.GetFormForPayload(payload, false);
374+
375+
if (startDate != null)
376+
{
377+
payload.Add("startAt", (long)startDate.Value.UnixTimestampFromDateTimeSeconds());
378+
}
379+
if (endDate != null)
380+
{
381+
payload.Add("endAt", (long)endDate.Value.UnixTimestampFromDateTimeSeconds());
382+
}
415383

416384
// The results of this Kucoin API call are also a mess. 6 different arrays (c,t,v,h,l,o) with the index of each shared for the candle values
417385
// It doesn't use their standard error format...
418386
JToken token = await MakeJsonRequestAsync<JToken>(
419-
"/market/candles?" + addPayload,
387+
"/market/candles?" + payload.GetFormForPayload(false),
420388
null,
421389
payload
422390
);

src/ExchangeSharp/Utility/CryptoUtility.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ The above copyright notice and this permission notice shall be included in all c
1212
#nullable enable
1313
using System;
1414
using System.Collections.Generic;
15-
using System.Collections.Specialized;
1615
using System.Globalization;
1716
using System.IO;
1817
using System.IO.Compression;
1918
using System.Linq;
2019
using System.Net;
21-
using System.Runtime.CompilerServices;
2220
using System.Runtime.InteropServices;
2321
using System.Security;
2422
using System.Security.Cryptography;
@@ -1329,19 +1327,23 @@ public static string SecondsToPeriodString(int seconds, bool capitalAfterMinute
13291327
{
13301328
return seconds / monthThreshold + "M";
13311329
}
1332-
else if (seconds >= weekThreshold)
1330+
1331+
if (seconds >= weekThreshold)
13331332
{
13341333
return seconds / weekThreshold + (capitalAfterMinute ? "W" : "w");
13351334
}
1336-
else if (seconds >= dayThreshold)
1335+
1336+
if (seconds >= dayThreshold)
13371337
{
13381338
return seconds / dayThreshold + (capitalAfterMinute ? "D" : "d");
13391339
}
1340-
else if (seconds >= hourThreshold)
1340+
1341+
if (seconds >= hourThreshold)
13411342
{
13421343
return seconds / hourThreshold + (capitalAfterMinute ? "H" : "h");
13431344
}
1344-
else if (seconds >= minuteThreshold)
1345+
1346+
if (seconds >= minuteThreshold)
13451347
{
13461348
return seconds / minuteThreshold + "m";
13471349
}
@@ -1366,23 +1368,28 @@ public static string SecondsToPeriodStringLong(int seconds)
13661368
{
13671369
return seconds / yearThreshold + "year";
13681370
}
1369-
else if (seconds >= monthThreshold)
1371+
1372+
if (seconds >= monthThreshold)
13701373
{
13711374
return seconds / monthThreshold + "mon";
13721375
}
1373-
else if (seconds >= weekThreshold)
1376+
1377+
if (seconds >= weekThreshold)
13741378
{
13751379
return seconds / weekThreshold + "week";
13761380
}
1377-
else if (seconds >= dayThreshold)
1381+
1382+
if (seconds >= dayThreshold)
13781383
{
13791384
return seconds / dayThreshold + "day";
13801385
}
1381-
else if (seconds >= hourThreshold)
1386+
1387+
if (seconds >= hourThreshold)
13821388
{
13831389
return seconds / hourThreshold + "hour";
13841390
}
1385-
else if (seconds >= minuteThreshold)
1391+
1392+
if (seconds >= minuteThreshold)
13861393
{
13871394
return seconds / minuteThreshold + "min";
13881395
}

0 commit comments

Comments
 (0)