Add account data fetching and display balances in dashboard
This commit is contained in:
47
main_app.py
47
main_app.py
@ -10,17 +10,18 @@ import sqlite3
|
||||
import pandas as pd
|
||||
from datetime import datetime
|
||||
import importlib
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
# --- REMOVED: import signal ---
|
||||
# --- REMOVED: from queue import Empty ---
|
||||
|
||||
from logging_utils import setup_logging
|
||||
# --- Using the new high-performance WebSocket utility for live prices ---
|
||||
from live_market_utils import start_live_feed
|
||||
# --- Import the base class for type hinting (optional but good practice) ---
|
||||
from strategies.base_strategy import BaseStrategy
|
||||
# --- Rich dashboard renderer ---
|
||||
from dashboard import DashboardRenderer
|
||||
from rich.live import Live
|
||||
from hyperliquid.info import Info
|
||||
from hyperliquid.utils import constants
|
||||
|
||||
# --- Configuration ---
|
||||
WATCHED_COINS = ["BTC", "ETH", "SOL", "BNB", "HYPE", "SUI", "xyz:BRENTOIL", "xyz:CL", "xyz:GOLD", "xyz:SILVER", "mkts:USTECH", "xyz:XYZ100"]
|
||||
@ -384,10 +385,17 @@ class MainApp:
|
||||
self.strategy_configs = strategy_configs
|
||||
self.strategy_statuses = {}
|
||||
self.indicators_status = {}
|
||||
self.account_data = None
|
||||
self.wallet_address = os.environ.get("MAIN_WALLET_ADDRESS")
|
||||
if self.wallet_address:
|
||||
self.info_client = Info(constants.MAINNET_API_URL, skip_ws=True)
|
||||
else:
|
||||
self.info_client = None
|
||||
self.renderer = DashboardRenderer(table_visibility={
|
||||
"market": True,
|
||||
"strategies": False,
|
||||
"indicators": True,
|
||||
"balances": True,
|
||||
})
|
||||
|
||||
def read_prices(self):
|
||||
@ -428,6 +436,35 @@ class MainApp:
|
||||
else:
|
||||
self.indicators_status = {}
|
||||
|
||||
def read_account_data(self):
|
||||
"""Fetches account balances and positions from Hyperliquid API."""
|
||||
if not self.wallet_address or not self.info_client:
|
||||
self.account_data = None
|
||||
return
|
||||
try:
|
||||
perp_state = self.info_client.user_state(self.wallet_address)
|
||||
spot_state = self.info_client.spot_user_state(self.wallet_address)
|
||||
|
||||
margin_summary = perp_state.get('marginSummary', {})
|
||||
account_value = float(margin_summary.get('accountValue', 0))
|
||||
margin_used = float(margin_summary.get('totalMarginUsed', 0))
|
||||
utilization = (margin_used / account_value) * 100 if account_value > 0 else 0
|
||||
|
||||
spot_balances = spot_state.get('balances', [])
|
||||
positions = perp_state.get('assetPositions', [])
|
||||
|
||||
self.account_data = {
|
||||
'account_value': account_value,
|
||||
'margin_used': margin_used,
|
||||
'utilization': utilization,
|
||||
'spot_balances': spot_balances,
|
||||
'positions': positions,
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Could not fetch account data: {e}")
|
||||
self.account_data = None
|
||||
|
||||
def check_process_status(self):
|
||||
"""Checks if the background processes are still running."""
|
||||
for name, process in self.background_processes.items():
|
||||
@ -445,7 +482,8 @@ class MainApp:
|
||||
COIN_DISPLAY_NAMES,
|
||||
self.strategy_statuses,
|
||||
self.strategy_configs,
|
||||
self.indicators_status
|
||||
self.indicators_status,
|
||||
self.account_data
|
||||
)
|
||||
|
||||
def run(self):
|
||||
@ -455,6 +493,7 @@ class MainApp:
|
||||
self.read_prices()
|
||||
self.read_strategy_statuses()
|
||||
self.read_indicators_status()
|
||||
self.read_account_data()
|
||||
live.update(self.display_dashboard())
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user