|
import streamlit as st |
|
import requests |
|
from Pandas_Market_Predictor import Pandas_Market_Predictor |
|
import pandas as pd |
|
from sklearn.decomposition import PCA |
|
|
|
|
|
API_KEY = "QR8F9B7T6R2SWTAT" |
|
|
|
def fetch_alpha_vantage_data(api_key): |
|
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=60min&apikey={api_key}' |
|
response = requests.get(url) |
|
alpha_vantage_data = response.json() |
|
return alpha_vantage_data |
|
|
|
def calculate_indicators(data): |
|
|
|
data = data.apply(pd.to_numeric, errors='coerce') |
|
|
|
|
|
data['Doji'] = abs(data['close'] - data['open']) <= 0.01 * (data['high'] - data['low']) |
|
data['Inside'] = (data['high'] < data['high'].shift(1)) & (data['low'] > data['low'].shift(1)) |
|
|
|
|
|
data['open'] = data['open_original'] |
|
data['high'] = data['high_original'] |
|
data['low'] = data['low_original'] |
|
|
|
return data |
|
|
|
def apply_pca(data, n_components=5): |
|
pca = PCA(n_components=n_components) |
|
reduced_data = pca.fit_transform(data) |
|
return pd.DataFrame(reduced_data, index=data.index, columns=[f'component_{i+1}' for i in range(n_components)]) |
|
|
|
def main(): |
|
st.title("Stock Trend Predictor") |
|
|
|
|
|
api_key = API_KEY |
|
|
|
|
|
alpha_vantage_data = fetch_alpha_vantage_data(api_key) |
|
|
|
|
|
alpha_vantage_time_series = alpha_vantage_data.get('Time Series (60min)', {}) |
|
df = pd.DataFrame(alpha_vantage_time_series).T |
|
df.index = pd.to_datetime(df.index) |
|
df = df.dropna(axis=0) |
|
|
|
|
|
df = df.rename(columns={'1. open': 'open_original', '2. high': 'high_original', '3. low': 'low_original', '4. close': 'Close', '5. volume': 'volume'}) |
|
|
|
|
|
df_reduced = apply_pca(df[['open_original', 'high_original', 'low_original', 'Close', 'volume']]) |
|
|
|
|
|
df_reduced = calculate_indicators(df_reduced) |
|
|
|
|
|
my_market_predictor = Pandas_Market_Predictor(df_reduced) |
|
|
|
|
|
indicators = ["Doji", "Inside"] |
|
trend = my_market_predictor.Trend_Detection(indicators, 10) |
|
|
|
|
|
st.subheader("Predicted Trend:") |
|
st.write("Buy Trend :", trend['BUY']) |
|
st.write("Sell Trend :", trend['SELL']) |
|
st.write(f"Standard Deviation Percentage: {my_market_predictor.PERCENT_STD}%") |
|
|
|
|
|
del df |
|
|
|
if __name__ == "__main__": |
|
main() |