File size: 9,535 Bytes
b8086d5
 
4c78965
b8086d5
 
 
 
1f26057
 
b8086d5
c267b11
b8086d5
 
 
 
 
4c78965
 
b8086d5
4c78965
b8086d5
af67f96
b8086d5
4c78965
b8086d5
 
4c78965
cf14392
313e2d0
4c78965
cf14392
313e2d0
b8086d5
 
4c78965
 
 
a469221
4c78965
 
 
 
c267b11
 
 
b8086d5
4c78965
c267b11
 
 
b8086d5
 
 
4c78965
a469221
c267b11
a469221
 
 
 
 
 
b8086d5
 
1f26057
c267b11
 
a469221
c267b11
4c78965
 
1f26057
93d1cfe
 
 
 
4c78965
 
 
a469221
 
 
4c78965
a469221
 
4c78965
a469221
 
4c78965
 
 
a469221
4c78965
 
c267b11
4c78965
 
 
 
1f26057
4c78965
1f26057
 
 
4c78965
 
93d1cfe
b8086d5
4c78965
c267b11
 
4c78965
c267b11
4c78965
c267b11
 
 
 
 
 
4c78965
 
 
 
 
 
 
 
 
 
c267b11
 
4b7c0ae
4c78965
a469221
4b7c0ae
a469221
 
4b7c0ae
a469221
1f26057
4c78965
4b7c0ae
 
c267b11
 
 
1f26057
c267b11
1f26057
 
 
4b7c0ae
c267b11
 
 
 
 
1ff5d0c
c267b11
4b7c0ae
c267b11
4c78965
4b7c0ae
a469221
 
 
 
 
 
 
4c78965
1f26057
a469221
 
 
 
 
 
 
4b7c0ae
 
 
 
c267b11
1f26057
 
c267b11
4b7c0ae
 
1ff5d0c
 
4c78965
 
 
 
 
 
93d1cfe
c267b11
 
93d1cfe
c267b11
4b7c0ae
 
 
1ff5d0c
4c78965
1ff5d0c
4b7c0ae
cf14392
1f26057
 
c267b11
4b7c0ae
c267b11
1ff5d0c
cf14392
4b7c0ae
c267b11
 
1ff5d0c
cf14392
4b7c0ae
4c78965
4b7c0ae
 
 
 
 
 
 
4c78965
af67f96
1f26057
93d1cfe
 
4c78965
c267b11
af67f96
 
1f26057
4b7c0ae
c267b11
4c78965
1ff5d0c
af67f96
c267b11
 
1ff5d0c
 
 
 
c267b11
4c78965
1ff5d0c
af67f96
c267b11
 
1ff5d0c
 
 
 
4b7c0ae
 
 
 
c267b11
cf14392
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import gradio as gr
import pandas as pd
import numpy as np
from data_processor import DataProcessor
from sentiment_analyzer import SentimentAnalyzer
from model_handler import ModelHandler
from trading_logic import TradingLogic
from plotter import create_mplfinance_chart
import plotly.graph_objects as go 

# Global instances
data_processor = DataProcessor()
sentiment_analyzer = SentimentAnalyzer()
model_handler = ModelHandler()
trading_logic = TradingLogic()

