/// <summary> /// Updates the funds when an order is cancelled /// </summary> /// <param name="quoteCurrency"> </param> /// <param name="accountId"></param> /// <param name="orderside"> </param> /// <param name="orderId"></param> /// <param name="openQuantity"> </param> /// <param name="price"> </param> /// <param name="baseCurrency"> </param> /// <returns></returns> public bool OrderCancelled(Currency baseCurrency, Currency quoteCurrency, AccountId accountId, string orderside, string orderId, decimal openQuantity, decimal price) { decimal amount = 0; Balance currencyBalance = null; // If the order is buy order, then we deducted price * volume from the quote currency. So now we add the // openQuantity*price in the quote currency, as the order may be partially filled if (orderside.Equals("buy", StringComparison.OrdinalIgnoreCase)) { amount = price * openQuantity; currencyBalance = GetBalanceForAccountId(accountId, quoteCurrency); } // If the order is a sell order, then we had deducted the volume from the base currency. So we now add the // volume back in the base currency else if (orderside.Equals("sell", StringComparison.OrdinalIgnoreCase)) { amount = openQuantity; currencyBalance = GetBalanceForAccountId(accountId, baseCurrency); } if (currencyBalance != null) { currencyBalance.CancelPendingTransaction(orderId, PendingTransactionType.Order, amount); _fundsPersistenceRepository.SaveOrUpdate(currencyBalance); return(true); } return(false); }
/// <summary> /// Cancel the withdraw with the given WithdrawID /// </summary> /// <param name="cancelWithdrawCommand"> </param> /// <returns></returns> public CancelWithdrawResponse CancelWithdraw(CancelWithdrawCommand cancelWithdrawCommand) { if (_withdrawSubmissionService.CancelWithdraw(cancelWithdrawCommand.WithdrawId)) { Withdraw withdraw = _withdrawRepository.GetWithdrawByWithdrawId(cancelWithdrawCommand.WithdrawId); if (withdraw != null) { withdraw.StatusCancelled(); _fundsPersistenceRepository.SaveOrUpdate(withdraw); Balance balance = _balanceRepository.GetBalanceByCurrencyAndAccountId(withdraw.Currency, withdraw.AccountId); if (balance != null) { if (balance.CancelPendingTransaction(withdraw.WithdrawId, PendingTransactionType.Withdraw, withdraw.Amount + withdraw.Fee)) { _fundsPersistenceRepository.SaveOrUpdate(balance); return(new CancelWithdrawResponse(true)); } } throw new NullReferenceException(string.Format("No balance found for Currency = {0} | Account ID = {1}", withdraw.Currency.Name, withdraw.AccountId.Value)); } throw new NullReferenceException(string.Format("No withdraw found in the repository for Withdraw ID: {0}", cancelWithdrawCommand.WithdrawId)); } throw new ApplicationException(string.Format("Could not cancel withdraw with Withdraw ID: {0}", cancelWithdrawCommand.WithdrawId)); }