Example #1
0
            /// <summary>
            /// performs a sell transaction.
            /// </summary>
            /// <seealso cref="PortfolioEntry.Sell(SellTransaction)"/>
            /// <param name="sellTransaction"></param>
            /// <exception cref="ManagerNotInitializedException">
            /// <seealso cref="GetPortfolioEntry(int)"/>
            /// </exception>
            /// <exception cref="CoinNotInPortfolioException">
            /// <seealso cref="GetPortfolioEntry(long)"/>
            /// </exception>
            /// <exception cref="InvalidPriceException">
            /// <seealso cref="PortfolioEntry.Sell(SellTransaction)"/>
            /// </exception>
            /// <exception cref="InsufficientFundsException">
            /// <seealso cref="PortfolioEntry.Sell(SellTransaction)"/>
            /// </exception>
            /// <exception cref="DatabaseCommunicationException">
            /// <seealso cref="GetPortfolioEntry(long)"/>
            /// <seealso cref="handleDatabaseHandlerException(string, SQLiteDatabaseHandlerException)"/>
            /// </exception>
            public void SellCoin(SellTransaction sellTransaction)
            {
                PortfolioEntry portfolioEntry = GetPortfolioEntry(sellTransaction.CoinId);

                try
                {
                    portfolioEntry.Sell(sellTransaction);
                }
                catch (SQLiteDatabaseHandlerException sqliteDatabaseHandlerException)
                {
                    handleDatabaseHandlerException("SellCoin", sqliteDatabaseHandlerException);
                }
            }
Example #2
0
        public DbOperationStatus InsertSellTransaction(SellTransaction sellTransaction)
        {
            var opStatus = new DbOperationStatus
            {
                OperationSuccessStatus = false,
                AffectedIndices        = new List <int>()
            };

            try
            {
                DataContext.Set <SellTransaction>().Add(sellTransaction);
                opStatus.OperationSuccessStatus = DataContext.SaveChanges() > 0;
                if (opStatus.OperationSuccessStatus)
                {
                    opStatus.AffectedIndices.Add(sellTransaction.Id);
                }
            }
            catch (Exception ex)
            {
                opStatus = DbOperationStatus.CreateFromException("Error inserting " + sellTransaction.GetType(), ex);
            }
            return(opStatus);
        }
Example #3
0
 /// <summary>
 /// handles specified <paramref name="sellTransaction"/>.
 /// </summary>
 /// <seealso cref="addTransaction(Transaction)"/>
 /// <param name="sellTransaction"></param>
 internal void Sell(SellTransaction sellTransaction)
 {
     addTransaction(sellTransaction);
 }
Example #4
0
        public int CreateSellTransaction(AddSellTransactionVm addSellTransactionVm)
        {
            var shareQuantitySold = addSellTransactionVm.ShareQuantity;
            var sellUnitPrice     = addSellTransactionVm.UnitPrice;
            var sellCommission    = addSellTransactionVm.Commission;
            var sellDate          = addSellTransactionVm.TransactionDate;

            decimal totalNetProfit              = 0;
            decimal longTermPositionsClosed     = 0;
            decimal longTermPositionsNetProfit  = 0;
            decimal shortTermPositionsClosed    = 0;
            decimal shortTermPositionsNetProfit = 0;

            var closingTransactionMatches = new List <ClosingTransactionMatch>();

            foreach (var purchaseTransaction in addSellTransactionVm.MatchingPurchaseTransactions)
            {
                if (purchaseTransaction.ShareQuantity > 0)
                {
                    var purchaseTransactionDetails =
                        PurchaseTransactionRepository.GetPurchaseTransaction(purchaseTransaction.PurchaseTransactionId);

                    // Requesting to close out more shares than the position has remaining open?
                    if (purchaseTransaction.ShareQuantity > purchaseTransactionDetails.RemainingShares)
                    {
                        return(-1);
                    }

                    var capitalGain = (purchaseTransaction.ShareQuantity * sellUnitPrice)                                                                       // Individual Match Sell Value
                                      - (purchaseTransaction.ShareQuantity / shareQuantitySold) * sellCommission                                                // Individual Match Sell Commission
                                      - (purchaseTransaction.ShareQuantity * purchaseTransactionDetails.UnitPrice)                                              // Individual Match Purchase Value
                                      - (purchaseTransaction.ShareQuantity / purchaseTransactionDetails.ShareQuantity) * purchaseTransactionDetails.Commission; // Individual Match Purchase Commission

                    totalNetProfit += capitalGain;

                    if ((sellDate - purchaseTransactionDetails.TransactionDate).TotalDays > 365)
                    {
                        // Long Term Capital Gain - Greater than 1 year
                        longTermPositionsClosed    += purchaseTransaction.ShareQuantity;
                        longTermPositionsNetProfit += capitalGain;
                    }
                    else
                    {
                        // Short Term Capital Gain - Less than 1 year
                        shortTermPositionsClosed    += purchaseTransaction.ShareQuantity;
                        shortTermPositionsNetProfit += capitalGain;
                    }

                    purchaseTransactionDetails.ClosedShares    += purchaseTransaction.ShareQuantity;
                    purchaseTransactionDetails.RemainingShares -= purchaseTransaction.ShareQuantity;
                    if (purchaseTransactionDetails.RemainingShares == 0)
                    {
                        purchaseTransactionDetails.PositionClosedStatus = true;
                    }

                    PurchaseTransactionRepository.UpdatePurchaseTransaction(purchaseTransactionDetails);

                    var closingTransactionMatch = new ClosingTransactionMatch
                    {
                        MatchingShareCount    = purchaseTransaction.ShareQuantity,
                        PurchaseTransactionId = purchaseTransactionDetails.Id,
                        TotalNetProfit        = capitalGain
                    };
                    closingTransactionMatches.Add(closingTransactionMatch);
                }
            }

            var sellTransaction = new SellTransaction
            {
                BrokerageAccountId          = addSellTransactionVm.BrokerageAccountId,
                UnitPrice                   = sellUnitPrice,
                ShareQuantity               = shareQuantitySold,
                SecurityId                  = addSellTransactionVm.SecurityId,
                Commission                  = sellCommission,
                TransactionDate             = sellDate,
                TotalNetProfit              = totalNetProfit,
                Profitable                  = (totalNetProfit >= 0),
                LongTermPositionsClosed     = longTermPositionsClosed,
                LongTermPositionsNetProfit  = longTermPositionsNetProfit,
                ShortTermPositionsClosed    = shortTermPositionsClosed,
                ShortTermPositionsNetProfit = shortTermPositionsNetProfit,
                ClosingTransactionMatches   = closingTransactionMatches
            };

            DbOperationStatus opStatus = SellTransactionRepository.InsertSellTransaction(sellTransaction);

            if (opStatus.OperationSuccessStatus)
            {
                return(opStatus.AffectedIndices.First());
            }
            return(-1);
        }
Example #5
0
 public DbOperationStatus DeleteSellTransaction(SellTransaction sellTransaction)
 {
     return(Delete(sellTransaction));
 }
Example #6
0
 public DbOperationStatus UpdateSellTransaction(SellTransaction sellTransaction)
 {
     return(Update(sellTransaction));
 }