// Stop limit order
        protected IResponse <LimitOrderResponseModel> CreateAndValidateStopLimitOrder(
            string assetPairId,
            OrderAction orderAction,
            double volume,
            double lowerLimitPrice,
            double lowerPrice,
            double upperLimitPrice,
            double upperPrice,
            string apiKey,
            HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            var request = new PlaceStopLimitOrderModel()
            {
                AssetPairId     = AssetPair,
                OrderAction     = orderAction,
                Volume          = volume,
                LowerLimitPrice = lowerLimitPrice,
                LowerPrice      = lowerPrice,
                UpperLimitPrice = upperLimitPrice,
                UpperPrice      = upperPrice
            };

            return(hft.Orders.PostOrdersStopLimitOrder(request, apiKey)
                   .Validate
                   .StatusCode(statusCode));
        }
예제 #2
0
 public IResponse <LimitOrderResponseModel> PostOrdersStopLimitOrder(PlaceStopLimitOrderModel request, string apiKey)
 {
     return(Request.Post($"/Orders/stoplimit")
            .AddJsonBody(request)
            .WithHeaders("api-key", apiKey)
            .Build().Execute <LimitOrderResponseModel>());
 }
            public void PostOrdersStopLimitBuySellTest(OrderAction orderAction)
            {
                var request = new PlaceStopLimitOrderModel()
                {
                    AssetPairId     = AssetPair,
                    OrderAction     = orderAction,
                    Volume          = 0.1,
                    LowerLimitPrice = 45,
                    LowerPrice      = 46,
                    UpperLimitPrice = 77,
                    UpperPrice      = 78
                };

                var response       = hft.Orders.PostOrdersStopLimitOrder(request, ApiKey);
                var orderId        = response.GetResponseObject().Id.ToString();
                var stopLimitOrder = hft.Orders.GetOrderById(orderId, ApiKey);
                var stopLimitObj   = stopLimitOrder.ResponseObject;

                Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
                Assert.That(stopLimitOrder.StatusCode, Is.EqualTo(HttpStatusCode.OK));

                Assert.That(stopLimitObj.AssetPairId, Is.EqualTo(AssetPair));
                Assert.That(stopLimitObj.CreatedAt, Is.EqualTo(DateTime.Now).Within(3).Minutes);
                Assert.That(stopLimitObj.Type, Is.EqualTo(OrderType.StopLimit));
                Assert.That(stopLimitObj.Volume, Is.EqualTo(orderAction == OrderAction.Buy ? request.Volume : -request.Volume));
                Assert.That(stopLimitObj.LowerLimitPrice, Is.EqualTo(request.LowerLimitPrice));
                Assert.That(stopLimitObj.LowerPrice, Is.EqualTo(request.LowerPrice));
                Assert.That(stopLimitObj.UpperLimitPrice, Is.EqualTo(request.UpperLimitPrice));
                Assert.That(stopLimitObj.UpperPrice, Is.EqualTo(request.UpperPrice));
            }
            public HttpStatusCode PostOrdersStopLimitNegativeTest(bool correctAssetPair, OrderAction orderAction, double volume, double lowerLimitPrice, double lowerPrice, double upperLimitPrice, double upperPrice, string apiKey)
            {
                var assetPairToUse = correctAssetPair ? AssetPair : "Test$%ۤ10";
                var keyToUse       = apiKey == CorrectApiKey ? ApiKey : apiKey;

                var request = new PlaceStopLimitOrderModel()
                {
                    AssetPairId     = assetPairToUse,
                    OrderAction     = orderAction,
                    Volume          = volume,
                    LowerLimitPrice = lowerLimitPrice,
                    LowerPrice      = lowerPrice,
                    UpperLimitPrice = upperLimitPrice,
                    UpperPrice      = upperPrice
                };

                return(hft.Orders.PostOrdersStopLimitOrder(request, keyToUse).StatusCode);
            }
