public void should_amend_sell_limit_orders_bulk()
        {
            // arrange
            var @params = new OrderBulkPOSTRequestParams
            {
                Orders = new[]
                {
                    OrderPOSTRequestParams.CreateSimpleHidenLimit("XBTUSD", 3, _xbtAvgPrice + LimitPriceSubtractor, OrderSide.Sell),
                    OrderPOSTRequestParams.CreateSimpleHidenLimit("XBTUSD", 3, _xbtAvgPrice + LimitPriceSubtractor, OrderSide.Sell)
                }
            };
            var result = Sut.Execute(BitmexApiUrls.Order.PostOrderBulk, @params).Result.Result;

            result.Should().NotBeNull();
            result.All(a => a.OrdStatus == "New").Should().BeTrue();
            result.All(a => !string.IsNullOrWhiteSpace(a.OrderId)).Should().BeTrue();

            var @paramsPut = new OrderBulkPUTRequestParams()
            {
                Orders = result.Select(a => new OrderPUTRequestParams()
                {
                    OrderID  = a.OrderId,
                    OrderQty = 2,
                }).ToArray()
            };

            // act
            var resultPut = Sut.Execute(BitmexApiUrls.Order.PutOrderBulk, @paramsPut).Result.Result;

            // assert
            resultPut.Should().NotBeNull();
            resultPut.All(a => a.OrdStatus == "New").Should().BeTrue();
            resultPut.All(a => a.OrderQty == 2).Should().BeTrue();
            resultPut.All(a => !string.IsNullOrWhiteSpace(a.OrderId)).Should().BeTrue();
        }
        public void should_cancel_all_sell_limit_orders_after()
        {
            // arrange
            var @params = OrderPOSTRequestParams.CreateSimpleHidenLimit("XBTUSD", 3, _xbtAvgPrice + LimitPriceSubtractor, OrderSide.Sell);
            var result  = Sut.Execute(BitmexApiUrls.Order.PostOrder, @params).Result.Result;

            result.Should().NotBeNull();
            result.OrdType.Should().Be("Limit");
            result.OrdStatus.Should().Be("New");
            result.OrderId.Should().NotBeNull();

            var paramCloseAfter = new OrderCancelAllAfterPOSTRequestParams
            {
                Timeout = 1000
            };

            // act
            Sut.Execute(BitmexApiUrls.Order.PostOrderCancelAllAfter, paramCloseAfter).Wait();
            Thread.Sleep(1500);
            var resultCloseAfter = Sut.Execute(BitmexApiUrls.Order.GetOrder, new OrderGETRequestParams {
                Symbol = "XBTUSD"
            }).Result.Result;

            // assert
            resultCloseAfter.Should().NotBeNull();
            resultCloseAfter.All(a => !string.IsNullOrWhiteSpace(a.OrderId)).Should().BeTrue();
        }
        public void should_delete_many_buy_orders()
        {
            // arrange
            var @params = new OrderBulkPOSTRequestParams
            {
                Orders = new[]
                {
                    OrderPOSTRequestParams.CreateSimpleHidenLimit("XBTUSD", 3, _xbtAvgPrice - LimitPriceSubtractor, OrderSide.Buy),
                    OrderPOSTRequestParams.CreateSimpleHidenLimit("XBTUSD", 3, _xbtAvgPrice - LimitPriceSubtractor, OrderSide.Buy)
                }
            };
            var resultInsert = Sut.Execute(BitmexApiUrls.Order.PostOrderBulk, @params).Result.Result;

            // act
            var @paramsToDelete = new OrderDELETERequestParams
            {
                OrderID = string.Join(",", resultInsert.Select(a => a.OrderId))
            };
            var result = Sut.Execute(BitmexApiUrls.Order.DeleteOrder, @paramsToDelete).Result.Result;

            // assert
            result.Should().NotBeNull();
            result.All(a => a.OrdStatus == "Canceled").Should().BeTrue();
            result.All(a => !string.IsNullOrWhiteSpace(a.OrderId)).Should().BeTrue();
        }
        public void should_receive_limit_order_notification()
        {
            try
            {
                // arrange
                var connected = Sut.Connect();
                var @params   = OrderPOSTRequestParams.CreateSimpleHidenLimit("XBTUSD", 3, _xbtAvgPrice - 500, OrderSide.Buy);

                // act
                IList <OrderDto> dtos = null;
                var dataReceived      = new ManualResetEvent(false);

                Sut.OrderResponseReceived += (sender, args) =>
                {
                    if (args.Response.Data.Any(b => b.Symbol == "XBTUSD") && args.Response.Action == BitmexActions.Insert)
                    {
                        dtos = args.Response.Data;
                        dataReceived.Set();
                    }
                };

                var subscription = new SubscriptionRequest(SubscriptionType.order);

                Subscription = subscription;

                Sut.Subscribe(subscription);
                var result = _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder, @params).Result.Result;
                result.Should().NotBeNull();
                //result.OrdType.Should().Be("Limit");
                result.OrdStatus.Should().Be("New");
                result.OrderId.Should().NotBeNull();

                Thread.Sleep(2000);
                var received = dataReceived.WaitOne(TimeSpan.FromSeconds(20));

                // assert
                // no exception raised
                connected.Should().BeTrue();
                received.Should().BeTrue();
                dtos.Should().NotBeNull();
                dtos.Count().Should().BeGreaterThan(0);
                dtos[0].OrdType.Should().Be("Limit");
                dtos[0].OrdStatus.Should().Be("New");
                dtos[0].OrderId.Should().Be(result.OrderId);
            }
            catch (BitmexWebSocketLimitReachedException)
            {
                Assert.Fail("connection limit reached");
            }
        }
        public void should_place_buy_limit_order()
        {
            // arrange
            var @params = OrderPOSTRequestParams.CreateSimpleHidenLimit("XBTUSD", 3, _xbtAvgPrice - LimitPriceSubtractor, OrderSide.Buy);

            // act
            var result = Sut.Execute(BitmexApiUrls.Order.PostOrder, @params).Result.Result;

            // assert

            result.Should().NotBeNull();
            result.OrdType.Should().Be("Limit");
            result.OrdStatus.Should().Be("New");
            result.OrderId.Should().NotBeNull();
        }
