|
|
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") |
|
|
|
|
|
|
|
|
quantum_optimizer = QuantumInspiredOptimizer() |
|
|
|
|
|
|
|
|
symbol = st.selectbox("π Select Stock", load_nifty50_symbols()) |
|
|
timeframe = st.selectbox("π
Select Timeframe", ["1mo", "3mo", "6mo", "1y"]) |
|
|
|
|
|
|
|
|
data_placeholder = st.empty() |
|
|
tabs = st.tabs(["π Quantum Pattern Detection", "β‘ Quantum Momentum", "π‘ Quantum State Analysis"]) |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
time.sleep(10) |
|
|
|
|
|
except Exception as e: |
|
|
st.error(f"β οΈ Unexpected error: {e}") |
|
|
time.sleep(10) |
|
|
|