Ken Sang Tang commited on
Commit
184ce6a
·
verified ·
1 Parent(s): 45ec040

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -7,6 +7,8 @@ import pandas as pd
7
  import pandas_ta as ta
8
  import torch
9
  import warnings
 
 
10
 
11
  # Suppress specific FutureWarning
12
  warnings.filterwarnings("ignore", category=FutureWarning)
@@ -42,19 +44,14 @@ def fetch_stock_data(symbol, start_date, end_date):
42
  # Step 2: Add Technical Indicators
43
  def add_technical_indicators(data):
44
  print("Adding technical indicators...")
45
- # Compute RSI
46
  data['RSI'] = ta.rsi(data['Close'], length=14)
47
-
48
- # Compute MACD (usually generates three columns: MACD, MACD_Signal, MACD_Histogram)
49
- macd = ta.macd(data['Close'], fast=12, slow=26, signal=9)
50
- data['MACD'] = macd['MACD_12_26_9']
51
-
52
- # Compute Bollinger Bands
53
  bbands = ta.bbands(data['Close'], length=20, std=2.0)
54
- data['BB_upper'] = bbands[f'BBU_20_2.0'] if f'BBU_20_2.0' in bbands.columns else bbands.iloc[:, 0]
55
- data['BB_middle'] = bbands[f'BBM_20_2.0'] if f'BBM_20_2.0' in bbands.columns else bbands.iloc[:, 1]
56
- data['BB_lower'] = bbands[f'BBL_20_2.0'] if f'BBL_20_2.0' in bbands.columns else bbands.iloc[:, 2]
57
-
 
58
  return data
59
 
60
  # Step 3: Analyze Sentiment using FinBERT
@@ -75,13 +72,29 @@ def generate_prediction(prompt):
75
 
76
  # Step 5: Execute Trade with Alpaca
77
  def execute_trade(signal, symbol='Genting Bhd', qty=1):
78
- print(f"Executing trade signal: {signal}")
79
- if signal == "buy":
80
- api.submit_order(symbol=symbol, qty=qty, side='buy', type='market', time_in_force='gtc')
81
- elif signal == "sell":
82
- api.submit_order(symbol=symbol, qty=qty, side='sell', type='market', time_in_force='gtc')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- # Step 6: Main Function to Run Pipeline
85
  def main():
86
  # 1. Fetch and Prepare Data
87
  klci_data = fetch_stock_data(SYMBOL, START_DATE, END_DATE)
@@ -103,6 +116,10 @@ def main():
103
  execute_trade("sell", symbol=SYMBOL, qty=1)
104
  else:
105
  print("No clear trade signal from prediction.")
 
 
 
 
106
 
107
  # Run the main function
108
  if __name__ == "__main__":
 
7
  import pandas_ta as ta
8
  import torch
9
  import warnings
10
+ import matplotlib.pyplot as plt
11
+
12
 
13
  # Suppress specific FutureWarning
14
  warnings.filterwarnings("ignore", category=FutureWarning)
 
44
  # Step 2: Add Technical Indicators
45
  def add_technical_indicators(data):
46
  print("Adding technical indicators...")
 
47
  data['RSI'] = ta.rsi(data['Close'], length=14)
48
+ data['MACD'], = ta.macd(data['Close'], fast=12, slow=26)['MACD_12_26_9]
 
 
 
 
 
49
  bbands = ta.bbands(data['Close'], length=20, std=2.0)
50
+ # Handle potential missing columns
51
+ if 'BBU_20_2.0' in bbands.columns:
52
+ data['BB_upper'], data['BB_middle'], data['BB_lower'] = bbands['BBU_20_2.0'], bbands['BBM_20_2.0'], bbands['BBL_20_2.0']
53
+ else:
54
+ print("Bollinger Bands data not available.")
55
  return data
56
 
57
  # Step 3: Analyze Sentiment using FinBERT
 
72
 
73
  # Step 5: Execute Trade with Alpaca
74
  def execute_trade(signal, symbol='Genting Bhd', qty=1):
75
+ try:
76
+ print(f"Executing trade signal: {signal}")
77
+ if signal == "buy":
78
+ api.submit_order(symbol=symbol, qty=qty, side='buy', type='market', time_in_force='gtc')
79
+ print(f"Executed buy order for {qty} shares of {symbol}.")
80
+ elif signal == "sell":
81
+ api.submit_order(symbol=symbol, qty=qty, side='sell', type='market', time_in_force='gtc')
82
+ print(f"Executed sell order for {qty} shares of {symbol}.")
83
+ except Exception as e:
84
+ print(f"Error executing trade: {e}")
85
+
86
+ # Step 6: Plot KLCI Data with Technical Indicators
87
+ def plot_klci(data):
88
+ plt.figure(figsize=(14, 7))
89
+ plt.plot(data['Close'], label='KLCI Close Price', color='blue')
90
+ plt.plot(data['BB_upper'], label='Bollinger Upper Band', color='red')
91
+ plt.plot(data['BB_middle'], label='Bollinger Middle Band', color='green')
92
+ plt.plot(data['BB_lower'], label='Bollinger Lower Band', color='red')
93
+ plt.title('KLCI with Technical Indicators')
94
+ plt.legend()
95
+ plt.show()
96
 
97
+ # Step 7: Main Function to Run Pipeline
98
  def main():
99
  # 1. Fetch and Prepare Data
100
  klci_data = fetch_stock_data(SYMBOL, START_DATE, END_DATE)
 
116
  execute_trade("sell", symbol=SYMBOL, qty=1)
117
  else:
118
  print("No clear trade signal from prediction.")
119
+
120
+ # 5. Plot KLCI Data with Indicators
121
+ plot_klci(klci_data)
122
+
123
 
124
  # Run the main function
125
  if __name__ == "__main__":