Beispiel #6
0
        public async Task <string> GoLong(MarketInfo marketInfo)
        {
            await _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder, OrderPOSTRequestParams.ClosePositionByMarket(marketInfo.Market));

            await SetLeverage(marketInfo.Market, marketInfo.Leverage);

            var buyPrice = await GetCurrentPrice(marketInfo.Market);

            var orderDto = await _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder,
                                                           OrderPOSTRequestParams.CreateSimpleLimit(marketInfo.Market, marketInfo.Quantity, buyPrice, OrderSide.Buy));

            var stopPrice = Math.Round((decimal)orderDto.Price * 1.005M, 0);
            //await _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder, OrderPOSTRequestParams.CreateSimpleHidenLimit(marketInfo.Market, marketInfo.Quantity, buyPrice - 50, OrderSide.Buy));
            await _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder, OrderPOSTRequestParams.CreateSimpleHidenLimit(marketInfo.Market, marketInfo.Quantity, stopPrice, OrderSide.Sell));

            return($"Bought {orderDto.OrderQty} {marketInfo.Market} at {orderDto.Price}, stop order set to {stopPrice}");
        }
Beispiel #7
0
        private static async Task PlayWithBitmex()
        {
            try
            {
                const string market = "XBTUSD";
                var          bitmexAuthorization = new BitmexAuthorization();
                bitmexAuthorization.BitmexEnvironment = BitmexEnvironment.Test;
                bitmexAuthorization.Key    = "wcZtcAiFMff8kLWaSLl8U877";
                bitmexAuthorization.Secret = "uOtP5-0sEtiis5d1_Qv1-LW8FLsV3qW9Qsmsf_OWBXVzw-c3";
                _bitmexApiService          = BitmexApiService.CreateDefaultApi(bitmexAuthorization);

                await _bitmexApiService.Execute(BitmexApiUrls.Order.DeleteOrderAll, new OrderAllDELETERequestParams
                {
                    Symbol = "BCHU18"
                });

                var bitcoinOrderBook = await _bitmexApiService.Execute(BitmexApiUrls.OrderBook.GetOrderBookL2,
                                                                       new OrderBookL2GETRequestParams { Depth = 1, Symbol = market });

                var currentPrice  = Math.Round((bitcoinOrderBook[0].Price + bitcoinOrderBook[1].Price) / 2);
                var stopSellOrder = await _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder, OrderPOSTRequestParams.CreateLimitStopOrder(market, 1500, 6500, 6467, OrderSide.Buy));

                var positions = await _bitmexApiService.Execute(BitmexApiUrls.Position.GetPosition, new PositionGETRequestParams { Count = 50 });

                var positionLeveragePostRequestParams = new PositionLeveragePOSTRequestParams();
                positionLeveragePostRequestParams.Leverage = 50;
                positionLeveragePostRequestParams.Symbol   = market;

                /*var positionDto = await bitmexApiService.Execute(BitmexApiUrls.Position.PostPositionLeverage,
                 *  positionLeveragePostRequestParams);*/
                foreach (var position in positions)
                {
                    await _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder, OrderPOSTRequestParams.ClosePositionByMarket(market));
                }
                var positionDto = await _bitmexApiService.Execute(BitmexApiUrls.Position.PostPositionLeverage, positionLeveragePostRequestParams);

                var initialOrder = await CreateLimitOrder(market, 1500, currentPrice, OrderSide.Sell);

                var round     = Math.Round((decimal)initialOrder.Price * 0.9M, 0);
                var stopOrder = await _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder, OrderPOSTRequestParams.CreateSimpleHidenLimit(market, 1500, round, OrderSide.Buy));

                var stopLoss = await CreateLimitOrder(market, 1500, currentPrice + 40, OrderSide.Sell);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        private async Task <OrderDto> ExecuteLimitOrder(TradeInfo tradeInfo, decimal limit)
        {
            var orderParams = OrderPOSTRequestParams.CreateSimpleHidenLimit(tradeInfo.Symbol, (int)(tradeInfo.Quantity * 0.75), limit, tradeInfo.EntryOrder.ToBitmexOrder());

            return(await _bitmexApiService.Execute(BitmexApiUrls.Order.PostOrder, orderParams));
        }