Exploring Trend Detection with 1-Minute Data in the First 15 Minutes of Each Day (Part 3)
In this third part of our trend detection series, we will explore the realm of neural networks as an advanced technique for identifying trends in financial markets.

Neural networks are a powerful class of machine learning models inspired by the human brain’s neural architecture. We will explain how neural networks work and apply them in conjunction with the baseline methods defined in the first part.
Understanding Neural Networks
Neural networks consist of interconnected layers of artificial neurons, commonly known as nodes or units. These nodes are organized into input, hidden, and output layers. Each node in a layer is connected to nodes in adjacent layers through weighted connections. The process of training a neural network involves iteratively adjusting these weights to learn patterns and relationships in the data.
The core components of a neural network are as follows:
- Input Layer: The input layer receives the features of the data as input.
- Hidden Layers: Hidden layers perform feature transformations by applying weights to the input data and passing the result through an activation function. Deep neural networks have multiple hidden layers.
- Output Layer: The output layer provides the final predictions based on the transformed features from the hidden layers.
The training process involves forward propagation, where data flows from the input layer through the hidden layers to the output layer. The output is compared to the actual target values, and an error is calculated. Backpropagation is then used to update the weights based on the error, allowing the network to gradually learn the underlying patterns in the data.
Before I continue sharing all the information, if you enjoy reading my articles, please hit the follow button — Diego Degese
Implementing trend detection with a Neural Network
In order to ensure consistent data processing, we will employ the same baseline methods and dataset as used in Part 1. By integrating the strength of neural networks in learning complex patterns, we aim to improve the accuracy and robustness of trend detection.
Now, let’s explore the code implementation of trend detection using a neural network.
Importing Required Libraries
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
from keras.utils import set_random_seedWe begin by importing the required components from the Keras library, which is a popular deep-learning framework.
Defining the method to predict the values
def get_predicted_values(model, features):
pred_y = model.predict(features)
pred_y = pred_y.flatten()
pred_y = np.where(pred_y > 0.5, 1, 0)
return pred_yIn this step, we define a method that receives the model and the features and returns all the predicted values.
Training the Neural Network
set_random_seed(42) # Allow reproducibility
early_stopping = EarlyStopping(patience=2) # Stop if the model does not improve
model = Sequential()
model.add(Dense(16, input_dim=15, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_x, train_y, epochs=100, batch_size=32, validation_data=(val_x, val_y), callbacks=[early_stopping])In this step, we create a neural network model using Keras. We specify two dense layers in the model. The first hidden layer has 16 nodes with the ReLU (Rectified Linear Unit) activation function, which introduces non-linearity to the model. The output layer consists of a single node with the sigmoid activation function, which is suitable for binary classification tasks.
We compile the model using the Adam optimizer and binary cross-entropy loss, which are commonly used for binary classification. We also track the accuracy metric during training.
We then train the model using the fit() method on the training data with early stopping as a callback to prevent overfitting.
Evaluating the Neural Network Model and Saving Results
# Predict and save train values
pred_y = get_predicted_values(model, train_x)
show_result(train_y, pred_y)
save_result(train_y, pred_y, 'nn.train.csv.gz')
# Predict and save validation values
pred_y = get_predicted_values(model, val_x)
show_result(val_y, pred_y)
save_result(val_y, pred_y, 'nn.val.csv.gz')
# Predict, show and save test values
pred_y = get_predicted_values(model, test_x)
show_result(test_y, pred_y)
save_result(test_y, pred_y, 'nn.test.csv.gz')Finally, we evaluate the neural network model’s performance on the training, validation, and test datasets. We use the get_predicted_values() function to obtain the model's predictions for each dataset. We then show the results using the show_result() function and save them in separate CSV files for later analysis.
Neural Network Results
********************* RESULT TEST **********************
* Confusion Matrix (Top: Predicted - Left: Real)
[[130 119]
[ 81 199]]
* Classification Report
precision recall f1-score support
0 0.62 0.52 0.57 249
1 0.63 0.71 0.67 280
accuracy 0.62 529
macro avg 0.62 0.62 0.62 529
weighted avg 0.62 0.62 0.62 529For the trend category ‘DOWN’ (class 0):
- The precision is approximately 62%, indicating that out of all the instances classified as ‘DOWN’, 62% of them are true negative predictions.
- The recall is approximately 52%, indicating that the model correctly captures 52% of all actual ‘DOWN’ trends.
- The F1-Score for the ‘DOWN’ category is around 57%, representing the harmonic mean of precision and recall.
For the trend category ‘UP’ (class 1):
- The precision is approximately 63%, indicating that out of all the instances classified as ‘UP’, 63% of them are true positive predictions.
- The recall is approximately 71%, indicating that the model correctly captures 71% of all actual ‘UP’ trends.
- The F1-Score for the ‘UP’ category is around 67%, representing the harmonic mean of precision and recall.
The overall accuracy of the Neural Network Model is approximately 62%, which means that it correctly predicted the trend (either ‘UP’ or ‘DOWN’) in about 62% of the test dataset instances.
The Neural Network Model shows an improvement in predicting both upward and downward trends compared to the previous models. It has higher recall and F1-Score for the ‘UP’ category, indicating its effectiveness in identifying profitable upward trends.
Conclusion
In this third part, we explored neural networks as an advanced technique for trend detection in financial markets.
The neural network model, with its multiple layers and activation functions, can effectively learn complex patterns and relationships in the data. The iterative training process of forward and backward propagation enables the model to gradually improve its predictions.
In a future part of our series, we will compare the performance of different models against the basic model using the F1-Score. We will utilize the F1-Score to evaluate the models and strive to identify the most accurate and reliable approach for detecting trends.
If you enjoy my work, please support me on Medium by becoming a member through my referral link, and consider giving it a clap as a small gesture of motivation. Thank you!
Download the full source code and the colab notebook of this article from here
Twitter / X: https://twitter.com/diegodegese LinkedIn: https://www.linkedin.com/in/ddegese Github: https://github.com/crapher
Disclaimer: Investing in the stock market involves risk and may not be suitable for all investors. The information provided in this article is for educational purposes only and should not be construed as investment advice or a recommendation to buy or sell any particular security. Always do your own research and consult with a licensed financial advisor before making any investment decisions. Past performance is not indicative of future results.
A Message from InsiderFinance

Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the InsiderFinance Wire
- 📚 Take our FREE Masterclass
- 📈 Discover Powerful Trading Tools






