Ken Sang Tang commited on
Commit
23fda02
·
verified ·
1 Parent(s): b607e46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
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-07-31'
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
- # 1. Fetch and Prepare Data
119
- klci_data = fetch_stock_data(SYMBOL, START_DATE, END_DATE)
 
120
 
121
- # 2. Run FinBERT Sentiment Analysis
122
- sample_text = "The market sentiment is bullish for KLCI." # Example text
123
- sentiment = analyze_sentiment(sample_text)
124
- print(f"Sentiment: {sentiment}")
125
 
126
- # 3. Generate Dolly Prediction
127
- 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()}"
128
- prediction = generate_prediction(prompt)
129
- print(f"Dolly Prediction: {prediction}")
 
 
 
 
130
 
131
- # 4. Decide Buy/Sell based on Prediction and Execute Trade
132
- if "buy" in prediction.lower():
133
- execute_trade("buy", symbol=SYMBOL, qty=1)
134
- elif "sell" in prediction.lower():
135
- execute_trade("sell", symbol=SYMBOL, qty=1)
136
- else:
137
- print("No clear trade signal from prediction.")
138
 
139
- # 5. Plot KLCI Data with Indicators
140
- plot_klci(klci_data)
 
 
 
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