How To Learn Algorithmic Trading Using ChatGPT - by Hanane D. Medium
How To Learn Algorithmic Trading Using ChatGPT - by Hanane D. Medium
ChatGPT
Hanane D. · Follow
8 min read · Feb 12
2- Channel Breakout
Introduction
As you may know now, ChatGPT is a language generation model developed by
OpenAI. you can ask it anything you want. So why not learn algorithmic trading
with ChatGPT?
At the beginning, I wanted to just play with it and see what answers I would get. I
didn’t believe in it, but I can admit I was really impressed.
So let me show you how you could use it to learn different types of algorithmic
trading (By practicing using Python code examples).
Take into account the warning raised by ChatGPT on many code python
examples it gives:
Keep in mind:
In some cases, you will have broader answers, but it’s still a very good starting
point to learn a lot of things in this area.
All the practical examples in Python, in this tutorial, use Close prices. You can
perform the same calculation using Intraday prices (bin =10 secs, 1min, 5min…)
Regenerate the answers, so you can get another complementary point of view or
another implementation of Python code.
Ask the same question differently, by modifying the wording. For example:
Strategy instead of Algorithm
Check elsewhere, because sometimes the answers are not exactly correct (not
completely wrong).
If you tell you do not agree with the answer, it will apologize and please you, but
be careful you need to check with another source.
Even with those caveats, you’ll find this to be a very powerful tool that you can use
to learn a ton of stuff. Let’s start!!
Using this answer, you can dig into more details. For example asking what types of
trading used by each type of investors: Hedge Funds, Institutional
investors…
Now, we need to have a general idea of the different types of algorithmic Trading.
This is a very broad answer. Some of the algorithms are very sophisticated, and
some of them are more simple.
For example, in Dark pools, some participants in the market are aiming to post
their orders there because they are seeking to trade very large orders. They do not
want to be discovered by the market to not impact the prices. In the dark pools, the
order books are hidden, contrary to the primary or the secondary markets. So it’s
not really a sophisticated algorithm which is trying to find some signal, it’s just a
simple one seeking for liquidity.
You can ask for each of these categories: Definition, different types of algorithms,
type of investors using these algorithms…
Channel Breakout
I didn’t use exactly this code, because the link wasn’t working for me. But I was
inspired by this answer to find the right solution.
import pandas as pd
import datetime as dt
def download_stock_data(ticker,timestamp_start,timestamp_end):
url=f"https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={
=1d&events=history&includeAdjustedClose=true"
df = pd.read_csv(url)
return df
datetime_start=dt.datetime(2022, 2, 8, 7, 35, 51)
datetime_end=dt.datetime(2023, 2, 8, 7, 35, 51)
# Convert to timestamp:
timestamp_start=int(datetime_start.timestamp())
timestamp_end=int(datetime_end.timestamp())
df = download_stock_data("AAPL",timestamp_start,timestamp_end)
df = df.set_index('Date')
df.head()
Python Code
I asked for a Python code example. ChatGPT also gives an explanation of the code it
generates. You can copy the code and use it (Sometimes there are some errors):
Let’s practice
I slightly modified the code to get the information I need: I modified the short and
long window of the moving average:
import pandas as pd
import numpy as np
short_ma = df["Close"].rolling(window=short_window).mean()
long_ma = df["Close"].rolling(window=long_window).mean()
You can modify the parameters to fit the study you are working on.
Channel Breakout
Python Code
Notice that as I asked for a dataset, it gives me Apple stock prices. ChatGPT
suggests it directly in the Channel Breakout Python code.
Let’s practice
I modified the code (some mistakes on it).
It calculates the upper and lower band of the moving average of the stock. When the
prices cross the upper band we sell, and we buy if the prices cross the lower band.
import pandas as pd
import matplotlib.pyplot as plt
buy_signal = None
sell_signal = None
hold_signal=None
for i in range(window, len(df)):
if df.iloc[i]['Close'] > upper_band[i]:
sell_signal = i
elif df.iloc[i]['Close'] < lower_band[i]:
buy_signal = i
else:
hold_signal=i
return buy_signal, sell_signal,hold_signal, rolling_mean, upper_band, lowe
Formula
Python Code example:
ChatGPT gives an example of the implementation of MACD in Python.
But still, this is an example of implementation, you can customize the formulas and
find the best computation fitting your situation.
I asked for the mistake:
When the MACD line crosses above the signal line, it triggered a buy signal
When the MACD line crosses below the signal line, it triggered a sell signal
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df[['Close']].plot(label='Prices',figsize=(10,6))
plt.plot(macd, label='MACD Line', linestyle='--')
plt.plot(signal, label='Signal Line', linestyle=':')
plt.legend()
plt.title("MACD")
plt.show()
df.query("Date>='2022-11-23'")[['MACD_line','Signal_line']].plot(figsize=(10,6
plt.legend()
plt.show()
Summary
In this article, you learnt: