Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import psycopg2 | |
| from psycopg2.extras import RealDictCursor | |
| import logging | |
| from decimal import getcontext | |
| import gradio as gr | |
| # Set precision for Decimal calculations | |
| getcontext().prec = 28 | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(levelname)s - %(message)s", | |
| filename="my_log_file.log", | |
| filemode="a" | |
| ) | |
| # --- Database Functions --- | |
| def get_db_connection(host, dbname, user, password, port): | |
| try: | |
| return psycopg2.connect( | |
| host=host, | |
| dbname=dbname, | |
| user=user, | |
| password=password, | |
| port=port | |
| ) | |
| except psycopg2.OperationalError as e: | |
| logging.critical(f"DB connection failed: {e}") | |
| return None | |
| def fetch_trades_by_address(conn, address): | |
| with conn.cursor(cursor_factory=RealDictCursor) as cur: | |
| cur.execute(""" | |
| SELECT * FROM trades | |
| WHERE address = %s | |
| ORDER BY "timestampupdated" ASC | |
| """, (address,)) | |
| return cur.fetchall() | |
| def separate_live_and_past_trades(trades): | |
| if not trades: | |
| return [], [] | |
| df = pd.DataFrame(trades) | |
| closed_ids = set(df[df['status']=='closed']['uniqueid']) | |
| live_trades = ( | |
| df[~df['uniqueid'].isin(closed_ids)] | |
| .sort_values('timestampupdated', ascending=False) | |
| .groupby('uniqueid', as_index=False) | |
| .first() | |
| .to_dict('records') | |
| ) | |
| past_trades = ( | |
| df[df['uniqueid'].isin(closed_ids)] | |
| .sort_values('timestampupdated', ascending=False) | |
| .to_dict('records') | |
| ) | |
| return live_trades, past_trades | |
| # --- Gradio Function --- | |
| def get_trades(address): | |
| # Convert to uppercase safely | |
| address = address.upper().strip() if address else "" | |
| if not address: | |
| return "β Please enter a valid address.", None, None | |
| conn = get_db_connection( | |
| host="54.206.116.126", | |
| dbname="mydb", | |
| user="myuser", | |
| password="mypassword", | |
| port=5432 | |
| ) | |
| if not conn: | |
| return "β Failed to connect to the database.", None, None | |
| trades = fetch_trades_by_address(conn, address) | |
| conn.close() | |
| if not trades: | |
| return f"β οΈ No trades found for address {address}.", None, None | |
| live_trades, past_trades = separate_live_and_past_trades(trades) | |
| live_df = pd.DataFrame(live_trades)[['symbol','tradetype','amount','entryprice','markprice','leverage','timestampupdated']] if live_trades else None | |
| past_df = pd.DataFrame(past_trades)[['symbol','tradetype','amount','entryprice','markprice','pnl','roe','timestampupdated','status']] if past_trades else None | |
| return f"β Trades fetched for address {address}", live_df, past_df | |
| # --- Gradio Blocks Layout --- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π Lead Trader Performance Dashboard") | |
| with gr.Row(): | |
| address_input = gr.Textbox(label="Trader Address", placeholder="Enter trader address here...") | |
| fetch_btn = gr.Button("Fetch Trades") | |
| status_output = gr.Textbox(label="Status") | |
| live_trades_output = gr.Dataframe(label="π Live Trades") | |
| past_trades_output = gr.Dataframe(label="π Past Trades") | |
| fetch_btn.click( | |
| fn=get_trades, | |
| inputs=address_input, | |
| outputs=[status_output, live_trades_output, past_trades_output] | |
| ) | |
| demo.launch() | |