DeepTrading With TensorFlow 1 - TodoTrader
DeepTrading With TensorFlow 1 - TodoTrader
DeepTrading With TensorFlow 1 - TodoTrader
BLACK BELT
o you want to maximize your trading knowledge using TensorFlow? Here are several tips that will surely help you.
D
Introduction
Within TodoTrader’s commitment related to the generation and dissemination of knowledge, I want to offer a series of tutorials on the use of TensorFlow for algorithmic
trading.
The objective of these tutorials, which I will publish periodically, is to offer in a simple and didactic way, through practical examples, the basics and basic concepts
essential for the task of algorithmic trading. At the end of the series, we will have developed an application that allows creating a neural network in TensorFlow, trainable
and able to perform operations in the financial markets.
TensorFlow was open-sourced in November 2015. Since the inception date, TensorFlow has become Github’s most prominent machine learning repository.
(https://github.com/tensorflow/tensorflow)
TensorFlow’s popularity is due to many things, but mainly because of the computational graph concept and the adaptability of the Tensorflow python API structure. This
makes solving real problems with TensorFlow accessible to most programmers, even the beginner ones.
https://github.com/parrondo/deeptrading
Tensorflow is supported on the three principal OS systems (Windows, Linux, and Mac). Throughout these Jupyter notebooks, we will only concern ourselves with the
Python library wrapper of Tensorflow. This book will use Python 3.X (https://www.python.org) and Tensorflow 0.10+ (https://www.tensorflow.org). Tensorflow can run
on the CPU, but it runs faster if it runs on the GPU, and it is supported on graphics cards with NVidia Compute Capability 3.0+. To run on a GPU, you will also need to
download and install the NVidia Cuda Toolkit (https://developer.nvidia.com/cuda-downloads).
As usual, we use Conda environments to develop our code h ( ttps://github.com/parrondo/quant-trading-project-structure). Please look into the file inside the main
directory of this repository, environment.yml , and run the command:
Important Note: As I mentioned in my previous post,Build TensorFlow from Source in Centos 7, the binary files of TensorFlow for Linux is only available in Conda for
CPU up to version 1.5. Therefore I preferred to limit the notebooks to this version to avoid possible problems for readers. However, these examples have been tested until
the stable released version 1.12 working perfectly (compiled by me for CPU).
Load configuration
This is usually the first step. Here you import libraries and modules as needed. Also, load environment variables and configuration files.
Ingest data
All of machine learning algorithms depend on data. So, we either generate data or use an outside source of data. Sometimes it is better to rely on generated data because
we will want to test the expected outcome. Most times we will access market data sets for the given research. in any case, it is convenient to have a well defined ingestion
data model as we provide in this tutorial.
Split data
This step is useful when you need to separate data into training and test sets. We can also customize the way to divide the data. Sometimes we need to support data
randomization; but, a certain type of data or model type needs the design of other split methods .
Output: two dataset training dataset and test dataset, usually they are resident in memory but in case we need to save them, then “data/interim” is our folder.
Transform features
In general, the data is not in the correct dimension, structure or type expected by our TensorFlow trading algorithms. We have to transform the raw or provisional (interim)
data before we can use them. Most algorithms also expect standardized (normalized) data and we will do this here as well. Tensorflow has built-in functions that can
normalize the data for you.
data = tf.nn.batch_norm_with_global_normalization(...)
Caution! Some algorithms require normalization of the data before training a model. Other algorithms, on the other hand, perform their own data scale or normalization.
So, when choosing an automatic learning algorithm to use in a predictive model, be sure to review the algorithm data requirements before applying the normalization to
the training data.
Finally, in this step, we must have clear what will be the structure (dimensions) of the tensors that are involved in the input of data and in all calculations.
Output: two datasets transformed training dataset and transformed test dataset. It may be, this step is accomplished several times given several pairs of train-test
datasets (i.e. normalized dataset, PCA dataset, standardized dataset,…)
learning_rate = 0.005
a=b
iterations = 1000
epochs=50
k_var = tf.constant(50)
x_train = tf.placeholder(tf.float32, [None, input_size])
y_train = tf.placeholder(tf.fload32, [None, num_classes])
Define the model structure
After we have the data and initialized variables and set placeholders, we have to define the model. This is done by mean of the powerful concept of a computational graph.
The graph nodes represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) that flow between them. We tell
Tensorflow what operations must be done on the variables and placeholders to get our model predictions. Most TensorFlow programs start with a dataflow graph
construction phase. In this phase, we invoke TensorFlow API functions that construct new tf.Operation (node) and tf.Tensor (edge) objects and add them to a tf.Graph
instance.
Hyperparameter optimization
This is not a mandatory step but it is convenient. The initial neural network is probably not the optimal one. So here we can tweak a bit in the parameters of the network to
try to improve them. Then train an evaluate again and again until meet the optimization condition. As result, we get the final selected network. Output: Final selected
trained model which is stored in the folder “models”
Predict
Yeees, this is the climax of our work!. We want to predict as much as possible, It is also important to know how to make predictions on new, unseen, data. The readers
can do this with all the models, once we have them trained. So, We could say that this is the goal of all our algorithmic trading efforts. Output: A prediction. This will help us
what to do with a selected financial instrument: Buy, Hold, Sell,…
Summary
TensorFlow is an open source software library for numerical computation using data flow graphs. To work with it, we have to setup the data, variables, placeholders, and
model before we tell the program to train. Tensorflow accomplishes this through the computational graph. The graph nodes represent mathematical operations, while the
graph edges represent the multidimensional data arrays (tensors) that flow between them. We tell it to minimize a loss function and Tensorflow does this by modifying the
variables in the model. Tensorflow knows how to modify the variables because it keeps track of the computations in the model and automatically calculates the gradients
for every variable.
TensorFlow algorithms are designed to have a cyclic workflow. We set up this cycle as a computational graph and (1) feed in data through the placeholders, (2) calculate
the output of the computational graph, (3) compare the output to the desired output with the aid of a loss function, (4) modify the model variables according to the
automatic back propagation, and finally (5) repeat the process until a stopping criterion is met. (6) Then we evaluate the trained model and if we are confortable with it
finally (6) we make predictions.
https://github.com/parrondo/deeptrading
TensorFlow
Related articles
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Your Name*
Email Address*
Save my name, email, and website in this browser for the next time I
comment.
L E A V E C O M M E N T
Type Here
Recent Posts
DeepTrading with TensorFlow VI
Archives
July 2019
June 2019
May 2019
April 2019
November 2017
Categories
Be Productive
Black Belt
Brown Belt
Create Systems
Software
Start to trade
Meta
Log in
Entries RSS
Comments RSS
WordPress.org
Theme by Powered by