Back

Machine Learning - TensorFlow Basic Example

"TensorFlow™ is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them." - TensorFlow Website

One of my favorite things about QuantConnect is the access to some very powerful Python modules not seen before in other quant platforms. One example is TensorFlow. Although usually associated with Deep Learning, TensorFlow can be used generally for numerical computation and comes with some great tools.

Today, my goal is to share the first TensorFlow tutorial, based on the MNIST For ML Beginners which offers a great and basic walkthrough for people new to Machine Learning and or TensorFlow. 

The change in price, over N steps, is used as input features to determine the change in price from step N to step N+1. Using Opening Price, the goal is to forecast the change from one market open to the next to allow for a daily rebalance to the target class(Cash or Long) for a single asset.

Please note, the attached backtest is more academic in that a softmax regression model is trained and then used to predict probabilities per class(Cash or Long positions). Little is done to attempt to achieve success or manage the training in an intelligent way to ensure a quality results or best practices in Machine Learning; making this a good starting point to explore and learn such things(learning rates, dropout, regularization, mini batch inputs, test train splits, cross validation, log loss vs accuracy, feature scaling, non overlapping features, etc). The "positive" result, I'm guessing, has more to do with the model favoring the long class for an appreciating assets vs any deep(darn a pun, we were so close to the end) insight.

Update Backtest






The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



Here is my attempt to provide tensorflow multiple pieces of info per timepoint - in this case, I train the TF on the changes in high/open/low/close/volume from the previous day, rather than on changes in price alone.

3


Derek Tishler

I am both new to quantconnect and to machine learning when it applies to backtesting but let me ask you this fundamental question, which I don't quite understand yet: Why do you train the machine learning algo in the backtester? 

My current understanding of the backtester is that it will recalculate the profit and the weights of the ML algo one rebalance date at a time. That sounds to me like a big for loop wich could be avoided by just having a big matrix where each sample index is the 'i'th rebalancing date. Doesn't it make more sense to train the machine learning algorithm offline / in the research lab and then once trained, plug in the optimized weights into the algorithm?

Sorry for that newbie question, but I am myself trying to figure out the best way to apply machine learning to trading and would like to understand the logic behind using the backtester instead of the research lab.

0

Custos Sure we would ideally train the model offline and then use it to perform inference in an algo. The only reason a real-time training was done via the backtester was that it is the only way to play with TF in QC right now and see results. The Research or Lab has no way to persist(save) a model as mentioned above so this was just a way to get things going. You will want to use Lean and your own data on a local machine to train and then use a machine learning model in live trading easily as I mentioned to Nolan above.

1

Thank you Derek, that makes things clearer for me.

0

So untill now there's no way to save a model (pickle or similar)? I'm starting with QC but it seems almost impossible to work with Machine Learning without pickling.

0

Wow, very interesting indeed. Thanks Derek Tishler !

0

I'm very new to this. I'm getting this error after I cloned the project in the second post in this thread.

How do I get QuantConnect to know about tensor flow?

0

When I run the strategy. It doesn't work and keep reminding me: Build Error: File: n/a Line:0 Column:0 - import tensorflow as tf. How to solve the problem?

0

Ravinia  and  ForeverYoungNow , since a change in the python environment you now have to be careful in what order you import TensorFlow and (i think) pandas or some other lib. Try importing tf first such as this and rebuild:
 

import tensorflow as tf
import random
import numpy as np
import pandas as pd
1

QC upgraded Python, which broke the original algo. 

I fixed those errors:

Lines 15-19:

import tensorflow as tf
import random
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
 

Line 226:        for i in range(self.n_features_per_asset+1, len(next(iter(all_data.items())))-1):

 

but now I am getting error:

 

BacktestingRealTimeHandler.Run(): There was an error in a scheduled event AAPL: EveryDay: AAPL: 0 min after MarketOpen. The error was ValueError : Cannot feed value of shape (0,) for Tensor 'Placeholder:0', which has shape '(?, 255)'

2

Getting the same error above and could not manage to fix it, I had a look on google and stackoverflow and seems to be a common problem easy to solve but I cannot get my head around it, any help is appreciated...

I assume it has to do with this two lines?

        self.x                  = tf.placeholder(tf.float32, shape=[None, self.n_features])
        self.y_                 = tf.placeholder(tf.float32, shape=[None, self.n_classes])

0

Dear Derek, really great code! I am using AI algorithms since more then 15 years in completely other areas (medicine) and I am new here to QC, thus a few questions to make sure:

1. Did I get it write that the model is on a per minute basis, but you train the network only on the rebalance event one time a day? Did you also tried to retrain at every data slice based on the lets say 200-300 last minutes? you currently just use the last 15 open prices for learning or am I wrong?

2. Is it possible to easily adopt your approach to use not only a vector for learning in tensorflow, but e.g. an array of data (e.g. open price, volume, e.g. some indicators)? I never used tensorflow, but we started to this offline for fun with other software and the results looked promising...

3. I know that is is just an easy template to start with tensorflow and increase the possibilities then. But what are the limitations now in QC? As far as I understood, I could not train the models offline because they cannot be stored and accessed them in backtesting of live mode, correct? But doing like this, what are the current limitations if you train in every ondata event? I still think it could be desirable to use more data points (or different additional informations per time) for training? How many points are allowed in history calls in on data events?

4. The second version of the multi asset approach looks also very interesting. Unfortunately, like other people, due to teh change in python, I cannot make it running (considering also having no debugger/breakpoints for python in QC....). Do you have maybe a version which works in the current environment? Would be so much appreciated!

THANKS and very best wishes from Germany!

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed