From 38aa244a19a4e6f9836b2e0e5459b40e8e6e1a87 Mon Sep 17 00:00:00 2001 From: bitsofwinter Date: Sun, 7 Oct 2018 17:10:04 +0200 Subject: [PATCH] Add --max-overdraft option --- README.md | 4 ++++ report.py | 2 ++ tax.py | 9 +++++---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7750efd..124a119 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,10 @@ optional arguments: --cointracking-usd Use this flag if you have configured cointracking calculate prices in USD. Conversion from USD to SEK will then be done by this script instead. + --max-overdraft MAX_OVERDRAFT + The maximum overdraft to allow for each coin, at the + event of an overdraft the coin balance will be set to + zero. ``` ### Example diff --git a/report.py b/report.py index 343c962..6aa0e00 100644 --- a/report.py +++ b/report.py @@ -29,6 +29,7 @@ def __str__(self): parser.add_argument('--rounding-report', help='Generate report of roundings done which can be pasted in Ovriga Upplysningar, the file will be put in the out folder.', action='store_true') parser.add_argument('--rounding-report-threshold', help='The number of percent difference required for an amount to be included in the report.', default='1') parser.add_argument('--cointracking-usd', help='Use this flag if you have configured cointracking calculate prices in USD. Conversion from USD to SEK will then be done by this script instead.', action='store_true') +parser.add_argument('--max-overdraft', type=float, help='The maximum overdraft to allow for each coin, at the event of an overdraft the coin balance will be set to zero.', default=1e-9) opts = parser.parse_args() if not os.path.isdir(opts.out): @@ -41,6 +42,7 @@ def __str__(self): tax_events = tax.compute_tax(trades, datetime.datetime(year=opts.year,month=1,day=1,hour=0, minute=0), datetime.datetime(year=opts.year,month=12,day=31,hour=23, minute=59), + opts.max_overdraft, exclude_groups=opts.exclude_groups if opts.exclude_groups else [], coin_report_filename=os.path.join(opts.out, "coin_report.csv") if opts.coin_report else None ) diff --git a/tax.py b/tax.py index b4288cf..943aa9f 100644 --- a/tax.py +++ b/tax.py @@ -9,10 +9,11 @@ def is_fiat(coin): class Coin: - def __init__(self, symbol): + def __init__(self, symbol, max_overdraft): self.symbol = symbol self.amount = 0.0 self.cost_basis = 0.0 + self.max_overdraft = max_overdraft def buy(self, amount:float, price:float): new_amount = self.amount + amount @@ -21,7 +22,7 @@ def buy(self, amount:float, price:float): def sell(self, amount:float, price:float) -> TaxEvent: amount_left = self.amount - amount - if amount_left < -1e-9: + if amount_left < -self.max_overdraft: raise Exception(f"Not enough coins available for {self.symbol}, {self.amount} < {amount}.") if amount_left < 0.0: amount_left = 0.0 @@ -33,7 +34,7 @@ def sell(self, amount:float, price:float) -> TaxEvent: return tax_event -def compute_tax(trades, from_date, to_date, native_currency='SEK', exclude_groups=[], coin_report_filename=None): +def compute_tax(trades, from_date, to_date, max_overdraft, native_currency='SEK', exclude_groups=[], coin_report_filename=None): tax_events = [] coins = {} @@ -41,7 +42,7 @@ def get_buy_coin(trade:Trade): if trade.buy_coin == native_currency: return None if trade.buy_coin not in coins: - coins[trade.buy_coin] = Coin(trade.buy_coin) + coins[trade.buy_coin] = Coin(trade.buy_coin, max_overdraft) return coins[trade.buy_coin] def get_sell_coin(trade:Trade):