def create_chart_analysis(ticker, interval):
    """Create chart with technical indicators and predictions"""
    try:
        df = data_processor.get_market_data(ticker, interval)
        if df.empty:
            return "No data available", None
        
        # Hitung indikator
        df = data_processor.calculate_indicators(df)
        
        # Prepare data for Chronos
        prepared_data = data_processor.prepare_for_chronos(df)

        # Generate predictions (Chronos-2 atau Fallback)
        predictions = model_handler.predict(prepared_data, horizon=10)

        current_price = df['Close'].iloc[-1]
        
        # Buat chart menggunakan MPLFINANCE (dikembalikan sebagai HTML)
        chart_html = create_mplfinance_chart(
            df, 
            ticker=f'{ticker} ({interval})', 
            predictions=predictions
        )

        # Hasilkan sinyal trading
        signal, confidence = trading_logic.generate_signal(
            predictions, current_price, df
        )
        
        # Hitung TP/SL
        tp, sl = trading_logic.calculate_tp_sl(
            current_price, df['ATR'].iloc[-1], signal
        )
        
        # Create metrics display
        metrics = {
            "Ticker": ticker,
            "Current Price": f"${current_price:.2f}",
            "Signal": signal.upper(),
            "Confidence": f"{confidence:.1%}",
            "Take Profit": f"${tp:.2f}" if tp else "N/A",
            "Stop Loss": f"${sl:.2f}" if sl else "N/A",
            "RSI": f"{df['RSI'].iloc[-1]:.1f}",
            "MACD": f"{df['MACD'].iloc[-1]:.4f}",
            "Volume": f"{df['Volume'].iloc[-1]:,.0f}"
        }
        
        return chart_html, metrics
        
    except Exception as e:
        return f"Error creating chart: {str(e)}", None

def analyze_sentiment(ticker):
    """Analyze gold/crypto market sentiment"""
    
    # KOREKSI: Panggil fungsi yang diperbarui dan kirim ticker
    sentiment_score, news_summary = sentiment_analyzer.analyze_market_sentiment(ticker)
    
    # Gunakan template terang untuk Plotly
    fig = go.Figure(go.Indicator(
        mode="gauge+number+delta",
        value=sentiment_score,
        domain={'x': [0, 1], 'y': [0, 1]},
        title={'text': f"{ticker} Market Sentiment (Simulated)"},
        delta={'reference': 0},
        gauge={
            'axis': {'range': [-1, 1]},
            'bar': {'color': "#FFD700"},
            'steps': [
                {'range': [-1, -0.5], 'color': "rgba(255,0,0,0.5)"},
                {'range': [-0.5, 0.5], 'color': "rgba(100,100,100,0.3)"},
                {'range': [0.5, 1], 'color': "rgba(0,255,0,0.5)"}
            ],
            'threshold': {
                'line': {'color': "black", 'width': 4},
                'thickness': 0.75,
                'value': 0
            }
        }
    ))
    
    fig.update_layout(
        template='plotly_white',
        height=300,
        paper_bgcolor='#f0f4f9', 
        plot_bgcolor='#f0f4f9',
        font=dict(color='black')
    )
    
    return fig, news_summary

def get_fundamentals(ticker):
    """Get fundamental analysis data"""
    try:
        fundamentals = data_processor.get_fundamental_data(ticker)
        
        # Buat fundamentals table
        table_data = []
        for key, value in fundamentals.items():
            table_data.append([key, value])
        
        df = pd.DataFrame(table_data, columns=['Metric', 'Value'])
        
        # Ambil nilai kunci untuk gauge
        if ticker == "BTC-USD":
            gauge_title = "Crypto Volatility Index"
            gauge_value = fundamentals.get(gauge_title, 100)
            gauge_range = [0, 200]
        else:
            gauge_title = "Gold Strength Index"
            gauge_value = fundamentals.get(gauge_title, 50)
            gauge_range = [0, 100]
            
        # Create fundamentals gauge chart
        fig = go.Figure(go.Indicator(
            mode="gauge+number",
            value=gauge_value,
            title={'text': gauge_title},
            gauge={
                'axis': {'range': gauge_range},
                'bar': {'color': "#FFD700"},
                'steps': [
                    {'range': [gauge_range[0], gauge_range[1] * 0.3], 'color': "rgba(255,0,0,0.5)"},
                    {'range': [gauge_range[1] * 0.3, gauge_range[1] * 0.7], 'color': "rgba(100,100,100,0.3)"},
                    {'range': [gauge_range[1] * 0.7, gauge_range[1]], 'color': "rgba(0,255,0,0.5)"}
                ]
            }
        ))
        
        fig.update_layout(
            template='plotly_white',
            height=300,
            paper_bgcolor='#f0f4f9', 
            plot_bgcolor='#f0f4f9',
            font=dict(color='black')
        )
        
        return fig, df
        
    except Exception as e:
        return str(e), None

