Update app.py
Browse files
app.py
CHANGED
@@ -7,55 +7,68 @@ import pandas as pd
|
|
7 |
API_KEY = "QR8F9B7T6R2SWTAT"
|
8 |
|
9 |
def fetch_alpha_vantage_data(api_key):
|
10 |
-
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=
|
11 |
response = requests.get(url)
|
12 |
alpha_vantage_data = response.json()
|
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 |
return data
|
23 |
|
24 |
def main():
|
25 |
st.title("Stock Trend Predictor")
|
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)
|
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 |
-
# Rename columns
|
40 |
df = df.rename(columns={'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'Close', '5. volume': 'volume'})
|
41 |
|
42 |
-
# Calculate indicators
|
43 |
df = calculate_indicators(df)
|
44 |
|
45 |
-
# Create predictor
|
46 |
my_market_predictor = Pandas_Market_Predictor(df)
|
47 |
|
48 |
-
|
49 |
-
indicators = ["Doji", "Inside"]
|
50 |
trend = my_market_predictor.Trend_Detection(indicators, 10)
|
51 |
|
52 |
-
# Display results
|
53 |
st.subheader("Predicted Trend:")
|
54 |
st.write("Buy Trend :", trend['BUY'])
|
55 |
st.write("Sell Trend :", trend['SELL'])
|
56 |
st.write(f"Standard Deviation Percentage: {my_market_predictor.PERCENT_STD}%")
|
57 |
|
58 |
-
# Delete the DataFrame to release memory
|
59 |
del df
|
60 |
|
61 |
if __name__ == "__main__":
|
|
|
7 |
API_KEY = "QR8F9B7T6R2SWTAT"
|
8 |
|
9 |
def fetch_alpha_vantage_data(api_key):
|
10 |
+
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey={api_key}'
|
11 |
response = requests.get(url)
|
12 |
alpha_vantage_data = response.json()
|
13 |
return alpha_vantage_data
|
14 |
|
15 |
+
def calculate_ichimoku_cloud(data):
|
16 |
+
short_window = 9
|
17 |
+
long_window = 26
|
18 |
+
span_b_window = 52
|
19 |
+
displacement = 26
|
20 |
+
|
21 |
+
data['tenkan_sen'] = (data['high'].rolling(window=short_window).max() + data['low'].rolling(window=short_window).min()) / 2
|
22 |
+
data['kijun_sen'] = (data['high'].rolling(window=long_window).max() + data['low'].rolling(window=long_window).min()) / 2
|
23 |
+
data['senkou_span_a'] = ((data['tenkan_sen'] + data['kijun_sen']) / 2).shift(displacement)
|
24 |
+
data['senkou_span_b'] = ((data['high'].rolling(window=span_b_window).max() + data['low'].rolling(window=span_b_window).min()) / 2).shift(displacement)
|
25 |
+
|
26 |
+
return data
|
27 |
+
|
28 |
def calculate_indicators(data):
|
|
|
29 |
data = data.apply(pd.to_numeric, errors='coerce')
|
30 |
|
|
|
31 |
data['Doji'] = abs(data['Close'] - data['open']) <= 0.01 * (data['high'] - data['low'])
|
32 |
data['Inside'] = (data['high'] < data['high'].shift(1)) & (data['low'] > data['low'].shift(1))
|
33 |
+
|
34 |
+
data['MA5'] = data['Close'].rolling(window=5).mean()
|
35 |
+
data['MA20'] = data['Close'].rolling(window=20).mean()
|
36 |
+
|
37 |
+
data['26EMA'] = data['Close'].ewm(span=26).mean()
|
38 |
+
data['12EMA'] = data['Close'].ewm(span=12).mean()
|
39 |
+
data['MACD'] = data['12EMA'] - data['26EMA']
|
40 |
+
|
41 |
+
# Calculate Ichimoku Cloud
|
42 |
+
data = calculate_ichimoku_cloud(data)
|
43 |
+
|
44 |
return data
|
45 |
|
46 |
def main():
|
47 |
st.title("Stock Trend Predictor")
|
48 |
|
|
|
49 |
api_key = API_KEY
|
50 |
|
|
|
51 |
alpha_vantage_data = fetch_alpha_vantage_data(api_key)
|
52 |
|
|
|
53 |
alpha_vantage_time_series = alpha_vantage_data.get('Time Series (5min)', {})
|
54 |
df = pd.DataFrame(alpha_vantage_time_series).T
|
55 |
df.index = pd.to_datetime(df.index)
|
56 |
df = df.dropna(axis=0)
|
57 |
|
|
|
58 |
df = df.rename(columns={'1. open': 'open', '2. high': 'high', '3. low': 'low', '4. close': 'Close', '5. volume': 'volume'})
|
59 |
|
|
|
60 |
df = calculate_indicators(df)
|
61 |
|
|
|
62 |
my_market_predictor = Pandas_Market_Predictor(df)
|
63 |
|
64 |
+
indicators = ["Doji", "Inside", "MA5", "MA20", "MACD", "tenkan_sen", "kijun_sen", "senkou_span_a", "senkou_span_b"]
|
|
|
65 |
trend = my_market_predictor.Trend_Detection(indicators, 10)
|
66 |
|
|
|
67 |
st.subheader("Predicted Trend:")
|
68 |
st.write("Buy Trend :", trend['BUY'])
|
69 |
st.write("Sell Trend :", trend['SELL'])
|
70 |
st.write(f"Standard Deviation Percentage: {my_market_predictor.PERCENT_STD}%")
|
71 |
|
|
|
72 |
del df
|
73 |
|
74 |
if __name__ == "__main__":
|