Geek7 commited on
Commit
2d2caa7
·
verified ·
1 Parent(s): c1177dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -38
app.py CHANGED
@@ -1,54 +1,86 @@
1
  import streamlit as st
2
- import requests
3
  import pandas as pd
 
 
 
 
4
 
5
  # Hard-coded API key for demonstration purposes
6
  API_KEY = "QR8F9B7T6R2SWTAT"
7
 
8
  def fetch_alpha_vantage_data(api_key, symbol):
9
- try:
10
- url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={api_key}'
11
- response = requests.get(url)
12
- response.raise_for_status() # Raise an error for bad responses
13
- alpha_vantage_data = response.json()
14
- return alpha_vantage_data
15
- except requests.RequestException as e:
16
- st.error(f"Error fetching data: {e}")
17
- return None
18
 
19
- def main():
20
- st.title("Stock Data Analysis")
 
 
 
 
 
 
21
 
22
- # User input for stock symbol
23
- symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")
 
24
 
25
- if not symbol:
26
- st.warning("Please enter a valid stock symbol.")
27
- st.stop()
28
 
29
- # Use the hard-coded API key
30
- api_key = API_KEY
31
 
32
  # Fetch Alpha Vantage data
33
- alpha_vantage_data = fetch_alpha_vantage_data(api_key, symbol)
34
-
35
- if alpha_vantage_data:
36
- # Extract relevant data from Alpha Vantage response
37
- time_series_key = 'Time Series (5min)'
38
- if time_series_key in alpha_vantage_data:
39
- alpha_vantage_time_series = alpha_vantage_data[time_series_key]
40
- df = pd.DataFrame(alpha_vantage_time_series).T
41
- df.index = pd.to_datetime(df.index)
42
- df = df.dropna(axis=0)
43
-
44
- if not df.empty:
45
- # Display the raw data
46
- st.subheader("Raw Data:")
47
- st.write(df)
48
- else:
49
- st.warning("No intraday data available for the specified symbol.")
50
- else:
51
- st.warning(f"Key '{time_series_key}' not found in API response.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  if __name__ == "__main__":
54
  main()
 
1
  import streamlit as st
2
+ import yfinance as yf
3
  import pandas as pd
4
+ from thronetrader import StrategicSignals
5
+ from Pandas_Market_Predictor import Pandas_Market_Predictor
6
+
7
+
8
 
9
  # Hard-coded API key for demonstration purposes
10
  API_KEY = "QR8F9B7T6R2SWTAT"
11
 
12
  def fetch_alpha_vantage_data(api_key, symbol):
13
+
14
+ url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={api_key}'
15
+ response = requests.get(url)
16
+ alpha_vantage_data = response.json()
17
+ return alpha_vantage_data
 
 
 
 
18
 
19
+ def calculate_indicators(data):
20
+ # Convert all columns to numeric
21
+ data = data.apply(pd.to_numeric, errors='coerce')
22
+
23
+ # Example: Simple condition for doji and inside
24
+ data['Doji'] = abs(data['Close'] - data['Open']) <= 0.01 * (data['High'] - data['Low'])
25
+ data['Inside'] = (data['High'] < data['High'].shift(1)) & (data['Low'] > data['Low'].shift(1))
26
+ return data
27
 
28
+ def display_signals(signal_type, signals):
29
+ st.subheader(f"{signal_type} Signals:")
30
+ st.write(signals)
31
 
32
+ def main():
33
+ st.title("Stock Trend Predictor")
 
34
 
35
+ # Input for stock symbol
36
+ symbol = st.text_input("Enter stock symbol (e.g., AAPL):", "AAPL")
37
 
38
  # Fetch Alpha Vantage data
39
+ alpha_vantage_data = fetch_alpha_vantage_data(API_KEY, symbol)
40
+
41
+ # Extract relevant data from Alpha Vantage response
42
+ alpha_vantage_time_series = alpha_vantage_data.get('Time Series (5min)', {})
43
+ df = pd.DataFrame(alpha_vantage_time_series).T
44
+ df.index = pd.to_datetime(df.index)
45
+ df = df.dropna(axis=0)
46
+
47
+ # Rename columns
48
+ df = df.rename(columns={'1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. volume': 'Volume'})
49
+
50
+ # Calculate indicators
51
+ df = calculate_indicators(df)
52
+
53
+ # Display stock trading signals
54
+ strategic_signals = StrategicSignals(symbol=symbol)
55
+
56
+ # Display loading message during processing
57
+ with st.spinner("Predicting signals using Strategic Indicators..."):
58
+ # Display signals
59
+ st.subheader(":orange[Strategic Indicators Trend Prediction]")
60
+ display_signals("Bollinger Bands", strategic_signals.get_bollinger_bands_signals())
61
+ display_signals("Breakout", strategic_signals.get_breakout_signals())
62
+ display_signals("Crossover", strategic_signals.get_crossover_signals())
63
+ display_signals("MACD", strategic_signals.get_macd_signals())
64
+ display_signals("RSI", strategic_signals.get_rsi_signals())
65
+
66
+ # Create predictor
67
+ my_market_predictor = Pandas_Market_Predictor(df)
68
+
69
+ # Predict Trend
70
+ indicators = ["Doji", "Inside"]
71
+
72
+ # Display loading message during prediction
73
+ with st.spinner("Predicting trend using AI ...."):
74
+ # Predict trend
75
+ trend = my_market_predictor.Trend_Detection(indicators, 10)
76
+
77
+ # Display results
78
+ st.subheader(":orange[AI Trend Prediction]")
79
+ st.write("Buy Trend :", trend['BUY'])
80
+ st.write("Sell Trend :", trend['SELL'])
81
+
82
+ # Delete the DataFrame to release memory
83
+ del df
84
 
85
  if __name__ == "__main__":
86
  main()