profits report endpoint
This commit is contained in:
parent
61f9e8bc04
commit
8522a1ef70
|
|
@ -746,6 +746,8 @@ class broker:
|
|||
|
||||
order_to_send = self.exchange.create_order(pair,"market",side,amount)
|
||||
time.sleep(self.wait_time)
|
||||
# Wait a bit more when dealing with Kucoin
|
||||
|
||||
return self.get_order(order_to_send["id"],pair)
|
||||
except Exception as e:
|
||||
self.logger.log_this(f"Exception in new_market_order: {e}",1,pair)
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours'
|
|||
last_60_days_rows = cursor.fetchall()
|
||||
|
||||
#Last 30 days query
|
||||
#cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
|
||||
cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
|
||||
SUM(amount) AS total_amount
|
||||
FROM profits_table
|
||||
|
|
@ -125,6 +124,9 @@ for row in by_exchange:
|
|||
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)}%)")
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import datetime
|
|||
import time
|
||||
import ccxt
|
||||
import credentials
|
||||
import calendar
|
||||
import requests
|
||||
import logging
|
||||
from flask import Flask, jsonify, request
|
||||
|
|
@ -59,6 +60,126 @@ def load_keys_from_db(file_name):
|
|||
return valid_keys
|
||||
|
||||
|
||||
|
||||
def profit_report():
|
||||
##Queries
|
||||
connection = sqlite3.connect(profits_database)
|
||||
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,
|
||||
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()
|
||||
#Per exchange
|
||||
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;""")
|
||||
per_exchange = cursor.fetchall()
|
||||
|
||||
#Close db
|
||||
cursor.close()
|
||||
|
||||
|
||||
#Projection calculation
|
||||
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])
|
||||
|
||||
#Per exchange
|
||||
binance_amount = 0
|
||||
gateio_amount = 0
|
||||
kucoin_amount = 0
|
||||
okex_amount = 0
|
||||
|
||||
for row in per_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]
|
||||
|
||||
total_amount = binance_amount+gateio_amount+kucoin_amount+okex_amount
|
||||
|
||||
last_60_days_result = {row[0]: round(row[1],2) for row in last_60_days_rows}
|
||||
last_18_months_result = {row[0]: round(row[1],2) for row in last_n_months_rows}
|
||||
last_30_days_average = last_30_days[0][1]/30
|
||||
last_7_days_average = last_7_days[0][1]/7
|
||||
this_month_projection = current_amount + daily_combined_media*(days_in_month-days_past_this_month)
|
||||
binance_percentage = binance_amount/total_amount*100
|
||||
gateio_percentage = gateio_amount/total_amount*100
|
||||
kucoin_percentage = kucoin_amount/total_amount*100
|
||||
okex_percentage = okex_amount/total_amount*100
|
||||
|
||||
return {"Last 60 days": last_60_days_result,
|
||||
"Last 18 months": last_18_months_result,
|
||||
"Last 30 days average": last_30_days_average,
|
||||
"Last 7 days average": last_7_days_average,
|
||||
"This month projection": this_month_projection,
|
||||
"Binance": binance_amount,
|
||||
"Binance percentage": binance_percentage,
|
||||
"Gateio": gateio_amount,
|
||||
"Gateio percentage": gateio_percentage,
|
||||
"Kucoin": kucoin_amount,
|
||||
"Kucoin percentage": kucoin_percentage,
|
||||
"OKX": okex_amount,
|
||||
"OKX percentage": okex_percentage,
|
||||
"Total profit": total_amount}
|
||||
|
||||
|
||||
|
||||
def query_total_profit(pair=None):
|
||||
'''
|
||||
Returns total profit of the trading pair.
|
||||
|
|
@ -320,6 +441,23 @@ def fetch_backtests():
|
|||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
|
||||
|
||||
@stats_api.route("/fetch_profit_report")
|
||||
def fetch_profit_report():
|
||||
'''
|
||||
GET request
|
||||
Parameters: None
|
||||
Returns: JSON object with profit report data
|
||||
'''
|
||||
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys:
|
||||
try:
|
||||
return jsonify(profit_report())
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({"Error": f"{e}"})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
|
||||
|
||||
@stats_api.route("/clear_caches")
|
||||
def clear_hashes():
|
||||
global hashes_db
|
||||
|
|
|
|||
Loading…
Reference in New Issue