Programming with ChatGPT: Trying to get an AI to create an animated profit and loss graph using Python.

ChatGPT

ChatGPT is an artificial intelligence model that learns human text and generates new text based on it. We would like to proceed below in an interactive manner with ChatGPT, as it is interactive and can communicate in a chat format, and can even be programmed.

I would like to program on ChatGPT. Is it possible to create animation graphs in Python, for example?
Yes, ChatGPT includes knowledge about programming, so it is possible to create graphs in Python. By using a graphing library called matplotlib, you can easily create various types of graphs.

Let’s create an animated graph using Python this time.

The objective of this occasion is…

We will use the following profit and loss graph from a certain EA’s backtest result as material.

This is a graph showing the profit and loss results (balance changes) for a period of three years from January 2020 to December 2022, assuming an initial capital of $10,000.

I would like to create a GIF data that gradually forms a line graph over time, similar to the backtest graphs in MT4 or MT5.

Challenge with more specific questions

I want to create a graph that runs on Python. The input data is “asset.csv”, with the index being a datetime format time. I would like to create a line graph of the transition of asset balances over time. Please use animation to create a moving graph.
To create an animated line graph in Python using a CSV file, you can use libraries like Pandas, Matplotlib, and FuncAnimation from Matplotlib’s animation module. Here’s a code snippet to help you get started:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# Read the CSV file
data = pd.read_csv('asset.csv', parse_dates=True, index_col='datetime')

# Set up the figure and axis
fig, ax = plt.subplots()
line, = ax.plot([], [])

# Set the labels and title
ax.set_xlabel('Time')
ax.set_ylabel('Asset Balance')
ax.set_title('Asset Balance over Time')

# Initialize the line data
def init():
line.set_data([], [])
return line,

# Update the line data
def update(frame):
x = data.index[:frame]
y = data['balance'].values[:frame]
line.set_data(x, y)
return line,

# Create the animation
ani = FuncAnimation(fig, update, frames=len(data), init_func=init, blit=True, interval=100)

# Display the animated plot
plt.show()

Make sure to replace ‘datetime’ and ‘balance’ with the appropriate column names in your CSV file. You can adjust the interval parameter in the FuncAnimation function to control the speed of the animation (the value is in milliseconds).

asset_data.csv

First of all, ChatGPT provided me with the foundation code, and even helped me prepare the asset_data.csv, so I will follow the instructions and prepare the data accordingly.

For this time, I have prepared the following numerical data for the graph mentioned above.

After placing this in the same folder and running it, it worked without any issues.

The graph was a bit small, so…

Can you make the graph size a bit larger? Please set the y-axis to display values in units of 1 million.
Sure, you can adjust the figure size and y-axis display by modifying the code as follows:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.ticker import FuncFormatter

# Read the CSV file
data = pd.read_csv('asset.csv', parse_dates=True, index_col='datetime')

# Set up the figure and axis with a larger size
fig, ax = plt.subplots(figsize=(12, 6))
line, = ax.plot([], [])

# Function to format the y-axis values in units of 1 million
def millions_formatter(x, pos):
return f'{x / 1_000_000}M'

# Set the labels and title
ax.set_xlabel('Time')
ax.set_ylabel('Asset Balance (Millions)')
ax.set_title('Asset Balance over Time')

# Set the y-axis to display values in units of 1 million
ax.yaxis.set_major_formatter(FuncFormatter(millions_formatter))

# Initialize the line data
def init():
line.set_data([], [])
return line,

# Update the line data
def update(frame):
x = data.index[:frame]
y = data['balance'].values[:frame]
line.set_data(x, y)
return line,

# Create the animation
ani = FuncAnimation(fig, update, frames=len(data), init_func=init, blit=True, interval=100)

# Display the animated plot
plt.show()
This code sets the figure size to 12 inches by 6 inches using figsize=(12, 6) when creating the figure and axis. It also adds a FuncFormatter to format the y-axis values in units of 1 million. Make sure to replace ‘datetime’ and ‘balance’ with the appropriate column names in your CSV file.

I would like to save it as a GIF, so…

Can you make the y-axis display values in units of 100,000 instead? Also, instead of using plt.show(), can you save the animation as an “animation.gif” file?
Sure, you can modify the y-axis display to show values in units of 100,000 and save the animation as a GIF file. Here’s the updated code:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.ticker import FuncFormatter

# Read the CSV file
data = pd.read_csv('asset.csv', parse_dates=True, index_col='datetime')

# Set up the figure and axis with a larger size
fig, ax = plt.subplots(figsize=(12, 6))
line, = ax.plot([], [])

# Function to format the y-axis values in units of 100,000
def hundred_thousands_formatter(x, pos):
return f'{x / 100_000} x10^5'

# Set the labels and title
ax.set_xlabel('Time')
ax.set_ylabel('Asset Balance (x10^5)')
ax.set_title('Asset Balance over Time')

# Set the y-axis to display values in units of 100,000
ax.yaxis.set_major_formatter(FuncFormatter(hundred_thousands_formatter))

# Initialize the line data
def init():
line.set_data([], [])
return line,

# Update the line data
def update(frame):
x = data.index[:frame]
y = data['balance'].values[:frame]
line.set_data(x, y)
return line,

# Create the animation
ani = FuncAnimation(fig, update, frames=len(data), init_func=init, blit=True, interval=100)

# Save the animation as a GIF file
ani.save('animation.gif', writer='imagemagick', fps=10)

This code updates the hundred_thousands_formatter function to display the y-axis values in units of 100,000. It also replaces the plt.show() command with ani.save(), which saves the animation as a GIF file named “animation.gif”. The writer parameter is set to 'imagemagick' and fps (frames per second) is set to 10. You may need to install ImageMagick on your system for this to work correctly.

Make sure to replace ‘datetime’ and ‘balance’ with the appropriate column names in your CSV file.

Completed Animated Graph (GIF Data)

The animated graph we have completed is as follows. What do you think? It has sufficiently achieved the intended goal.

Summary

As we have seen above, even if you have no knowledge of programming, you can communicate what you want to do using only words, make detailed adjustments by providing specific instructions, and create the desired outcome. I demonstrated a concrete example of programming with ChatGPT through the interactions described above.

Finally, I’ll address the following question:

Thank you. Lastly, please provide an overview of the code you provided.

The code provided reads a CSV file named ‘asset.csv’ which contains time data in the ‘datetime’ column and asset balance data in the ‘balance’ column. The code then creates an animated line graph of the asset balance over time using the Matplotlib library.

The graph is customized with a larger size, and the y-axis is set to display values in units of 100,000. The animation is created using the FuncAnimation function from the Matplotlib animation module. Instead of displaying the animation using plt.show(), the code saves the animation as a GIF file named ‘animation.gif’ with a frame rate of 10 frames per second using the ImageMagick writer.

Comments

Copied title and URL