Exemple #1
0
        public void PlacingAnOrder_Should_FailIfErrorResponse()
        {
            // arrange
            var socket = new TestSocket();

            socket.CanConnect = true;
            var client = TestHelpers.CreateAuthenticatedSocketClient(socket);
            var order  = new BitfinexOrder()
            {
                ClientOrderId = 123
            };

            // act
            var placeTask = client.PlaceOrderAsync(OrderType.ExchangeLimit, "tBTCUSD", 1, price: 1, clientOrderId: 123);

            socket.InvokeMessage(new BitfinexAuthenticationResponse()
            {
                Event = "auth", Status = "OK"
            });
            Thread.Sleep(100);
            socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(order)}, 0, \"error\", \"order placing failed\"]]");
            var result = placeTask.Result;

            // assert
            Assert.IsFalse(result.Success);
            Assert.IsTrue(result.Error.Message.Contains("order placing failed"));
        }
Exemple #2
0
        public void PlacingAnFOKOrder_Should_SucceedIfCanceledResponse()
        {
            // arrange
            var socket = new TestSocket();

            socket.CanConnect = true;
            var client = TestHelpers.CreateAuthenticatedSocketClient(socket);

            var expected = new BitfinexOrder()
            {
                Price         = 0.1m,
                Amount        = 0.2m,
                Symbol        = "tBTCUSD",
                Type          = OrderType.ExchangeFillOrKill,
                ClientOrderId = 1234,
                StatusString  = "CANCELED"
            };

            // act
            var placeTask = client.PlaceOrderAsync(OrderType.ExchangeFillOrKill, "tBTCUSD", 1, price: 1, clientOrderId: 1234);

            socket.InvokeMessage(new BitfinexAuthenticationResponse()
            {
                Event = "auth", Status = "OK"
            });
            Thread.Sleep(100);
            socket.InvokeMessage($"[0, \"oc\", {JsonConvert.SerializeObject(expected)}]");
            var result = placeTask.Result;

            // assert
            Assert.IsTrue(result.Success);
            Assert.IsTrue(TestHelpers.AreEqual(expected, result.Data));
        }
Exemple #3
0
        public void PlacingAnMarketOrder_Should_SucceedIfSuccessResponse()
        {
            // arrange
            var socket = new TestSocket();

            socket.CanConnect = true;
            var client = TestHelpers.CreateAuthenticatedSocketClient(socket);

            var expected = new BitfinexOrder()
            {
                Price         = 0.1m,
                Quantity      = 0.2m,
                Symbol        = "tBTCUSD",
                Type          = OrderType.ExchangeMarket,
                ClientOrderId = 1234,
                StatusString  = "EXECUTED"
            };

            // act
            var placeTask = client.SpotStreams.PlaceOrderAsync(OrderType.ExchangeMarket, "tBTCUSD", 1, price: 1, clientOrderId: 1234);

            socket.InvokeMessage(new BitfinexAuthenticationResponse()
            {
                Event = "auth", Status = "OK"
            });
            Thread.Sleep(100);
            socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]");
            var result = placeTask.Result;

            // assert
            Assert.IsTrue(result.Success);
            Assert.IsTrue(TestHelpers.AreEqual(expected, result.Data));
        }
        /// <summary>
        /// Places a new order
        /// </summary>
        /// <param name="type">The type of the order</param>
        /// <param name="symbol">The symbol the order is for</param>
        /// <param name="amount">The amount of the order, positive for buying, negative for selling</param>
        /// <param name="groupId">Group id to assign to the order</param>
        /// <param name="clientOrderId">Client order id to assign to the order</param>
        /// <param name="price">Price of the order</param>
        /// <param name="priceTrailing">Trailing price of the order</param>
        /// <param name="priceAuxiliaryLimit">Auxiliary limit price of the order</param>
        /// <param name="priceOcoStop">Oco stop price of ther order</param>
        /// <param name="flags">Additional flags</param>
        /// <returns></returns>
        public async Task <CallResult <BitfinexOrder> > PlaceOrderAsync(OrderType type, string symbol, decimal amount, int?groupId = null, int?clientOrderId = null, decimal?price = null, decimal?priceTrailing = null, decimal?priceAuxiliaryLimit = null, decimal?priceOcoStop = null, OrderFlags?flags = null)
        {
            if (!CheckConnection())
            {
                return(new CallResult <BitfinexOrder>(null, new WebError("Socket needs to be started before placing an order")));
            }

            if (!authenticated)
            {
                return(new CallResult <BitfinexOrder>(null, new NoApiCredentialsError()));
            }

            log.Write(LogVerbosity.Info, "Going to place order");
            var order = new BitfinexNewOrder()
            {
                Amount              = amount,
                OrderType           = type,
                Symbol              = symbol,
                Price               = price,
                ClientOrderId       = clientOrderId,
                Flags               = flags,
                GroupId             = groupId,
                PriceAuxiliaryLimit = priceAuxiliaryLimit,
                PriceOCOStop        = priceOcoStop,
                PriceTrailing       = priceTrailing
            };

            var wrapper = new object[] { 0, "on", null, order };
            var data    = JsonConvert.SerializeObject(wrapper, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore,
                Culture           = CultureInfo.InvariantCulture
            });

            BitfinexOrder orderConfirm = null;
            await Task.Run(() =>
            {
                var waitAction = new WaitAction <BitfinexOrder>();
                pendingOrders.Add(order, waitAction);
                Send(data);
                orderConfirm = waitAction.Wait(20000);
                pendingOrders.Remove(order);
            }).ConfigureAwait(false);

            if (orderConfirm != null)
            {
                log.Write(LogVerbosity.Info, "Order canceled");
            }

            return(new CallResult <BitfinexOrder>(orderConfirm, orderConfirm != null ? null : new ServerError("No confirmation received for placed order")));
        }