Spaces:
Runtime error
Runtime error
Ken Sang Tang commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ MODEL_NAME = "databricks/dolly-v2-3b"
|
|
| 21 |
FINBERT_MODEL_NAME = "yiyanghkust/finbert-tone"
|
| 22 |
SYMBOL = '^KLSE' # KLCI index symbol
|
| 23 |
START_DATE = '2020-01-01'
|
| 24 |
-
END_DATE = '2024-
|
| 25 |
|
| 26 |
# Initialize Alpaca API
|
| 27 |
api = REST(ALPACA_API_KEY, ALPACA_SECRET_KEY, ALPACA_BASE_URL)
|
|
@@ -115,29 +115,37 @@ def plot_klci(data):
|
|
| 115 |
|
| 116 |
# Step 7: Main Function to Run Pipeline
|
| 117 |
def main():
|
| 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 |
# Run the main function
|
|
|
|
| 21 |
FINBERT_MODEL_NAME = "yiyanghkust/finbert-tone"
|
| 22 |
SYMBOL = '^KLSE' # KLCI index symbol
|
| 23 |
START_DATE = '2020-01-01'
|
| 24 |
+
END_DATE = '2024-10-31'
|
| 25 |
|
| 26 |
# Initialize Alpaca API
|
| 27 |
api = REST(ALPACA_API_KEY, ALPACA_SECRET_KEY, ALPACA_BASE_URL)
|
|
|
|
| 115 |
|
| 116 |
# Step 7: Main Function to Run Pipeline
|
| 117 |
def main():
|
| 118 |
+
try:
|
| 119 |
+
# 1. Fetch and Prepare Data
|
| 120 |
+
klci_data = fetch_stock_data(SYMBOL, START_DATE, END_DATE)
|
| 121 |
|
| 122 |
+
# 2. Run FinBERT Sentiment Analysis
|
| 123 |
+
sample_text = "The market sentiment is bullish for KLCI." # Example text
|
| 124 |
+
sentiment = analyze_sentiment(sample_text)
|
| 125 |
+
print(f"Sentiment: {sentiment}")
|
| 126 |
|
| 127 |
+
# 3. Generate Dolly Prediction
|
| 128 |
+
indicators = ['RSI', 'MACD', 'BB_upper', 'BB_middle', 'BB_lower']
|
| 129 |
+
available_columns = [col for col in indicators if col in klci_data.columns]
|
| 130 |
+
indicator_data = klci_data[available_columns].iloc[-1].to_dict()
|
| 131 |
+
|
| 132 |
+
prompt = f"The market trend for KLCI with sentiment {sentiment} and indicators {klci_data[['RSI', 'MACD', 'BB_upper', 'BB_middle', 'BB_lower']].iloc[-1].to_dict()}"
|
| 133 |
+
prediction = generate_prediction(prompt)
|
| 134 |
+
print(f"Dolly Prediction: {prediction}")
|
| 135 |
|
| 136 |
+
# 4. Decide Buy/Sell based on Prediction and Execute Trade
|
| 137 |
+
if "buy" in prediction.lower():
|
| 138 |
+
execute_trade("buy", symbol=SYMBOL, qty=1)
|
| 139 |
+
elif "sell" in prediction.lower():
|
| 140 |
+
execute_trade("sell", symbol=SYMBOL, qty=1)
|
| 141 |
+
else:
|
| 142 |
+
print("No clear trade signal from prediction.")
|
| 143 |
|
| 144 |
+
# 5. Plot KLCI Data with Indicators
|
| 145 |
+
plot_klci(klci_data)
|
| 146 |
+
|
| 147 |
+
except Exception as e:
|
| 148 |
+
print (f"An error occured: {e}")
|
| 149 |
|
| 150 |
|
| 151 |
# Run the main function
|