-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_stock_gains.py
33 lines (28 loc) · 1.02 KB
/
plot_stock_gains.py
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
# filename: plot_stock_gains.py
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
def fetch_and_plot_stocks(stocks, start_date, end_date, output_file):
# Fetch stock data
data = yf.download(stocks, start=start_date, end=end_date)
# Calculate YTD gain as percentage from the first day of the year
ytd_gains = (data['Close'] / data['Close'].iloc[0] - 1) * 100
# Plotting
plt.figure(figsize=(10, 6))
for stock in stocks:
plt.plot(ytd_gains[stock], label=f'{stock} YTD Gain (%)')
plt.title('YTD Stock Gains for NVDA and TSLA in 2025')
plt.xlabel('Date')
plt.ylabel('Gain (%)')
plt.legend()
plt.grid(True)
plt.savefig(output_file)
plt.close()
# Define stock symbols, dates, and output file name
stocks = ['NVDA', 'TSLA']
start_date = '2025-01-01'
end_date = '2025-01-27'
output_file = 'ydt_stock_gains.png'
# Call the function
fetch_and_plot_stocks(stocks, start_date, end_date, output_file)
print("Plot saved successfully as 'ydt_stock_gains.png'.")