TradingView
Alien_Algorithms
Oct 13, 2023 11:16 PM

Machine Learning using Neural Networks | Educational 

Bitcoin Liquid IndexBrave New Coin

Description

The script provided is a comprehensive illustration of how to implement and execute a simplistic Neural Network (NN) on TradingView using PineScript.

It encompasses the entire workflow from data input, weight initialization, implicit neuron calculation, feedforward computation, backpropagation for weight adjustments, generating predictions, to visualizing the Mean Squared Error (MSE) Loss Curve for monitoring the training phase.

In the visual example above, you can see that the prediction is not aligned with the actual value. This is intentional for demonstrative purposes, and by incrementing the Epochs or Learning Rate, you will see these two values converge as the accuracy increases.

Hyperparameters:
Learning Rate, Epochs, and the choice between Simple Backpropagation and a verbose version are declared as script inputs, allowing users to tailor the training process.

Initialization:
Random initialization of weight matrices (w1, w2) is performed to ensure asymmetry, promoting effective gradient updates. A seed is added for reproducibility.

Utility Functions:
Functions for matrix randomization, sigmoid activation, MSE loss calculation, data normalization, and standardization are defined to streamline the computation process.

Neural Network Computation:
The feedforward function computes the hidden and output layer values given the input.
Two variants of the backpropagation function are provided for weight adjustment, with one offering a more verbose step-by-step computation of gradients.

A wrapper train_nn function iterates through epochs, performing feedforward, loss computation, and backpropagation in each epoch while logging and collecting loss values.

Training Invocation:
The input data is prepared by normalizing it to a value between 0 and 1 using the maximum standardized value, and the training process is invoked only on the last confirmed bar to preserve computational resources.

Output Forecasting and Visualization:
Post training, the NN's output (predicted price) is computed, standardized and visualized alongside the actual price on the chart.

The MSE loss between the predicted and actual prices is visualized, providing insight into the prediction accuracy.

Optionally, the MSE Loss Curve is plotted on the chart, illustrating the loss trajectory through epochs, assisting in understanding the training performance.

Customizable Visualization:
Various inputs control visualization aspects like Chart Scaling, Chart Horizontal Offset, and Chart Vertical Offset, allowing users to adapt the visualization to their preference.

-------------------------------------------------------

The following is this Neural Network structure, consisting of one hidden layer, with two hidden neurons.



Through understanding the steps outlined in my code, one should be able to scale the NN in any way they like, such as changing the input / output data and layers to fit their strategy ideas.

Additionally, one could forgo the backpropagation function, and load their own trained weights into the w1 and w2 matrices, to have this code run purely for inference.

-------------------------------------------------------

While this demonstration does create a “prediction”, it is on historical data. The purpose here is educational, rather than providing a ready tool for non-programmer consumers.

Normally in Machine Learning projects, the training process would be split into two segments, the Training and the Validation parts. For the purpose of conveying the core concept in a concise and non-repetitive way, I have foregone the Validation part. However, it is merely the application of your trained network on new data (feedforward), and monitoring the loss curve.

Essentially, checking the accuracy on “unseen” data, while training it on “seen” data.

-------------------------------------------------------

I hope that this code will help developers create interesting machine learning applications within the Tradingview ecosystem.

Release Notes

+ License
Comments
PineCoders
In the name of all TradingViewers, thank you for your valuable contribution to the community, and congrats!
Alien_Algorithms
@PineCoders, Glad to help :)
Texmoonbeam
I have found this very helpful, thank you. Can you suggest how the output loss is adjusted when you have two output variables, that are not dependent on each other. I thought perhaps
d_loss_d_output = 2 * (predicted_output1 - actual_output1) * (predicted_output2 - actual_output2)
Texmoonbeam
@Texmoonbeam, I think I will be using a categorical model, with softmax activation and cross entropy loss, so nevermind!
torche071396799
how do we use this?
MOMOPro
@torche071396799, Exactly. I've seen a lot of ML indicators and built one myself. After 9mo. determined it was too flakey - even after making it multi-modal with VIX, interest rates, and bunch of other inputs
byStoyan
Hi, can you give an example, e.g. a use case scenario?
Alien_Algorithms
@byStoyan, I have a very busy schedule, but I will try to find some time for displaying an implementation of it.

Generally though, if one understands how a neural network operates, it should not be hard to implement my code for any use-case.
shortenuff5
@Alien_Algorithms, this would be cool, if it could retrain the data, unless that’s something that’s already included. Forgive me, don’t have a huge knowledge on neural networks. Any advice for settings for swinging 2 week options? Just started messing with it. I understand there is risk with anything, so any reply is a good reply. Thank you!

Josh
michaelzappe1981
Hi Algo, Can I test your indicator? Could you program an entry point and stop Loss+ Take Profit? Sincerely, Mike
More