Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,12 @@ import streamlit as st
|
|
2 |
import requests
|
3 |
from Pandas_Market_Predictor import Pandas_Market_Predictor
|
4 |
import pandas as pd
|
5 |
-
from sklearn.decomposition import PCA
|
6 |
|
7 |
# Hard-coded API key for demonstration purposes
|
8 |
API_KEY = "QR8F9B7T6R2SWTAT"
|
9 |
|
10 |
def fetch_alpha_vantage_data(api_key):
|
11 |
-
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=
|
12 |
response = requests.get(url)
|
13 |
alpha_vantage_data = response.json()
|
14 |
return alpha_vantage_data
|
@@ -20,19 +19,8 @@ def calculate_indicators(data):
|
|
20 |
# Example: Simple condition for doji and inside
|
21 |
data['Doji'] = abs(data['close'] - data['open']) <= 0.01 * (data['high'] - data['low'])
|
22 |
data['Inside'] = (data['high'] < data['high'].shift(1)) & (data['low'] > data['low'].shift(1))
|
23 |
-
|
24 |
-
# Include the necessary columns from the original dataset
|
25 |
-
data['open'] = data['open_original']
|
26 |
-
data['high'] = data['high_original']
|
27 |
-
data['low'] = data['low_original']
|
28 |
-
|
29 |
return data
|
30 |
|
31 |
-
def apply_pca(data, n_components=5):
|
32 |
-
pca = PCA(n_components=n_components)
|
33 |
-
reduced_data = pca.fit_transform(data)
|
34 |
-
return pd.DataFrame(reduced_data, index=data.index, columns=[f'component_{i+1}' for i in range(n_components)])
|
35 |
-
|
36 |
def main():
|
37 |
st.title("Stock Trend Predictor")
|
38 |
|
@@ -43,22 +31,19 @@ def main():
|
|
43 |
alpha_vantage_data = fetch_alpha_vantage_data(api_key)
|
44 |
|
45 |
# Extract relevant data from Alpha Vantage response
|
46 |
-
alpha_vantage_time_series = alpha_vantage_data.get('Time Series (
|
47 |
df = pd.DataFrame(alpha_vantage_time_series).T
|
48 |
df.index = pd.to_datetime(df.index)
|
49 |
df = df.dropna(axis=0)
|
50 |
|
51 |
# Rename columns
|
52 |
-
df = df.rename(columns={'1. open': '
|
53 |
-
|
54 |
-
# Apply PCA for dimensionality reduction
|
55 |
-
df_reduced = apply_pca(df[['open_original', 'high_original', 'low_original', 'Close', 'volume']])
|
56 |
|
57 |
-
# Calculate indicators
|
58 |
-
|
59 |
|
60 |
# Create predictor
|
61 |
-
my_market_predictor = Pandas_Market_Predictor(
|
62 |
|
63 |
# Predict Trend
|
64 |
indicators = ["Doji", "Inside"]
|
|
|
2 |
import requests
|
3 |
from Pandas_Market_Predictor import Pandas_Market_Predictor
|
4 |
import pandas as pd
|
|
|
5 |
|
6 |
# Hard-coded API key for demonstration purposes
|
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
|
|
|
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 |
|
|
|
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 |
# Predict Trend
|
49 |
indicators = ["Doji", "Inside"]
|