Skip to content

Commit

Permalink
Improve Bitstamp API and add a way to retrieve fees (#212)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
atp4al authored and jjxtra committed Jul 19, 2018
1 parent 405d1b5 commit b080572
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
9 changes: 9 additions & 0 deletions .nuget/Nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="..\..\packages" />
</config>
<packageRestore>
<add key="enabled" value="True" />
</packageRestore>
</configuration>
53 changes: 34 additions & 19 deletions ExchangeSharp/API/Exchanges/Bitstamp/ExchangeBitstampAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,36 +151,45 @@ protected override async Task<Dictionary<string, decimal>> OnGetAmountsAsync()
{
string url = "/balance/";
var payload = await OnGetNoncePayloadAsync();
JObject responseObject = await MakeJsonRequestAsync<JObject>(url, null, payload, "POST");
Dictionary<string, decimal> balances = new Dictionary<string, decimal>();
foreach (var property in responseObject)
{
if (property.Key.Contains("_balance"))
{
decimal balance = property.Value.ConvertInvariant<decimal>();
if (balance == 0) { continue; }
balances.Add(property.Key.Replace("_balance", "").Trim(), balance);
}
}
return balances;
var responseObject = await MakeJsonRequestAsync<JObject>(url, null, payload, "POST");
return ExtractDictionary(responseObject, "balance");
}


protected override async Task<Dictionary<string, decimal>> OnGetFeesAsync()
{
string url = "/balance/";
var payload = await OnGetNoncePayloadAsync();
var responseObject = await MakeJsonRequestAsync<JObject>(url, null, payload, "POST");
return ExtractDictionary(responseObject, "fee");
}

protected override async Task<Dictionary<string, decimal>> OnGetAmountsAvailableToTradeAsync()
{
string url = "/balance/";
var payload = await OnGetNoncePayloadAsync();
JObject responseObject = await MakeJsonRequestAsync<JObject>(url, null, payload, "POST");
Dictionary<string, decimal> balances = new Dictionary<string, decimal>();
var responseObject = await MakeJsonRequestAsync<JObject>(url, null, payload, "POST");
return ExtractDictionary(responseObject, "available");
}

private static Dictionary<string, decimal> ExtractDictionary(JObject responseObject, string key)
{
var result = new Dictionary<string, decimal>();
var suffix = $"_{key}";
foreach (var property in responseObject)
{
if (property.Key.Contains("_available"))
if (property.Key.Contains(suffix))
{
decimal balance = property.Value.ConvertInvariant<decimal>();
if (balance == 0) { continue; }
balances.Add(property.Key.Replace("_available", "").Trim(), balance);
decimal value = property.Value.ConvertInvariant<decimal>();
if (value == 0)
{
continue;
}

result.Add(property.Key.Replace(suffix, "").Trim(), value);
}
}
return balances;
return result;
}

protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order)
Expand All @@ -197,6 +206,12 @@ protected override async Task<ExchangeOrderResult> 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);

Expand Down
19 changes: 19 additions & 0 deletions ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ await GetHistoricalTradesAsync((e) =>
protected virtual Task<IEnumerable<ExchangeTransaction>> OnGetDepositHistoryAsync(string symbol) => throw new NotImplementedException();
protected virtual Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(string symbol, int periodSeconds, DateTime? startDate = null, DateTime? endDate = null, int? limit = null) => throw new NotImplementedException();
protected virtual Task<Dictionary<string, decimal>> OnGetAmountsAsync() => throw new NotImplementedException();
protected virtual Task<Dictionary<string, decimal>> OnGetFeesAsync() => throw new NotImplementedException();
protected virtual Task<Dictionary<string, decimal>> OnGetAmountsAvailableToTradeAsync() => throw new NotImplementedException();
protected virtual Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order) => throw new NotImplementedException();
protected virtual Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string orderId, string symbol = null) => throw new NotImplementedException();
Expand Down Expand Up @@ -846,6 +847,24 @@ public async Task<Dictionary<string, decimal>> GetAmountsAsync()
return await OnGetAmountsAsync();
}


/// <summary>
/// Get fees
/// </summary>
/// <returns>The customer trading fees</returns>
public Dictionary<string, decimal> GetFees() => GetFeesAync().GetAwaiter().GetResult();

/// <summary>
/// ASYNC - Get fees
/// </summary>
/// <returns>The customer trading fees</returns>
public async Task<Dictionary<string, decimal>> GetFeesAync()
{
await new SynchronizationContextRemover();
return await OnGetFeesAsync();
}


/// <summary>
/// Get amounts available to trade, symbol / amount dictionary
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions ExchangeSharp/API/Exchanges/_Base/IExchangeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,18 @@ public interface IExchangeAPI : IDisposable
/// <param name="symbol">Symbol</param>
/// <returns>Close margin position result</returns>
Task<ExchangeCloseMarginPositionResult> CloseMarginPositionAsync(string symbol);

/// <summary>
/// Get fees
/// </summary>
/// <returns>The customer trading fees</returns>
Dictionary<string, decimal> GetFees();

/// <summary>
/// ASYNC - Get fees
/// </summary>
/// <returns>The customer trading fees</returns>
Task<Dictionary<string, decimal>> GetFeesAync();

#endregion REST

Expand Down

0 comments on commit b080572

Please sign in to comment.