From b0805724de94bc223e27d402c5a2299c401d46c6 Mon Sep 17 00:00:00 2001 From: atp4al <36592911+atp4al@users.noreply.github.com> Date: Thu, 19 Jul 2018 17:19:50 +0200 Subject: [PATCH] Improve Bitstamp API and add a way to retrieve fees (#212) * Add the possibility to send market orders with Bitstamp When the order type is Market: - we add "/market" in the url - we do not send the price * Delete .orig file * Add .nuget * Add the possibility to send market orders with Bitstamp When the order type is Market: - we add "/market" in the url - we do not send the price * Add .nuget * Add a way to retrieve fees for each currency pair and implement the method for Bitstamp Refactor the methods OnGetAmountsAsync, OnGetAmountsAvailableToTradeAsync to use the same logic as in OnGetFeesAsync --- .nuget/Nuget.config | 9 ++++ .../Exchanges/Bitstamp/ExchangeBitstampAPI.cs | 53 ++++++++++++------- .../API/Exchanges/_Base/ExchangeAPI.cs | 19 +++++++ .../API/Exchanges/_Base/IExchangeAPI.cs | 12 +++++ 4 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 .nuget/Nuget.config diff --git a/.nuget/Nuget.config b/.nuget/Nuget.config new file mode 100644 index 00000000..49430d5e --- /dev/null +++ b/.nuget/Nuget.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/ExchangeSharp/API/Exchanges/Bitstamp/ExchangeBitstampAPI.cs b/ExchangeSharp/API/Exchanges/Bitstamp/ExchangeBitstampAPI.cs index cf00d5b1..802d01b7 100644 --- a/ExchangeSharp/API/Exchanges/Bitstamp/ExchangeBitstampAPI.cs +++ b/ExchangeSharp/API/Exchanges/Bitstamp/ExchangeBitstampAPI.cs @@ -151,36 +151,45 @@ protected override async Task> OnGetAmountsAsync() { string url = "/balance/"; var payload = await OnGetNoncePayloadAsync(); - JObject responseObject = await MakeJsonRequestAsync(url, null, payload, "POST"); - Dictionary balances = new Dictionary(); - foreach (var property in responseObject) - { - if (property.Key.Contains("_balance")) - { - decimal balance = property.Value.ConvertInvariant(); - if (balance == 0) { continue; } - balances.Add(property.Key.Replace("_balance", "").Trim(), balance); - } - } - return balances; + var responseObject = await MakeJsonRequestAsync(url, null, payload, "POST"); + return ExtractDictionary(responseObject, "balance"); + } + + + protected override async Task> OnGetFeesAsync() + { + string url = "/balance/"; + var payload = await OnGetNoncePayloadAsync(); + var responseObject = await MakeJsonRequestAsync(url, null, payload, "POST"); + return ExtractDictionary(responseObject, "fee"); } protected override async Task> OnGetAmountsAvailableToTradeAsync() { string url = "/balance/"; var payload = await OnGetNoncePayloadAsync(); - JObject responseObject = await MakeJsonRequestAsync(url, null, payload, "POST"); - Dictionary balances = new Dictionary(); + var responseObject = await MakeJsonRequestAsync(url, null, payload, "POST"); + return ExtractDictionary(responseObject, "available"); + } + + private static Dictionary ExtractDictionary(JObject responseObject, string key) + { + var result = new Dictionary(); + var suffix = $"_{key}"; foreach (var property in responseObject) { - if (property.Key.Contains("_available")) + if (property.Key.Contains(suffix)) { - decimal balance = property.Value.ConvertInvariant(); - if (balance == 0) { continue; } - balances.Add(property.Key.Replace("_available", "").Trim(), balance); + decimal value = property.Value.ConvertInvariant(); + if (value == 0) + { + continue; + } + + result.Add(property.Key.Replace(suffix, "").Trim(), value); } } - return balances; + return result; } protected override async Task OnPlaceOrderAsync(ExchangeOrderRequest order) @@ -197,6 +206,12 @@ protected override async Task OnPlaceOrderAsync(ExchangeOrd payload["price"] = order.Price.ToStringInvariant(); } + payload["amount"] = order.Amount.ToStringInvariant(); + foreach (var kv in order.ExtraParameters) + { + payload["price"] = order.Price.ToStringInvariant(); + } + payload["amount"] = order.RoundAmount().ToStringInvariant(); order.ExtraParameters.CopyTo(payload); diff --git a/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs b/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs index 5c5b7067..3bd2a4a6 100644 --- a/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs +++ b/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs @@ -118,6 +118,7 @@ await GetHistoricalTradesAsync((e) => protected virtual Task> OnGetDepositHistoryAsync(string symbol) => throw new NotImplementedException(); protected virtual Task> OnGetCandlesAsync(string symbol, int periodSeconds, DateTime? startDate = null, DateTime? endDate = null, int? limit = null) => throw new NotImplementedException(); protected virtual Task> OnGetAmountsAsync() => throw new NotImplementedException(); + protected virtual Task> OnGetFeesAsync() => throw new NotImplementedException(); protected virtual Task> OnGetAmountsAvailableToTradeAsync() => throw new NotImplementedException(); protected virtual Task OnPlaceOrderAsync(ExchangeOrderRequest order) => throw new NotImplementedException(); protected virtual Task OnGetOrderDetailsAsync(string orderId, string symbol = null) => throw new NotImplementedException(); @@ -846,6 +847,24 @@ public async Task> GetAmountsAsync() return await OnGetAmountsAsync(); } + + /// + /// Get fees + /// + /// The customer trading fees + public Dictionary GetFees() => GetFeesAync().GetAwaiter().GetResult(); + + /// + /// ASYNC - Get fees + /// + /// The customer trading fees + public async Task> GetFeesAync() + { + await new SynchronizationContextRemover(); + return await OnGetFeesAsync(); + } + + /// /// Get amounts available to trade, symbol / amount dictionary /// diff --git a/ExchangeSharp/API/Exchanges/_Base/IExchangeAPI.cs b/ExchangeSharp/API/Exchanges/_Base/IExchangeAPI.cs index f2942407..60d51346 100644 --- a/ExchangeSharp/API/Exchanges/_Base/IExchangeAPI.cs +++ b/ExchangeSharp/API/Exchanges/_Base/IExchangeAPI.cs @@ -438,6 +438,18 @@ public interface IExchangeAPI : IDisposable /// Symbol /// Close margin position result Task CloseMarginPositionAsync(string symbol); + + /// + /// Get fees + /// + /// The customer trading fees + Dictionary GetFees(); + + /// + /// ASYNC - Get fees + /// + /// The customer trading fees + Task> GetFeesAync(); #endregion REST