Class InvestUtil

java.lang.Object
com.infinitekind.moneydance.model.InvestUtil

public abstract class InvestUtil
extends java.lang.Object
Miscellaneous investment functions
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String UNREALIZED_MARKER
    Marker to identify special sale transactions that are used for unrealized gain calcs.
  • Method Summary

    Modifier and Type Method Description
    static java.util.List<CapitalGainBuy> buildBuyTransactionList​(Account sec)
    Create a sorted map of date versus buy amount, which includes both cash purchases as well as reinvestments.
    static double computeROI​(java.util.List<ROICashFlowEntry> timeSeries, double seedPercent)
    Compute the Return On Investment, or Internal Rate of Return, or Annual Return Percent.
    static SplitTxn generateUnrealizedSaleTxn​(Account secAcct, long shares, int asofDate)
    Generate a sale transaction as if all remaining shares of a security were sold off as of a specific date.
    static SplitTxn generateUnrealizedSaleTxn​(Account secAcct, java.util.Hashtable<java.lang.String,​AvailableSharesTracker> lotsRemaining, int asofDate)
    Generate a sale transaction as if all remaining shares of a security were sold off as of a specific date.
    static CapitalGainBuy getBuyTracker​(Account sec, CurrencyType secCurr, CurrencyType investCurr, SplitTxn split)
    Generate a tracking class for a buy transaction when using average cost method.
    static long getCostBasis​(Account sec)
    Returns the current cost basis for the given security.
    static CapitalGainResult getCostBasisCapGain​(Account sec, java.util.List<CapitalGainBuy> buyTxns, SplitTxn sellTxn)
    Returns the current cost basis for the shares in the given sell transaction in the given security.
    static double getCostPerShareValue​(Account sec, long shares, long cost)
    Compute the cost per share, avoiding a divide by zero fault (returns zero if no shares).
    static long getPerShareSalesNet​(AbstractTxn atxn)
    Compute the net sale amount, in the currency of the parent investment account, of a sale transaction.
    static java.util.Hashtable<java.lang.String,​AvailableSharesTracker> getRemainingLots​(AccountBook book, Account secAcct, int unrealizedAsOfDate)
    Determine the remaining lots in a lot-based security account, as of a specified date, accounting for all sales on or prior to that date.
    static java.lang.String getSecurityBalanceInfo​(Account secAccount, long shares, int asofDate, CurrencyType baseCurrency, char decimal)
    Get security balance detailed info on the shares and price per share.
    static boolean isCostBasisValid​(Account sec)  
    static boolean isInvestmentIncome​(InvestTxnType txnType)
    Determine if a transaction is an income from the fund or if it is a 'bank' transaction where the user is putting money in or taking money out.
    static boolean isSaleTransaction​(InvestTxnType txnType)
    Determine if a transaction is a transaction that can be considered a sale, which is treated specially for capital gains.
    static boolean isUserCashFlow​(InvestTxnType txnType)
    Determine if a transaction is a 'bank' transaction where the user is putting money in or taking money out.
    static void processLotBasedTxn​(TxnSet acctTxns, SplitTxn split, CurrencyType secCurr, int unrealizedAsOfDate, java.util.Hashtable<java.lang.String,​AvailableSharesTracker> result)
    Process a transaction for a security that uses lot-based cost.
    static int recomputeStartDate​(java.util.List<ROICashFlowEntry> timeSeries, int currentBeginDate)
    The start date is the parameter that modifies the result of an ROI computation the most, since every cash flow is computed against the start date.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UNREALIZED_MARKER

      public static final java.lang.String UNREALIZED_MARKER
      Marker to identify special sale transactions that are used for unrealized gain calcs.
      See Also:
      Constant Field Values
  • Method Details

    • getCostBasisCapGain

      public static CapitalGainResult getCostBasisCapGain​(Account sec, java.util.List<CapitalGainBuy> buyTxns, SplitTxn sellTxn)
      Returns the current cost basis for the shares in the given sell transaction in the given security. If the given sell transaction is null then the cost basis for outstanding shares in the given security account is returned.
      Parameters:
      sec - The security account.
      buyTxns - Ignored as of Moneydance 2018 build 1684
      sellTxn - The sale transaction to analyze.
      Returns:
      The capital gain values for the sales transaction, including short term and long term portions. The return value will be in the parent investment account's currency.
    • getPerShareSalesNet

      public static long getPerShareSalesNet​(AbstractTxn atxn)
      Compute the net sale amount, in the currency of the parent investment account, of a sale transaction. Returns zero if not a sale transaction. Returns the cash proceeds of the sale less any transaction fees or commission.
      Parameters:
      atxn - The transaction to compute with.
      Returns:
      If atxn is a sale transaction, the net proceeds of the sale. Otherwise returns zero.
      Since:
      MD2011
    • getCostBasis

      public static long getCostBasis​(Account sec)
      Returns the current cost basis for the given security.
    • buildBuyTransactionList

      public static java.util.List<CapitalGainBuy> buildBuyTransactionList​(Account sec)
      Create a sorted map of date versus buy amount, which includes both cash purchases as well as reinvestments. This map can be used to determine whether a sale transaction represents long term capital gains, short term capital gains, or both.
      Parameters:
      sec - the security account to build the list of transactions for.
      Returns:
      A map of integer date versus the total amount of the buy transaction, including any fees or commissions.
    • getBuyTracker

      public static CapitalGainBuy getBuyTracker​(Account sec, CurrencyType secCurr, CurrencyType investCurr, SplitTxn split)
      Generate a tracking class for a buy transaction when using average cost method. This tracker is used to figure out which shares are sold first (FIFO) so that long term and short term capital gains can be computed.
      Parameters:
      sec - The security account for which the buy belongs.
      secCurr - The security account's currency (shares).
      investCurr - The parent investment account's currency (dollars, euro, etc.).
      split - The buy transaction.
      Returns:
      A tracker class containing the cost basis of the buy transaction, including any transaction fees.
    • recomputeStartDate

      public static int recomputeStartDate​(java.util.List<ROICashFlowEntry> timeSeries, int currentBeginDate)
      The start date is the parameter that modifies the result of an ROI computation the most, since every cash flow is computed against the start date. There are times when you should recompute the start date because it is estimated too low, for example when the user picks 'All Dates' and the start date is set to 1970. This would make the ROI computation artificially low. By and large the start date is fixed by a specific date, so the time series will have already have the correct start date. For those situations where it doesn't, call this method prior to calling computeROI(java.util.List, double) This method finds the first date in the time series, and compares it to the current start date. If the first date in the time series is at or before the current start date, use the current start date. If the first date in the time series is after the current, then it is used instead.
      Parameters:
      timeSeries - The time series to recompute the start date for, sorted in ascending date order.
      currentBeginDate - The current starting date.
      Returns:
      The new start date that was computed.
    • computeROI

      public static double computeROI​(java.util.List<ROICashFlowEntry> timeSeries, double seedPercent)
      Compute the Return On Investment, or Internal Rate of Return, or Annual Return Percent. This method uses the Secant method to iteratively find the percent to 0.00001 accuracy, or 0.001%. This method will not iterate more than 100 times. The equation for secant iterative rate computation is:
      Rn+1 = Rn - NPVn*( (Rn - Rn-1) / (NPVn - NPVn-1)
      Where R is the rate and NPV is the net present value
      Parameters:
      timeSeries - The time series of cash flows. This time series must include the initial balance as a positive and the ending balance as a negative value.
      seedPercent - The starting seed value to begin computations with.
      Returns:
      The best estimate rate of return for the cash flow series. If the time series is not valid, returns Double.NaN.
    • getCostPerShareValue

      public static double getCostPerShareValue​(Account sec, long shares, long cost)
      Compute the cost per share, avoiding a divide by zero fault (returns zero if no shares).
      Parameters:
      sec - The security account the shares belong to.
      shares - The number of shares, in the security currency.
      cost - The total cost, in the parent investment account's currency.
      Returns:
      The cost per share as a double precision floating point.
    • isCostBasisValid

      public static boolean isCostBasisValid​(Account sec)
    • getSecurityBalanceInfo

      public static java.lang.String getSecurityBalanceInfo​(Account secAccount, long shares, int asofDate, CurrencyType baseCurrency, char decimal)
      Get security balance detailed info on the shares and price per share.
      Parameters:
      secAccount - The security account.
      shares - The amount for the account (in shares)
      asofDate - The date to check the price for.
      baseCurrency - The base currency used to display the price.
      decimal - Decimal point for formatting.
      Returns:
      The balance information shown as '[ {shares} @ {price} ]'
    • generateUnrealizedSaleTxn

      public static SplitTxn generateUnrealizedSaleTxn​(Account secAcct, long shares, int asofDate)
      Generate a sale transaction as if all remaining shares of a security were sold off as of a specific date. This method should be used for securities using the average cost method.
      Parameters:
      secAcct - The security account.
      shares - The share balance as of the given date.
      asofDate - The date to compute the hypothetical sale for.
      Returns:
      A hypothetical sale of all remaining shares as of the given date.
    • generateUnrealizedSaleTxn

      public static SplitTxn generateUnrealizedSaleTxn​(Account secAcct, java.util.Hashtable<java.lang.String,​AvailableSharesTracker> lotsRemaining, int asofDate)
      Generate a sale transaction as if all remaining shares of a security were sold off as of a specific date. This method should be used for securities using lot-based cost tracking.
      Parameters:
      secAcct - The security account.
      lotsRemaining - A list of the remaining lots to be sold, or null to use average cost basis.
      asofDate - The date to compute the hypothetical sale for.
      Returns:
      A hypothetical sale of all remaining shares as of the given date.
    • getRemainingLots

      public static java.util.Hashtable<java.lang.String,​AvailableSharesTracker> getRemainingLots​(AccountBook book, Account secAcct, int unrealizedAsOfDate)
      Determine the remaining lots in a lot-based security account, as of a specified date, accounting for all sales on or prior to that date.
      Parameters:
      book - The data set that contains the transactions
      secAcct - The security to compute the remaining lots for.
      unrealizedAsOfDate - The date prior to which to get the unused lots.
      Returns:
      A map of transaction ID (key) to number of shares remaining for that transaction (value) of all remaining (unsold) shares in the account as of that date. Will return null if the security account doesn't use lot-based accounting.
    • processLotBasedTxn

      public static void processLotBasedTxn​(TxnSet acctTxns, SplitTxn split, CurrencyType secCurr, int unrealizedAsOfDate, java.util.Hashtable<java.lang.String,​AvailableSharesTracker> result)
      Process a transaction for a security that uses lot-based cost. For buys, the shares bought are added to the resulting available shares. For sales, the shares sold are removed from the respective lots, as they are no longer available. If a split had occurred, the available shares are adjusted accordingly.
      Parameters:
      acctTxns - The transactions for the account.
      split - The transaction to process.
      secCurr - The security currency.
      unrealizedAsOfDate - The date prior to which to get the unused/available lots.
      result - A map of transaction ID to number of shares available. This is an [in, out] parameter as the map is modified by this method. The available shares will be correct as of the date of split
    • isUserCashFlow

      public static boolean isUserCashFlow​(InvestTxnType txnType)
      Determine if a transaction is a 'bank' transaction where the user is putting money in or taking money out. For the purposes of this test, we need to know whether some bank account in the list of accounts will change in cash value. This includes the investment account cash value and any external accounts via transfers. Currently there is no ROI cash flow portion only for reinvested dividends/capital gains, because shares are added without changing the cash amount of the investment account. The shares will already be counted by the ROI computation in the ending balance for the time period.
      Parameters:
      txnType - The transaction type.
      Returns:
      True if the transaction is one where the user is adding or subtracting money, false if the transaction is internal.
    • isInvestmentIncome

      public static boolean isInvestmentIncome​(InvestTxnType txnType)
      Determine if a transaction is an income from the fund or if it is a 'bank' transaction where the user is putting money in or taking money out.
      Parameters:
      txnType - The transaction type.
      Returns:
      True if the transaction is an income or expense transaction that represents an income from the security, false if it is some type of user-driven buy or sale transaction.
    • isSaleTransaction

      public static boolean isSaleTransaction​(InvestTxnType txnType)
      Determine if a transaction is a transaction that can be considered a sale, which is treated specially for capital gains.
      Parameters:
      txnType - The transaction type.
      Returns:
      True if the transaction is a sale transaction, false otherwise..