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

Fix KuCoin PeriodSecondsToString #833

Merged
merged 5 commits into from
Apr 6, 2024
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
56 changes: 12 additions & 44 deletions src/ExchangeSharp/API/Exchanges/KuCoin/ExchangeKuCoinAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,7 @@ private ExchangeKuCoinAPI()
WebSocketOrderBookType = WebSocketOrderBookType.FullBookFirstThenDeltas;
}

public override string PeriodSecondsToString(int seconds)
{
switch (seconds)
{
case 60:
return "1min";
case 180:
return "3min";
case 300:
return "5min";
case 900:
return "15min";
case 1800:
return "30min";
case 3600:
return "1hour";
case 7200:
return "2hour";
case 14400:
return "4hour";
case 21600:
return "6hour";
case 28800:
return "8hour";
case 43200:
return "12hour";
case 86400:
return "1D";
case 604800:
return "1W";
default:
throw new ArgumentException(
$"{nameof(seconds)} must be 60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 28800, 43200, 86400, 604800"
);
}
}
public override string PeriodSecondsToString(int seconds) => CryptoUtility.SecondsToPeriodStringLong(seconds);

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

string periodString = PeriodSecondsToString(periodSeconds);
endDate = endDate ?? CryptoUtility.UtcNow;
startDate = startDate ?? CryptoUtility.UtcNow.AddDays(-1);

var payload = new Dictionary<string, object>
{
{ "symbol", marketSymbol },
{ "type", periodString },
{ "startAt", (long)startDate.Value.UnixTimestampFromDateTimeSeconds() }, // the nonce is milliseconds, this is seconds without decimal
{ "endAt", (long)endDate.Value.UnixTimestampFromDateTimeSeconds() } // the nonce is milliseconds, this is seconds without decimal
{ "type", periodString }
};
var addPayload = CryptoUtility.GetFormForPayload(payload, false);

if (startDate != null)
{
payload.Add("startAt", (long)startDate.Value.UnixTimestampFromDateTimeSeconds());
}
if (endDate != null)
{
payload.Add("endAt", (long)endDate.Value.UnixTimestampFromDateTimeSeconds());
}

// 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
// It doesn't use their standard error format...
JToken token = await MakeJsonRequestAsync<JToken>(
"/market/candles?" + addPayload,
"/market/candles?" + payload.GetFormForPayload(false),
null,
payload
);
Expand Down
29 changes: 18 additions & 11 deletions src/ExchangeSharp/Utility/CryptoUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ The above copyright notice and this permission notice shall be included in all c
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
Expand Down Expand Up @@ -1329,19 +1327,23 @@ public static string SecondsToPeriodString(int seconds, bool capitalAfterMinute
{
return seconds / monthThreshold + "M";
}
else if (seconds >= weekThreshold)

if (seconds >= weekThreshold)
{
return seconds / weekThreshold + (capitalAfterMinute ? "W" : "w");
}
else if (seconds >= dayThreshold)

if (seconds >= dayThreshold)
{
return seconds / dayThreshold + (capitalAfterMinute ? "D" : "d");
}
else if (seconds >= hourThreshold)

if (seconds >= hourThreshold)
{
return seconds / hourThreshold + (capitalAfterMinute ? "H" : "h");
}
else if (seconds >= minuteThreshold)

if (seconds >= minuteThreshold)
{
return seconds / minuteThreshold + "m";
}
Expand All @@ -1366,23 +1368,28 @@ public static string SecondsToPeriodStringLong(int seconds)
{
return seconds / yearThreshold + "year";
}
else if (seconds >= monthThreshold)

if (seconds >= monthThreshold)
{
return seconds / monthThreshold + "mon";
}
else if (seconds >= weekThreshold)

if (seconds >= weekThreshold)
{
return seconds / weekThreshold + "week";
}
else if (seconds >= dayThreshold)

if (seconds >= dayThreshold)
{
return seconds / dayThreshold + "day";
}
else if (seconds >= hourThreshold)

if (seconds >= hourThreshold)
{
return seconds / hourThreshold + "hour";
}
else if (seconds >= minuteThreshold)

if (seconds >= minuteThreshold)
{
return seconds / minuteThreshold + "min";
}
Expand Down
Loading