-
Notifications
You must be signed in to change notification settings - Fork 4
/
CoinTaxes.py
executable file
·181 lines (157 loc) · 7 KB
/
CoinTaxes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
import argparse
import os
import yaml
import exchanges
from formats import fill_8949, turbo_tax
def get_exchange(name, config):
"""Gets the exchange object based on the name and instantiates it
Source: https://stackoverflow.com/a/1176180/2965993
:param name: exchange name
:param config: exchange configuration
:return:
"""
return getattr(exchanges, name.title())(config)
def fix_orders(exchange, buys, sells):
"""
:param exchange:
:param buys:
:param sells:
:return:
"""
buys_fixed = []
sells_fixed = []
for orders in [buys, sells]:
for order in orders:
# See if the exchange currency is BTC
# if order[6] == 'BTC':
if order['currency'] == 'BTC':
# This is a coin-coin transaction
# We need to get the btc value in $$ and create another trade (a sell order)
price_usd = exchange.get_price(order['order_time'], product='BTC-USD')
cost_btc = order['cost']
cost_usd = cost_btc * price_usd
cost_per_coin_usd = cost_usd / order['amount']
# get the coin name
product = order['product']
# Fix any coin discrepancies (right now call all bitcoin cash BCH, sometimes it is called BCC)
if product == 'BCC':
product = 'BCH'
if order['buysell'] == 'buy':
# buys_fixed.append([
# order['order_time'], product, 'buy', cost_usd, order['amount'], cost_per_coin_usd, 'USD'
# ])
# sells_fixed.append([
# order['order_time'], 'BTC', 'sell', cost_usd, order['cost'], price_usd, 'USD'
# ])
buys_fixed.append({
'order_time': order['order_time'],
'product': product,
'currency': 'USD',
'currency_pair': product + '-USD', # TODO: review
'buysell': 'buy',
'cost': cost_usd,
'amount': order['amount'],
'cost_per_coin': cost_per_coin_usd,
})
sells_fixed.append({
'order_time': order['order_time'],
'product': 'BTC',
'currency': 'USD',
'currency_pair': 'BTC-USD', # TODO: review
'buysell': 'sell',
'cost': cost_usd,
'amount': order['cost'],
'cost_per_coin': price_usd,
})
elif order['buysell'] == 'sell':
# sells_fixed.append([
# order['order_time'], product, 'sell', cost_usd, order['amount'], cost_per_coin_usd, 'USD'
# ])
# buys_fixed.append([
# order['order_time'], 'BTC', 'buy', cost_usd, order['cost'], price_usd, 'USD'
# ])
sells_fixed.append({
'order_time': order['order_time'],
'product': product,
'currency': 'USD',
'currency_pair': product + '-USD', # TODO: review
'buysell': 'sell',
'cost': cost_usd,
'amount': order['cost'],
'cost_per_coin': price_usd,
})
# was order['order_time'], product, 'sell', cost_usd, order['amount'], cost_per_coin_usd, 'USD'
buys_fixed.append({
'order_time': order['order_time'],
'product': 'BTC',
'currency': 'USD',
'currency_pair': 'BTC-USD', # TODO: review
'buysell': 'buy',
'cost': cost_usd,
'amount': order['cost'],
'cost_per_coin': price_usd,
})
else:
print("WEIRD! Unknown order buy sell type!")
print(order)
else:
# This order was already paid/received with USD
if order['buysell'] == 'buy':
buys_fixed.append(order)
elif order['buysell'] == 'sell':
sells_fixed.append(order)
else:
print("WEIRD! Unknown order buy/sell type!")
print(order)
return buys_fixed, sells_fixed
def main():
"""Main function to collect information and create the forms."""
parser = argparse.ArgumentParser(description='CoinTaxes', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--input', default='config.yml', required=True,
help='Configuration file to read.')
parser.add_argument('--output', default='output',
help='Directory to output autogenerated files.')
args = parser.parse_args()
# create output directory
if not os.path.exists(args.output):
os.makedirs(args.output)
# read yaml config file
with open(args.input, 'r') as f:
config = yaml.load(f.read())
# also shares the name in the configs
# exchanges_supported = ['gdax', 'coinbase', 'bittrex', 'gemini', 'poloniex']
exchanges_supported = ['gdax', 'coinbase']
buys = []
sells = []
# go through all supported exchanges
for exchange_name in exchanges_supported:
# instantiate the exchange and aggregate the buys and sells
if exchange_name in config:
exchange = get_exchange(exchange_name, config[exchange_name])
if exchange_name == 'bittrex':
exchange_buys, exchange_sells = exchange.get_buys_sells(order_file=config['bittrex']['file'])
else:
exchange_buys, exchange_sells = exchange.get_buys_sells()
exchange_buys, exchange_sells = fix_orders(exchange, exchange_buys, exchange_sells)
buys += exchange_buys
sells += exchange_sells
# sort the buys and sells by date
buys_sorted = sorted(buys, key=lambda buy_order: buy_order['order_time'])
sells_sorted = sorted(sells, key=lambda buy_order: buy_order['order_time'])
# Get the full order information to be used on form 8949
full_orders = fill_8949.get_cost_basis(
sells_sorted,
buys_sorted,
basis_type='highest',
tax_year=config['year']
)
# Save the files in a pickle
# pickle.dump([buys_sorted, sells_sorted, full_orders], open("save.p", "wb"))
# Make the Turbo Tax import file
if 'txf' in config and config['txf']:
turbo_tax.make_txf(full_orders, year=config['year'])
# Make the 8949 forms
fill_8949.make_pdf(full_orders, "test", config['name'], config['social'], config['year'])
if __name__ == '__main__':
main()