From 8fd4382ccb5b0e21d0fa156892c773b07a045b23 Mon Sep 17 00:00:00 2001 From: psycodad Date: Sat, 3 Dec 2022 14:06:09 +0100 Subject: [PATCH] works, pulls prices from kraken directly --- kraken-report.py | 97 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 18 deletions(-) diff --git a/kraken-report.py b/kraken-report.py index 0bff3f6..ef086b1 100755 --- a/kraken-report.py +++ b/kraken-report.py @@ -32,8 +32,14 @@ ####################### # IMPORTS ####################### -import csv, sys +import csv, sys, getopt, requests +####################### +# CONSTANTS +####################### +price_pair = "XXBTZEUR" +linelen = 60 +KRK_TICKER_URL = "https://api.kraken.com/0/public/Ticker" ####################### # FUNCTIONS @@ -44,8 +50,10 @@ def calc_totals(trades): sum_s2 = 0 sum_b1 = 0 sum_b2 = 0 + global quiet for t in trades: - print t['time'], t['pair'], t['type'], t['price'], t['vol'], t['cost'] + if not quiet: + print "%s %s %s %0.4f %f %0.2f" %(t['time'].split(".")[0], t['pair'], t['type'], float(t['price']), float(t['vol']), float(t['cost'])) if t['type'] == 'sell': sum_s1 += float(t['vol']) sum_s2 += float(t['cost']) @@ -61,26 +69,68 @@ def calc_totals(trades): else: avg_b = 0 - print "SUMS:", sum_s1, sum_s2, avg_s, sum_b1, sum_b2, avg_b - return [sum_s1, sum_s2, avg_s, sum_b1, sum_b2, avg_b,] + if not quiet: hrule("=") + print "*BUY*:\tAmount\t\tVolume\t\tAvg. price" + print "\t%f\t%f\t%f" %(sum_b1, sum_b2, avg_b) + print "*SELL*:\tAmount\t\tVolume\t\tAvg. price" + print "\t%f\t%f\t%f" %(sum_s1, sum_s2, avg_s) + hrule("=") + return [sum_b1, sum_b2, avg_b, sum_s1, sum_s2, avg_s,] + + +def get_krakenticker(url): + resp = requests.get(url) + return resp.json() + + +def hrule(character): + print character*linelen + + +def parse_param(argv): + """read cmd line params and switches""" + try: + opts, args = getopt.getopt(argv, "p:f:hqs", ["price=", "file=", "help", "quiet", "short", ]) + return opts, args + except getopt.GetoptError: + show_usage() + sys.exit(2) + + +def show_usage(): + print """ +Usage: +%s -f [--help] [--quiet] [--short] +""" %sys.argv[0] + sys.exit(1) + ####################### # MAIN ####################### if __name__ == '__main__': - print sys.argv - if len(sys.argv) < 3: - print """ -Usage: - %s -""" %sys.argv[0] - sys.exit(1) + if len(sys.argv) < 3: show_usage() + + opts, args = parse_param(sys.argv[1:]) + #print opts, args + quiet = False + + for opt, arg in opts: + if opt in ("-h", "--help"): + show_usage() + if opt in ("-f", "--file"): + filename = arg + if opt in ("-p", "--price"): + btc_price = float(arg) + if opt in ("-q", "--quiet", "-s", "--short"): + quiet = True - filename = sys.argv[1] - btc_price = float(sys.argv[2]) + #filename = sys.argv[1] + #btc_price = float(sys.argv[2]) data = {} - + krakenprice = get_krakenticker(KRK_TICKER_URL)['result'] + #print krakenprice, type(krakenprice) # loop over all data and sort into pairs fh = open(filename, 'r') @@ -95,9 +145,20 @@ Usage: # loop over each pair pairs = data.keys() for pair in pairs: - print "*"*20 + pair + "*"*20 + print + hrule("-") + print " "*25 + pair + " "*25 + hrule("-") result_data[pair] = calc_totals(data[pair]) - print "Result:", result_data - - + price = float(krakenprice[pair]['c'][0].rstrip("0")) + print "Current Krakenprice [%s]: %0.6f" %(pair, price,) + + profit_buy = ( result_data[pair][0] * price ) - (result_data[price_pair][0] * result_data[pair][2]) + print "Profit [BUY] =", profit_buy + profit_sell = ( result_data[pair][3] * price ) - (result_data[price_pair][3] * result_data[pair][5]) + print "Profit [SELL] =", profit_sell + + hrule("*") + #print "Result:", result_data + #print result_data[price_pair] \ No newline at end of file