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

add trade stream to LBank #741

Merged
merged 1 commit into from
Feb 20, 2022
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
59 changes: 59 additions & 0 deletions src/ExchangeSharp/API/Exchanges/LBank/ExchangeLBankAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class ExchangeLBankAPI : ExchangeAPI
/// </summary>
public override string BaseUrl { get; set; } = "https://api.lbank.info/v1";

public override string BaseUrlWebSocket { get; set; } = "wss://www.lbkex.net/ws/V2/";

/// <summary>
/// Gets the name of the API.
/// </summary>
Expand Down Expand Up @@ -539,6 +541,63 @@ private List<ExchangeWithdrawalResponse> ParseWithdrawListResponse(JToken withdr

#endregion PARSERS PrivateAPI

#region Websockets
protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValuePair<string, ExchangeTrade>, Task> callback, params string[] marketSymbols)
{
if (marketSymbols == null || marketSymbols.Length == 0)
{
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
}
return await ConnectPublicWebSocketAsync("", async (_socket, msg) =>
{
/* {
"trade":{
"volume":6.3607,
"amount":77148.9303,
"price":12129,
"direction":"sell",
"TS":"2019-06-28T19:55:49.460"
},
"type":"trade",
"pair":"btc_usdt",
"SERVER":"V2",
"TS":"2019-06-28T19:55:49.466"
}*/
JToken token = JToken.Parse(msg.ToStringFromUTF8());
if (token["status"].ToStringInvariant() == "error")
{
if (token["message"].ToStringInvariant().Contains("Invalid order pairs"))
{
// ignore, bc invalid order pairs are normal in LBank
}
else throw new APIException(token["message"].ToStringInvariant());
}
else if (token["type"].ToStringInvariant() == "trade")
{
var trade = token["trade"].ParseTrade("amount", "price", "direction", "TS", TimestampType.Iso8601China, null);
string marketSymbol = token["pair"].ToStringInvariant();
await callback(new KeyValuePair<string, ExchangeTrade>(marketSymbol, trade));
}
}, async (_socket) =>
{ /* {
"action":"subscribe",
"subscribe":"trade",
"pair":"eth_btc"
}*/
foreach (var marketSymbol in marketSymbols)
{
var subscribeRequest = new
{
action = "subscribe",
subscribe = "trade",
pair = marketSymbol,
};
await _socket.SendMessageAsync(subscribeRequest);
}
});
}
#endregion

#region HELPERS

protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary<string, object> payload)
Expand Down
14 changes: 13 additions & 1 deletion src/ExchangeSharp/Utility/CryptoUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public static class CryptoUtility
internal static readonly DateTime UnixEpochLocal = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
internal static readonly Encoding Utf8EncodingNoPrefix = new UTF8Encoding(false, true);
static bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
static string chinaZoneId = isWindows ? "China Standard Time" : "Asia/Shanghai";
static TimeZoneInfo chinaZone = TimeZoneInfo.FindSystemTimeZoneById(chinaZoneId);
static string koreanZoneId = isWindows ? "Korea Standard Time" : "Asia/Seoul";
static TimeZoneInfo koreaZone = TimeZoneInfo.FindSystemTimeZoneById(koreanZoneId);

Expand Down Expand Up @@ -188,7 +190,7 @@ public enum SourceTimeZone
{
/// <summary> time zone is specifically specified in string </summary>
AsSpecified,
Local, Korea, UTC
Local, China, Korea, UTC,
}
/// <summary>
/// Convert object to a UTC DateTime
Expand All @@ -215,6 +217,8 @@ public static DateTime ToDateTimeInvariant(this object obj, SourceTimeZone sourc
throw new NotImplementedException(); // TODO: implement this when needed
case SourceTimeZone.Local:
return DateTime.SpecifyKind(dt, DateTimeKind.Local).ToUniversalTime(); // convert to UTC
case SourceTimeZone.China:
return TimeZoneInfo.ConvertTime(dt, chinaZone, TimeZoneInfo.Utc); // convert to UTC
case SourceTimeZone.Korea:
return TimeZoneInfo.ConvertTime(dt, koreaZone, TimeZoneInfo.Utc); // convert to UTC
case SourceTimeZone.UTC:
Expand Down Expand Up @@ -693,6 +697,9 @@ public static DateTime ParseTimestamp(object value, TimestampType type)
case TimestampType.Iso8601Local:
return value.ToDateTimeInvariant(SourceTimeZone.Local);

case TimestampType.Iso8601China:
return value.ToDateTimeInvariant(SourceTimeZone.China);

case TimestampType.Iso8601Korea:
return value.ToDateTimeInvariant(SourceTimeZone.Korea);

Expand Down Expand Up @@ -1481,6 +1488,11 @@ public enum TimestampType
/// </summary>
Iso8601Local,

/// <summary>
/// ISO 8601 in china Standard Time
/// </summary>
Iso8601China,

/// <summary>
/// ISO 8601 in Korea Standard Time
/// </summary>
Expand Down