File size: 12,129 Bytes
7458986
 
 
 
 
561d1d4
7458986
 
 
 
 
 
961b441
7458986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561d1d4
ec4d569
7458986
 
ec4d569
 
7458986
7b68220
7458986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b19c127
 
 
 
7458986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b19c127
 
 
7458986
b19c127
7458986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c9c00d
7458986
 
 
 
 
 
b19c127
 
 
1c9c00d
 
7458986
 
 
 
 
 
 
 
1c9c00d
7458986
 
1c9c00d
7458986
 
 
 
 
 
 
 
1c9c00d
7458986
 
 
 
 
 
 
 
 
 
 
1c9c00d
7458986
 
 
 
 
1c9c00d
7458986
 
 
 
 
 
 
 
 
1c9c00d
7458986
fbcd158
7458986
 
 
 
 
 
 
 
1c9c00d
7458986
 
 
 
 
 
 
 
 
 
1c9c00d
7458986
 
 
 
 
 
 
 
1c9c00d
7458986
 
 
 
 
 
b19c127
7458986
 
b19c127
7458986
b19c127
7458986
 
 
 
 
b19c127
7458986
 
 
 
 
b19c127
7458986
 
 
 
 
 
b19c127
 
 
 
7458986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561d1d4
 
 
7458986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import gradio as gr
import yfinance as yf
import pandas as pd
import numpy as np
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from datetime import datetime, timedelta
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import warnings
warnings.filterwarnings('ignore')
import spaces

# Import utility functions
from utils import (
    get_indonesian_stocks, 
    calculate_technical_indicators,
    generate_trading_signals,
    get_fundamental_data,
    format_large_number,
    predict_prices,
    create_price_chart,
    create_technical_chart,
    create_prediction_chart
)
from config import IDX_STOCKS, TECHNICAL_INDICATORS, PREDICTION_CONFIG

# Load Chronos-Bolt model
@spaces.GPU(duration=120)
def load_model():
    """Load the Amazon Chronos-Bolt model for time series forecasting"""
    model = AutoModelForSeq2SeqLM.from_pretrained(
        "amazon/chronos-bolt-base",
        torch_dtype=torch.bfloat16,
        device_map="auto",
        trust_remote_code=True
    )
    tokenizer = None 
    return model, tokenizer

# Initialize model
model, tokenizer = load_model()

def get_stock_data(symbol, period="1y"):
    """Fetch historical stock data using yfinance"""
    try:
        stock = yf.Ticker(symbol)
        data = stock.history(period=period)
        if data.empty:
            return None, None
        return data, stock
    except Exception as e:
        print(f"Error fetching data for {symbol}: {e}")
        return None, None

def analyze_stock(symbol, prediction_days=30):
    """Main analysis function"""
    # FIX: Add .JK suffix if it's missing (case-insensitive check)
    if not symbol.upper().endswith(".JK"):
        symbol += ".JK"
        
    # Get stock data
    data, stock = get_stock_data(symbol)
    
    if data is None or stock is None:
        return None, None, None, None, None, None
    
    # Get fundamental data
    fundamental_info = get_fundamental_data(stock)
    
    # Calculate technical indicators
    indicators = calculate_technical_indicators(data)
    
    # Generate trading signals
    signals = generate_trading_signals(data, indicators)
    
    # Make predictions using Chronos-Bolt
    predictions = predict_prices(data, model, tokenizer, prediction_days)
    
    # Create charts
    fig_price = create_price_chart(data, indicators) 
    fig_technical = create_technical_chart(data, indicators) 
    fig_prediction = create_prediction_chart(data, predictions) 
    
    return fundamental_info, indicators, signals, fig_price, fig_technical, fig_prediction

