Skip to content

[FIX] [Bitfinex] Submit stop order at margin account (#698) #701

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

Merged
merged 1 commit into from
Dec 10, 2021
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
33 changes: 20 additions & 13 deletions src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,23 +471,30 @@ protected override async Task<ExchangeOrderResult> 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);
Expand Down
44 changes: 44 additions & 0 deletions tests/ExchangeSharpTests/ExchangeBitfinexTests.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}