Skip to content

Commit

Permalink
Update parser for Coinbase Pro.
Browse files Browse the repository at this point in the history
This addresses part of #16.
  • Loading branch information
robertwb committed Feb 7, 2021
1 parent 669b57b commit 5604666
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions bitcoin_gains.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def parse_file(self, filename):
for ix, row in enumerate(csv.reader(open(filename))):
if not row or first:
first = False
self.header = row
continue
elif row[0].startswith('#'):
continue
Expand Down Expand Up @@ -430,46 +431,61 @@ def parse_row(self, row):
return Transaction(timestamp, type, btc, usd, info=info, account=account, txid=txid)


# AKA Coinbase Pro
class GdaxParser(CsvParser):

def parse_time(self, stime):
return time.strptime(stime.replace('Z', '000'), '%Y-%m-%dT%H:%M:%S.%f')

def default_account(self):
return 'gdax'
return 'CoinbasePro'

class GdaxFillsParser(GdaxParser):
expected_header = 'trade id,product,side,created at,size,size unit,price,fee,total,price/fee/total unit'
expected_header = '(portfolio,)?trade id,product,side,created at,size,size unit,price,fee,total,price/fee/total unit'

def parse_row(self, row):
if self.header[0] == 'portfolio':
account = row.pop(0)
if account == 'default':
account = self.default_account()
else:
account = self.default_account()
trade, product, buy_sell, stime, btc, unit, price, fee_usd, total, pft_unit = row
if product != 'BTC-USD':
return None
assert unit == 'BTC' and pft_unit == 'USD'
timestamp = self.parse_time(stime)
usd = decimal.Decimal(total) + decimal.Decimal(fee_usd)
if buy_sell == 'BUY':
return Transaction(timestamp, 'trade', btc, usd, fee_usd=fee_usd)
return Transaction(timestamp, 'trade', btc, usd, fee_usd=fee_usd, account=account)
elif buy_sell == 'SELL':
return Transaction(timestamp, 'trade', '-' + btc, usd, fee_usd=fee_usd)
return Transaction(timestamp, 'trade', '-' + btc, usd, fee_usd=fee_usd, account=account)
else:
raise ValueError("Unknown transactiont type: %s" % buy_sell)


class GdaxAccountParser(GdaxParser):
expected_header = 'type,time,amount,balance,amount/balance unit,transfer id,trade id,order id'
expected_header = '(portfolio,)?type,time,amount,balance,amount/balance unit,transfer id,trade id,order id'

def parse_row(self, row):
if self.header[0] == 'portfolio':
account = row.pop(0)
if account == 'default':
account = self.default_account()
else:
account = self.default_account()
type, stime, amount, _, unit, tid, _, _ = row
if unit == 'USD':
return None
if unit != 'BTC':
raise ValueError("Only BTC accounts supported.")
timestamp = self.parse_time(stime)
if type == 'match':
return None # handled in fills
elif type == 'deposit':
return Transaction(timestamp, 'deposit', amount, 0, id=tid)
return Transaction(timestamp, 'deposit', amount, 0, id=tid, account=account)
elif type == 'withdrawal':
return Transaction(timestamp, 'withdraw', amount, 0, id=tid)
return Transaction(timestamp, 'withdraw', amount, 0, id=tid, account=account)
else:
raise ValueError("Unknown transactiont type: %s" % type)

Expand Down

0 comments on commit 5604666

Please sign in to comment.