ChatGPT is a chat-based AI provided by OpenAI, known for its programming skills necessary for EA development.
This time, I would like to create a machine learning model capable of predicting FX prices. I will specifically try deep learning, a subset of machine learning.
In this experiment, I will:
- Request ChatGPT to create a machine learning model’s code
- Pass the results of executing the code to ChatGPT for improvement
- Pass the results of executing the improved code again to ChatGPT for further improvement
The goal is to test if ChatGPT can continuously validate and improve the model by iterating through this process.
- What is Deep Learning?
- Key Points for Deep Learning
- Writing Code in Python
- Specifying Input Data
- Results from the provided model
- Analysis of the results
- Improving the Model
- Analyzing the Results After Model Improvement
- Improving the Model Further
- Analysis of Results After Further Improvements
- Summary of the Process So Far
- Conclusion
What is Deep Learning?
What does it mean to use deep learning for FX price prediction? ChatGPT is incredibly useful for summarizing such concepts.
FX price prediction using deep learning is a technique that aims to predict future currency prices based on various information such as past exchange rate data, economic indicators, and news. Deep learning utilizes multi-layer neural networks to extract complex features and patterns from data to build predictive models.
For FX price prediction, deep learning models such as the following are commonly used:
- LSTM (Long Short-Term Memory): A type of recurrent neural network (RNN) suitable for time series data, capable of retaining past information while learning new information.
- CNN (Convolutional Neural Network): Originally used for image recognition, this type of neural network can also be applied to time series data to extract local features.
- GRU (Gated Recurrent Unit): Like LSTM, it is a type of recurrent neural network suitable for time series data learning. Compared to LSTM, it has fewer parameters and lower computational load.
Using these models, future currency prices can be predicted by inputting past exchange rate data and other economic indicators. However, the market is influenced by many factors, and there are elements, such as sudden events and political factors, that cannot be fully incorporated into the model. Therefore, there are limitations to FX price predictions using deep learning models, and absolute predictions are difficult.
As with EA, it is always emphasized that making a perfect prediction is difficult.
Key Points for Deep Learning
ChatGPT is based on GPT-4, and compared to GPT-3.5, it seems to provide additional information more readily with just a few questions.
The conversation with ChatGPT continues.
When using deep learning for FX price prediction, it is important to understand its limitations, avoid excessive expectations for the prediction results, and properly manage risk. While adjusting the model’s parameters and structure can improve prediction accuracy, it is fundamentally impossible to eliminate market uncertainty. Nevertheless, deep learning-based FX price prediction can be effectively utilized in combination with other prediction methods. Here are some points to consider when using deep learning:
- Data preprocessing: Perform preprocessing, such as normalization or standardization, on exchange rate data and economic indicators to convert them into a form that is easier for the model to learn.
- Feature selection: Select important features and exclude noisy, unnecessary features to improve the learning efficiency of the model.
- Hyperparameter tuning: Adjust hyperparameters such as the learning rate, batch size, and number of epochs to improve the model’s prediction accuracy.
- Ensemble learning: Combine multiple models and methods to capitalize on the strengths of each model and potentially improve prediction accuracy.
- Preventing overfitting: Properly control the complexity of the model and introduce regularization or dropout to prevent overfitting and improve prediction accuracy for unknown data.
In conclusion, deep learning-based FX price prediction should be considered a supplementary tool, and perfect predictions should not be expected. Using deep learning as part of an investment strategy while properly managing risk and funds is a more important point. Understanding market uncertainty and making cautious judgments while utilizing deep learning models is desirable.
Simply repeating similar questions can give you a sense of learning, as if you were reading a book called “Deep Learning for Beginners.” Perhaps ChatGPT learning will become more common in the future.
Writing Code in Python
To predict forex prices using deep learning, we will write Python code following the steps below. In this example, we will use the LSTM model in Keras.
- Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from keras.optimizers import Adam
- Load and preprocess the data
# Load the data
data = pd.read_csv("your_data_file.csv")
Select necessary columns (e.g., USD/JPY Close price)
data = data['USD_JPY_Close']
Normalize the data
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(np.array(data).reshape(-1, 1))
Split the data into training and testing sets
train_size = int(len(data) * 0.8)
train_data = data[:train_size]
test_data = data[train_size:]
- Create the dataset
def create_dataset(data, look_back=1):
x_data, y_data = [], []
for i in range(len(data) - look_back - 1):
x_data.append(data[i:(i + look_back), 0])
y_data.append(data[i + look_back, 0])
return np.array(x_data), np.array(y_data)
look_back = 60
x_train, y_train = create_dataset(train_data, look_back)
x_test, y_test = create_dataset(test_data, look_back)
Convert to the input format for LSTM
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
- Define and train the model
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(look_back, 1)))
model.add(Dropout(0.2))
model.add(LSTM(50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(50))
model.add(Dense(1))
Compile the model
model.compile(optimizer=Adam(lr=0.001), loss='mean_squared_error')
Train the model
history = model.fit(x_train, y_train, epochs=100, batch_size=64, validation_data=(x_test, y_test))
- Prediction and evaluation
train_predict = model.predict(x_train)
test_predict = model.predict(x_test)
Inverse transform the normalization
train_predict = scaler.inverse_transform(train_predict)
y_train = scaler.inverse_transform([y_train])
test_predict = scaler.inverse_transform(test_predict)
y_test = scaler.inverse_transform([y_test])
Evaluation (RMSE)
train_rmse = np.sqrt(mean_squared_error(y_train[0], train_predict[:, 0]))
test_rmse = np.sqrt(mean_squared_error(y_test[0], test_predict[:, 0]))
print("Training data RMSE: ", train_rmse)
print("Testing data RMSE: ", test_rmse)
- Visualize the results
plt.figure(figsize=(14, 5))
plt.plot(scaler.inverse_transform(data), label='Actual price')
train_predicted = np.empty_like(data)
train_predicted[:, :] = np.nan
train_predicted[look_back:len(train_predict) + look_back, :] = train_predict
plt.plot(train_predicted, label='Predicted price on training data')
test_predicted = np.empty_like(data)
test_predicted[:, :] = np.nan
test_predicted[len(train_predict) + (look_back * 2) + 1:len(data) - 1, :] = test_predict
plt.plot(test_predicted, label='Predicted price on testing data')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
plt.show()
By using the code above, you can visualize the predicted results for training and test data. This model is just an example, and its accuracy may change depending on the adjustment of hyperparameters and model structure. Additionally, trying other deep learning models or methods may help improve accuracy further. However, it’s important to keep in mind that absolute predictions are difficult for any model, as market fluctuations are influenced by many factors.
“I mentioned that input can be prepared for multiple currency pairs with timeframes ranging from 1-minute to 4-hour bars.” However, it seems that you can simply input the closing prices of the four price values (open, high, low, close) without considering that particular aspect.
Specifying Input Data
In the code that ChatGPT provided, you need to have a file named “your_data_file.csv” with a column called ‘USD_JPY_Close’.
To accommodate that, modify the relevant section as follows:
Before modification
# Loading data
data = pd.read_csv("your_data_file.csv")
# Selecting the necessary column (e.g., close price of USD/JPY)
data = data['USD_JPY_Close']
After modification
# Loading the data
data = pd.read_pickle('/content/drive/My Drive/backtest/bar/df_H4.pkl')
Selecting necessary columns
data = data['Close']
Prepared input data
This time, we used 4-hour data of GOLD from XM (June 2013 to March 2023) as the input data.

Results from the provided model
Running the provided code as-is yielded the following results.
RMSE
Training data RMSE: 14.73236547363949
Test data RMSE: 22.703382167670906
Result graph

Analysis of the results
Training data RMSE: 12.68107382309471
Test data RMSE: 21.30095528606679
As the results show, the RMSE (Root Mean Square Error) for the training data is approximately 12.68, while the RMSE for the test data is about 21.30. The RMSE represents the magnitude of the prediction error, with smaller values indicating higher prediction accuracy.From these results, it is clear that while the model can make relatively accurate predictions for the training data, its prediction accuracy for the test data is lower. This could be due to the model overfitting the training data.
To prevent overfitting, the following measures can be considered:
- Reduce the complexity of the model: By decreasing the number of layers or units, you can simplify the model and potentially reduce overfitting.
- Introduce regularization: Using L1 or L2 regularization, you can apply constraints to the model’s parameters to prevent them from taking on large values.
- Adjust dropout: By changing the dropout rate, you can expect the model to better control overfitting.
- Increase the amount of data: If there is insufficient data, overfitting is more likely to occur. If possible, preparing more data and training the model with it can help prevent overfitting.
Additionally, tuning hyperparameters and trying other models or techniques may improve prediction accuracy. However, it is important to keep in mind that making absolute predictions is difficult for any model.
Improving the Model
To prevent overfitting, the model has been improved as follows. Here, we incorporate methods for reducing model complexity and introducing regularization.
- Add a library for regularization to the required library imports
from keras.regularizers import l1_l2
- Adjust the number of layers and units, and introduce regularization in the model definition and training
model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=(look_back, 1), kernel_regularizer=l1_l2(l1=1e-5, l2=1e-4)))
model.add(Dropout(0.2))
model.add(LSTM(32, return_sequences=False, kernel_regularizer=l1_l2(l1=1e-5, l2=1e-4)))
model.add(Dropout(0.2))
model.add(Dense(1))
Compile the model
model.compile(optimizer=Adam(lr=0.001), loss='mean_squared_error')
Train the model
history = model.fit(x_train, y_train, epochs=100, batch_size=64, validation_data=(x_test, y_test))
In the code above, we reduced the number of units in the LSTM layer from 50 to 32 to limit the complexity of the model and introduced regularization. The number of layers in the model has also been reduced to two.
Try the improved code, calculate the RMSE of the training and test data again, and check if overfitting has been suppressed. However, keep in mind that it is difficult to make absolute predictions with any model, as market fluctuations are influenced by many factors.
Analyzing the Results After Model Improvement
Training data RMSE: 14.73236547363949
Test data RMSE: 22.703382167670906
From the results of the improved code, the RMSE of the training data is about 14.73, and the RMSE of the test data is about 22.70. These results do not clearly indicate whether overfitting has been improved. The difference between the RMSEs of the training and test data has decreased slightly, but there is still room for improvement.For further improvements, consider the following methods:
- Increase the dropout ratio: Increasing the dropout from 0.2 to 0.4 or 0.5 can help suppress overfitting.
- Change the data window size: By adjusting the value of
look_back, you can change the period of data the model learns from. An appropriate window size may improve the model’s generalization performance. - Change the way training and validation data are split: For time-series data, using techniques such as time-series cross-validation to split the data can improve the model’s generalization performance.
- Try other models or algorithms: You can try models using GRU or CNN instead of LSTM or try algorithms like ensemble learning to potentially improve accuracy.
To find the optimal model, it’s important to conduct repeated experiments while trying these methods. However, keep in mind that it is difficult to make absolute predictions with any model, as market fluctuations are influenced by many factors.
Improving the Model Further
Furthermore, ChatGPT will propose improvements and have ChatGPT itself enhance the code. This is one of ChatGPT’s strengths. It can analyze and propose improvements, then execute those improvements. This process can be repeated indefinitely to increase the model’s accuracy.
Here’s the code that incorporates the proposed improvements. We’ve increased the dropout rate, adjusted the data window size, and introduced time series cross-validation. Additionally, we’ve used a GRU as the model.
- Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import TimeSeriesSplit
from keras.models import Sequential
from keras.layers import Dense, Dropout, GRU
from keras.optimizers import Adam
from keras.regularizers import l1_l2
- Data loading and preprocessing
# Load the data
data = # Write code here to load the data
Normalize the data
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(data)
Function for splitting the data
def create_dataset(dataset, look_back=1):
x, y = [], []
for i in range(len(dataset) - look_back - 1):
x.append(dataset[i:(i + look_back), 0])
y.append(dataset[i + look_back, 0])
return np.array(x), np.array(y)
Set the window size
look_back = 60
Configure time series cross-validation
tscv = TimeSeriesSplit(n_splits=5)
- Model definition, training, and evaluation
# Training and evaluating the model using time series cross-validation
for train_index, test_index in tscv.split(data):
x_train, x_test = data[train_index], data[test_index]
x_train, y_train = create_dataset(x_train, look_back)
x_test, y_test = create_dataset(x_test, look_back)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
model = Sequential()
model.add(GRU(32, return_sequences=True, input_shape=(look_back, 1), kernel_regularizer=l1_l2(l1=1e-5, l2=1e-4)))
model.add(Dropout(0.4))
model.add(GRU(32, return_sequences=False, kernel_regularizer=l1_l2(l1=1e-5, l2=1e-4)))
model.add(Dropout(0.4))
model.add(Dense(1))
model.compile(optimizer=Adam(lr=0.001), loss='mean_squared_error')
history = model.fit(x_train, y_train, epochs=100, batch_size=64, validation_data=(x_test, y_test))
train_predict = model.predict(x_train)
test_predict = model.predict(x_test)
train_rmse = np.sqrt(mean_squared_error(y_train, train_predict))
test_rmse = np.sqrt(mean_squared_error(y_test, test_predict))
print(f"Training data RMSE: {train_rmse}")
print(f"Test data RMSE: {test_rmse}")
In the above code, we used a GRU with a dropout rate of 0.4 and a window size of 60. Furthermore, we used time series cross-validation to split the data and train and evaluate the model. Try this code to check the RMSE of the training and test data and verify whether the improvements are effective. However, it is important to keep in mind that absolute predictions are difficult for any model as market fluctuations are influenced by many factors.
Making a small modification
Since the code for the “inverse transformation of normalization” that existed in the initial code has been removed, I would like to propose a small modification.
Apologies for that. I have added the inverse transformation of normalization part. With the following code, you can perform the inverse transformation of normalization and calculate the RMSE.
# Inverse transformation of normalization
train_predict = scaler.inverse_transform(train_predict)
y_train = scaler.inverse_transform([y_train])
test_predict = scaler.inverse_transform(test_predict)
y_test = scaler.inverse_transform([y_test])
Calculation of RMSE
train_rmse = np.sqrt(mean_squared_error(y_train[0], train_predict[:, 0]))
test_rmse = np.sqrt(mean_squared_error(y_test[0], test_predict[:, 0]))
print(f"Training data RMSE: {train_rmse}")
print(f"Test data RMSE: {test_rmse}")
Analysis of Results After Further Improvements
Training data RMSE: 13.70486076472181
Test data RMSE: 20.398845630998892
From the results, the RMSE of the training data is approximately 13.70, and the RMSE of the test data is approximately 20.40. When compared to the results before the improvements were applied, the difference between the RMSEs of the training and test data has slightly decreased. This may indicate that overfitting has been somewhat mitigated.However, it is not possible to say that overfitting has been completely resolved. Further improvements are necessary. Additionally, predicting FX prices is a very difficult task, with many factors influencing the outcome. It is important to keep in mind that absolute predictions are challenging for any model.
Possible further improvements include:
- Hyperparameter tuning: By adjusting various hyperparameters such as the learning rate, batch size, number of epochs, and regularization coefficient, the performance of the model may be improved.
- Feature engineering: Adding more technical indicators and features based on fundamental analysis will enable the model to learn more information.
- Ensemble learning: Combining multiple models can compensate for the weaknesses of each model and improve overall performance.
To find the optimal model, it is important to conduct iterative experiments while trying these methods.
Summary of the Process So Far
We have gone through the following steps with ChatGPT. As for us, we have only been executing the returned code, presenting the results, and requesting code improvements, which doesn’t require much effort. The fact that all this can be accomplished with just a few instructions is the beauty of ChatGPT.
- Requested FX price prediction using deep learning
- Created a code using an LSTM model with Keras
- Executed the provided code, presented the obtained RMSE results, and requested improvements
- Proposed improvements by reducing the number of LSTM layer units from 50 to 32, introducing regularization, and reducing the model to two layers
- Executed the improved code, presented the obtained RMSE results, and requested further improvements
- Proposed improvements by increasing the dropout rate, adjusting the data window size, introducing time series cross-validation, and using a GRU model
- Executed the improved code, presented the obtained RMSE results, and requested verification of whether the improvements were effective
So, how was it?
With just a little instruction, we received sample codes for learning with LSTM and GRU models right away.
Until now, many people needed to go through a process of searching for websites with sample codes of machine learning models, and even if they found them, they would encounter errors if their environment was slightly different. They then had to search Google again to investigate the cause of the error.
With ChatGPT, you can easily get sample codes, and if there are any errors, you can inform the AI of the error content, and it will fix the code for you. Not only does it respond to improvement instructions, but it also proposes its own improvement measures, which greatly increases learning efficiency.
In the next article, we plan to further test the improvement measures proposed by ChatGPT at the end of this process.
Conclusion
Working with ChatGPT has proven to be a highly efficient way to develop machine learning models. By providing sample codes quickly and adjusting them according to our needs, we have been able to focus on improving the models and their performance.
In addition, the AI’s ability to suggest improvements and fix errors has made the learning process much smoother than before. This new approach to developing models with the help of AI has the potential to save time and effort, allowing us to focus on other important aspects of the project.
Stay tuned for the next article, where we will delve deeper into the improvement measures suggested by ChatGPT and explore their effectiveness in enhancing our FX price prediction model.


Comments