File size: 2,612 Bytes
3f96245
 
adbc0ad
3f96245
8b59267
 
 
3f96245
dd448e5
 
 
3f96245
7d80d52
3f96245
 
 
 
adbc0ad
b5416e9
8b59267
 
adbc0ad
 
8b59267
 
 
 
 
 
 
 
 
 
3f96245
8b59267
3f96245
dcf0ee0
dd448e5
3f96245
dcf0ee0
dd448e5
 
dcf0ee0
4f7b738
dd448e5
 
 
 
dcf0ee0
8b59267
d7fdf9f
dcf0ee0
4f7b738
adbc0ad
dcf0ee0
4f7b738
adbc0ad
8b59267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c198c2
dcf0ee0
adbc0ad
3f96245
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st
import requests
from Pandas_Market_Predictor import Pandas_Market_Predictor
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Hard-coded API key for demonstration purposes
API_KEY = "QR8F9B7T6R2SWTAT"

def fetch_alpha_vantage_data(api_key):
    url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&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))
    return data

def prepare_data(data, target_column='Close'):
    X = data.drop(target_column, axis=1)
    y = data[target_column]
    return X, y

def train_linear_regression(X_train, y_train):
    model = LinearRegression()
    model.fit(X_train, y_train)
    return model

def main():
    st.title("Stock Price Predictor")

    # Use the hard-coded API key
    api_key = API_KEY

    # Fetch Alpha Vantage data
    alpha_vantage_data = fetch_alpha_vantage_data(api_key)

    # Extract relevant data from Alpha Vantage response
    alpha_vantage_time_series = alpha_vantage_data.get('Time Series (5min)', {})
    df = pd.DataFrame(alpha_vantage_time_series).T
    df.index = pd.to_datetime(df.index)
    df = df.dropna(axis=0)

    # Rename columns
    df = df.rename(columns={'1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. volume': 'Volume'})

    # Calculate indicators
    df = calculate_indicators(df)

    # Create predictor
    my_market_predictor = Pandas_Market_Predictor(df)

    # Prepare data for linear regression
    X, y = prepare_data(df)

    # Split data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    # Train linear regression model
    model = train_linear_regression(X_train, y_train)

    # Make predictions on the test set
    y_pred = model.predict(X_test)

    # Display linear regression results
    st.subheader("Linear Regression Results:")
    st.write("Mean Squared Error:", mean_squared_error(y_test, y_pred))
    st.write("R-squared Score:", r2_score(y_test, y_pred))

    # Delete the DataFrame to release memory
    del df

if __name__ == "__main__":
    main()