Update app.py
Browse files
app.py
CHANGED
@@ -13,14 +13,18 @@ def fetch_alpha_vantage_data(api_key):
|
|
13 |
return alpha_vantage_data
|
14 |
|
15 |
def calculate_indicators(data):
|
|
|
16 |
data = data.apply(pd.to_numeric, errors='coerce')
|
17 |
|
|
|
18 |
data['Doji'] = abs(data['Close'] - data['open']) <= 0.01 * (data['high'] - data['low'])
|
19 |
data['Inside'] = (data['high'] < data['high'].shift(1)) & (data['low'] > data['low'].shift(1))
|
20 |
|
|
|
21 |
data['MA5'] = data['Close'].rolling(window=5).mean()
|
22 |
data['MA20'] = data['Close'].rolling(window=20).mean()
|
23 |
|
|
|
24 |
data['26EMA'] = data['Close'].ewm(span=26).mean()
|
25 |
data['12EMA'] = data['Close'].ewm(span=12).mean()
|
26 |
data['MACD'] = data['12EMA'] - data['26EMA']
|
@@ -30,33 +34,38 @@ def calculate_indicators(data):
|
|
30 |
def main():
|
31 |
st.title("Stock Trend Predictor")
|
32 |
|
|
|
33 |
api_key = API_KEY
|
34 |
|
|
|
35 |
alpha_vantage_data = fetch_alpha_vantage_data(api_key)
|
36 |
|
|
|
37 |
alpha_vantage_time_series = alpha_vantage_data.get('Time Series (5min)', {})
|
38 |
df = pd.DataFrame(alpha_vantage_time_series).T
|
39 |
df.index = pd.to_datetime(df.index)
|
40 |
df = df.dropna(axis=0)
|
41 |
|
|
|
42 |
df = df.rename(columns={'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'Close', '5. volume': 'volume'})
|
43 |
|
|
|
44 |
df = calculate_indicators(df)
|
45 |
|
|
|
46 |
my_market_predictor = Pandas_Market_Predictor(df)
|
47 |
|
48 |
-
#
|
49 |
-
ichimoku_columns = ["tenkan_sen", "kijun_sen", "senkou_span_a", "senkou_span_b"]
|
50 |
-
df = df.drop(columns=ichimoku_columns)
|
51 |
-
|
52 |
indicators = ["Doji", "Inside", "MA5", "MA20", "MACD"]
|
53 |
trend = my_market_predictor.Trend_Detection(indicators, 10)
|
54 |
|
|
|
55 |
st.subheader("Predicted Trend:")
|
56 |
st.write("Buy Trend :", trend['BUY'])
|
57 |
st.write("Sell Trend :", trend['SELL'])
|
58 |
st.write(f"Standard Deviation Percentage: {my_market_predictor.PERCENT_STD}%")
|
59 |
|
|
|
60 |
del df
|
61 |
|
62 |
if __name__ == "__main__":
|
|
|
13 |
return alpha_vantage_data
|
14 |
|
15 |
def calculate_indicators(data):
|
16 |
+
# Convert all columns to numeric
|
17 |
data = data.apply(pd.to_numeric, errors='coerce')
|
18 |
|
19 |
+
# Example: Simple condition for doji and inside
|
20 |
data['Doji'] = abs(data['Close'] - data['open']) <= 0.01 * (data['high'] - data['low'])
|
21 |
data['Inside'] = (data['high'] < data['high'].shift(1)) & (data['low'] > data['low'].shift(1))
|
22 |
|
23 |
+
# Calculate Moving Averages
|
24 |
data['MA5'] = data['Close'].rolling(window=5).mean()
|
25 |
data['MA20'] = data['Close'].rolling(window=20).mean()
|
26 |
|
27 |
+
# Calculate MACD
|
28 |
data['26EMA'] = data['Close'].ewm(span=26).mean()
|
29 |
data['12EMA'] = data['Close'].ewm(span=12).mean()
|
30 |
data['MACD'] = data['12EMA'] - data['26EMA']
|
|
|
34 |
def main():
|
35 |
st.title("Stock Trend Predictor")
|
36 |
|
37 |
+
# Use the hard-coded API key
|
38 |
api_key = API_KEY
|
39 |
|
40 |
+
# Fetch Alpha Vantage data
|
41 |
alpha_vantage_data = fetch_alpha_vantage_data(api_key)
|
42 |
|
43 |
+
# Extract relevant data from Alpha Vantage response
|
44 |
alpha_vantage_time_series = alpha_vantage_data.get('Time Series (5min)', {})
|
45 |
df = pd.DataFrame(alpha_vantage_time_series).T
|
46 |
df.index = pd.to_datetime(df.index)
|
47 |
df = df.dropna(axis=0)
|
48 |
|
49 |
+
# Rename columns
|
50 |
df = df.rename(columns={'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'Close', '5. volume': 'volume'})
|
51 |
|
52 |
+
# Calculate indicators
|
53 |
df = calculate_indicators(df)
|
54 |
|
55 |
+
# Create predictor
|
56 |
my_market_predictor = Pandas_Market_Predictor(df)
|
57 |
|
58 |
+
# Predict Trend
|
|
|
|
|
|
|
59 |
indicators = ["Doji", "Inside", "MA5", "MA20", "MACD"]
|
60 |
trend = my_market_predictor.Trend_Detection(indicators, 10)
|
61 |
|
62 |
+
# Display results
|
63 |
st.subheader("Predicted Trend:")
|
64 |
st.write("Buy Trend :", trend['BUY'])
|
65 |
st.write("Sell Trend :", trend['SELL'])
|
66 |
st.write(f"Standard Deviation Percentage: {my_market_predictor.PERCENT_STD}%")
|
67 |
|
68 |
+
# Delete the DataFrame to release memory
|
69 |
del df
|
70 |
|
71 |
if __name__ == "__main__":
|