Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
import requests
|
4 |
-
from Pandas_Market_Predictor import Pandas_Market_Predictor
|
5 |
import pandas as pd
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
def fetch_alpha_vantage_data(api_key, symbol):
|
12 |
-
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={api_key}'
|
13 |
-
response = requests.get(url)
|
14 |
-
alpha_vantage_data = response.json()
|
15 |
-
return alpha_vantage_data
|
16 |
-
|
17 |
-
def main():
|
18 |
-
st.title("Stock Trend Predictor")
|
19 |
-
|
20 |
-
# User input for stock symbol
|
21 |
-
symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")
|
22 |
-
|
23 |
-
if not symbol:
|
24 |
-
st.warning("Please enter a valid stock symbol.")
|
25 |
-
st.stop()
|
26 |
-
|
27 |
-
# Use the hard-coded API key
|
28 |
-
api_key = API_KEY
|
29 |
-
|
30 |
-
# Fetch Alpha Vantage data
|
31 |
-
alpha_vantage_data = fetch_alpha_vantage_data(api_key, symbol)
|
32 |
-
|
33 |
-
# Extract relevant data from Alpha Vantage response
|
34 |
-
alpha_vantage_time_series = alpha_vantage_data.get('Time Series (5min)', {})
|
35 |
-
df = pd.DataFrame(alpha_vantage_time_series).T
|
36 |
-
df.index = pd.to_datetime(df.index)
|
37 |
-
df = df.dropna(axis=0)
|
38 |
-
|
39 |
-
# Display the raw data
|
40 |
-
st.subheader("Raw Data:")
|
41 |
-
st.write(df)
|
42 |
-
|
43 |
-
if __name__ == "__main__":
|
44 |
-
main()
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# Hard-coded API key for demonstration purposes
|
50 |
-
API_KEY = "QR8F9B7T6R2SWTAT"
|
51 |
-
|
52 |
-
def fetch_alpha_vantage_data(api_key, symbol):
|
53 |
-
|
54 |
-
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={api_key}'
|
55 |
-
response = requests.get(url)
|
56 |
-
alpha_vantage_data = response.json()
|
57 |
-
return alpha_vantage_data
|
58 |
|
59 |
def calculate_indicators(data):
|
60 |
# Convert all columns to numeric
|
@@ -65,62 +16,33 @@ def calculate_indicators(data):
|
|
65 |
data['Inside'] = (data['High'] < data['High'].shift(1)) & (data['Low'] > data['Low'].shift(1))
|
66 |
return data
|
67 |
|
68 |
-
def display_signals(signal_type, signals):
|
69 |
-
st.subheader(f"{signal_type} Signals:")
|
70 |
-
st.write(signals)
|
71 |
-
|
72 |
def main():
|
73 |
-
st.title("Stock Trend Predictor")
|
74 |
|
75 |
# Input for stock symbol
|
76 |
symbol = st.text_input("Enter stock symbol (e.g., AAPL):", "AAPL")
|
77 |
|
78 |
-
# Fetch
|
79 |
-
|
80 |
-
|
81 |
-
# Extract relevant data from Alpha Vantage response
|
82 |
-
alpha_vantage_time_series = alpha_vantage_data.get('Time Series (5min)', {})
|
83 |
-
df = pd.DataFrame(alpha_vantage_time_series).T
|
84 |
-
df.index = pd.to_datetime(df.index)
|
85 |
-
df = df.dropna(axis=0)
|
86 |
-
|
87 |
-
# Rename columns
|
88 |
-
df = df.rename(columns={'1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. volume': 'Volume'})
|
89 |
|
90 |
# Calculate indicators
|
91 |
-
|
92 |
-
|
93 |
-
# Display stock trading signals
|
94 |
-
strategic_signals = StrategicSignals(symbol=symbol)
|
95 |
-
|
96 |
-
# Display loading message during processing
|
97 |
-
with st.spinner("Predicting signals using Strategic Indicators..."):
|
98 |
-
# Display signals
|
99 |
-
st.subheader(":orange[Strategic Indicators Trend Prediction]")
|
100 |
-
display_signals("Bollinger Bands", strategic_signals.get_bollinger_bands_signals())
|
101 |
-
display_signals("Breakout", strategic_signals.get_breakout_signals())
|
102 |
-
display_signals("Crossover", strategic_signals.get_crossover_signals())
|
103 |
-
display_signals("MACD", strategic_signals.get_macd_signals())
|
104 |
-
display_signals("RSI", strategic_signals.get_rsi_signals())
|
105 |
|
106 |
# Create predictor
|
107 |
-
my_market_predictor = Pandas_Market_Predictor(
|
108 |
|
109 |
# Predict Trend
|
110 |
indicators = ["Doji", "Inside"]
|
111 |
-
|
112 |
-
# Display loading message during prediction
|
113 |
-
with st.spinner("Predicting trend using AI ...."):
|
114 |
-
# Predict trend
|
115 |
-
trend = my_market_predictor.Trend_Detection(indicators, 10)
|
116 |
|
117 |
# Display results
|
118 |
-
st.subheader("
|
119 |
st.write("Buy Trend :", trend['BUY'])
|
120 |
st.write("Sell Trend :", trend['SELL'])
|
|
|
121 |
|
122 |
# Delete the DataFrame to release memory
|
123 |
-
del
|
124 |
|
125 |
if __name__ == "__main__":
|
126 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
import yfinance as yf
|
|
|
|
|
3 |
import pandas as pd
|
4 |
+
from Pandas_Market_Predictor import Pandas_Market_Predictor
|
5 |
|
6 |
+
def fetch_yfinance_data(symbol):
|
7 |
+
data = yf.download(symbol, start="2022-01-01", end="2022-12-31", interval="5m")
|
8 |
+
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def calculate_indicators(data):
|
11 |
# Convert all columns to numeric
|
|
|
16 |
data['Inside'] = (data['High'] < data['High'].shift(1)) & (data['Low'] > data['Low'].shift(1))
|
17 |
return data
|
18 |
|
|
|
|
|
|
|
|
|
19 |
def main():
|
20 |
+
st.title("AI Stock Trend Predictor")
|
21 |
|
22 |
# Input for stock symbol
|
23 |
symbol = st.text_input("Enter stock symbol (e.g., AAPL):", "AAPL")
|
24 |
|
25 |
+
# Fetch yfinance data
|
26 |
+
stock_data = fetch_yfinance_data(symbol)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Calculate indicators
|
29 |
+
stock_data = calculate_indicators(stock_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Create predictor
|
32 |
+
my_market_predictor = Pandas_Market_Predictor(stock_data)
|
33 |
|
34 |
# Predict Trend
|
35 |
indicators = ["Doji", "Inside"]
|
36 |
+
trend = my_market_predictor.Trend_Detection(indicators, 10)
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Display results
|
39 |
+
st.subheader("Predicted Trend:")
|
40 |
st.write("Buy Trend :", trend['BUY'])
|
41 |
st.write("Sell Trend :", trend['SELL'])
|
42 |
+
st.write("Hold Trend :", trend['HOLD'])
|
43 |
|
44 |
# Delete the DataFrame to release memory
|
45 |
+
del stock_data
|
46 |
|
47 |
if __name__ == "__main__":
|
48 |
main()
|