Class AccountLedgerService

java.lang.Object
com.bluebell.greenhouse.radicle.services.AbstractGenericEntityService<Account, AccountRepository>
com.bluebell.greenhouse.radicle.services.account.AccountLedgerService

@Service @Transactional(readOnly=true) public class AccountLedgerService extends AbstractGenericEntityService<Account, AccountRepository>
Service-layer for building and reading account ledger entries from positions and transactions.
Version:
3.0.6
Author:
Stephen Prizio
  • Constructor Details

  • Method Details

    • rebuildLedger

      @Transactional public List<AccountLedgerEntry> rebuildLedger(Account account)
      Rebuilds the ledger for the given Account from persisted account events.
      Parameters:
      account - Account
      Returns:
      rebuilt ledger entries
    • recordManualBalanceAdjustment

      @Transactional public Optional<AccountLedgerEntry> recordManualBalanceAdjustment(Account account, double targetBalance)
      Records a manual balance adjustment when the requested balance differs from the latest ledger balance.
      Parameters:
      account - Account
      targetBalance - requested account balance
      Returns:
      Optional adjustment entry
    • deleteEntriesForAccount

      @Transactional public void deleteEntriesForAccount(Account account)
      Deletes all ledger entries for the given Account.
      Parameters:
      account - Account
    • deleteEntriesForTransaction

      @Transactional public void deleteEntriesForTransaction(Transaction transaction)
      Removes the ledger entries linked to the given Transaction. Must be called before the transaction itself is deleted, otherwise the entries are left referencing a row that no longer exists and the flush fails on the dangling association.
      Parameters:
      transaction - Transaction
    • calculateProfitPercentage

      public double calculateProfitPercentage(Position position)
      Calculates a position's realized profit percentage from its ledger entry.
      Parameters:
      position - Position
      Returns:
      realized profit percentage
    • calculateNetProfit

      public double calculateNetProfit(Account account)
      Calculates an account's closed position net profit.
      Parameters:
      account - Account
      Returns:
      account closed position net profit
    • calculateFundedCapital

      public double calculateFundedCapital(Account account)
      Calculates the account's net funded capital from its initial balance, deposits, and withdrawals. Legacy accounts without capital ledger entries fall back to their recorded initial balance.
      Parameters:
      account - Account
      Returns:
      net funded capital
    • calculateFundedCapital

      public double calculateFundedCapital(Account account, List<AccountLedgerEntry> entries)
      Calculates funded capital from ledger entries already loaded by an enclosing computation.
      Parameters:
      account - Account
      entries - that account's ledger entries
      Returns:
      net funded capital
    • calculatePercentageReturn

      public double calculatePercentageReturn(Account account)
      Calculates an account's closed position net profit percentage return against its funded capital.
      Parameters:
      account - Account
      Returns:
      account percentage return
    • summariseAll

      public Map<Long, AccountLedgerSummary> summariseAll(Collection<Account> accounts)
      Calculates the AccountLedgerSummary for every given Account in two queries.
      Parameters:
      accounts - Collection of Accounts
      Returns:
      summaries keyed by account id
    • summarise

      public AccountLedgerSummary summarise(Account account)
      Calculates an account's net profit and percentage return together.
      Parameters:
      account - Account
      Returns:
      AccountLedgerSummary
    • summarise

      public AccountLedgerSummary summarise(Account account, List<Position> closedPositions, List<AccountLedgerEntry> entries)
      Summarises an account from collections already loaded by an enclosing computation.
      Parameters:
      account - Account
      closedPositions - that account's closed positions
      entries - that account's ledger entries
      Returns:
      AccountLedgerSummary
    • findEntryForPosition

      public Optional<AccountLedgerEntry> findEntryForPosition(Position position)
      Finds the ledger entry linked to the given Position.
      Parameters:
      position - Position
      Returns:
      Optional AccountLedgerEntry
    • findEntriesForAccount

      public List<AccountLedgerEntry> findEntriesForAccount(Account account)
      Finds all ledger entries for the given Account.
      Parameters:
      account - Account
      Returns:
      List of AccountLedgerEntry
    • findEntriesForAccounts

      public List<AccountLedgerEntry> findEntriesForAccounts(List<Account> accounts)
      Finds all ledger entries for the given Accounts.
      Parameters:
      accounts - List of Accounts
      Returns:
      List of AccountLedgerEntry