''' Display last three months of profits grouped by exchange ''' import sqlite3 import datetime linewidth = 40 exchanges = ["binance","gateio","kucoin","okex"] #Connect to db connection = sqlite3.connect("profits_database.db") cursor = connection.cursor() cursor.execute("""SELECT exchange_name, CASE WHEN strftime('%Y-%m', timestamp, 'unixepoch', '-3 hours') = strftime('%Y-%m', 'now', 'localtime') THEN strftime('%Y-%m', 'now', 'localtime') WHEN strftime('%Y-%m', timestamp, 'unixepoch', '-3 hours') = strftime('%Y-%m', 'now', 'localtime', '-1 month') THEN strftime('%Y-%m', 'now', 'localtime', '-1 month') WHEN strftime('%Y-%m', timestamp, 'unixepoch', '-3 hours') = strftime('%Y-%m', 'now', 'localtime', '-2 month') THEN strftime('%Y-%m', 'now', 'localtime', '-2 month') ELSE 'Other Months' END AS month_group, SUM(amount) AS total_amount FROM profits_table WHERE strftime('%s', 'now') - timestamp <= 365 * 24 * 60 * 60 -- 365 days in seconds GROUP BY exchange_name, month_group ORDER BY exchange_name, month_group;""") by_exchange = cursor.fetchall() # Get the current date current_date = datetime.date.today() # Get the current month in YYYY-MM format current_month = current_date.strftime("%Y-%m") # Get the last three months last_three_months = [current_month] for i in range(3): # Calculate the date for the previous month previous_month = current_date.replace(day=1) - datetime.timedelta(days=current_date.day) previous_month = previous_month.replace(month=previous_month.month - i) # Format the previous month to YYYY-MM format previous_month_str = previous_month.strftime("%Y-%m") # Append the previous month to the list last_three_months.append(previous_month_str) #Now we got the month list and the db data, let's present it in a readable fashion print("Revenue per month:") print("="*linewidth) for yearmonth in last_three_months[:3][::-1]: print(f"{yearmonth}:") total = 0 for exchange in exchanges: if exchange=="binance": for item in by_exchange: if item[0]=="binance" and item[1]==yearmonth: binance_total = item[2] total+=item[2] elif exchange=="gateio": for item in by_exchange: if item[0]=="gateio" and item[1]==yearmonth: gateio_total = item[2] total+=item[2] elif exchange=="kucoin": for item in by_exchange: if item[0]=="kucoin" and item[1]==yearmonth: kucoin_total = item[2] total+=item[2] elif exchange=="okex": for item in by_exchange: if item[0]=="okex" and item[1]==yearmonth: okex_total = item[2] total+=item[2] print(f"Binance: {round(binance_total,2)} ({round(binance_total/total*100,2)}%)") print(f"Gate.io: {round(gateio_total,2)} ({round(gateio_total/total*100,2)}%)") print(f"KuCoin: {round(kucoin_total,2)} ({round(kucoin_total/total*100,2)}%)") print(f"OKX: {round(okex_total,2)} ({round(okex_total/total*100,2)}%)") print("-"*linewidth) print(f"Total: {round(total,2)}") print("="*linewidth)