You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.1 KiB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Copyright (c) 2022 BOXTEC AG <src@boxtec.ch>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# --------------------------------------------------------------------------
#######################
# IMPORTS
#######################
import csv, sys
#######################
# FUNCTIONS
#######################
def calc_totals(trades):
#print trades
sum_s1 = 0
sum_s2 = 0
sum_b1 = 0
sum_b2 = 0
for t in trades:
print t['time'], t['pair'], t['type'], t['price'], t['vol'], t['cost']
if t['type'] == 'sell':
sum_s1 += float(t['vol'])
sum_s2 += float(t['cost'])
elif t['type'] == 'buy':
sum_b1 += float(t['vol'])
sum_b2 += float(t['cost'])
if sum_s1 > 0 and sum_s2 >0:
avg_s = sum_s2 / sum_s1
else:
avg_s = 0
if sum_b1 > 0 and sum_b2 >0:
avg_b = sum_b2 / sum_b1
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,]
#######################
# MAIN
#######################
if __name__ == '__main__':
print sys.argv
if len(sys.argv) < 3:
print """
Usage:
%s <csv-file> <btc-price>
""" %sys.argv[0]
sys.exit(1)
filename = sys.argv[1]
btc_price = float(sys.argv[2])
data = {}
# loop over all data and sort into pairs
fh = open(filename, 'r')
csv_data = csv.DictReader(fh)
for line in csv_data:
if not data.has_key(line['pair']):
data[line['pair']] = []
data[line['pair']].append(line)
# dict of data to hold all totals for all pairs
result_data = {}
# loop over each pair
pairs = data.keys()
for pair in pairs:
print "*"*20 + pair + "*"*20
result_data[pair] = calc_totals(data[pair])
print "Result:", result_data