Candlestick chart
A candlestick chart is a financial chart used to describe price movements of an asset (like stocks) over time. Each "candlestick" typically displays one day's worth of data and represents the opening, closing, high, and low prices for that day. Candlestick charts are widely used in financial and stock market analysis because they provide a visual representation of price action, helping traders to make more informed decisions.
!pip install openbb
from openbb_terminal.sdk import openbb
from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource
# Fetch data
data = openbb.stocks.load(symbol = 'AAPL')
start_date = '2023-09-01'
filtered_data = data[start_date:]
# Prepare data for Bokeh
source = ColumnDataSource(data={
'date': filtered_data.index,
'open': filtered_data['Open'],
'close': filtered_data['Close'],
'high': filtered_data['High'],
'low': filtered_data['Low']
})
# Create the candlestick chart
p = figure(x_axis_type="datetime", width=1000, height=400, title="AAPL Candlestick Chart for September 1st 2023 onwards")
p.grid.grid_line_alpha = 0.3
# Define segments for high and low
p.segment('date', 'high', 'date', 'low', color="black", source=source)
# Define vbars for open and close prices
p.vbar('date', 12*60*60*1000, 'open', 'close', fill_color="green", line_color="black", source=source, legend_label="Price Rise")
p.vbar('date', 12*60*60*1000, 'close', 'open', fill_color="red", line_color="black", source=source, legend_label="Price Drop")
# Display
output_notebook()
show(p)
Explanation:
- The code begins by installing the openbb package and importing necessary libraries.
- Stock data for Apple (AAPL) is fetched using the openbb.stocks.load() function.
- A starting date is specified as September 1st, 2023, and the data is filtered to include only records from this date onward.
- The data is then prepared for plotting by creating a ColumnDataSource, which is a fundamental Bokeh data structure to efficiently interface with the data.
- A candlestick chart is created using Bokeh's figure function. This chart type is common for visualizing stock market data, displaying the open, close, high, and low values for each day.
- Segments representing the high and low values of stocks for each day are plotted.
- Vertical bars (vbars) representing the open and close values are plotted. These bars are colored green when the close value is higher than the open value (indicating a price rise) and red otherwise (indicating a price drop).
- Finally, the generated plot is displayed in the notebook using output_notebook() and show() functions.
In essence, the code visualizes the stock market performance of Apple (AAPL) from September 1st, 2023, using a candlestick chart.
When interpreting this chart, green candlesticks represent days where the stock price increased (closed higher than it opened), while red candlesticks represent days where the stock price decreased (closed lower than it opened). The wicks on the candlesticks represent the range between the high and low prices for the day. This candlestick chart represents the stock performance of Apple (AAPL).
- Red bars denote days when the opening price was higher than the closing price, indicating a "Price Drop" for the day. Green bars represent days when the closing price was higher than the opening price, indicating a "Price Rise."
- The chart begins with a significant price drop on September 1st, with a high starting near 190 and closing around 175.
- Subsequent days in the first half of September show fluctuations in the stock price, with no clear upward or downward trend.
- Starting from late September, there's a noticeable pattern of alternating rises and drops until around mid-October.
- From mid-October onward, the chart shows a more pronounced downward trend, indicating a bearish phase for the stock towards the end of the month.
Overall, while AAPL had periods of gains in October 2023, it ended the month on a declining note, with more days showing price drops than rises.