Beispiel #1
0
        public async Task Invoke(HttpContext context)
        {
            var isMaintenanceMode = false;

            try
            {
                isMaintenanceMode = !context.Request.Path.ToString().StartsWith("/swagger") &&
                                    context.Request.Path != $"/api/service/{LykkeConstants.MaintenanceModeRoute}" &&
                                    _maintenanceModeService.CheckIsEnabled();
            }
            catch (Exception ex)
            {
                await _log.WriteErrorAsync("MIDDLEWARE", "MaintenanceModeMiddleware", "", ex);
            }
            finally
            {
                if (!isMaintenanceMode)
                {
                    await _next.Invoke(context);
                }
                else
                {
                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode  = 503;
                    var response = new MtBackendResponse <string> {
                        ErrorMessage = "Maintenance Mode"
                    };
                    await context.Response.WriteAsync(response.ToJson());
                }
            }
        }
Beispiel #2
0
 private async Task SendError(HttpContext ctx, string errorMessage)
 {
     ctx.Response.ContentType = "application/json";
     ctx.Response.StatusCode  = 500;
     var response = new MtBackendResponse <string> {
         Result = "Technical problems", ErrorMessage = errorMessage
     };
     await ctx.Response.WriteAsync(response.ToJson());
 }
Beispiel #3
0
        public async Task <MtBackendResponse <bool> > CloseOrder([FromBody] CloseOrderBackendRequest request)
        {
            if (!_ordersCache.ActiveOrders.TryGetOrderById(request.OrderId, out var order))
            {
                return(new MtBackendResponse <bool> {
                    Message = "Order not found"
                });
            }

            if (_assetDayOffService.IsDayOff(order.Instrument))
            {
                return(new MtBackendResponse <bool> {
                    Message = "Trades for instrument are not available"
                });
            }

            if (order.ClientId != request.ClientId || order.AccountId != request.AccountId)
            {
                return(new MtBackendResponse <bool> {
                    Message = "Order is not available for user"
                });
            }

            order = await _tradingEngine.CloseActiveOrderAsync(request.OrderId, OrderCloseReason.Close);

            var result = new MtBackendResponse <bool>
            {
                Result  = order.Status == OrderStatus.Closed || order.Status == OrderStatus.Closing,
                Message = order.CloseRejectReasonText
            };

            _consoleWriter.WriteLine(
                $"action order.close for clientId = {request.ClientId}, orderId = {request.OrderId}");
            _operationsLogService.AddLog("action order.close", request.ClientId, order.AccountId, request.ToJson(),
                                         result.ToJson());

            return(result);
        }
Beispiel #4
0
        public MtBackendResponse <bool> CancelOrder([FromBody] CloseOrderBackendRequest request)
        {
            if (!_ordersCache.WaitingForExecutionOrders.TryGetOrderById(request.OrderId, out var order))
            {
                return(new MtBackendResponse <bool> {
                    Message = "Order not found"
                });
            }

            if (_assetDayOffService.IsDayOff(order.Instrument))
            {
                return(new MtBackendResponse <bool> {
                    Message = "Trades for instrument are not available"
                });
            }

            if (order.ClientId != request.ClientId || order.AccountId != request.AccountId)
            {
                return(new MtBackendResponse <bool> {
                    Message = "Order is not available for user"
                });
            }

            _tradingEngine.CancelPendingOrder(order.Id, OrderCloseReason.Canceled);

            var result = new MtBackendResponse <bool> {
                Result = true
            };

            _consoleWriter.WriteLine(
                $"action order.cancel for clientId = {request.ClientId}, orderId = {request.OrderId}");
            _operationsLogService.AddLog("action order.cancel", request.ClientId, order.AccountId, request.ToJson(),
                                         result.ToJson());

            return(result);
        }
Beispiel #5
0
        public MtBackendResponse <bool> ChangeOrderLimits([FromBody] ChangeOrderLimitsBackendRequest request)
        {
            if (!_ordersCache.TryGetOrderById(request.OrderId, out var order))
            {
                return(new MtBackendResponse <bool> {
                    Message = "Order not found"
                });
            }

            if (_assetDayOffService.IsDayOff(order.Instrument))
            {
                return(new MtBackendResponse <bool> {
                    Message = "Trades for instrument are not available"
                });
            }

            try
            {
                _tradingEngine.ChangeOrderLimits(request.OrderId, request.StopLoss, request.TakeProfit,
                                                 request.ExpectedOpenPrice);
            }
            catch (ValidateOrderException ex)
            {
                return(new MtBackendResponse <bool> {
                    Result = false, Message = ex.Message
                });
            }

            var result = new MtBackendResponse <bool> {
                Result = true
            };

            _consoleWriter.WriteLine($"action order.changeLimits for clientId = {request.ClientId}, orderId = {request.OrderId}");
            _operationsLogService.AddLog("action order.changeLimits", request.ClientId, order.AccountId, request.ToJson(), result.ToJson());

            return(result);
        }