Spaces:
Runtime error
Runtime error
Ken Sang Tang commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,6 +38,8 @@ finbert_model = AutoModelForSequenceClassification.from_pretrained(FINBERT_MODEL
|
|
| 38 |
def fetch_stock_data(symbol, start_date, end_date):
|
| 39 |
print("Fetching stock data...")
|
| 40 |
data = yf.download(symbol, start=start_date, end=end_date)
|
|
|
|
|
|
|
| 41 |
data = add_technical_indicators(data)
|
| 42 |
return data
|
| 43 |
|
|
@@ -48,7 +50,7 @@ def add_technical_indicators(data):
|
|
| 48 |
|
| 49 |
# Calculate MACD and select only the MACD line
|
| 50 |
macd = ta.macd(data['Close'], fast=12, slow=26)
|
| 51 |
-
if 'MACD_12_26_9' in macd.columns:
|
| 52 |
data['MACD'] = macd['MACD_12_26_9']
|
| 53 |
else:
|
| 54 |
print("MACD data not available.")
|
|
@@ -62,8 +64,8 @@ def add_technical_indicators(data):
|
|
| 62 |
data['BB_lower'] = bbands.get('BBL_20_2.0')
|
| 63 |
|
| 64 |
# Ensure the columns are added only if they exist
|
| 65 |
-
if data[['BB_upper', 'BB_middle', 'BB_lower']].isnull().
|
| 66 |
-
print("Bollinger Bands data is incomplete or
|
| 67 |
data.drop(columns=['BB_upper', 'BB_middle', 'BB_lower'], inplace=True, errors='ignore')
|
| 68 |
else:
|
| 69 |
print("Bollinger Bands data not available.")
|
|
@@ -103,9 +105,10 @@ def execute_trade(signal, symbol='Genting Bhd', qty=1):
|
|
| 103 |
def plot_klci(data):
|
| 104 |
plt.figure(figsize=(14, 7))
|
| 105 |
plt.plot(data['Close'], label='KLCI Close Price', color='blue')
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
plt.title('KLCI with Technical Indicators')
|
| 110 |
plt.legend()
|
| 111 |
plt.show()
|
|
|
|
| 38 |
def fetch_stock_data(symbol, start_date, end_date):
|
| 39 |
print("Fetching stock data...")
|
| 40 |
data = yf.download(symbol, start=start_date, end=end_date)
|
| 41 |
+
if data.empty:
|
| 42 |
+
raise ValueError("Stock data could not be fetched. Please check the symbol and date range.")
|
| 43 |
data = add_technical_indicators(data)
|
| 44 |
return data
|
| 45 |
|
|
|
|
| 50 |
|
| 51 |
# Calculate MACD and select only the MACD line
|
| 52 |
macd = ta.macd(data['Close'], fast=12, slow=26)
|
| 53 |
+
if macd is not None and 'MACD_12_26_9' in macd.columns:
|
| 54 |
data['MACD'] = macd['MACD_12_26_9']
|
| 55 |
else:
|
| 56 |
print("MACD data not available.")
|
|
|
|
| 64 |
data['BB_lower'] = bbands.get('BBL_20_2.0')
|
| 65 |
|
| 66 |
# Ensure the columns are added only if they exist
|
| 67 |
+
if data[['BB_upper', 'BB_middle', 'BB_lower']].isnull().any().any():
|
| 68 |
+
print("Bollinger Bands data is incomplete or has NaNs. Dropping these columns.")
|
| 69 |
data.drop(columns=['BB_upper', 'BB_middle', 'BB_lower'], inplace=True, errors='ignore')
|
| 70 |
else:
|
| 71 |
print("Bollinger Bands data not available.")
|
|
|
|
| 105 |
def plot_klci(data):
|
| 106 |
plt.figure(figsize=(14, 7))
|
| 107 |
plt.plot(data['Close'], label='KLCI Close Price', color='blue')
|
| 108 |
+
if 'BB_upper' in data.columns and 'BB_middle' in data.columns and 'BB_lower' in data.columns:
|
| 109 |
+
plt.plot(data['BB_upper'], label='Bollinger Upper Band', color='red')
|
| 110 |
+
plt.plot(data['BB_middle'], label='Bollinger Middle Band', color='green')
|
| 111 |
+
plt.plot(data['BB_lower'], label='Bollinger Lower Band', color='red')
|
| 112 |
plt.title('KLCI with Technical Indicators')
|
| 113 |
plt.legend()
|
| 114 |
plt.show()
|