From 12999a2189be5b6523d7f7a1da80be653a2d7b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Sun, 8 Feb 2026 12:04:22 -0300 Subject: [PATCH] Statistics server API update --- .gitignore | 1 + utils/short_report.py | 104 ++++++++++++++++++---------------- utils/statistics_server_v3.py | 28 +++++++-- 3 files changed, 81 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index 0fed6b0..ba776d2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ utils/close.py utils/credentials.py utils/set_exchange.py utils/stuff.py +utils/shorts_summary.py logs/binance.log logs/okex.log logs/gateio.log diff --git a/utils/short_report.py b/utils/short_report.py index d269eaf..c8ffefa 100644 --- a/utils/short_report.py +++ b/utils/short_report.py @@ -1,53 +1,61 @@ -import sqlite3 -import os -import sys -import json -import datetime +''' +Returns information on all running short traders: +''' -pair = sys.argv[1].replace("/","") +import json, datetime, credentials, requests, time -#Connect to db -connection = sqlite3.connect("../profits/profits_database.db") -cursor = connection.cursor() +def profit_since_date(base, quote, timestamp): + url = f"{base_statistics_url}/total_profit_by_pair_since_date?base={base}"e={quote}×tamp={timestamp}" #type: ignore + result = json.loads(requests.get(url,headers=headers).content) + return result["Total profit"] -#Load old_long info -try: - with open(f"../status/{pair}.oldlong") as f: - old_long = json.load(f) -except Exception as e: - print(e) - print("No old_long file") - os._exit(0) +def timestamp_to_seconds(timestamp_string): + ''' + Converts a timestamp string from this format "[YYYY/mm/dd HH:MM:SS]" to seconds. + + :param timestamp_string: timestamp string. + :return: linux time in seconds. + ''' + + dt = datetime.datetime.strptime(timestamp_string.strip('[]'), "%Y/%m/%d %H:%M:%S") + return int(dt.timestamp()) + -#Get time of switch to unix time -old_date = old_long["datetime"][1:11] -linux_time = datetime.datetime.strptime(old_date,"%Y/%m/%d").timestamp() +if __name__=="__main__": + api_key = credentials.get_credentials("mainnet_api_key")["key"] + base_statistics_url = f"{credentials.get_url('mainnet')}/statistics_server" #type: ignore + base_url = f"{credentials.get_url('mainnet')}" #type: ignore + results_dict = {} -#Query database -query = f"""SELECT pair, SUM(amount) AS total_profit - FROM profits_table - WHERE timestamp >= '{linux_time}' - GROUP BY pair;""" -cursor.execute(query) -query_result = cursor.fetchall() - -#Calculate profits -total = 0 -for item in query_result: - if item[0].replace("/","")==pair: - total = item[1] - -print(f"Profits since switch ({old_date}): {round(total,2)}") -print(f"Profit needed to cover expenses: {round(old_long['quote_spent'],2)}") -print(f"Difference: {round(old_long['quote_spent']-total,2)}") - -try: - with open(f"../status/{pair}.status") as f: - status_file = json.load(f) -except Exception as e: - print(e) - print("No status file") - os._exit(0) - -print(f"Old long price: {old_long['tp_price']}") -print(f"Current price: {status_file['price']}") \ No newline at end of file + headers = {"X-API-KEY": api_key} + + exchanges = ["binance", "gateio", "kucoin", "okex"] + + for exchange in exchanges: + #fetch all traders status + url = f"{base_url}/{exchange}/get_all_worker_status" + while True: + query = requests.get(url,headers=headers) + if query.status_code != 503: + break + time.sleep(.2) + workers_status = json.loads(query.content) + + #for each status_dict, extract pair, switch_date, old_long_amount + for item in workers_status: + if workers_status[item]["is_short"]: + print("Processing",item) + base,quote = item.split("/") + switch_date = timestamp_to_seconds(workers_status[item]["old_long"]["datetime"]) + profit_since_short = profit_since_date(base,quote,switch_date) + total_current_value = workers_status[item]["quote_spent"] + (workers_status[item]["old_long"]["tp_amount"]-workers_status[item]["base_bought"])*workers_status[item]["price"] + + results_dict[item] = { + "switch_date": workers_status[item]["old_long"]["datetime"], + "profit_since_short": round(profit_since_short,2), + "old_long_quote_spent": round(workers_status[item]["old_long"]["quote_spent"]), + "total_current_value": round(total_current_value,2) + } + + for item in results_dict: + print(item, results_dict[item]) diff --git a/utils/statistics_server_v3.py b/utils/statistics_server_v3.py index b4928ca..cfe87a1 100644 --- a/utils/statistics_server_v3.py +++ b/utils/statistics_server_v3.py @@ -174,24 +174,25 @@ def profit_report(): "Total profit": total_amount} -def query_total_profit(pair=None): +def query_total_profit(pair=None, start_date=0): ''' Returns total profit of the trading pair. If no pair specified, returns the grand total of all pairs. ''' if pair is None: - query = "SELECT SUM(amount) AS total_profit FROM profits_table" + query = "SELECT SUM(amount) AS total_profit FROM profits_table WHERE timestamp >= ?" with db_cursor() as cursor: - cursor.execute(query) + cursor.execute(query, (start_date,)) query_result = cursor.fetchall() return query_result[0][0] else: query = """SELECT pair, SUM(amount) AS total_profit FROM profits_table + WHERE timestamp >= ? GROUP BY pair;""" with db_cursor() as cursor: - cursor.execute(query) + cursor.execute(query, (start_date,)) query_result = cursor.fetchall() for item in query_result: if item[0].replace("/","")==pair: @@ -566,6 +567,25 @@ def total_profit_by_pair(): return jsonify({'Error': 'Halp'}) +@stats_api.route("/total_profit_by_pair_since_date") +def total_profit_by_pair_since_date(): + ''' + GET request + Parameters: 'base' -> string + 'quote' -> string + 'timestamp' -> int + ''' + if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys(): + return jsonify({'Error': 'API key invalid'}), 401 + try: + base = request.args.get("base") + quote = request.args.get("quote") + timestamp = int(request.args.get("timestamp")) # type: ignore + total = query_total_profit(f"{base}{quote}", timestamp) + return jsonify({"Total profit": total}) + except Exception as e: + print(e) + return jsonify({'Error': 'Halp'}) if __name__=="__main__":