# Create Gradio interface
with gr.Blocks(
    theme=gr.themes.Default(primary_hue="yellow", secondary_hue="yellow"),
    title="Ultimate Market Analysis & Prediction",
    css="""
        .gradio-container {background-color: #f0f4f9; color: black}
        .gr-button-primary {background-color: #FFD700 !important; color: #000000 !important}
        .gr-button-secondary {border-color: #FFD700 !important; color: #000000 !important}
        .gr-tab button {color: black !important}
        .gr-tab button.selected {background-color: #FFD700 !important; color: #000000 !important}
        .gr-highlighted {background-color: #CCCCCC !important}
        .anycoder-link {color: #FFD700 !important; text-decoration: none; font-weight: bold}
        .mpl-chart-container {
            border: 1px solid #CCCCCC; 
            border-radius: 5px; 
            overflow: hidden; 
            background: white; 
            width: 100%;
        }
        .chart-title {color: black !important;}
        .metric-label {color: black !important;}
    """
) as demo:
    
    gr.HTML("""
        <div style="text-align: center; padding: 20px;">
            <h1 style="color: black;">Ultimate Market Analysis & Prediction (Chronos-2)</h1>
            <p style="color: black;">AI-powered analysis for Gold Futures (GC=F) and Bitcoin (BTC-USD)</p>
            <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="anycoder-link">Built with anycoder</a>
        </div>
    """)
    
    with gr.Row():
        ticker_dropdown = gr.Dropdown(
            choices=["GC=F", "BTC-USD"],
            value="GC=F",
            label="Market Ticker",
            info="Select asset for analysis"
        )
        # KOREKSI: Mengembalikan semua opsi interval waktu
        interval_dropdown = gr.Dropdown(
            choices=[
                "5m", "15m", "30m", "1h", "4h", "1d", "1wk", "1mo", "3mo"
            ],
            value="1d",
            label="Time Interval",
            info="Select analysis timeframe"
        )
        refresh_btn = gr.Button("売 Refresh Data & Predict", variant="primary")
    
    with gr.Tabs():
        with gr.TabItem("投 Chart Analysis"):
            # Chart MPLFinance lebar penuh
            chart_html = gr.HTML(label="Price Chart & Indicators", elem_classes=["mpl-chart-container"])
            
            with gr.Row():
                metrics_output = gr.JSON(label="Trading Metrics")
        
        with gr.TabItem("堂 Sentiment Analysis"):
            with gr.Row():
                sentiment_gauge = gr.Plot(label="Sentiment Score")
                news_display = gr.HTML(label="Market News")
        
        with gr.TabItem("嶋 Fundamentals"):
            with gr.Row():
                fundamentals_gauge = gr.Plot(label="Strength Index Gauge")
                fundamentals_table = gr.Dataframe(
                    headers=["Metric", "Value"],
                    label="Key Fundamentals",
                    interactive=False
                )
    
    # Event handlers
    def update_all(ticker, interval):
        # FIX: create_chart_analysis sekarang hanya mengembalikan 2 output
        chart, metrics = create_chart_analysis(ticker, interval)
        # FIX: Panggil fungsi yang sudah diperbarui dan kirim ticker
        sentiment_gauge, news_display = analyze_sentiment(ticker) 
        fund_gauge, fund_table = get_fundamentals(ticker)
        
        # Total 5 outputs Gradio components
        return chart, metrics, sentiment_gauge, news_display, fund_gauge, fund_table
    
    refresh_btn.click(
        fn=update_all,
        inputs=[ticker_dropdown, interval_dropdown],
        outputs=[
            chart_html, metrics_output,
            sentiment_gauge, news_display,
            fundamentals_gauge, fundamentals_table
        ]
    )
    
    demo.load(
        fn=update_all,
        inputs=[ticker_dropdown, interval_dropdown],
        outputs=[
            chart_html, metrics_output,
            sentiment_gauge, news_display,
            fundamentals_gauge, fundamentals_table
        ]
    )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_api=True
    )