Close Navigation
Learn more about IBKR accounts
Convolutional Neural Networks – Part III

Convolutional Neural Networks – Part III

Posted October 13, 2023
Chainika Thakar
QuantInsti

Read Part I and Part II to learn about convolutional neural networks.

Steps to use convolutional neural networks in trading with Python

We will now see a simple model with the CNN architecture for the image with the candlestick patterns. The model will be trained for 10 epochs. Here, one Epoch is equivalent to one cycle for training a machine learning model.

The number of epochs keeps increasing until the validation error reduces.

The Conv2D layers define the convolutional layers with ReLU activation, while MaxPooling2D is used for regularisation. Also, the Dense layers are used for classification.

Hence, the final outcome will help you find out the performance of the model.

Step 1: Importing necessary libraries

We will first of all import TensorFlow and will use tf.keras.

# Importing libraries
import numpy as np
import tensorflow as tf

Import_lib.py hosted with ❤ by GitHub

Step 2: Generate random train and test data for demonstration

# We create image data for train and test purposes with the following inputs
num_train_samples = 1000
num_test_samples = 200
image_width = 128
image_height = 128
channels = 1 # Grayscale image (single channel)
num_classes = 2 # Binary classification (two classes)

# Generate random training and test data for demonstration
X_train = np.random.random((num_train_samples, image_width, image_height, channels))
y_train = np.random.randint(low=0, high=num_classes, size=num_train_samples)
X_test = np.random.random((num_test_samples, image_width, image_height, channels))

Random_train_test.py hosted with ❤ by GitHub

Step 3: Define the CNN model

Now, we will define the CNN model that will help with prediction in trading.

# Define the CNN model
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(image_width, image_height, channels)),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(num_classes, activation='softmax')
])

Define_CNN.py hosted with ❤ by GitHub

The model is defined using the Sequential API, and the layers are added sequentially. The architecture consists of several Conv2D layers with ReLU activation, followed by MaxPooling2D layers to reduce spatial dimensions. The final layers include a Flatten layer to flatten the output, fully connected Dense layers, and an output layer with softmax activation for classification.

Step 4: Normalise the training and test data

# Normalise the training and test images if necessary
X_train = X_train / 255.0
X_test = X_test / 255.0

Normalise.py hosted with ❤ by GitHub

Step 5: Compile and train the model

Finally, the model is compiled, trained and made to make predictions on the new images.

# Compile and train the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.fit(X_train, y_train, epochs=10, batch_size=32)

# Use the trained model to make predictions on new images
predictions = model.predict(X_test)

model.fit(X_train, y_train, epochs=10, batch_size=32)


# Use the trained model to make predictions on new images
predictions = model.predict(X_test)

Compile_train.py hosted with ❤ by GitHub

The model is compiled with the Adam optimizer, sparse categorical cross-entropy loss function, and accuracy as the evaluation metric.

Output:

Epoch 1/10 32/32 [==============================] – 8s 223ms/step – loss: 2.3030 – accuracy: 0.0990

Epoch 2/10 32/32 [==============================] – 10s 330ms/step – loss: 2.2998 – accuracy: 0.1200

Epoch 3/10 32/32 [==============================] – 5s 172ms/step – loss: 2.3015 – accuracy: 0.1200

Epoch 4/10 32/32 [==============================] – 6s 201ms/step – loss: 2.2994 – accuracy: 0.1200

Epoch 5/10 32/32 [==============================] – 6s 183ms/step – loss: 2.2996 – accuracy: 0.1200

Epoch 6/10 32/32 [==============================] – 5s 170ms/step – loss: 2.2981 – accuracy: 0.1200

Epoch 7/10 32/32 [==============================] – 7s 210ms/step – loss: 2.2987 – accuracy: 0.1200

Epoch 8/10 32/32 [==============================] – 5s 168ms/step – loss: 2.2981 – accuracy: 0.1200

Epoch 9/10 32/32 [==============================] – 7s 216ms/step – loss: 2.2993 – accuracy: 0.1200 Epoch 10/10 32/32 [==============================] – 5s 167ms/step – loss: 2.2975 – accuracy: 0.1200 7/7 [==============================] – 0s 43ms/step

The above output shows the final loss and accuracy values on the test set.

In this specific output, the model did not achieve a very high accuracy on both the training and test sets. Hence, the output is not indicating a good performance.

Also, the final outcome shows that the loss values are not decreasing over the epochs, indicating that the model is not learning and improving its predictions.

For making the loss values decrease over the epochs and to make the model achieve a high accuracy rate, you need to input the model with more number of epochs and you can change the parameters accordingly.

In the similar manner, you can fetch the image data (candlestick pattern, line chart) for a stock (for example, AAPL, TSLA, GOOGL etc.) and train the model on a certain number of epochs.

Python codes for trading with CNN

For trading, you will need the following lines of code below to give you the result. In this case, also the result will be the computation of final loss and accuracy.

# Import libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Conv1D, MaxPooling1D, Flatten, Dense

# Load and preprocess the data
data = pd.read_csv('trading_data.csv')

# Perform data preprocessing steps as per your requirements
# Split the data into training and testing sets
train_data = data.loc[data['date'] < date_to_split]
test_data = data.loc[data['date'] >= date_to_split]

# Define the input and output variables
x_train = train_data[['feature1', 'feature2', 'feature3']].values
y_train = train_data['target'].values
x_test = test_data[['feature1', 'feature2', 'feature3']].values
y_test = test_data['target'].values

Trading_with_CNN.py hosted with ❤ by GitHub

And, we reach the end of this blog! You can now use the convolutional neural networks on your own for training the CNN model.

Originally posted on QuantInsti Blog. Visit their website for additional insights on this topic.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.