Skip to content

Commit 06fc071

Browse files
committed
Update High Leverage API and Streamlit Page Enhancements
- Increased the hardcoded maximum number of bootable spots in the High Leverage API from 200 to 400, matching what is configured in the Drift app. - Improved data processing in the Streamlit page by ensuring numeric types for sorting after renaming columns, enhancing data integrity. - Updated the display of high leverage positions to utilize Pandas Styler for better formatting, improving the visual presentation of data in the Streamlit application. - Removed outdated comments and streamlined the code for clarity and maintainability. These changes enhance the functionality and user experience of the High Leverage features in the application.
1 parent 56a507f commit 06fc071

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

backend/api/high_leverage_api.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def get_high_leverage_stats(request: BackendRequest):
5252
- Bootable spots: Users opted-in but not actively using high leverage (no significant perp positions).
5353
"""
5454

55-
total_spots = 200 # Hardcoded as per user request
55+
total_spots = 400 # Hardcoded maximum number of spots
5656

5757
opted_in_users_count = 0
5858
bootable_count = 0
@@ -250,10 +250,4 @@ async def get_high_leverage_positions_detailed(request: BackendRequest):
250250
continue # Skip to next user on error
251251

252252
logger.info(f"Returning {len(detailed_hl_positions)} high leverage positions.")
253-
return detailed_hl_positions
254-
255-
256-
# Example of how to include this router in a main FastAPI app:
257-
# from fastapi import FastAPI
258-
# app = FastAPI()
259-
# app.include_router(router, prefix="/high-leverage", tags=["High Leverage"])
253+
return detailed_hl_positions

src/page/high_leverage_page.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def high_leverage_page():
9393
'position_leverage': 'Position Leverage'
9494
}, inplace=True)
9595

96-
# Ensure leverage columns are numeric for sorting
96+
# Ensure numeric types for sorting
97+
# Convert after renaming as column names in df_positions have changed
98+
df_positions['Base Asset Amount'] = pd.to_numeric(df_positions['Base Asset Amount'], errors='coerce')
99+
df_positions['Notional Value (USD)'] = pd.to_numeric(df_positions['Notional Value (USD)'], errors='coerce')
97100
df_positions['Position Leverage'] = pd.to_numeric(df_positions['Position Leverage'], errors='coerce')
98101
df_positions['Account Leverage'] = pd.to_numeric(df_positions['Account Leverage'], errors='coerce')
99102

@@ -110,15 +113,9 @@ def high_leverage_page():
110113
'User Account',
111114
'Authority',
112115
'Market Index'
113-
]].copy() # Use .copy() to avoid SettingWithCopyWarning on the formatted DataFrame
116+
]].copy() # Use .copy() to avoid SettingWithCopyWarning
114117

115-
# Formatting for display
116-
# Base Asset Amount and Notional Value formatting remains the same
117-
display_df['Base Asset Amount'] = display_df['Base Asset Amount'].apply(lambda x: f"{x:,.4f}" if pd.notnull(x) else 'N/A')
118-
display_df['Notional Value (USD)'] = display_df['Notional Value (USD)'].apply(lambda x: f"${x:,.2f}" if pd.notnull(x) else 'N/A')
119-
# Leverage columns formatted for display with 'x' - keep original df_positions for numerical sorting
120-
display_df['Position Leverage'] = display_df['Position Leverage'].apply(lambda x: f"{x:.2f}x" if pd.notnull(x) else 'N/A')
121-
display_df['Account Leverage'] = display_df['Account Leverage'].apply(lambda x: f"{x:.2f}x" if pd.notnull(x) else 'N/A')
118+
# Display formatting is now handled by Pandas Styler object below.
122119

123120
except Exception as df_e:
124121
st.error(f"Error processing detailed position data into DataFrame: {df_e}")
@@ -137,7 +134,24 @@ def high_leverage_page():
137134

138135
st.subheader("Detailed High Leverage Positions")
139136
if not display_df.empty:
140-
st.dataframe(display_df, hide_index=True, use_container_width=True)
137+
# Apply Pandas Styler for formatting
138+
styled_df = display_df.style.format({
139+
'Base Asset Amount': '{:,.4f}', # Commas, 4 decimal places
140+
'Notional Value (USD)': '${:,.2f}', # Commas, 2 decimal places, no $
141+
'Position Leverage': '{:.2f}x', # 2 decimal places, 'x' suffix
142+
'Account Leverage': '{:.2f}x' # 2 decimal places, 'x' suffix
143+
})
144+
st.dataframe(
145+
styled_df, # Pass the styled DataFrame
146+
hide_index=True,
147+
use_container_width=True,
148+
column_config={ # Keep config for non-styled columns or for relabeling if needed
149+
"Market Symbol": st.column_config.TextColumn(label="Market Symbol"),
150+
"User Account": st.column_config.TextColumn(label="User Account"),
151+
"Authority": st.column_config.TextColumn(label="Authority"),
152+
"Market Index": st.column_config.TextColumn(label="Market Index"),
153+
}
154+
)
141155
else:
142156
st.info("No detailed high leverage position data available, or an error occurred during processing.")
143157

0 commit comments

Comments
 (0)