Spaces:
Sleeping
Sleeping
File size: 13,289 Bytes
7458986 8ce0053 326ddbe 7458986 022eb10 7458986 e8bdf75 7458986 c930ac9 7458986 e8bdf75 7458986 326ddbe 7458986 326ddbe 7458986 326ddbe 7458986 326ddbe 7458986 326ddbe c930ac9 7458986 326ddbe 7d19279 7458986 c930ac9 7458986 7d19279 7458986 c930ac9 7458986 c930ac9 7458986 c930ac9 7458986 47b8dda 7458986 c930ac9 7458986 c930ac9 |
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 |
import yfinance as yf
import pandas as pd
import numpy as np
import torch
from datetime import datetime, timedelta
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import spaces
from chronos import BaseChronosPipeline
def get_indonesian_stocks():
return {
"BBCA.JK": "Bank Central Asia",
"BBRI.JK": "Bank BRI",
"BBNI.JK": "Bank BNI",
"BMRI.JK": "Bank Mandiri",
"TLKM.JK": "Telkom Indonesia",
"UNVR.JK": "Unilever Indonesia",
"ASII.JK": "Astra International",
"INDF.JK": "Indofood Sukses Makmur",
"KLBF.JK": "Kalbe Farma",
"HMSP.JK": "HM Sampoerna",
"GGRM.JK": "Gudang Garam",
"ADRO.JK": "Adaro Energy",
"PGAS.JK": "Perusahaan Gas Negara",
"JSMR.JK": "Jasa Marga",
"WIKA.JK": "Wijaya Karya",
"PTBA.JK": "Tambang Batubara Bukit Asam",
"ANTM.JK": "Aneka Tambang",
"SMGR.JK": "Semen Indonesia",
"INTP.JK": "Indocement Tunggal Prakasa",
"ITMG.JK": "Indo Tambangraya Megah"
}
def calculate_technical_indicators(data):
indicators = {}
def calculate_rsi(prices, period=14):
delta = prices.diff()
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
rs = gain / loss
rsi = 100 - (100 / (1 + rs))
return rsi
indicators['rsi'] = {
'current': calculate_rsi(data['Close']).iloc[-1],
'values': calculate_rsi(data['Close'])
}
def calculate_macd(prices, fast=12, slow=26, signal=9):
exp1 = prices.ewm(span=fast).mean()
exp2 = prices.ewm(span=slow).mean()
macd = exp1 - exp2
signal_line = macd.ewm(span=signal).mean()
histogram = macd - signal_line
return macd, signal_line, histogram
macd, signal_line, histogram = calculate_macd(data['Close'])
indicators['macd'] = {
'macd': macd.iloc[-1],
'signal': signal_line.iloc[-1],
'histogram': histogram.iloc[-1],
'signal_text': 'BUY' if histogram.iloc[-1] > 0 else 'SELL',
'macd_values': macd,
'signal_values': signal_line
}
def calculate_bollinger_bands(prices, period=20, std_dev=2):
sma = prices.rolling(window=period).mean()
std = prices.rolling(window=period).std()
upper_band = sma + (std * std_dev)
lower_band = sma - (std * std_dev)
return upper_band, sma, lower_band
upper, middle, lower = calculate_bollinger_bands(data['Close'])
current_price = data['Close'].iloc[-1]
bb_position = (current_price - lower.iloc[-1]) / (upper.iloc[-1] - lower.iloc[-1])
indicators['bollinger'] = {
'upper': upper.iloc[-1],
'middle': middle.iloc[-1],
'lower': lower.iloc[-1],
'position': 'UPPER' if bb_position > 0.8 else 'LOWER' if bb_position < 0.2 else 'MIDDLE'
}
sma_20_series = data['Close'].rolling(20).mean()
sma_50_series = data['Close'].rolling(50).mean()
indicators['moving_averages'] = {
'sma_20': sma_20_series.iloc[-1],
'sma_50': sma_50_series.iloc[-1],
'sma_200': data['Close'].rolling(200).mean().iloc[-1],
'ema_12': data['Close'].ewm(span=12).mean().iloc[-1],
'ema_26': data['Close'].ewm(span=26).mean().iloc[-1],
'sma_20_values': sma_20_series,
'sma_50_values': sma_50_series
}
indicators['volume'] = {
'current': data['Volume'].iloc[-1],
'avg_20': data['Volume'].rolling(20).mean().iloc[-1],
'ratio': data['Volume'].iloc[-1] / data['Volume'].rolling(20).mean().iloc[-1]
}
return indicators
def generate_trading_signals(data, indicators):
signals = {}
current_price = data['Close'].iloc[-1]
buy_signals = 0
sell_signals = 0
signal_details = []
rsi = indicators['rsi']['current']
if rsi < 30:
buy_signals += 1
signal_details.append(f"β
RSI ({rsi:.1f}) - Oversold - BUY signal")
elif rsi > 70:
sell_signals += 1
signal_details.append(f"β RSI ({rsi:.1f}) - Overbought - SELL signal")
else:
signal_details.append(f"βͺ RSI ({rsi:.1f}) - Neutral")
macd_hist = indicators['macd']['histogram']
if macd_hist > 0:
buy_signals += 1
signal_details.append(f"β
MACD Histogram ({macd_hist:.4f}) - Positive - BUY signal")
else:
sell_signals += 1
signal_details.append(f"β MACD Histogram ({macd_hist:.4f}) - Negative - SELL signal")
bb_position = indicators['bollinger']['position']
if bb_position == 'LOWER':
buy_signals += 1
signal_details.append(f"β
Bollinger Bands - Near lower band - BUY signal")
elif bb_position == 'UPPER':
sell_signals += 1
signal_details.append(f"β Bollinger Bands - Near upper band - SELL signal")
else:
signal_details.append("βͺ Bollinger Bands - Middle position")
sma_20 = indicators['moving_averages']['sma_20']
sma_50 = indicators['moving_averages']['sma_50']
if current_price > sma_20 > sma_50:
buy_signals += 1
signal_details.append(f"β
Price above MA(20,50) - Bullish - BUY signal")
elif current_price < sma_20 < sma_50:
sell_signals += 1
signal_details.append(f"β Price below MA(20,50) - Bearish - SELL signal")
else:
signal_details.append("βͺ Moving Averages - Mixed signals")
volume_ratio = indicators['volume']['ratio']
if volume_ratio > 1.5:
buy_signals += 0.5
signal_details.append(f"β
High volume ({volume_ratio:.1f}x avg) - Strengthens BUY signal")
elif volume_ratio < 0.5:
sell_signals += 0.5
signal_details.append(f"β Low volume ({volume_ratio:.1f}x avg) - Weakens SELL signal")
else:
signal_details.append(f"βͺ Normal volume ({volume_ratio:.1f}x avg)")
total_signals = buy_signals + sell_signals
signal_strength = (buy_signals / max(total_signals, 1)) * 100
if buy_signals > sell_signals:
overall_signal = "BUY"
elif sell_signals > buy_signals:
overall_signal = "SELL"
else:
overall_signal = "HOLD"
recent_high = data['High'].tail(20).max()
recent_low = data['Low'].tail(20).min()
signals = {
'overall': overall_signal,
'strength': signal_strength,
'details': '\n'.join(signal_details),
'support': recent_low,
'resistance': recent_high,
'stop_loss': recent_low * 0.95 if overall_signal == "BUY" else recent_high * 1.05
}
return signals
def get_fundamental_data(stock):
try:
info = stock.info
history = stock.history(period="1d")
fundamental_info = {
'name': info.get('longName', 'N/A'),
'current_price': history['Close'].iloc[-1] if not history.empty else 0,
'market_cap': info.get('marketCap', 0),
'pe_ratio': info.get('forwardPE', 0),
'dividend_yield': info.get('dividendYield', 0) * 100 if info.get('dividendYield') else 0,
'volume': history['Volume'].iloc[-1] if not history.empty else 0,
'info': f"""
Sector: {info.get('sector', 'N/A')}
Industry: {info.get('industry', 'N/A')}
Market Cap: {format_large_number(info.get('marketCap', 0))}
52 Week High: {info.get('fiftyTwoWeekHigh', 'N/A')}
52 Week Low: {info.get('fiftyTwoWeekLow', 'N/A')}
Beta: {info.get('beta', 'N/A')}
EPS: {info.get('forwardEps', 'N/A')}
Book Value: {info.get('bookValue', 'N/A')}
Price to Book: {info.get('priceToBook', 'N/A')}
""".strip()
}
return fundamental_info
except Exception as e:
print(f"Error getting fundamental data: {e}")
return {
'name': 'N/A',
'current_price': 0,
'market_cap': 0,
'pe_ratio': 0,
'dividend_yield': 0,
'volume': 0,
'info': 'Unable to fetch fundamental data'
}
def format_large_number(num):
if num >= 1e12:
return f"{num/1e12:.2f}T"
elif num >= 1e9:
return f"{num/1e9:.2f}B"
elif num >= 1e6:
return f"{num/1e6:.2f}M"
elif num >= 1e3:
return f"{num/1e3:.2f}K"
else:
return f"{num:.2f}"
@spaces.GPU(duration=120)
def predict_prices(data, model=None, tokenizer=None, prediction_days=30):
try:
prices = data['Close'].values.astype(np.float32)
pipeline = BaseChronosPipeline.from_pretrained("amazon/chronos-bolt-base", device_map="auto")
with torch.no_grad():
forecast = pipeline.predict(context=torch.tensor(prices), prediction_length=prediction_days)
mean_forecast = forecast.mean(dim=1).squeeze().cpu().numpy()
pred_len = len(mean_forecast)
last_price = prices[-1]
predicted_high = np.max(mean_forecast)
predicted_low = np.min(mean_forecast)
predicted_mean = np.mean(mean_forecast)
change_pct = ((predicted_mean - last_price) / last_price) * 100
return {
'values': mean_forecast,
'dates': pd.date_range(start=data.index[-1] + timedelta(days=1), periods=pred_len, freq='D'),
'high_30d': predicted_high,
'low_30d': predicted_low,
'mean_30d': predicted_mean,
'change_pct': change_pct,
'summary': f"""
AI Model: Amazon Chronos-Bolt (Base)
Prediction Period: {pred_len} days
Expected Change: {change_pct:.2f}%
Confidence: Medium
Note: AI predictions are for reference only and not financial advice
""".strip()
}
except Exception as e:
print(f"Error in prediction: {e}")
return {
'values': [],
'dates': [],
'high_30d': 0,
'low_30d': 0,
'mean_30d': 0,
'change_pct': 0,
'summary': f'Prediction unavailable due to model error: {e}'
}
def create_price_chart(data, indicators):
fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.05, subplot_titles=('Price & Moving Averages', 'RSI', 'MACD'), row_width=[0.2, 0.2, 0.7])
fig.add_trace(go.Candlestick(x=data.index, open=data['Open'], high=data['High'], low=data['Low'], close=data['Close'], name='Price'), row=1, col=1)
fig.add_trace(go.Scatter(x=data.index, y=indicators['moving_averages']['sma_20_values'], name='SMA 20', line=dict(color='orange', width=1)), row=1, col=1)
fig.add_trace(go.Scatter(x=data.index, y=indicators['moving_averages']['sma_50_values'], name='SMA 50', line=dict(color='blue', width=1)), row=1, col=1)
fig.add_trace(go.Scatter(x=data.index, y=indicators['rsi']['values'], name='RSI', line=dict(color='purple')), row=2, col=1)
fig.add_hline(y=70, line_dash="dash", line_color="red", row=2, col=1)
fig.add_hline(y=30, line_dash="dash", line_color="green", row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=indicators['macd']['macd_values'], name='MACD', line=dict(color='blue')), row=3, col=1)
fig.add_trace(go.Scatter(x=data.index, y=indicators['macd']['signal_values'], name='Signal', line=dict(color='red')), row=3, col=1)
fig.update_layout(title='Technical Analysis Dashboard', height=900, showlegend=True, xaxis_rangeslider_visible=False)
return fig
def create_technical_chart(data, indicators):
fig = make_subplots(rows=2, cols=2, subplot_titles=('Bollinger Bands', 'Volume', 'Price vs MA', 'RSI Analysis'), specs=[[{"secondary_y": False}, {"secondary_y": False}], [{"secondary_y": False}, {"secondary_y": False}]])
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Price', line=dict(color='black')), row=1, col=1)
fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='Volume', marker_color='lightblue'), row=1, col=2)
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Price', line=dict(color='black')), row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=indicators['moving_averages']['sma_20_values'], name='SMA 20', line=dict(color='orange', dash='dash')), row=2, col=1)
fig.update_layout(title='Technical Indicators Overview', height=600, showlegend=False)
return fig
def create_prediction_chart(data, predictions):
if not len(predictions['values']):
return go.Figure()
fig = go.Figure()
fig.add_trace(go.Scatter(x=data.index[-60:], y=data['Close'].values[-60:], name='Historical Price', line=dict(color='blue', width=2)))
fig.add_trace(go.Scatter(x=predictions['dates'], y=predictions['values'], name='AI Prediction', line=dict(color='red', width=2, dash='dash')))
pred_std = np.std(predictions['values'])
upper_band = predictions['values'] + (pred_std * 1.96)
lower_band = predictions['values'] - (pred_std * 1.96)
fig.add_trace(go.Scatter(x=predictions['dates'], y=upper_band, name='Upper Band', line=dict(color='lightcoral', width=1), fill=None))
fig.add_trace(go.Scatter(x=predictions['dates'], y=lower_band, name='Lower Band', line=dict(color='lightcoral', width=1), fill='tonexty', fillcolor='rgba(255,182,193,0.2)'))
fig.update_layout(title=f'Price Prediction - Next {len(predictions["dates"])} Days', xaxis_title='Date', yaxis_title='Price (IDR)', hovermode='x unified', height=500)
return fig
|