Skip to content

Instantly share code, notes, and snippets.

@seblin
Last active March 26, 2019 21:09
Show Gist options
  • Save seblin/bbf61cb89ccd56e6782585226ea972e8 to your computer and use it in GitHub Desktop.
Save seblin/bbf61cb89ccd56e6782585226ea972e8 to your computer and use it in GitHub Desktop.
import io
import pandas as pd
TESTDATA = io.StringIO(
"""DateTime, Type, Amount, Rate
2017-01-08 11:43:00,buy,0.10000001,200.11
2017-01-08 11:43:00,buy,1.00000002,210.55
2017-01-10 16:11:44,sell,0.10000002,290.44
2017-01-11 18:43:00,buy,0.30000003,300.88
2017-01-12 18:55:00,buy,0.40000004,311.22
2017-01-14 22:22:11,sell,0.90000000,444.88
2018-02-15 09:22:55,sell,0.100000001,555.77
""")
def parse_transactions(csv_stream):
df = pd.read_csv(csv_stream, parse_dates=[0], skipinitialspace=True)
return (df[df.Type == 'buy'], df[df.Type == 'sell'])
def make_sell(sell, buys, amount=0, balance=0):
for buy in buys.itertuples():
amount += buy.Amount
balance += buy.Amount * (sell.Rate - buy.Rate)
if amount >= sell.Amount:
return (buys.loc[buy.Index + 1:], amount - sell.Amount, balance)
raise ValueError(f'Need {sell.Amount} BTC, have {amount} BTC')
def print_info(count, buys, amount, balance):
print()
print(f'{5 * "-"} Verkauf #{count} {25 * "-"}')
print(f'Verbleibende Bitcoins: {amount}')
print(f'Gesamtsaldo (€): {balance:.2f}')
print()
print('Verbleibende Käufe:')
print('Keine' if buys.empty else buys)
def main():
buys, sells = parse_transactions(TESTDATA)
balance = amount = 0
for count, sell in enumerate(sells.itertuples(), 1):
buys, amount, balance = make_sell(sell, buys, amount, balance)
print_info(count, buys, amount, balance)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment