Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Tensorflow For Traders
#1

Tensorflow is an open source library developed by Google. Tensorflow has become very popular in machine learning, deep learning and reinforcement learning. If you want to develop algorithmic trading strategies using Python than you must master Tensorflow in addition to Pandas and Numpy. I will start separate threads for Pandas and Numpy. In this thread we only discuss Tensorflow. Tensorflow has been developed to efficiently do numerically computations involving very big arrays which are mathematically known as tensors. Tensors are just arrays with many indices. Matrix is a rank 1 tensor. Scalars like real numbers are rank zero tensors. Machine learning/deep learning involves heavy matrix operations and tensor operations.

In a different thread in MQL4 forum, I discussed how difficult it is to develop machine learning/deep learning/reinforcement learning libraries. Tensorflow development took many months spanning a few years. Tensorflow was developed by the Google Brain Team. Lot of effort went into its development so you can well imagine how much effort developing good machine learning/deep learning/reinforcement learning libraries can take. These libraries are developed by teams of professionals. Tensorflow is basically written in C++ with a Python wrapper at the front end.

Tensorflow GPU versus CPU

This is how Tensorflow works conceptually. First we define a computation graph using standard tensorflow components. Tensorflow takes that graph and performs the numerical computations using C++ in a highly efficient manner. Tensorflow has two versions CPU as well as GPU. GPU version is suited for doing very heavy numerical computations involving millions of parameters. First you need a GPU computer. If you have a GPU computer, like an NVIDIA GPU computer, you can install tensorflow gpu on it. As a trader, we won't be doing very heavy computations so CPU is good enough for us. You can install GPU version, its speed is almost the same as that for CPU for small datasets. Tensorflow can also be used for distributed computing and is being used in Google large scale systems. Tensorflow can run on Windows, Linux, macOs as well as mobile devices using iOs and Android. What this means is that you should master tensorflow, you can always find it handy. You can install tensorflow with this simple  pip command

pip install tensorflow

Installing tensorflow gpu requires first installing CUDA and other things before you can install it. As I said I have personally tested it for small datasets there is no advantage going through the extra steps involved in installing gpu version as cpu version provides the services in almost the same time as gpu version. I used the following code to check how much time it takes tensorflow to load on my computer:

from time import time    
t=time()
import tensorflow as tf
print(time()-t)
print(time()-t)

7.185936212539673


it took 7 seconds for tensorflow to load on my computer which is pretty fast. Let's start with a simple calculation x^3/y+8. As I have said first we will build a computation graph:

import tensorflow as tf
x=tf.Variable(89, name="x")
y=tf.Variable(27, name="y")
z=x*x*x/y+8

In the above code we have build a computation graph. Now we need to open a tensorflow session that will build this computation graph. We don't have to worry. Tensorflow will decide how many CPUs/GPUs to use. Of course this is a simple code example and not very complicated. But imagine we had over 20 million parameters. It would have been headache to decide how to do the numerical computation efficiently. Tensorflow would have helped us by deciding how to do numerical computations involving more than 20 million parameters by taking account of the computer's capabilities. For now this is how we are going to start the tensorflow session:

sess = tf.Session()
sess.run(x.initializer)
sess.run(y.initializer)
answer=sess.run(z)
print(answer)
print(answer)
26118.0
sess.close()

In the above code we first start the tensorflow session and then use run to build the graph. In the end we close the session which is important.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#2

TensorBoard Graphs
Computation graphs are at the core of tensorflow. Computation graph is a directed graph that has nodes and edges. Nodes are the tensor operations and edges are the data. Before tensorflow makes any computation it builds a computation graphs. This helps a lot in doing the computations in an efficient manner without wasting resources. Computation graph allows the user to construct a complex function by breaking it into smaller simpler functions. Nodes are drawn as circles and represent some action that is taken on the data that is fed into the node. Edges are the actual data values that get passed to the nodes. In tensorflow, we first define the computation graph and then run it with data. Everything will become clear with a simple example.

from time import time
t=time()
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import random
import tensorflow as tf
import os
print(time()-t)

We just need tensorflow for this example. But I wanted to show that we have to import many libraries when writing a machine learning python program.

