Skip to content

Commit

Permalink
Add --max-overdraft option
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsofwinter committed Oct 7, 2018
1 parent c567486 commit 38aa244
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
)
Expand Down
9 changes: 5 additions & 4 deletions tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -33,15 +34,15 @@ 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 = {}

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):
Expand Down

0 comments on commit 38aa244

Please sign in to comment.