DCAv2/profits/monthly_details.py

53 lines
1.3 KiB
Python

'''
Usage: python3 monthly_details.py YYYY-MM
Generates a csv file per exchange with details of profit per pair in the format "pair,profit"
'''
import sqlite3
import csv
import sys
try:
month = sys.argv[1]
except Exception as e:
print(e)
print("Usage: python3 monthly_details.py YYYY-MM")
sys.exit()
result = {"binance":[],"gateio":[],"kucoin":[],"okex":[]}
#Connect to db
connection = sqlite3.connect("profits_database.db")
cursor = connection.cursor()
query = """SELECT exchange_name, pair, strftime('%Y-%m', datetime(timestamp, 'unixepoch')) AS month, SUM(amount) AS total_profit
FROM profits_table
GROUP BY exchange_name, pair, month;"""
print("Querying the database")
cursor.execute(query)
by_exchange = cursor.fetchall()
to_binance = []
to_gateio = []
to_kucoin = []
to_okex = []
print("Generating reports")
for item in by_exchange:
if item[0] in result and item[2]==month:
result[item[0]].append((item[1],item[3]))
#Descending order
for item in result.copy():
result[item].sort(key=lambda tup: tup[1], reverse=True)
print("Writing reports to disk")
for item in result:
with open(f"{item}_{month}_report.csv","w",newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerows(result[item])
print("done")