a=tf.constant(15, name="a")

b=tf.constant(23, name="b")

c=tf.multiply(a,b, name="c")

d=tf.add(a,b, name="d")

e=tf.add(c,d, name="e")

#run session

sess=tf.Session()

outs=sess.run(e)

print("outs={}".format(outs))
outs=383

This is what I did. I defined two constants a and b. Then I multiplied them and added them as c and d and finally I added c and d. After that I ran a tensorflow session that did the computation and the answer was 383. Let's now use a tensorboard and visualize the computation with a computation graph.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#3

When you create applications that create, transform or process a tensorflow function, the function doesn't execute automatically like the standard python. Instead the application stores the operations in a special data structure known as a Tensorboard Graph. A Tensorboard Graph can hold many functions. These functions only get executed when the application run Session command. When the session is run, graph gets executed. This you must have realized by now when reading the previous posts. We can define tensorflow functions but we cannot execute them without running a session. You might be wondering what's the use of this graph. Graph saves tensorflow commands. We can export the graph to a new file as well as launch it on a new system. 

Basically graph is a container class. There can only be one graph in an application. We cannot nest graph in another graph. So there can be only one graph active at a time. You can call a graph with the following command:

graph=tf.get_default_graph()

You can define a new graph and make it a default also:

newGraph=tf.Graph()
with newGraph.as.default():

Graph provides you with a number of methods that you can use. Let's do some examples:

a = tf.constant(10.3, name='A')
b=tf.constant(24.1, name='B')
c=a+b
print(tf.get_default_graph().get_operations())

>>> print(tf.get_default_graph().get_operations())

[<tf.Operation 'A' type=Const>, <tf.Operation 'B' type=Const>, <tf.Operation 'add' type=Add>]

So using the method get_operations() tell us that first we defined a constant A and then another constant B and after that we took the sum of A and B. There are more things that we can do with graph like graph collection keys. As we progress in this tutorial we take more examples. In short, graphs let's you access tensors, their operators and other data types. You can use the method as_graph() to get GraphDef which will define nodes and edges in the graph using JSON.

print(tf.get_default_graph().as_graph_def())

>>> print(tf.get_default_graph().as_graph_def())
node {
  name: "A"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 10.300000190734863
      }
    }
  }
}
node {
  name: "B"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 24.100000381469727
      }
    }
  }
}
node {
  name: "add"
  op: "Add"
  input: "A"
  input: "B"
  attr {
    key: "T"
    value {
      type: DT_FLOAT
    }
  }
}
versions {
  producer: 24
}

These are very basic simple examples just to make things easy to understand. A graph can contain hundreds of nodes and operations. So you can well imagine the complexity of a graph. We can save the GraphDef to a file using the write_graph function from tf.train module.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#4

I was talking about how to save the graph. We will use the following command:

>>> tf.train.write_graph(tf.get_default_graph(),os.getcwd(),
... 'graph1.dat')
'C:\\Python\\py-metatrader-copy\\graph1.dat'

As you can see the graph1.dat file got saved in the C drive Python folder. As I said previously, in standard python, the code gets executed line by line. But when we are using tensorflow, we first build a graph and then execute that graph by running a session. Just like graphs, you can only execute one session at one time. 

>>> tensor1=tf.constant([2,8])
>>>
>>> with tf.Session() as sess:
...     res=sess.run(tensor1)
...     print(res)
...
[2 8]

InteractiveSession class allows us to create an interactive session. We can use an interactive session that allows us to execute individual commands:

>>> a1=tf.constant(10)
>>> a2=tf.constant(30)
>>> a3=tf.multiply(a1,a2)
>>> sess=tf.InteractiveSession()
>>> print(a3.eval())
300

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#5

We can also use the logging package that has more functionality than the print function. Tensorflow tf.logging package provides logging using the standard Python logging package and has almost same methods.

>>> a1=tf.constant([[2,3,5],[2,3,5]])
>>> tf.logging.set_verbosity(tf.logging.INFO)
>>>
>>> with tf.Session() as sess:
...     output=sess.run(a1)
...     tf.logging.info(output)
...
INFO:tensorflow:[[2 3 5]
 [2 3 5]]


Tensorflow logging writes message to the standard output and cannot write to a log message. This might change in the future versions. 

Data Visualization With TensorBoard
Logging has limitations when it comes to handling large datasets. Tensorflow has TensorBoard that allows us to visualize large datasets. TensorBoard reads the application data and then displays on the web browser. But first you need to format the data into a form known as summary data. There can be problems in executing TensorBoard command. If this is the case, you should include TensorBoard in the PATH of windows environment variables. 

Default TensorBoard IP is 127.0.0.1 which can be treated as localhost. Default port is 6006. So the TensorBoard default URL will be http://localhost/6006. You need logdir flag before TensorBoard can be activated. But first you should understand what is a summary operation. When a session command executes the summary statement the  result is a protocol buffer that contains the summary data.After you create the summary data you need to create a directory and write the summary data to that directory event file using FileWriter and its method. Things sound complicated.  Let's make this clear with an example.

>>> a1=tf.constant([[2,3,5],[2,3,5]])
>>> a2=tf.constant([[3,5,6],[1,3,4]])
>>>
>>> a3=a1+a2
>>> #create the summary
...
>>> tf.summary.scalar("a1",a1)
<tf.Tensor 'a1:0' shape=() dtype=string>
>>> tf.summary.scalar("a2",a2)
<tf.Tensor 'a2:0' shape=() dtype=string>
>>> tf.summary.scalar("a3",a3)
<tf.Tensor 'a3:0' shape=() dtype=string>

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#6

Before we build a TensorBoard graph, we will build a Tensorflow training example and then use it to build a graph in detail. This will help in illustrating lot of things that cannot be illustrated with the help of simple and easy examples.  In supervised learning, we build a model. The first step in building a model requires defining variables. Then we define a loss function. This loss function measures the difference between the target values predicted by the model and the actual values. We need to minimize this loss function. For this we will need to define an optimizer and then minimize using that optimizer. Then we need to run the session in batches. Let's do a supervised learning example. This will help clear many things. So let's start.

The first step is obviously creating variables. The major difference between variables and tensors is that we need special operations to initialize variables. We cannot use uninitialized variables with Tensorflow. Once we have initialized the variables we need to minimize the loss function. Loss function is also known as the cost function. Tensorflow usually uses the term loss function. The process of minimization of the loss function is known as Optimization. Optimization is done in Tensorflow using four methods: GradientDescentOptimizer,  MomentumOptimizer, AdagradOptimizer and AdamOptimizer.

GradientDescentOptimzier: The gradient descent optimizer used the gradient descent algorithm to optimize the loss function. Gradient Descent algorithm uses the mathematical fact that a function decreases fastest in the direction of the negative gradient descent. Gradient descent algorithm first calculates the gradient of a function. Gradient always point in the direction of steepest ascent so we take the negative of gradient which gives us the direction of steepest descent. The problem with gradient descent algorithm is that it can reach a local minimum instead of global minimum. It can also be slow to reach the minimum. Sometimes it can oscillate between values and never find the minimum. So there are shortcoming of the gradient descent algorithm.

MomentumOptimizer: Momentum optimizer has many things in common with the gradient descent optimizer. Momentum optimizer is faster than the gradient descent optimizer. This is achieved by using the accumulation variable.

AdagradOptimizer: In Adagrad Optimizer the learning rate varies from variable to variable and from step to step. Adagrad Optimizer uses subgradient which also applies to nondifferentiable functions. Adagrad Optimizer uses pretty complicated maths. But you don't need to worry as Tensorflow implements the AdagradOptimizer and you can use AdagradOptimizer Tensorflow Class by invoking the constructor.

AdamOptimizer: Adam algorithm closely resembles the Adagrad Algorithm and the Momentum Algorithm.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#7

Now that was theory. Let's now run some Tensorflow code. In the beginning you will find Tensorflow to be a bit complicated but with practice, you will start finding it easy. So let's start with a simple example and try to minimize a quadratic loss function using GradientDescentOptimizer. Let's start:

