Multi-Agent AI: Coding
Multi-agent GenAI app based on the Microsoft AutoGen framework.
- Agents: 1. Code Writer (LLM: gpt-4o) and 2. Code Executor (LLM: n/a)
- Tools: Today and Code Execution
See also multi-agent multi-modal multi-model General AI Assistant.
Code Writer
To create a plot showing the year-to-date (YTD) stock gain percentages for NVDA, MSFT, AAPL, and GOOG, we will need to perform the following steps:
- Retrieve stock data for each company.
- Calculate the YTD gain percentages for each stock.
- Plot the YTD gain percentages over time.
- Save the figure to a file named
ytd_stock_gains.png
.
Here's an example of how you might implement this using Python with libraries like yfinance
, pandas
, and matplotlib
.
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf
from datetime import datetime
def calculate_ytd_gains(stock_data):
"""Calculate the year-to-date gain percentage for the stock data."""
start_price = stock_data.iloc[0]
ytd_gains = (stock_data - start_price) / start_price * 100
return ytd_gains
def plot_ytd_gains(stock_symbols):
"""Plot the YTD gain percentages for the given stock symbols."""
# Initialize a figure and axis for plotting
plt.figure(figsize=(12, 6))
# Retrieve today's date
today = datetime.now().strftime('%Y-%m-%d')
# Loop through each stock symbol
for symbol in stock_symbols:
# Download historical data
stock_data = yf.download(symbol, start='2023-01-01', end=today)['Close']
# Calculate the YTD gain percentages
ytd_gains = calculate_ytd_gains(stock_data)
# Plot the YTD gains
plt.plot(ytd_gains.index, ytd_gains, label=symbol)
# Customize the plot
plt.title('YTD Stock Gain % for NVDA, MSFT, AAPL, and GOOG')
plt.xlabel('Day')
plt.ylabel('YTD Gain %')
plt.legend()
plt.grid()
# Save the plot to a file
plt.savefig('ytd_stock_gains.png')
plt.close()
# List of stock symbols to plot
stocks = ['NVDA', 'MSFT', 'AAPL', 'GOOG']
# Generate the plot
plot_ytd_gains(stocks)
Note:
- Ensure you have the
yfinance
module installed. You can install it usingpip install yfinance
. - The script downloads data from Yahoo Finance starting from January 1, 2023, up to the current day.
- The plot is saved to a file named
ytd_stock_gains.png
which will be in the same directory as your script. - The
calculate_ytd_gains
function computes the percentage gain based on the closing prices retrieved for each stock ticker.
This script should produce a line plot showing the YTD stock gain percentages for NVDA, MSFT, AAPL, and GOOG. Adjust the timeframe and symbols as needed for your specific use case.
Code Executor
exitcode: 0 (execution succeeded)
Code output:
[100%**] 1 of 1 completed
[100%**] 1 of 1 completed
[100%**] 1 of 1 completed
[100%**] 1 of 1 completed
YF.download() has changed argument auto_adjust default to True
Code Writer
It looks like your script executed successfully, and it downloaded the stock data as expected. The output indicates that yf.download()
completed without any errors for each of the stock symbols (NVDA, MSFT, AAPL, GOOG). The message you're seeing (YF.download() has changed argument auto_adjust default to True
) is just a note about a recent change in the yfinance
library, indicating that the default behavior now adjusts prices for corporate actions like splits and dividends automatically.
Here's a quick explanation of what's going on:
Data Download Completion: The stock data for each ticker successfully downloaded, as indicated by the progress bars reaching 100%.
Message on
auto_adjust
: Theyfinance
library has a parameterauto_adjust
that ensures that the downloaded data considers stock splits, dividends, etc. This note is informing you that the default setting is nowTrue
, which is generally what you want for accurate gain calculations.File Output: The plot of YTD stock gains should be saved as
ytd_stock_gains.png
in the directory from which your script was run.
If you haven't observed the resultant plot image, you might want to check your current working directory to view ytd_stock_gains.png
. If you would like further customization or have additional questions, feel free to ask!
Code Executor
TERMINATE