import pandas as pd
from lightweight_charts import Chart
import yfinance as yf
import streamlit as st
def main():
# Define the ticker symbol
tickerSymbol = 'AAPL'
# Set the start and end dates for the data
startDate = '2023-01-01'
endDate = '2024-01-01'
# Get the historical data for the ticker within the specified date range and at a daily interval
data = yf.download(tickerSymbol, start=startDate, end=endDate)
# Create a new Streamlit chart object
st.write("Candlestick Chart:")
chart = Chart()
chart.add_ohlc(data)
st.write(chart)
if __name__ == '__main__':
main()
thanks bro it really helped me
i found another solution. but there was problem in that also. i was not able change height and width of chart . your solution is very perfect
my code solution was as follows
ticker_symbol = 'AAPL'
# Download the stock data
stock_data = yf.download(ticker_symbol, start='2023-01-01', end='2024-01-01')
# Reset the index to make the date a column
stock_data.reset_index(inplace=True)
stock_data['Date'] = pd.to_datetime(stock_data['Date'])
# Convert datetime objects to Unix timestamps
stock_data['Time'] = stock_data['Date'].apply(lambda x: int(x.timestamp()))
# Calculate SMA with a window of 20 days
stock_data['SMA'] = stock_data['Close'].rolling(window=20).mean()
# Calculate EMA with a span of 20 days
stock_data['EMA'] = stock_data['Close'].ewm(span=20, adjust=False).mean()
chartOptions = {
"layout": {
"textColor": 'black',
"background": {
"type": 'solid',
"color": 'white'
}
}
}
seriesCandlestickChart = [{
"type": 'Candlestick',
"data": [
{"open": row['Open'], "high": row['High'], "low": row['Low'], "close": row['Close'], "time": row['Time']}
for index, row in stock_data.iterrows()
],
"options": {
"upColor": '#26a69a',
"downColor": '#ef5350',
"borderVisible": False,
"wickUpColor": '#26a69a',
"wickDownColor": '#ef5350'
}
}]
seriesSMA = [{
"type": 'Line',
"data": [
{"value": row['SMA'], "time": row['Time']} for index, row in stock_data.iterrows() if not pd.isnull(row['SMA'])
],
"options": {
"color": 'blue',
"lineStyle": 0,
"lineWidth": 1,
}
}]
seriesEMA = [{
"type": 'Line',
"data": [
{"value": row['EMA'], "time": row['Time']} for index, row in stock_data.iterrows() if not pd.isnull(row['EMA'])
],
"options": {
"color": 'red',
"lineStyle": 0,
"lineWidth": 1,
}
}]
st.subheader("Candlestick Chart with SMA and EMA")
renderLightweightCharts([
{
"chart": chartOptions,
"series": seriesCandlestickChart + seriesSMA + seriesEMA
}
], 'candlestick')
hi @dataprofessor
is there any library that can plot more interactive charts than this
and also have indicators and toolkit for drawing trendline and stuff
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.