Class CapitalGainBuy

  • All Implemented Interfaces:
    java.lang.Comparable<CapitalGainBuy>

    public class CapitalGainBuy
    extends java.lang.Object
    implements java.lang.Comparable<CapitalGainBuy>
    Tracks a buy transaction for capital gain calculations, particularly average cost with long term and short term gains. Per U.S. IRS Publication 564, single-category average cost allocates to the earliest shares acquired first:
    Even though you include all unsold shares of a fund in a single category to compute average basis, you may have both short-term and long-term gains or losses when you sell these shares. To determine your holding period, the shares disposed of are considered to be those acquired first.
    This class tracks how many shares of a particular buy transaction has already been allotted by one or more sale transactions, and also helps track the remaining average cost basis. Note that these objects should always be processed in date order. If the date provided to methods allot(long, int), getRemainingShares(int) or setRemainingBasis(double, int) were ever to go backwards in time, this could give incorrect results because of stock splits.
    • Constructor Summary

      Constructors 
      Constructor Description
      CapitalGainBuy​(Account securityAccount, int date, long totalAmount, long sharesPurchased)
      Constructor to allow most fields to be final.
    • Constructor Detail

      • CapitalGainBuy

        public CapitalGainBuy​(Account securityAccount,
                              int date,
                              long totalAmount,
                              long sharesPurchased)
        Constructor to allow most fields to be final.
        Parameters:
        securityAccount - The associated security account.
        date - The date of the buy transaction.
        totalAmount - The total buy amount, in the investment account's currency.
        sharesPurchased - The number of shares purchased in the buy transaction.
    • Method Detail

      • getDateInt

        public int getDateInt()
        Returns:
        The purchase date.
      • getTotalAmount

        public long getTotalAmount()
        Returns:
        The total purchase amount plus any fees purchasing.
      • getSharesPurchased

        public long getSharesPurchased()
        Returns:
        The total number of shares purchased, not corrected for any stock splits.
      • isAllocated

        public boolean isAllocated()
        Returns:
        True if any shares were allotted and remaining basis defined, false if none are allocated, use getTotalAmount() to get the cost basis.
      • allot

        public long allot​(long sharesRequested,
                          int asofDate)
        Allocate the requested number of shares from the total amount, if possible.
        Parameters:
        sharesRequested - The number of shares requested to be allotted.
        asofDate - The date of the allotment.
        Returns:
        The actual number of shares that could be allotted from this buy transaction.
      • getRemainingShares

        public long getRemainingShares​(int asofDate)
        Compute the remaining shares for a buy transaction
        Parameters:
        asofDate - The date the remaining shares should be computed for, adjusting for any splits previous.
        Returns:
        The number of shares for this buy transaction that have not yet been allocated as of the given date.
      • setRemainingBasis

        public void setRemainingBasis​(double averageCost,
                                      int asofDate)
        Compute the remaining basis for this buy transaction, given a particular average cost.
        Parameters:
        averageCost - The current average cost, computed for a particular sale transaction.
        asofDate - The date the remaining basis is valid for. The shares remaining are brought forward to this date, adjusting for splits.
      • getRemainingBasis

        public long getRemainingBasis()
        Returns:
        The remaining cost basis for the shares not yet allocated to a sale transaction.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object