>>> import pandas as pd
>>> import numpy as np
>>> import pyalgotrade as algo
>>> import time
>>> t=time.time()
>>> import tensorflow as tf
>>> time.time()-t
14.052058219909668
>>> import os
>>> #Gradient Descent Algorithm Tensorflow Example
... #define the training variable
...
>>> varX=tf.Variable(0., name='Result')
>>> #define the untrainable variable
...
>>> varStep=tf.Variable(0., trainable=False)
>>> #define the cubic loss function
...
>>> lossFunction=varX**2+3*varX-1
>>> #minimize this loss function
...
>>> learnRate=0.05
>>>
>>> numEpochs=50
>>> #optimizer class constructor
...
>>> optimizer=tf.train.GradientDescentOptimizer(learnRate).minimize(\
... lossFunction, global_step=varStep)
>>> #now initialize the variables
...
>>> init=tf.global_variables_initializer()
>>> #train the saver
...
>>> saver=tf.train.Saver()
>>> #create the summary data
...
>>> summaryData=tf.summary.scalar('x', varX)
>>> #create the file writer
...
>>> fileWriter=tf.summary.FileWriter('log', graph=tf.get_default_graph())
>>> #launch the session
...
>>> with tf.Session() as sess:
...     sess.run(init)
...     for epoch in range(numEpochs):
...         _, step, result, summary=sess.run([optimizer,varStep,\
...         varX, summaryData])
...         print('Step %d: Calculated Result=%f' %(step, result))
...         #print summary data
...         fileWriter.add_summary(summary, global_step=step)
...         fileWriter.flush()
...     #save the variable data now
...     saver.save(sess, os.getcwd()+'/output')
...     print('Final varX: %f' % sess.run(varX))
...
2018-09-16 14:53:28.139920: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Step 1: Calculated Result=-0.150000
Step 2: Calculated Result=-0.285000
Step 3: Calculated Result=-0.406500
Step 4: Calculated Result=-0.515850
Step 5: Calculated Result=-0.614265
Step 6: Calculated Result=-0.702839
Step 7: Calculated Result=-0.782555
Step 8: Calculated Result=-0.854299
Step 9: Calculated Result=-0.918869
Step 10: Calculated Result=-0.976982
Step 11: Calculated Result=-1.029284
Step 12: Calculated Result=-1.076356
Step 13: Calculated Result=-1.118720
Step 14: Calculated Result=-1.156848
Step 15: Calculated Result=-1.191163
Step 16: Calculated Result=-1.222047
Step 17: Calculated Result=-1.249842
Step 18: Calculated Result=-1.274858
Step 19: Calculated Result=-1.297372
Step 20: Calculated Result=-1.317635
Step 21: Calculated Result=-1.335872
Step 22: Calculated Result=-1.352284
Step 23: Calculated Result=-1.367056
Step 24: Calculated Result=-1.380350
Step 25: Calculated Result=-1.392315
Step 26: Calculated Result=-1.403084
Step 27: Calculated Result=-1.412775
Step 28: Calculated Result=-1.421498
Step 29: Calculated Result=-1.429348
Step 30: Calculated Result=-1.436413
Step 31: Calculated Result=-1.442772
Step 32: Calculated Result=-1.448495
Step 33: Calculated Result=-1.453645
Step 34: Calculated Result=-1.458281
Step 35: Calculated Result=-1.462453
Step 36: Calculated Result=-1.466207
Step 37: Calculated Result=-1.469587
Step 38: Calculated Result=-1.472628
Step 39: Calculated Result=-1.475365
Step 40: Calculated Result=-1.477829
Step 41: Calculated Result=-1.480046
Step 42: Calculated Result=-1.482041
Step 43: Calculated Result=-1.483837
Step 44: Calculated Result=-1.485453
Step 45: Calculated Result=-1.486908
Step 46: Calculated Result=-1.488217
Step 47: Calculated Result=-1.489395
Step 48: Calculated Result=-1.490456
Step 49: Calculated Result=-1.491410
Step 50: Calculated Result=-1.492269
'D:\\Shared\\Python\\py-metatrader/output'
Final varX: -1.492269

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#8

