Esempio n. 1
0
        private void processAggregateCollection()
        {
            if (!TradeAggregates.Any())
            {
                return;
            }
            TransactionAggregate latest = TradeAggregates.MaxBy(t => t.LastTransactionDate);

            Stock          = latest.Stock;
            InventoryValue = latest.InventoryValue;

            TransactionAggregate first = TradeAggregates.MinBy(t => t.FirstTransactionDate);

            if (first != null)
            {
                OpeningBuyPrice  = first.OpeningBuyPrice;
                OpeningSellPrice = first.OpeningSellPrice;
            }

            TransactionAggregate last = TradeAggregates.MaxBy(t => t.FirstTransactionDate);

            if (last != null)
            {
                ClosingBuyPrice      = last.ClosingBuyPrice;
                ClosingSellPrice     = last.ClosingSellPrice;
                PerpetualAverageCost = last.PerpetualAverageCost;
            }
        }
Esempio n. 2
0
 public TransactionAggregate(IEnumerable <IGrouping <InvType, Transaction> > grouping)
     : this()
 {
     foreach (var group in grouping)
     {
         var entry = new TransactionAggregate(group.Key, group);
         processAggregate(entry);
         TradeAggregates.Add(entry);
     }
     processAggregateCollection();
     calculateTotals();
 }
Esempio n. 3
0
 public TransactionAggregate(IEnumerable <IGrouping <DateTime, Transaction> > grouping, InvType invType, Order order)
     : this()
 {
     InvType = invType;
     Order   = order;
     foreach (var group in grouping)
     {
         var entry = new TransactionAggregate(group);
         processAggregate(entry);
         TradeAggregates.Add(entry);
     }
     processAggregateCollection();
     calculateTotals();
 }
Esempio n. 4
0
 private void processAggregate(TransactionAggregate aggregate)
 {
     if (aggregate.FirstTransactionDate < FirstTransactionDate)
     {
         FirstTransactionDate = aggregate.FirstTransactionDate;
     }
     if (aggregate.LastTransactionDate > LastTransactionDate)
     {
         LastTransactionDate = aggregate.LastTransactionDate;
     }
     MaterialCost            += aggregate.MaterialCost;
     Sales                   += aggregate.Sales;
     BuyQuantity             += aggregate.BuyQuantity;
     SellQuantity            += aggregate.SellQuantity;
     CostOfGoodsSold         += aggregate.CostOfGoodsSold;
     UnaccountedStock        += aggregate.UnaccountedStock;
     SalesTax                += aggregate.SalesTax;
     BuyOrderBrokerFees      += aggregate.BuyOrderBrokerFees;
     SellOrderBrokerFees     += aggregate.SellOrderBrokerFees;
     MaterialCostOfGoodsSold += aggregate.MaterialCostOfGoodsSold;
     BrokerFeesOfGoodsSold   += aggregate.BrokerFeesOfGoodsSold;
 }
Esempio n. 5
0
        private void initialize()
        {
            List <List <Transaction> > groups =
                Transactions.GroupBy(f => f.TransactionDate.Date).Select(f => f.ToList()).ToList();

            FirstTransactionDate = DateTime.MaxValue;
            LastTransactionDate  = DateTime.MinValue;
            foreach (var transactions in groups)
            {
                var entry = new TransactionAggregate(transactions);
                Entries.Add(entry);
                Balance      += entry.Balance;
                SellQuantity += entry.SellQuantity;
                BuyQuantity  += entry.BuyQuantity;
                BuyTotal     += entry.MaterialCost;
                SellTotal    += entry.Sales;
                PerpetualAverageTotalCost += entry.CostOfGoodsSold;
                UnaccountedStock          += entry.UnaccountedStock;
                if (entry.FirstTransactionDate < FirstTransactionDate)
                {
                    FirstTransactionDate = entry.FirstTransactionDate;
                }
                if (entry.LastTransactionDate > LastTransactionDate)
                {
                    LastTransactionDate = entry.LastTransactionDate;
                }
            }

            TradeDuration = LastTransactionDate - FirstTransactionDate;

            if (BuyQuantity > 0)
            {
                AvgBuyPrice = BuyTotal / BuyQuantity;
            }
            if (SellQuantity > 0)
            {
                AvgSellPrice = SellTotal / SellQuantity;
            }

            Transaction latest = Transactions.MaxBy(t => t.TransactionDate);

            Stock                = latest.PostTransactionStock;
            StockValue           = Stock * latest.PerpetualAverageCost;
            PerpetualAverageCost = latest.PerpetualAverageCost;

            PeriodicAverageProfit  = SellTotal - AvgBuyPrice * SellQuantity;
            PerpetualAverageProfit = SellTotal - PerpetualAverageTotalCost;
            if (SellQuantity > 0)
            {
                ProfitPerItem = PerpetualAverageProfit / SellQuantity;
            }
            if (AvgSellPrice > 0)
            {
                AvgMargin = (double)(ProfitPerItem / AvgSellPrice);
            }

            if (groups.Any() && TradeDuration.TotalDays > 0)
            {
                AvgProfitPerDay = PerpetualAverageProfit / (decimal)TradeDuration.TotalDays;
            }
        }