Skip to content

Commit 8d5199f

Browse files
authored
fix: Extends the order data from the kraken order placed (#813)
1 parent 140e4d7 commit 8d5199f

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

src/ExchangeSharp/API/Exchanges/Kraken/ExchangeKrakenAPI.cs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,19 @@ private async Task<ExchangeOrderResult> ParseHistoryOrder(string orderId, JToken
236236
//}
237237
// }
238238

239-
ExchangeOrderResult orderResult = new ExchangeOrderResult { OrderId = orderId };
240-
orderResult.Result = ExchangeAPIOrderResult.Filled;
241-
orderResult.Message = "";
242-
orderResult.MarketSymbol = order["pair"].ToStringInvariant();
243-
orderResult.IsBuy = (order["type"].ToStringInvariant() == "buy");
244-
orderResult.Amount = order["vol"].ConvertInvariant<decimal>();
245-
orderResult.Fees = order["fee"].ConvertInvariant<decimal>();
246-
orderResult.Price = order["price"].ConvertInvariant<decimal>();
247-
orderResult.AveragePrice = order["price"].ConvertInvariant<decimal>();
248-
orderResult.TradeId = order["postxid"].ToStringInvariant(); //verify which is orderid & tradeid
239+
ExchangeOrderResult orderResult = new ExchangeOrderResult
240+
{
241+
OrderId = orderId,
242+
Result = ExchangeAPIOrderResult.Filled,
243+
Message = "",
244+
MarketSymbol = order["pair"].ToStringInvariant(),
245+
IsBuy = order["type"].ToStringInvariant() == "buy",
246+
Amount = order["vol"].ConvertInvariant<decimal>(),
247+
Fees = order["fee"].ConvertInvariant<decimal>(),
248+
Price = order["price"].ConvertInvariant<decimal>(),
249+
AveragePrice = order["price"].ConvertInvariant<decimal>(),
250+
TradeId = order["postxid"].ToStringInvariant() //verify which is orderid & tradeid
251+
};
249252
orderResult.OrderId = order["ordertxid"].ToStringInvariant(); //verify which is orderid & tradeid
250253
orderResult.AmountFilled = order["vol"].ConvertInvariant<decimal>();
251254
// orderResult.OrderDate - not provided here. ideally would be null but ExchangeOrderResult.OrderDate is not nullable
@@ -258,6 +261,21 @@ private async Task<ExchangeOrderResult> ParseHistoryOrder(string orderId, JToken
258261
return orderResult;
259262
}
260263

264+
internal ExchangeOrderResult ExtendResultsWithOrderDescr(ExchangeOrderResult result, string orderStr)
265+
{
266+
//"buy 0.00000001 XBTUSD @ limit 1000000"
267+
//"buy 58.00000000 ADAUSDT @ market"
268+
string[] orderStrParts = orderStr.Split(' ');
269+
result.IsBuy = string.Equals(orderStrParts[0], "buy", StringComparison.InvariantCultureIgnoreCase);
270+
result.Amount = orderStrParts[1].ConvertInvariant<decimal>();
271+
result.MarketSymbol = orderStrParts[2];
272+
var isMarket = string.Equals(orderStrParts[4], "market", StringComparison.InvariantCultureIgnoreCase);
273+
if (!isMarket) {
274+
result.Price = orderStrParts[5].ConvertInvariant<decimal>();
275+
}
276+
return result;
277+
}
278+
261279
private async Task<IEnumerable<ExchangeOrderResult>> QueryOrdersAsync(string symbol, string path)
262280
{
263281
await PopulateLookupTables();
@@ -496,12 +514,12 @@ protected override async Task<IEnumerable<KeyValuePair<string, ExchangeTicker>>>
496514
{
497515
tickers.Add(new KeyValuePair<string, ExchangeTicker>(marketSymbol, await ConvertToExchangeTickerAsync(marketSymbol, ticker)));
498516
}
499-
catch(Exception e)
517+
catch (Exception e)
500518
{
501519
Logger.Error(e);
502520
}
503521
}
504-
if(unfoundSymbols.Count > 0)
522+
if (unfoundSymbols.Count > 0)
505523
{
506524
Logger.Warn($"Of {marketSymbols.Count()} symbols, tickers could not be found for {unfoundSymbols.Count}: [{String.Join(", ", unfoundSymbols)}]");
507525
}
@@ -700,12 +718,17 @@ protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrd
700718
JToken token = await MakeJsonRequestAsync<JToken>("/0/private/AddOrder", null, payload);
701719
ExchangeOrderResult result = new ExchangeOrderResult
702720
{
703-
OrderDate = CryptoUtility.UtcNow
721+
OrderDate = CryptoUtility.UtcNow,
722+
Result = ExchangeAPIOrderResult.Open
704723
};
705724
if (token["txid"] is JArray array)
706725
{
707726
result.OrderId = array[0].ToStringInvariant();
708727
}
728+
if (token["descr"] is JObject descrArray)
729+
{
730+
result = ExtendResultsWithOrderDescr(result, descrArray["order"].ToStringInvariant());
731+
}
709732
return result;
710733
}
711734

@@ -869,7 +892,7 @@ [3]pair string Asset pair
869892
string marketSymbol = token[3].ToStringInvariant();
870893
//Kraken updates the candle open time to the current time, but we want it as open-time i.e. close-time - interval
871894
token[1][0] = token[1][1].ConvertInvariant<long>() - interval * 60;
872-
var candle = this.ParseCandle(token[1], marketSymbol, interval * 60, 2, 3, 4, 5, 0, TimestampType.UnixSeconds, 7, null, 6,8);
895+
var candle = this.ParseCandle(token[1], marketSymbol, interval * 60, 2, 3, 4, 5, 0, TimestampType.UnixSeconds, 7, null, 6, 8);
873896
await callbackAsync(candle);
874897
}
875898
}, connectCallback: async (_socket) =>
@@ -895,7 +918,7 @@ protected override async Task<IWebSocket> OnGetPositionsWebSocketAsync(Action<Ex
895918
{
896919
if (token.Count == 3 && token[1].ToString() == "openOrders")
897920
{
898-
foreach(JToken element in token[0])
921+
foreach (JToken element in token[0])
899922
{
900923
if (element is JObject position)
901924
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Threading.Tasks;
2+
using ExchangeSharp;
3+
using FluentAssertions;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Newtonsoft.Json;
6+
7+
namespace ExchangeSharpTests
8+
{
9+
[TestClass]
10+
public sealed class ExchangeKrakenTests
11+
{
12+
private ExchangeKrakenAPI api;
13+
14+
[TestInitialize()]
15+
public async Task Startup()
16+
{
17+
api = (await ExchangeAPI.GetExchangeAPIAsync(ExchangeName.Kraken)).As<ExchangeKrakenAPI>();
18+
return;
19+
}
20+
21+
[TestMethod]
22+
public void ExtendResultsWithOrderDescrTest()
23+
{
24+
string toParse = "buy 58.00000000 ADAUSDT @ market";
25+
var extendedOrder = api.ExtendResultsWithOrderDescr(new ExchangeOrderResult(), toParse);
26+
27+
extendedOrder.IsBuy.Should().BeTrue();
28+
extendedOrder.Amount.Should().Be(58);
29+
extendedOrder.MarketSymbol.Should().Be("ADAUSDT");
30+
}
31+
32+
[TestMethod]
33+
public void ExtendResultsWithOrderDescrAndPriceTest()
34+
{
35+
string toParse = "buy 0.001254 BTCUSDT @ limit 1000";
36+
var extendedOrder = api.ExtendResultsWithOrderDescr(new ExchangeOrderResult(), toParse);
37+
38+
extendedOrder.IsBuy.Should().BeTrue();
39+
extendedOrder.Amount.Should().Be(0.001254);
40+
extendedOrder.MarketSymbol.Should().Be("BTCUSDT");
41+
extendedOrder.Price.Should().Be(1000);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)