Now we want to display the stepwise calculation results on Tensorboard. We also want to show the tensorflow graph. For that if we read the above code we had created a log file with the tf.summary.FileWriter method. This log file contains the event files that contain the tensorflow graph as well as the results of stepwise calculations. We have to move to the command line and give this command:

PS C:\Users\ahmad> cd D:/Shared/Python/py-metatrader
PS D:\Shared\Python\py-metatrader> tensorboard --logdir=log
TensorBoard 0.4.0rc2 at http://localhost:6006 (Press CTRL+C to quit)
W0916 15:57:29.727377 Reloader tf_logging.py:86] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.
W0916 15:57:29.727377 Reloader tf_logging.py:86] Found more than one metagraph event per run. Overwriting the metagraph with the newest event.
W0916 15:57:29.740346 Reloader tf_logging.py:86] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.
W0916 15:57:29.741510 Reloader tf_logging.py:86] Found more than one metagraph event per run. Overwriting the metagraph with the newest event.

Now we type http://localhost:6006 in the browser and we have our tensorflow graph as well as the scalars. The tensorboard graph is:

[Image: tensorboard_graph.png]

This is a simple model that just minimizes a Quadratic Loss function. When we have complicated deep learning neural network models this tensorboard graph can be a handy thing. Tensorboard graph shows the flow of data with the nodes and edges. Below is the tensorboard scalars graph:

[Image: tensorboard1.png]
As you can see the loss function was very easy to minimize. Now this was a very simple example. The purpose was only to illustrate how to develop a simple tensorflow model and then write the data summary with the FileWriter method. Once we have the data summary saved in an event file we can use the tensorboard to show the model graph as well as the loss function calculations. When we will build complicated tensorflow models these things will become useful and help us a lot. We can easily automate the whole process of saving the tensorboard as images by including few more lines in our python code. There are advantages to learning Tensorflow. Don't get daunted if you find it difficult to learn. Tensorflow is now also available for Javascript. The model you build with tensorflow can be run on a mobile as well. So if you are using a mobile phone for trading, you can run your tensorflow model on your mobile phone. Keep reading this thread. I will in the near future show you how to develop good trading models using tensorflow.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#9

After the release of Tensorflow 2.0 a lot of change. Now we are at Tensorflow 2.8 stable release. A lot of traders get fascinated with the idea that maybe a neural network can solve their trading problems. Deep Neural Network DNN is what may traders want in their trading system. I have developed a simple DNN Tensorflow Trading System that can predict the direction of the next candle on any timeframe. Building a Deep Neural Network with Tensorflow is now a lot easy as compared to that in the past. Tensorflow is now a mature technology. We can easily build models using it. In the beginning Tensorflow complained when I tried to make model again on each new run. So I solved that problem by making the model once and then using it again and again and Tensorflow stopped complaining. The prediction from the Tensorflow model tooks a few seconds to make!


The test of this Tensorflow Deep Neural Network is how good are its predictions. We need to do a backtest that can give us a rough idea how good the accuracy of this Tensorflow Deep Neural Network is.Whatever trading system that you develop should be backtested to get a rough idea how good that trading system is. Backtest is no guarantee that this trading system is going to work under live conditions. Financial markets are highly sentimental now a days. Breaking news will be driving the market up at one moment and the next moment another breaking news will be driving the market down. So we have many hidden variable that we cannot identify when building a model.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#10

As long as we don't backtest this tensorflow neural network model we don't know what type of predictions it makes. Backtesting is no guarantee that this trading system will work under live market conditions. Backtesting most of the time disregards taking stop loss into account. We always trade with a stop loss and a take profit. When we disregard stop loss and take profit, we don't get a really realistic picture of how the trading system will perform in live market conditions. In this backtest, I will just compare the predicted direction of the candlestick with the actual candlestick direction. This way we can know at least that this tensorflow deep learning neural network model has this much accuracy in predicting the next candlestick. Watch the video below and discover how much accurate this tensorflow deep learning neural network model is. The backtest was carried on 500 daily candles. It took python around 30 minutes to backtest this model. We train the tensorflow NN model for each bar and then make the prediction.


Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)