From b418d6560079122b54818b27da7cff9266f43c8b Mon Sep 17 00:00:00 2001 From: Matheus Klauck Date: Fri, 10 Dec 2021 16:33:38 -0300 Subject: [PATCH 1/2] [FIX] [Bitfinex] Submit stop order at margin account (#698) --- .../Exchanges/Bitfinex/ExchangeBitfinexAPI.cs | 33 ++++++++------ .../ExchangeBitfinexTests.cs | 44 +++++++++++++++++++ 2 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 tests/ExchangeSharpTests/ExchangeBitfinexTests.cs diff --git a/src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs b/src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs index c97edecd..40233ab9 100644 --- a/src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs +++ b/src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs @@ -471,23 +471,30 @@ protected override async Task OnPlaceOrderAsync(ExchangeOrd payload["amount"] = (await ClampOrderQuantity(marketSymbol, order.Amount)).ToStringInvariant(); payload["side"] = (order.IsBuy ? "buy" : "sell"); - if (order.IsMargin) + switch (order.OrderType) { - payload["type"] = order.OrderType == OrderType.Market ? "market" : "limit"; - } - else - { - payload["type"] = order.OrderType == OrderType.Market ? "exchange market" : "exchange limit"; - } + case OrderType.Market: + if (order.Price == null) throw new ArgumentNullException(nameof(order.Price)); + payload["type"] = "market"; + payload["price"] = (await ClampOrderPrice(marketSymbol, order.Price.Value)).ToStringInvariant(); + break; - if (order.OrderType != OrderType.Market) - { - if (order.Price == null) throw new ArgumentNullException(nameof(order.Price)); - payload["price"] = (await ClampOrderPrice(marketSymbol, order.Price.Value)).ToStringInvariant(); + case OrderType.Limit: + if (order.Price == null) throw new ArgumentNullException(nameof(order.Price)); + payload["type"] = "limit"; + payload["price"] = (await ClampOrderPrice(marketSymbol, order.Price.Value)).ToStringInvariant(); + break; + + case OrderType.Stop: + if (order.StopPrice < 0) throw new ArgumentOutOfRangeException(nameof(order.StopPrice)); + payload["type"] = "stop"; + payload["price"] = (await ClampOrderPrice(marketSymbol, order.StopPrice)).ToStringInvariant(); + break; } - else + + if (!order.IsMargin) { - payload["price"] = "1"; + payload["type"] = $"exchange {payload["type"]}"; } if (order.IsPostOnly == true) payload["flags"] = "4096"; // The post-only limit order option ensures the limit order will be added to the order book and not match with a pre-existing order unless the pre-existing order is a hidden order. order.ExtraParameters.CopyTo(payload); diff --git a/tests/ExchangeSharpTests/ExchangeBitfinexTests.cs b/tests/ExchangeSharpTests/ExchangeBitfinexTests.cs new file mode 100644 index 00000000..fd358344 --- /dev/null +++ b/tests/ExchangeSharpTests/ExchangeBitfinexTests.cs @@ -0,0 +1,44 @@ +/* +MIT LICENSE + +Copyright 2017 Digital Ruby, LLC - http://www.digitalruby.com + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ExchangeSharp; +using System.Net; +using System.Security; + +namespace ExchangeSharpTests +{ + [TestClass] + public class ExchangeBitfinexTests + { + private SecureString _pvtKey = new NetworkCredential("", "privKey").SecurePassword; + private SecureString _pubKey = new NetworkCredential("", "SecKey").SecurePassword; + + [TestMethod] + public void SubmitStopMarginOrder() + { + IExchangeAPI api = ExchangeAPI.GetExchangeAPIAsync("Bitfinex").Result; + ExchangeOrderRequest order = new ExchangeOrderRequest + { + MarketSymbol = "ADAUSD", + Amount = System.Convert.ToDecimal(0.0001), + IsBuy = true, + IsMargin = true, + OrderType = OrderType.Stop, + StopPrice = System.Convert.ToDecimal(100) + }; + api.PrivateApiKey = _pvtKey; + api.PublicApiKey = _pubKey; + ExchangeOrderResult result = api.PlaceOrderAsync(order).Result; + } + } +} From d8c036fc0040fd73ca117f874a884be5c66bbbfb Mon Sep 17 00:00:00 2001 From: Matheus Klauck Date: Tue, 14 Dec 2021 15:47:04 -0300 Subject: [PATCH 2/2] [FIX] [Bitfinex] Fixed market symbol separator (#699) --- .../API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs | 2 +- tests/ExchangeSharpTests/ExchangeBitfinexTests.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs b/src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs index 40233ab9..4f7190a0 100644 --- a/src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs +++ b/src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs @@ -62,7 +62,7 @@ private ExchangeBitfinexAPI() ["ZEC"] = "zcash", }; - MarketSymbolSeparator = string.Empty; + MarketSymbolSeparator = ":"; } public override string PeriodSecondsToString(int seconds) diff --git a/tests/ExchangeSharpTests/ExchangeBitfinexTests.cs b/tests/ExchangeSharpTests/ExchangeBitfinexTests.cs index fd358344..71e6b40f 100644 --- a/tests/ExchangeSharpTests/ExchangeBitfinexTests.cs +++ b/tests/ExchangeSharpTests/ExchangeBitfinexTests.cs @@ -14,6 +14,8 @@ The above copyright notice and this permission notice shall be included in all c using ExchangeSharp; using System.Net; using System.Security; +using System; +using FluentAssertions; namespace ExchangeSharpTests { @@ -40,5 +42,16 @@ public void SubmitStopMarginOrder() api.PublicApiKey = _pubKey; ExchangeOrderResult result = api.PlaceOrderAsync(order).Result; } + + [TestMethod] + public void GetDataFromMarketWithSpecialChar() + { + IExchangeAPI api = ExchangeAPI.GetExchangeAPIAsync("Bitfinex").Result; + string marketTicker = "DOGE:USD"; + DateTime start = new DateTime(2021, 12, 1); + DateTime end = DateTime.Today; + System.Collections.Generic.IEnumerable result = api.GetCandlesAsync(marketTicker, 86400, start, end, 1000).Result; + result.Should().HaveCountGreaterThan(0, "Returned data"); + } } }