diff --git a/beancount_reds_plugins/capital_gains_classifier/long_short.py b/beancount_reds_plugins/capital_gains_classifier/long_short.py index a57ec37..ec44c64 100644 --- a/beancount_reds_plugins/capital_gains_classifier/long_short.py +++ b/beancount_reds_plugins/capital_gains_classifier/long_short.py @@ -56,7 +56,7 @@ def is_interesting_entry(entry): return contains_generic(entry) and not contains_shortlong_postings(entry) def reductions(entry): - return [p for p in entry.postings if (p.cost and p.units.number and p.price)] + return [p for p in entry.postings if (p.cost and p.units.number and p.price != None)] def sale_type(p, entry_date): diff = relativedelta.relativedelta(entry_date, p.cost.date) @@ -73,6 +73,8 @@ def sale_type(p, entry_date): if isinstance(entry, data.Transaction) and is_interesting_entry(entry): rewrite_count_matches += 1 sale_types = [sale_type(p, entry.date) for p in reductions(entry)] + if not sale_types: + continue short_gains = sum(s[1] for s in sale_types if s[0] is False) long_gains = sum(s[1] for s in sale_types) - short_gains diff --git a/beancount_reds_plugins/capital_gains_classifier/test_gain_loss.py b/beancount_reds_plugins/capital_gains_classifier/test_gain_loss.py index a52a5d3..5134b04 100644 --- a/beancount_reds_plugins/capital_gains_classifier/test_gain_loss.py +++ b/beancount_reds_plugins/capital_gains_classifier/test_gain_loss.py @@ -15,7 +15,7 @@ }""" -class TestLongShort(cmptest.TestCase): +class TestGainLoss(cmptest.TestCase): def test_empty_entries(self): entries, _ = gain_loss([], options.OPTIONS_DEFAULTS.copy(), config) diff --git a/beancount_reds_plugins/capital_gains_classifier/test_long_short.py b/beancount_reds_plugins/capital_gains_classifier/test_long_short.py index 62bdf15..9176b2e 100644 --- a/beancount_reds_plugins/capital_gains_classifier/test_long_short.py +++ b/beancount_reds_plugins/capital_gains_classifier/test_long_short.py @@ -302,3 +302,46 @@ def test_shortposition_reduction(self, entries, _, options_map): Assets:Bank -50 USD Income:Capital-Gains:Long -50.00 USD """, new_entries) + + @loader.load_doc() + def test_zero_missing_price(self, entries, _, options_map): + """ + 2014-01-01 open Assets:Brokerage + 2014-01-01 open Assets:Bank + 2014-01-01 open Income:Capital-Gains + + 2014-02-01 * "Buy" + Assets:Brokerage 100 ORNG {1 USD} + Assets:Bank -100 USD + + 2016-03-01 * "Sell at complete loss" + Assets:Brokerage -50 ORNG {1 USD} @ 0 USD + Income:Capital-Gains + + 2016-03-01 * "Sell but forgot price" + Assets:Brokerage -50 ORNG {1 USD} + Assets:Bank 100 USD + Income:Capital-Gains + """ + new_entries, _ = long_short(entries, options_map, config) + + self.assertEqualEntries(""" + 2014-01-01 open Assets:Brokerage + 2014-01-01 open Assets:Bank + 2014-01-01 open Income:Capital-Gains + 2014-01-01 open Income:Capital-Gains:Long + + 2014-02-01 * "Buy" + Assets:Brokerage 100 ORNG {1 USD} + Assets:Bank -100 USD + + 2016-03-01 * "Sell at complete loss" + Assets:Brokerage -50 ORNG {1 USD, 2014-02-01} @ 0 USD + Income:Capital-Gains:Long 50 USD + + 2016-03-01 * "Sell but forgot price" + Assets:Brokerage -50 ORNG {1 USD, 2014-02-01} + Assets:Bank 100 USD + Income:Capital-Gains -50 USD + + """, new_entries)