#Import
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
from mplfinance.original_flavor import candlestick_ohlc
import matplotlib.dates as mdates
#Dates to get stocks
start_date = "2020-01-01"
end_date = "2023-05-08"
#Get to the site
tesla_data = yf.download("TSLA", start = start_date, end = end_date)
#Get info off the site
tesla_weekly_data = tesla_data.resample("W").agg({"Open":"first", "High":"max", "Low":"min", "Close":"last", "Volume":"sum"}).dropna()
tesla_weekly_data.reset_index(inplace = True)
tesla_weekly_data["Date"] = tesla_weekly_data["Date"].map(mdates.date2num)
#Configer the plot
fig, ax = plt.subplots(figsize=(16,8))
#Show Canddlestick
candlestick_ohlc(ax, tesla_weekly_data.values, width = 5, colorup = "green", colordown = "red")
#Fonts
font1 = {"family":"serif", "color":"black", "size":40, "fontweight":"bold"}
font2 = {"family":"serif", "color":"black", "size":20, "fontweight":"bold"}
#Show Descripton of title and labels
plt.xlabel("Date", fontdict = font2)
plt.ylabel("Price",fontdict = font2)
plt.title("Tesla Stock Prices", fontdict = font1)
#Show the stocks line
plt.plot(tesla_data["Close"], color = "cyan", linewidth = 2)
#Add legend
plt.legend({"Tesla Close Prices":"cyan", "Price Up":"green", "Price Down":"red"})
#Show the plot
plt.show()
When it opens a window, the legend is not correct even thought it support to be!
When you click on run, the legend box is there but not correctly, First the tesla close prices are support to be cyan but is a green line, the price up is good but not the price down.