Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 0 price (for total loss) and skip transactions without a price #33

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions beancount_reds_plugins/capital_gains_classifier/test_long_short.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)