public async Task <BackendResponse <AccountDepositWithdrawResponse> > AccountDeposit([FromBody] AccountDepositWithdrawRequest request) { var account = _accountsCacheService.Get(request.ClientId, request.AccountId); var changeTransferLimit = _marginSettings.IsLive && request.PaymentType == PaymentType.Transfer && !IsCrypto(account.BaseAssetId); try { var transactionId = await _accountManager.UpdateBalanceAsync(account, Math.Abs(request.Amount), AccountHistoryType.Deposit, "Account deposit", request.TransactionId, changeTransferLimit); _operationsLogService.AddLog($"account deposit {request.PaymentType}", request.ClientId, request.AccountId, request.ToJson(), true.ToJson()); return(BackendResponse <AccountDepositWithdrawResponse> .Ok( new AccountDepositWithdrawResponse { TransactionId = transactionId })); } catch (Exception e) { await _log.WriteErrorAsync(nameof(AccountsBalanceController), "AccountDeposit", request?.ToJson(), e); return(BackendResponse <AccountDepositWithdrawResponse> .Error(e.Message)); } }
public async Task <OpenOrderBackendResponse> PlaceOrder([FromBody] OpenOrderBackendRequest request) { var code = await _identityGenerator.GenerateIdAsync(nameof(Order)); var order = new Order { Id = Guid.NewGuid().ToString("N"), Code = code, CreateDate = DateTime.UtcNow, ClientId = request.ClientId, AccountId = request.Order.AccountId, Instrument = request.Order.Instrument, Volume = request.Order.Volume, ExpectedOpenPrice = request.Order.ExpectedOpenPrice, TakeProfit = request.Order.TakeProfit, StopLoss = request.Order.StopLoss }; var placedOrder = await _tradingEngine.PlaceOrderAsync(order); var result = BackendContractFactory.CreateOpenOrderBackendResponse(placedOrder); _consoleWriter.WriteLine($"action order.place for clientId = {request.ClientId}"); _operationsLogService.AddLog("action order.place", request.ClientId, request.Order.AccountId, request.ToJson(), result.ToJson()); return(result); }
public void NotifyOrderChanged(Order order) { _rabbitMqNotifyService.OrderChanged(order); var queueName = QueueHelper.BuildQueueName(_marginSettings.RabbitMqQueues.OrderChanged.ExchangeName, _marginSettings.Env); _consoleWriter.WriteLine($"send order changed to queue {queueName}"); _operationsLogService.AddLog($"queue {queueName}", order.ClientId, order.AccountId, null, order.ToJson()); }
void IEventConsumer <StopOutEventArgs> .ConsumeEvent(object sender, StopOutEventArgs ea) { var account = ea.Account; var orders = ea.Orders; var eventTime = _dateService.Now(); var accountMarginEventMessage = AccountMarginEventMessageConverter.Create(account, true, eventTime); var accuracy = _assetsCache.GetAssetAccuracy(account.BaseAssetId); var totalPnl = Math.Round(orders.Sum(x => x.GetTotalFpl()), accuracy); _threadSwitcher.SwitchThread(async() => { _operationsLogService.AddLog("stopout", account.ClientId, account.Id, "", ea.ToJson()); var marginEventTask = _rabbitMqNotifyService.AccountMarginEvent(accountMarginEventMessage); _notifyService.NotifyAccountStopout(account.ClientId, account.Id, orders.Length, totalPnl); var notificationTask = SendMarginEventNotification(account.ClientId, string.Format(MtMessages.Notifications_StopOutNotification, orders.Length, totalPnl, account.BaseAssetId)); var clientEmail = await _clientAccountService.GetEmail(account.ClientId); var emailTask = !string.IsNullOrEmpty(clientEmail) ? _emailService.SendStopOutEmailAsync(clientEmail, account.BaseAssetId, account.Id) : Task.CompletedTask; await Task.WhenAll(marginEventTask, notificationTask, emailTask); }); }
void IEventConsumer <MarginCallEventArgs> .ConsumeEvent(object sender, MarginCallEventArgs ea) { var account = ea.Account; var eventTime = _dateService.Now(); var accountMarginEventMessage = AccountMarginEventMessageConverter.Create(account, false, eventTime); _threadSwitcher.SwitchThread(async() => { if (LastNotifications.TryGetValue(account.Id, out var lastNotification) && lastNotification.AddMinutes(NotificationsTimeout) > eventTime) { return; } var marginEventTask = _rabbitMqNotifyService.AccountMarginEvent(accountMarginEventMessage); _operationsLogService.AddLog("margin call", account.ClientId, account.Id, "", ea.ToJson()); var marginUsageLevel = account.GetMarginUsageLevel(); var marginUsedPerc = marginUsageLevel == 0 ? 0 : 1 / marginUsageLevel; var notificationTask = SendMarginEventNotification(account.ClientId, string.Format( MtMessages.Notifications_MarginCall, marginUsedPerc, account.BaseAssetId)); var emailTask = _emailService.SendMarginCallEmailAsync(account); await Task.WhenAll(marginEventTask, notificationTask, emailTask); LastNotifications.AddOrUpdate(account.Id, eventTime, (s, time) => eventTime); }); }
public async Task ProcessAccountChanged(AccountChangedMessage accountChangedMessage) { if (accountChangedMessage.EventType != AccountEventTypeEnum.Updated) { return; } var account = accountChangedMessage.Account; var queueName = QueueHelper.BuildQueueName(_settings.MarginTradingFront.RabbitMqQueues.AccountChanged.ExchangeName, _settings.MarginTradingFront.Env); _consoleWriter.WriteLine($"Get account change from {queueName} queue for clientId = {account.ClientId}"); var notificationId = await _clientNotificationService.GetNotificationId(account.ClientId); var userTopic = _realm.Services.GetSubject <NotifyResponse <MarginTradingAccountClientContract> >($"user.{notificationId}"); var notifyResponse = new NotifyResponse <MarginTradingAccountClientContract> { Entity = account.ToClientContract(), Type = NotifyEntityType.Account }; userTopic.OnNext(notifyResponse); _operationsLog.AddLog($"topic user.{notificationId} (account changed)", account.ClientId, account.Id, null, notifyResponse.ToJson()); _consoleWriter.WriteLine($"topic user.{notificationId} (account changed) for clientId = {account.ClientId}"); var userUpdateTopic = _realm.Services.GetSubject <NotifyResponse>($"user.updates.{notificationId}"); var userUpdateTopicResponse = new NotifyResponse { Account = notifyResponse.Entity, Order = null }; userUpdateTopic.OnNext(userUpdateTopicResponse); _operationsLog.AddLog($"topic user.updates.{notificationId} (account changed)", account.ClientId, account.Id, null, userUpdateTopicResponse.ToJson()); _consoleWriter.WriteLine($"topic user.updates.{notificationId} (account changed) for clientId = {account.ClientId}"); }