Skip to content

Instantly share code, notes, and snippets.

@ts95
Last active October 22, 2020 23:19
Show Gist options
  • Save ts95/2b2337bcfb1268f0eb54c8400cd6f4fe to your computer and use it in GitHub Desktop.
Save ts95/2b2337bcfb1268f0eb54c8400cd6f4fe to your computer and use it in GitHub Desktop.
Script for calculating rollover fees (Kraken)
#!/usr/bin/env python3.6
"""Script for calculating rollover fees"""
import argparse
import requests
# One rollover for every 4 hours
ROLLOVER_RATE = 4
# 0.02% for most currency pairs
KRAKEN_ROLLOVER_FEE = 0.00002
def calc_fee(amount, step, fee):
"""Returns the fee for a given step"""
return round(amount-round(amount*step*fee, 5), 5)
def calc_sum(amount, rollover_count, fee):
"""Returns the sum of the fees"""
rollovers = range(rollover_count)
sum_fees = 0
remaining_amount = amount
for step in rollovers:
fee_diff = remaining_amount-calc_fee(remaining_amount, step, fee)
sum_fees += fee_diff
remaining_amount -= fee_diff
return remaining_amount, sum_fees
def print_btc_rate(btc):
"""Prints the current exchange rate of BTC for multiple currencies"""
url = 'https://blockchain.info/ticker'
res = requests.get(url)
if not 200 <= res.status_code <= 299:
raise Exception(f"GET request for {url} failed with status code {res.status_code}")
json = res.json()
def print_for_currency(last, symbol):
value = last * btc
print(f"{value:.2f}{symbol}")
print_for_currency(json['USD']['last'], json['USD']['symbol'])
print_for_currency(json['EUR']['last'], json['EUR']['symbol'])
def main():
"""Entry point"""
parser = argparse.ArgumentParser(description="Calculate sum of rollover fees")
parser.add_argument('-a', '--amount', type=float, help="The base amount from which the "
"rollover fees will be derived.",
required=True)
parser.add_argument('-d', '--days', type=int, help="Number of days", required=True)
parser.add_argument('-f', '--fee', type=float, default=KRAKEN_ROLLOVER_FEE,
help="Define fee (as decimal value)", required=False)
args = parser.parse_args()
rollover_count = int((args.days * 24) / ROLLOVER_RATE)
remaining_amount, sum_fees = calc_sum(args.amount, rollover_count, args.fee)
print("Total fees")
print(f"{sum_fees:.5f}฿")
print_btc_rate(sum_fees)
print()
print("Remaining amount")
print(f"{remaining_amount:.5f}฿")
print_btc_rate(remaining_amount)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment