Skip to main content

Overview

The Streamlit application provides a web-based dashboard for running market analysis and viewing trading signals in real-time.

Application Structure

Page Configuration

The application must be configured before any other Streamlit commands:
import streamlit as st

st.set_page_config(
    page_title="XAUUSD Trading Bot",
    page_icon="📈",
    layout="wide"
)
page_title
string
Browser tab title: “XAUUSD Trading Bot”
page_icon
string
Browser tab icon: ”📈”
layout
string
Page layout mode: “wide” for full-width content

Bot Initialization

from XAUSD_AI import XAUUSDTradingBot

bot = XAUUSDTradingBot(api_key=st.secrets["GROQ_API_KEY"])
See Environment Variables for configuration details.

UI Components

The sidebar contains the control panel for user interactions:
with st.sidebar:
    st.header("Control Panel")
    
    # Auto-refresh toggle
    auto_refresh = st.toggle("🔄 Auto Refresh (30min)", value=False)
    
    # Run analysis button
    if st.button("📈 Run New Analysis"):
        with st.spinner("Analyzing market data..."):
            st.session_state['analysis_result'] = bot.run_analysis()
            st.session_state['last_update'] = datetime.now()

Main Content Tabs

The dashboard uses tabs to organize different views:
tab1, tab2 = st.tabs(["📊 Analysis", "🎯 Trading Signal"])

Analysis Tab

Displays technical analysis and market data across timeframes:
with tab1:
    # Technical Analysis
    st.markdown("### 📊 Technical Analysis")
    st.markdown(format_signal(result['technical_features']))
    
    # Market Data
    st.markdown("### 📈 Market Data by Timeframe")
    cols = st.columns(2)
    for idx, (tf, data) in enumerate(result['market_data'].items()):
        with cols[idx % 2]:
            display_market_data(data, tf)

Trading Signal Tab

Displays the generated trading signal:
with tab2:
    st.markdown("### 🎯 Trading Signal")
    st.markdown(format_signal(result['trading_signal']))

Helper Functions

display_market_data()

Formats and displays market data in an expandable section:
def display_market_data(data_str, timeframe):
    """Display formatted market data in an expander"""
    with st.expander(f"📊 {timeframe} Market Data", expanded=False):
        lines = data_str.split('\n')
        for line in lines:
            if line.strip():
                st.text(line)
data_str
string
required
Multi-line string containing market data for a specific timeframe
timeframe
string
required
Timeframe identifier (e.g., “D1”, “H4”, “H1”). See Timeframes for supported values.

format_signal()

Formats signal content that may be returned as string or object:
def format_signal(signal_content):
    """Format and display trading signal content"""
    if isinstance(signal_content, str):
        return signal_content
    return signal_content.content if hasattr(signal_content, 'content') else str(signal_content)
signal_content
string | object
required
Signal content from bot analysis results
return
string
Formatted signal content as a string

Auto-Refresh Feature

Implements automatic analysis refresh every 30 minutes:
if auto_refresh:
    current_time = datetime.now()
    if 'last_refresh' not in st.session_state:
        st.session_state['last_refresh'] = current_time
    
    time_diff = (current_time - st.session_state['last_refresh']).total_seconds()
    remaining_time = 1800 - time_diff  # 1800 seconds = 30 minutes

Session State Management

The application uses Streamlit session state to persist data:
analysis_result
dict
Cached result from bot.run_analysis() to avoid redundant API calls
last_update
datetime
Timestamp of the last successful analysis run
last_refresh
datetime
Timestamp of the last auto-refresh for progress calculation

Custom Styling

The application includes custom CSS for dark theme UI:
st.markdown("""
    <style>
    .stApp {background-color: #0e1117}
    .metric-container {
        background-color: #1f2937;
        padding: 10px;
        border-radius: 5px;
        margin: 5px;
    }
    .stTabs [data-baseweb="tab-list"] {
        gap: 2px;
        background-color: #1f2937;
    }
    .stTabs [data-baseweb="tab"] {
        background-color: #374151;
        padding: 10px 20px;
        border-radius: 5px 5px 0 0;
    }
    </style>
""", unsafe_allow_html=True)

Error Handling

try:
    # Main application logic
    if 'analysis_result' in st.session_state:
        result = st.session_state['analysis_result']
        # Display results...
    else:
        st.warning("⚠️ No analysis results yet. Click 'Run Analysis' to start.")
except Exception as e:
    st.error(f"Error: {str(e)}")

Entry Point

if __name__ == "__main__":
    main()

Build docs developers (and LLMs) love