public async Task <List <IOrder> > CloseAccountOrders(string accountId) { var openedOrders = _ordersCache.ActiveOrders.GetOrdersByAccountIds(accountId).ToArray(); var closedOrders = new List <IOrder>(); foreach (var order in openedOrders) { try { var closedOrder = await _tradingEngine.CloseActiveOrderAsync(order.Id, OrderCloseReason.ClosedByBroker, "Close orders for account"); closedOrders.Add(closedOrder); } catch (Exception e) { await _log.WriteWarningAsync(nameof(AccountManager), "CloseAccountActiveOrders", $"AccountId: {accountId}, OrderId: {order.Id}", $"Error closing order: {e.Message}"); } } var pendingOrders = _ordersCache.WaitingForExecutionOrders.GetOrdersByAccountIds(accountId); foreach (var order in pendingOrders) { try { var closedOrder = _tradingEngine.CancelPendingOrder(order.Id, OrderCloseReason.CanceledByBroker, "Close orders for account"); closedOrders.Add(closedOrder); } catch (Exception e) { await _log.WriteWarningAsync(nameof(AccountManager), "CloseAccountOrders", $"AccountId: {accountId}, OrderId: {order.Id}", $"Error cancelling order: {e.Message}"); } } return(closedOrders); }
public async Task <BackendResponse <bool> > CloseOrder([FromBody] CloseOrderBackendRequest request) { if (!_ordersCache.ActiveOrders.TryGetOrderById(request.OrderId, out var order)) { return(BackendResponse <bool> .Error("Order not found")); } if (_assetDayOffService.IsDayOff(order.Instrument)) { return(BackendResponse <bool> .Error("Trades for instrument are not available")); } if (order.ClientId != request.ClientId || order.AccountId != request.AccountId) { return(BackendResponse <bool> .Error("Order is not available for user")); } if (request.IsForcedByBroker && string.IsNullOrEmpty(request.Comment)) { return(BackendResponse <bool> .Error("For operation forced by broker, comment is mandatory")); } var reason = request.IsForcedByBroker ? OrderCloseReason.ClosedByBroker : OrderCloseReason.Close; order = await _tradingEngine.CloseActiveOrderAsync(request.OrderId, reason, request.Comment); var result = new BackendResponse <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); }
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); }