139 lines
4.9 KiB
Python
139 lines
4.9 KiB
Python
'''
|
|
Profits report from db
|
|
'''
|
|
|
|
import sqlite3
|
|
import calendar
|
|
import datetime
|
|
|
|
|
|
exchanges = ["binance","gateio","kucoin","okex"]
|
|
line_width = 40
|
|
|
|
#Connect to db
|
|
connection = sqlite3.connect("profits_database.db")
|
|
cursor = connection.cursor()
|
|
|
|
#Last 60 days query
|
|
cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
|
|
SUM(amount) AS total_amount
|
|
FROM profits_table
|
|
WHERE strftime('%s', 'now') - timestamp <= 60 * 24 * 60 * 60 -- 60 days in seconds
|
|
GROUP BY day_utc3
|
|
ORDER BY day_utc3;""")
|
|
last_60_days_rows = cursor.fetchall()
|
|
|
|
#Last 30 days query
|
|
cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
|
|
SUM(amount) AS total_amount
|
|
FROM profits_table
|
|
WHERE strftime('%s', 'now') - timestamp <= 30 * 24 * 60 * 60 -- 30 days in seconds;""")
|
|
last_30_days = cursor.fetchall()
|
|
|
|
#Last 7 days query
|
|
cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
|
|
SUM(amount) AS total_amount
|
|
FROM profits_table
|
|
WHERE strftime('%s', 'now') - timestamp <= 7 * 24 * 60 * 60 -- 7 days in seconds;""")
|
|
last_7_days = cursor.fetchall()
|
|
|
|
#Last n months query
|
|
cursor.execute("""SELECT strftime('%Y-%m', timestamp, 'unixepoch', '-3 hours') AS year_month_utc3,
|
|
SUM(amount) AS total_amount
|
|
FROM profits_table
|
|
WHERE strftime('%s', 'now') - timestamp <= 18 * 30 * 24 * 60 * 60 -- 18 months in seconds
|
|
GROUP BY year_month_utc3
|
|
ORDER BY year_month_utc3;""")
|
|
last_n_months_rows = cursor.fetchall()
|
|
|
|
#Yearly totals
|
|
cursor.execute("""SELECT strftime('%Y', timestamp, 'unixepoch', '-3 hours') AS year_utc3,
|
|
SUM(amount) AS total_amount
|
|
FROM profits_table
|
|
WHERE strftime('%s', 'now') - timestamp <= 24 * 365 * 60 * 60 -- 365 days in seconds
|
|
GROUP BY year_utc3
|
|
ORDER BY year_utc3;""")
|
|
yearly_totals = cursor.fetchall()
|
|
|
|
print("="*line_width)
|
|
print("Last 60 days:")
|
|
print("-"*line_width)
|
|
for row in last_60_days_rows:
|
|
print(f"{row[0]}: {round(row[1],2)}")
|
|
print("="*line_width)
|
|
print("Last 18 months:")
|
|
print("-"*line_width)
|
|
for row in last_n_months_rows[1:]:
|
|
print(f"{row[0]}: {round(row[1],2)}")
|
|
print("-"*line_width)
|
|
print(f"Last 30 days average: {round(last_30_days[0][1]/30,2)}")
|
|
print(f"Last 7 days average: {round(last_7_days[0][1]/7,2)}")
|
|
cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
|
|
SUM(amount) AS total_amount
|
|
FROM profits_table
|
|
WHERE timestamp > strftime('%s', 'now', 'start of month', 'utc')
|
|
GROUP BY day_utc3;""")
|
|
last_month = cursor.fetchall()
|
|
|
|
#The projection calculation is: the amount of profit so far in the month + the averages of the last 30 days and the last 7 days times the number of days left in the month.
|
|
days_in_month = calendar.monthrange(datetime.date.today().year, datetime.date.today().month)[1]
|
|
daily_combined_media = (last_30_days[0][1]/30+last_7_days[0][1]/7)/2
|
|
current_amount = last_n_months_rows[-1][1]
|
|
days_past_this_month = int(last_60_days_rows[-1][0][8:10])
|
|
projection_calculation = current_amount + daily_combined_media*(days_in_month-days_past_this_month)
|
|
|
|
print(f"This month projection: {round(projection_calculation,2)}")
|
|
print("="*line_width)
|
|
print("Per exchange:")
|
|
print("-"*line_width)
|
|
cursor.execute("""SELECT
|
|
exchange_name,
|
|
CASE
|
|
WHEN strftime('%Y-%m', timestamp, 'unixepoch', '-3 hours') = strftime('%Y-%m', 'now', 'localtime') THEN 'This Month'
|
|
WHEN strftime('%Y-%m', timestamp, 'unixepoch', '-3 hours') = strftime('%Y-%m', 'now', 'localtime', '-1 month') THEN 'Last Month'
|
|
ELSE 'Other Months'
|
|
END AS month_group,
|
|
SUM(amount) AS total_amount
|
|
FROM
|
|
profits_table
|
|
WHERE
|
|
strftime('%s', 'now') - timestamp <= 60 * 24 * 60 * 60 -- 60 days in seconds
|
|
GROUP BY
|
|
exchange_name, month_group
|
|
ORDER BY
|
|
exchange_name, month_group;""")
|
|
|
|
#So type checking goes away
|
|
binance_amount = 0
|
|
gateio_amount = 0
|
|
kucoin_amount = 0
|
|
okex_amount = 0
|
|
|
|
by_exchange = cursor.fetchall()
|
|
for row in by_exchange:
|
|
if row[0]=="binance":
|
|
if row[1]=="This Month":
|
|
binance_amount = row[2]
|
|
elif row[0]=="gateio":
|
|
if row[1]=="This Month":
|
|
gateio_amount = row[2]
|
|
elif row[0]=="kucoin":
|
|
if row[1]=="This Month":
|
|
kucoin_amount = row[2]
|
|
elif row[0]=="okex":
|
|
if row[1]=="This Month":
|
|
okex_amount = row[2]
|
|
|
|
#Close db
|
|
cursor.close()
|
|
|
|
total_amount = binance_amount+gateio_amount+kucoin_amount+okex_amount
|
|
|
|
print(f"Binance: {round(binance_amount,2)} USDT ({round(binance_amount/total_amount*100,2)}%)")
|
|
print(f"Gate.io: {round(gateio_amount,2)} USDT ({round(gateio_amount/total_amount*100,2)}%)")
|
|
print(f"KuCoin: {round(kucoin_amount,2)} USDT ({round(kucoin_amount/total_amount*100,2)}%)")
|
|
print(f"OKX: {round(okex_amount,2)} USDT ({round(okex_amount/total_amount*100,2)}%)")
|
|
print("="*line_width)
|
|
|
|
|