File size: 3,129 Bytes
3f96245 adbc0ad 3f96245 cc21455 3f96245 dd448e5 3f96245 7d80d52 3f96245 7d80d52 6f86b47 7d80d52 adbc0ad b5416e9 76337a5 7d0ce76 235c728 7d80d52 cc21455 7d80d52 adbc0ad 3f96245 dd448e5 3f96245 dd448e5 4f7b738 dd448e5 4f7b738 d7fdf9f 4f7b738 adbc0ad 4f7b738 adbc0ad cc21455 adbc0ad dd448e5 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 80 81 82 |
import streamlit as st
import requests
from Pandas_Market_Predictor import Pandas_Market_Predictor
import pandas as pd
import numpy as np
# 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_ichimoku_cloud(data):
short_window = 9
long_window = 26
span_b_window = 52
displacement = 26
data['tenkan_sen'] = (data['high'].rolling(window=short_window).max() + data['low'].rolling(window=short_window).min()) / 2
data['kijun_sen'] = (data['high'].rolling(window=long_window).max() + data['low'].rolling(window=long_window).min()) / 2
data['senkou_span_a'] = ((data['tenkan_sen'] + data['kijun_sen']) / 2).shift(displacement)
data['senkou_span_b'] = ((data['high'].rolling(window=span_b_window).max() + data['low'].rolling(window=span_b_window).min()) / 2).shift(displacement)
return data # Make sure to return the updated DataFrame
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['MA5'] = data['Close'].rolling(window=5).mean()
data['MA20'] = data['Close'].rolling(window=20).mean()
data['26EMA'] = data['Close'].ewm(span=26).mean()
data['12EMA'] = data['Close'].ewm(span=12).mean()
data['MACD'] = data['12EMA'] - data['26EMA']
# Calculate Ichimoku Cloud
data = calculate_ichimoku_cloud(data)
return data
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 (5min)', {})
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', '2. high': 'high', '3. low': 'low', '4. close': 'Close', '5. volume': 'volume'})
df = calculate_indicators(df)
my_market_predictor = Pandas_Market_Predictor(df)
# Print data for each indicator before making predictions
for indicator in ["THR", "LGR", "THd", "THM"]:
indicator_data = my_market_predictor.get_indicator_data(indicator)
print(f"Data for {indicator}:")
print(indicator_data.head()) # Print the first few rows of data for the indicator
indicators = ["Doji", "Inside", "MA5", "MA20", "MACD", "tenkan_sen", "kijun_sen", "senkou_span_a", "senkou_span_b"]
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() |