import streamlit as st import matplotlib.pyplot as plt import numpy as np import pandas as pd import time from utils.data_loader import load_nifty50_symbols, fetch_stock_data from utils.quantum_algorithms import QuantumInspiredOptimizer st.set_page_config(page_title="Quantum Pattern Analysis", layout="wide") st.title("⚛️ Quantum Pattern Analysis") # 🎯 Initialize quantum optimizer quantum_optimizer = QuantumInspiredOptimizer() # 📌 Stock selection symbol = st.selectbox("🔍 Select Stock", load_nifty50_symbols()) timeframe = st.selectbox("📅 Select Timeframe", ["1mo", "3mo", "6mo", "1y"]) # 📊 Fetch data data_placeholder = st.empty() tabs = st.tabs(["🔍 Quantum Pattern Detection", "⚡ Quantum Momentum", "📡 Quantum State Analysis"]) # 🔄 Live Data Streaming while True: try: data = fetch_stock_data(symbol, period=timeframe) if data is None or data.empty: st.error("⚠️ Unable to fetch data. Please try again later.") time.sleep(10) continue prices = data['Close'].values index_vals = data.index # 🛠️ Pattern Detection with tabs[0]: st.subheader("🔬 Quantum-Inspired Pattern Detection") pattern_strength = quantum_optimizer.quantum_pattern_detection(prices) fig, ax1 = plt.subplots(figsize=(12, 6)) ax1.plot(index_vals, prices, label="Price", color="blue") ax1.set_ylabel("Stock Price (₹)", color="blue") ax2 = ax1.twinx() ax2.plot(index_vals, pattern_strength[:len(data)], label="Pattern Strength", color="orange", linestyle="dashed") ax2.set_ylabel("Pattern Strength", color="orange") ax1.set_title("Quantum Pattern Detection") ax1.legend(loc="upper left") ax2.legend(loc="upper right") st.pyplot(fig) # ⚡ Quantum Momentum with tabs[1]: st.subheader("⚡ Quantum Momentum Indicator") momentum = quantum_optimizer.quantum_momentum_indicator(prices) fig, ax1 = plt.subplots(figsize=(12, 6)) ax1.plot(index_vals, prices, label="Price", color="blue") ax1.set_ylabel("Stock Price (₹)", color="blue") ax2 = ax1.twinx() ax2.plot(index_vals, momentum, label="Quantum Momentum", color="red", linestyle="dashed") ax2.set_ylabel("Momentum", color="red") ax1.set_title("Quantum Momentum Analysis") ax1.legend(loc="upper left") ax2.legend(loc="upper right") st.pyplot(fig) # 📡 Quantum State Analysis with tabs[2]: st.subheader("📡 Quantum State Analysis") encoded_data = quantum_optimizer.quantum_inspired_encoding(prices) amplitudes = np.abs(encoded_data) phases = np.angle(encoded_data) fig, ax1 = plt.subplots(figsize=(12, 6)) ax1.plot(index_vals, amplitudes.flatten(), label="Amplitude", color="green") ax1.set_ylabel("Amplitude", color="green") ax2 = ax1.twinx() ax2.plot(index_vals, phases.flatten(), label="Phase", color="purple", linestyle="dashed") ax2.set_ylabel("Phase", color="purple") ax1.set_title("Quantum State Representation") ax1.legend(loc="upper left") ax2.legend(loc="upper right") st.pyplot(fig) # 📌 Display quantum state statistics col1, col2, col3 = st.columns(3) with col1: st.metric("📊 Average Amplitude", f"{np.mean(amplitudes):.4f}") with col2: st.metric("📡 Phase Coherence", f"{np.std(phases):.4f}") with col3: st.metric("🔗 Quantum Entropy", f"{-np.sum(amplitudes**2 * np.log(amplitudes**2 + 1e-10)):.4f}") # ✅ Refresh Every **10 Seconds** time.sleep(10) except Exception as e: st.error(f"⚠️ Unexpected error: {e}") time.sleep(10) # Prevent app from breaking and wait before retrying