omniverse1 commited on
Commit
72779a3
·
verified ·
1 Parent(s): 8b0d75b

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +13 -35
utils.py CHANGED
@@ -59,7 +59,15 @@ def calculate_technical_indicators(data):
59
  upper, middle, lower = calculate_bollinger_bands(data['Close'])
60
  current_price = data['Close'].iloc[-1]
61
  bb_position = (current_price - lower.iloc[-1]) / (upper.iloc[-1] - lower.iloc[-1])
62
- 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'}
 
 
 
 
 
 
 
 
63
  sma_20_series = data['Close'].rolling(20).mean()
64
  sma_50_series = data['Close'].rolling(50).mean()
65
  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}
@@ -197,47 +205,17 @@ def create_price_chart(data, indicators):
197
  fig.update_layout(title='Technical Analysis Dashboard', height=900, showlegend=True, xaxis_rangeslider_visible=False)
198
  return fig
199
 
200
- def create_prediction_chart(data, predictions):
201
- if not len(predictions['values']):
202
- return go.Figure()
203
- fig = go.Figure()
204
- fig.add_trace(go.Scatter(x=data.index[-60:], y=data['Close'].values[-60:], name='Historical Price', line=dict(color='blue', width=2)))
205
- fig.add_trace(go.Scatter(x=predictions['dates'], y=predictions['values'], name='AI Prediction', line=dict(color='red', width=2, dash='dash')))
206
- pred_std = np.std(predictions['values'])
207
- upper_band = predictions['values'] + (pred_std * 1.96)
208
- lower_band = predictions['values'] - (pred_std * 1.96)
209
- fig.add_trace(go.Scatter(x=predictions['dates'], y=upper_band, name='Upper Band', line=dict(color='lightcoral', width=1), fill=None))
210
- 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)'))
211
- fig.update_layout(title=f'Price Prediction - Next {len(predictions["dates"])} Days', xaxis_title='Date', yaxis_title='Price (IDR)', hovermode='x unified', height=500)
212
- return fig
213
-
214
  def create_technical_chart(data, indicators):
215
- fig = make_subplots(
216
- rows=2,
217
- cols=2,
218
- subplot_titles=('Bollinger Bands', 'Volume', 'Price vs MA', 'RSI Analysis'),
219
- specs=[[{"secondary_y": False}, {"secondary_y": False}], [{"secondary_y": False}, {"secondary_y": False}]]
220
- )
221
-
222
  fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Price', line=dict(color='black')), row=1, col=1)
223
- fig.add_trace(go.Scatter(x=data.index, y=indicators['bollinger']['upper'], name='Upper Band', line=dict(color='red', width=1)), row=1, col=1)
224
- fig.add_trace(go.Scatter(x=data.index, y=indicators['bollinger']['lower'], name='Lower Band', line=dict(color='green', width=1), fill='tonexty', fillcolor='rgba(0,255,0,0.1)'), row=1, col=1)
225
-
226
  fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='Volume', marker_color='lightblue'), row=1, col=2)
227
-
228
  fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Price', line=dict(color='gray')), row=2, col=1)
229
  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)
230
  fig.add_trace(go.Scatter(x=data.index, y=indicators['moving_averages']['sma_50_values'], name='SMA 50', line=dict(color='blue', dash='dash')), row=2, col=1)
231
-
232
  fig.add_trace(go.Scatter(x=data.index, y=indicators['rsi']['values'], name='RSI', line=dict(color='purple')), row=2, col=2)
233
  fig.add_hline(y=70, line_dash="dash", line_color="red", row=2, col=2)
234
  fig.add_hline(y=30, line_dash="dash", line_color="green", row=2, col=2)
235
-
236
- fig.update_layout(
237
- title='Technical Indicators Overview',
238
- height=800,
239
- showlegend=False,
240
- hovermode='x unified'
241
- )
242
  return fig
243
-
 
59
  upper, middle, lower = calculate_bollinger_bands(data['Close'])
60
  current_price = data['Close'].iloc[-1]
61
  bb_position = (current_price - lower.iloc[-1]) / (upper.iloc[-1] - lower.iloc[-1])
62
+ indicators['bollinger'] = {
63
+ 'upper': upper.iloc[-1],
64
+ 'middle': middle.iloc[-1],
65
+ 'lower': lower.iloc[-1],
66
+ 'upper_values': upper,
67
+ 'middle_values': middle,
68
+ 'lower_values': lower,
69
+ 'position': 'UPPER' if bb_position > 0.8 else 'LOWER' if bb_position < 0.2 else 'MIDDLE'
70
+ }
71
  sma_20_series = data['Close'].rolling(20).mean()
72
  sma_50_series = data['Close'].rolling(50).mean()
73
  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}
 
205
  fig.update_layout(title='Technical Analysis Dashboard', height=900, showlegend=True, xaxis_rangeslider_visible=False)
206
  return fig
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  def create_technical_chart(data, indicators):
209
+ 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}]])
 
 
 
 
 
 
210
  fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Price', line=dict(color='black')), row=1, col=1)
211
+ fig.add_trace(go.Scatter(x=data.index, y=indicators['bollinger']['upper_values'], name='Upper Band', line=dict(color='red', width=1)), row=1, col=1)
212
+ fig.add_trace(go.Scatter(x=data.index, y=indicators['bollinger']['lower_values'], name='Lower Band', line=dict(color='green', width=1), fill='tonexty', fillcolor='rgba(0,255,0,0.1)'), row=1, col=1)
 
213
  fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='Volume', marker_color='lightblue'), row=1, col=2)
 
214
  fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Price', line=dict(color='gray')), row=2, col=1)
215
  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)
216
  fig.add_trace(go.Scatter(x=data.index, y=indicators['moving_averages']['sma_50_values'], name='SMA 50', line=dict(color='blue', dash='dash')), row=2, col=1)
 
217
  fig.add_trace(go.Scatter(x=data.index, y=indicators['rsi']['values'], name='RSI', line=dict(color='purple')), row=2, col=2)
218
  fig.add_hline(y=70, line_dash="dash", line_color="red", row=2, col=2)
219
  fig.add_hline(y=30, line_dash="dash", line_color="green", row=2, col=2)
220
+ fig.update_layout(title='Technical Indicators Overview', height=800, showlegend=False, hovermode='x unified')
 
 
 
 
 
 
221
  return fig