Skip to content

Stock balance calculation in ModelStock and summary report always uses current number of shares #5058

@n-stein

Description

@n-stein

MMEX version:

  • 1.5.19
  • 1.5.20

Note: bug reporters are expected to have verified the bug still exists
either in the last stable version of MMEX or on updated development code
(master branch).

Operating System:

  • Windows
  • Mac OSX
  • Linux

Description of the bug

Function getDailyBalanceAt multiplies the historical price by the current number of shares rather than the number of shares on the date in question. This causes the account summary report to show incorrect balances in stock accounts. The same issue appears in both the ModelStock class and the summary report code.

Reproduction

Is the bug reproducible?

  • Always
  • Randomly
  • Happened only once

Reproduction steps:
Create new security with one transaction for 1 share on 8/30/2022 price $100.
Add a second transaction for 1 share on 9/5/2022 price $80.
Execute Monthly summary report.

Expected result:
Monthly summary report will show $100 in stock for August (1 share * $100) and $160 in stock for September (2 shares * $80).

Actual result:
Monthly summary report shows $200 in stock for August (2 shares * $100) and $160 in stock for September (2 shares * $80).

Additional information

In addition to the above reproduction steps, also try this:

Add a price (but no share transaction) for 7/31/2022 price $200.
Create any other account (e.g. checking) and add any transaction for any date in 7/2022.
Result -->The report will show $400 in stock in July even though the first purchase of the stock wasn't until the end of August...

Note -- I was able to correct the issue for my personal install by changing this line in ModelStock::getDailyBalanceAt

totBalance[stock.id()] += stock.NUMSHARES * valueAtDate;

to

double numShares = 0.0;

Model_Checking::Data_Set shareTxns = Model_Checking::instance().find(
    Model_Checking::TRANSDATE(date, LESS_OR_EQUAL),
    Model_Checking::TOACCOUNTID(32702),
    Model_Checking::TRANSACTIONNUMBER(account->ACCOUNTNAME),
    Model_Checking::ACCOUNTID(Model_Account::instance().get(stock.STOCKNAME)->ACCOUNTID));

for (const auto& txn : shareTxns)
{
    numShares += Model_Shareinfo::instance().ShareEntry(txn.TRANSID)->SHARENUMBER;
}

totBalance[stock.id()] += numShares * valueAtDate;

For simplicity I also replaced the entire code of getDailyBalanceAt in summary.cpp to
return Model_Stock::instance().getDailyBalanceAt(account, date);

However this code only works if the stock name exactly matches the name of the share account and if the share transactions always have the investment account name in the "Number" field (which I believe is the default), which is true for my use but is probably not generic enough.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions