create an app which visualizes yahoo finance data with the tradingview javscript charting library
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import requests
st.title('Yahoo Finance Data Visualizer')
# Ask for the stock symbol
symbol = st.text_input('Enter Stock Symbol (e.g., AAPL, MSFT):')
if symbol:
# Fetch data from Yahoo Finance API
url = f'https://query1.finance.yahoo.com/v8/finance/chart/{symbol}'
response = requests.get(url)
data = response.json()
if 'chart' in data:
# Extract required information
series = data['chart']['result'][0]
timestamps = series['timestamp']
indicators = series['indicators']['quote'][0]
open_prices = indicators['open']
high_prices = indicators['high']
low_prices = indicators['low']
close_prices = indicators['close']
# Convert timestamps to a readable format and create a DataFrame
df = pd.DataFrame({
'Date': pd.to_datetime(pd.Series(timestamps), unit='s'),
'Open': open_prices,
'High': high_prices,
'Low': low_prices,
'Close': close_prices
})
# Use altair to create a line chart
st.subheader(f'{symbol} Price Chart')
import altair as alt
chart = alt.Chart(df).mark_line().encode(
x='Date:T',
y='Close:Q'
)
st.altair_chart(chart, use_container_width=True)
else:
st.error('Unable to fetch data. Please check the stock symbol.')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?