예제 #5
0
        public async Task PlaceStopLimitOrder()
        {
            var client = GetClient <IOrdersApi>();
            var order  = new PlaceStopLimitOrderModel
            {
                AssetPairId     = "BTCUSD",
                OrderAction     = GetRandomItem(_actions),
                LowerLimitPrice = 500,
                LowerPrice      = 550,
                UpperPrice      = 600,
                UpperLimitPrice = 650,
                Volume          = 0.001m
            };

            var result = await client.PlaceStopLimitOrder(order).TryExecute();

            if (result.Success)
            {
                result.Result.Id.Should().NotBe(Guid.Empty);
            }
            else
            {
                result.Error.Should().NotBeNull();
            }

            if (result.Success)
            {
                var orderId = result.Result.Id;
                var saved   = await client.GetOrder(orderId);

                saved.AssetPairId.Should().Be(order.AssetPairId);
                saved.Type.Should().Be(OrderType.StopLimit);
                saved.LowerLimitPrice.Should().Be(order.LowerLimitPrice);
                saved.LowerPrice.Should().Be(order.LowerPrice);
                saved.UpperPrice.Should().Be(order.UpperPrice);
                saved.UpperLimitPrice.Should().Be(order.UpperLimitPrice);
                saved.Volume.Should().Be(order.Volume);
            }
        }
        public async Task <IActionResult> PlaceStopLimitOrder([FromBody] PlaceStopLimitOrderModel order)
        {
            var assetPair = _assetPairsReadModel.TryGetIfEnabled(order.AssetPairId);

            if (assetPair == null)
            {
                return(NotFound($"Asset-pair {order.AssetPairId} could not be found or is disabled."));
            }

            if (!_requestValidator.ValidateAssetPair(order.AssetPairId, assetPair, out var badRequestModel))
            {
                return(BadRequest(badRequestModel));
            }

            var asset = _assetsReadModel.TryGetIfEnabled(assetPair.BaseAssetId);

            if (asset == null)
            {
                throw new InvalidOperationException($"Base asset '{assetPair.BaseAssetId}' for asset pair '{assetPair.Id}' not found.");
            }


            var lowerPrice = order.LowerPrice;

            if (lowerPrice.HasValue && !_requestValidator.ValidatePrice(lowerPrice.Value, out badRequestModel, nameof(PlaceStopLimitOrderModel.LowerPrice)))
            {
                return(BadRequest(badRequestModel));
            }

            var lowerLimitPrice = order.LowerLimitPrice;

            if (lowerLimitPrice.HasValue && !_requestValidator.ValidatePrice(lowerLimitPrice.Value, out badRequestModel, nameof(PlaceStopLimitOrderModel.LowerLimitPrice)))
            {
                return(BadRequest(badRequestModel));
            }

            if ((lowerPrice.HasValue && !lowerLimitPrice.HasValue) ||
                (!lowerPrice.HasValue && lowerLimitPrice.HasValue))
            {
                return(BadRequest(ResponseModel.CreateInvalidFieldError(nameof(order.LowerPrice), "When lower price is send then also lower limit price is required and vice versa.")));
            }

            var upperPrice = order.UpperPrice;

            if (upperPrice.HasValue && !_requestValidator.ValidatePrice(upperPrice.Value, out badRequestModel, nameof(PlaceStopLimitOrderModel.UpperPrice)))
            {
                return(BadRequest(badRequestModel));
            }

            var upperLimitPrice = order.UpperLimitPrice;

            if (upperLimitPrice.HasValue && !_requestValidator.ValidatePrice(upperLimitPrice.Value, out badRequestModel, nameof(PlaceStopLimitOrderModel.UpperLimitPrice)))
            {
                return(BadRequest(badRequestModel));
            }

            if ((upperPrice.HasValue && !upperLimitPrice.HasValue) ||
                (!upperPrice.HasValue && upperLimitPrice.HasValue))
            {
                return(BadRequest(ResponseModel.CreateInvalidFieldError(nameof(order.UpperPrice), "When upper price is send then also upper limit price is required and vice versa.")));
            }

            if (new[] { lowerPrice, lowerLimitPrice, upperPrice, upperLimitPrice }.All(x => !x.HasValue))
            {
                return(BadRequest(ResponseModel.CreateFail(ErrorCodeType.Rejected,
                                                           "At least lower or upper prices are needed for a stop order.")));
            }

            var volume    = order.Volume;
            var minVolume = assetPair.MinVolume;

            if (!_requestValidator.ValidateVolume(volume, minVolume, asset.DisplayId, out badRequestModel))
            {
                return(BadRequest(badRequestModel));
            }

            var walletId = User.GetUserId();
            var response = await _matchingEngineAdapter.PlaceStopLimitOrderAsync(
                clientId : walletId,
                assetPair : assetPair,
                orderAction : order.OrderAction,
                volume : volume,
                lowerPrice : lowerPrice,
                lowerLimitPrice : lowerLimitPrice,
                upperPrice : upperPrice,
                upperLimitPrice : upperLimitPrice);

            if (response.Error != null)
            {
                return(BadRequest(response));
            }

            return(Ok(response.Result));
        }