def create_ui():
    """Create the Gradio interface"""
    with gr.Blocks(
        title="IDX Stock Analysis & Prediction",
        theme=gr.themes.Soft(),
        css="""
        .header { 
            text-align: center; 
            padding: 20px; 
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        .metric-card {
            background: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin: 10px 0;
        }
        .positive { color: #10b981; font-weight: bold; }
        .negative { color: #ef4444; font-weight: bold; }
        .neutral { color: #6b7280; font-weight: bold; }
        """
    ) as demo:
        
        with gr.Row():
            gr.HTML("""
            <div class="header">
                <h1>IDX Stock Analysis & Prediction</h1>
                <p>Advanced Technical Analysis & AI-Powered Predictions for Indonesian Stock Exchange</p>
            </div>
            """)
        
        with gr.Row():
            with gr.Column(scale=2):
                # FIX: Change Dropdown to Textbox for flexible user input
                stock_selector = gr.Textbox(
                    value="BBCA",
                    label="Input Simbol Saham IDX",
                    info="Contoh: BBCA, ADRO, TLKM"
                )
                
                with gr.Row():
                    prediction_days = gr.Slider(
                        minimum=7,
                        maximum=90,
                        value=30,
                        step=7,
                        label="Prediction Days"
                    )
                    analyze_btn = gr.Button(
                        "Analyze Stock",
                        variant="primary",
                        size="lg"
                    )
        
        # Results sections
        with gr.Tabs() as tabs:
            
            # Tab 1: Stock Overview & Fundamentals
            with gr.TabItem("Stock Overview"):
                with gr.Row():
                    company_name = gr.Textbox(label="Company Name", interactive=False)
                    current_price = gr.Number(label="Current Price (IDR)", interactive=False)
                    market_cap = gr.Textbox(label="Market Cap", interactive=False)
                
                with gr.Row():
                    pe_ratio = gr.Number(label="P/E Ratio", interactive=False)
                    dividend_yield = gr.Number(label="Dividend Yield (%)", interactive=False)
                    volume = gr.Number(label="Volume", interactive=False)
                
                fundamentals_text = gr.Textbox(
                    label="Company Information",
                    lines=8,
                    interactive=False
                )
            
            # Tab 2: Technical Analysis
            with gr.TabItem("Technical Analysis"):
                price_chart = gr.Plot(label="Price & Technical Indicators")
                technical_chart = gr.Plot(label="Technical Indicators Analysis")
                
                with gr.Row():
                    rsi_value = gr.Number(label="RSI (14)", interactive=False)
                    macd_signal = gr.Textbox(label="MACD Signal", interactive=False)
                    bb_position = gr.Textbox(label="Bollinger Band Position", interactive=False)
            
            # Tab 3: Trading Signals
            with gr.TabItem("Trading Signals"):
                with gr.Row():
                    overall_signal = gr.Textbox(label="Overall Signal", interactive=False, scale=2)
                    signal_strength = gr.Slider(
                        minimum=0,
                        maximum=100,
                        label="Signal Strength",
                        interactive=False
                    )
                
                signals_text = gr.Textbox(
                    label="Detailed Signals",
                    lines=10,
                    interactive=False
                )
                
                with gr.Row():
                    support_level = gr.Number(label="Support Level", interactive=False)
                    resistance_level = gr.Number(label="Resistance Level", interactive=False)
                    stop_loss = gr.Number(label="Recommended Stop Loss", interactive=False)
            
            # Tab 4: AI Predictions
            with gr.TabItem("AI Predictions"):
                prediction_chart = gr.Plot(label="Price Forecast (Chronos-Bolt)")
                
                with gr.Row():
                    predicted_high = gr.Number(label="Predicted High (30d)", interactive=False)
                    predicted_low = gr.Number(label="Predicted Low (30d)", interactive=False)
                    predicted_change = gr.Number(label="Expected Change (%)", interactive=False)
                
                prediction_summary = gr.Textbox(
                    label="Prediction Analysis",
                    lines=5,
                    interactive=False
                )
        
        # Event handlers
        def update_analysis(symbol, pred_days):
            fundamental_info, indicators, signals, fig_price, fig_technical, fig_prediction = analyze_stock(symbol, pred_days)
            
            if fundamental_info is None:
                # FIX: Return appropriate initial values or error message
                return {
                    company_name: "Gagal memuat data",
                    current_price: 0,
                    market_cap: "N/A",
                    pe_ratio: 0,
                    dividend_yield: 0,
                    volume: 0,
                    fundamentals_text: f"Tidak dapat mengambil data saham untuk {symbol}. Pastikan simbol benar.",
                    rsi_value: 0,
                    macd_signal: "N/A",
                    bb_position: "N/A",
                    overall_signal: "N/A",
                    signal_strength: 0,
                    signals_text: "Tidak ada sinyal tersedia",
                    support_level: 0,
                    resistance_level: 0,
                    stop_loss: 0,
                    predicted_high: 0,
                    predicted_low: 0,
                    predicted_change: 0,
                    prediction_summary: "Prediksi tidak tersedia",
                    price_chart: go.Figure(),
                    technical_chart: go.Figure(),
                    prediction_chart: go.Figure()
                }
            
            # Format outputs
            return {
                company_name: fundamental_info.get('name', 'N/A'),
                current_price: fundamental_info.get('current_price', 0),
                market_cap: format_large_number(fundamental_info.get('market_cap', 0)),
                pe_ratio: fundamental_info.get('pe_ratio', 0),
                dividend_yield: fundamental_info.get('dividend_yield', 0),
                volume: fundamental_info.get('volume', 0),
                fundamentals_text: fundamental_info.get('info', ''),
                rsi_value: indicators.get('rsi', {}).get('current', 0),
                macd_signal: indicators.get('macd', {}).get('signal', 'N/A'),
                bb_position: indicators.get('bollinger', {}).get('position', 'N/A'),
                overall_signal: signals.get('overall', 'HOLD'),
                signal_strength: signals.get('strength', 50),
                signals_text: signals.get('details', ''),
                support_level: signals.get('support', 0),
                resistance_level: signals.get('resistance', 0),
                stop_loss: signals.get('stop_loss', 0),
                predicted_high: indicators.get('predictions', {}).get('high_30d', 0),
                predicted_low: indicators.get('predictions', {}).get('low_30d', 0),
                predicted_change: indicators.get('predictions', {}).get('change_pct', 0),
                prediction_summary: indicators.get('predictions', {}).get('summary', ''),
                price_chart: fig_price,
                technical_chart: fig_technical,
                prediction_chart: fig_prediction
            }
        
        analyze_btn.click(
            fn=update_analysis,
            inputs=[stock_selector, prediction_days],
            outputs=[
                company_name, current_price, market_cap, pe_ratio, dividend_yield, volume, fundamentals_text,
                rsi_value, macd_signal, bb_position, overall_signal, signal_strength, signals_text,
                support_level, resistance_level, stop_loss, predicted_high, predicted_low, predicted_change,
                prediction_summary, price_chart, technical_chart, prediction_chart
            ]
        )
        
        # Load initial analysis
        demo.load(
            fn=update_analysis,
            inputs=[stock_selector, prediction_days],
            outputs=[
                company_name, current_price, market_cap, pe_ratio, dividend_yield, volume, fundamentals_text,
                rsi_value, macd_signal, bb_position, overall_signal, signal_strength, signals_text,
                support_level, resistance_level, stop_loss, predicted_high, predicted_low, predicted_change,
                prediction_summary, price_chart, technical_chart, prediction_chart
            ]
        )
    
    return demo

if __name__ == "__main__":
    demo = create_ui()
    demo.launch()