File size: 4,132 Bytes
47551b9
fed2431
47551b9
fed2431
 
47551b9
 
 
fed2431
 
47551b9
fed2431
47551b9
 
fed2431
 
 
47551b9
fed2431
 
 
47551b9
fed2431
 
 
 
 
 
 
 
47551b9
 
fed2431
47551b9
fed2431
 
 
47551b9
fed2431
 
47551b9
fed2431
 
47551b9
fed2431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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