Tensorflow Deep Learning Neural Network Algorithmic Trading System That Predicts Next Candle

Tensorflow Deep Learning Neural Network Algorithmic Trading System That Predicts Next Candle

Show Video

In this video I am going to teach  you a deep neural network model   that uses the tensorflow library to predict the next candle...this deep neural network model   will predict whether the next candle will be bullish or   bearish...deep learning is the cutting  edge of artificial intelligence I will show in this video how we are  going to use deep learning in algorithmic trading deep learning uses a lot of concepts I'll try to explain in simple terms those concepts in this video and in subsequent videos also   so don't worry if you don't understand a concept   in this video...in the subsequent videos I'll again revisit those things and explain them in detail trading is all about predictions we predict the price that it's going to go up and down if we   think the price is going to go up we buy and if we think the price is going to go down we sell what we want is we want a high win rate so that most of the time we win and this is the only way   to make money if you keep on losing then of course you're not going to make any money in order to have a high wind rate we develop models in the past technical analysis was being used to predict price   now with the recent advancements in computation we have got machine learning and deep learning also in statistics once we make a model we have to first decide the probability distribution   which is a difficult thing because  returns in financial markets are distributed in a long tail distribution and most of the time   people have been using normal distribution which is not a long tail distribution which ultimately predicted things that people  later on lost lot of money   acting on those predictions the advantage of deep learning is this that we are not assuming anything   about the probability distribution we  just gave that model the past data and   deep learning model with itself read that data and then make a model and then tell us that what is the prediction according to that deep learning model   so we don't make any assumptions  something that is very powerful so let's start so this is a EURJPY chart we are thinking of opening a trade here we want to know whether this candle that is not being formed is going to be a bullish candle or bearish candle let's say we want to open a short trade and if our model predicts that it is going to   be bullish candle then we should avoid opening a short trade if you have watched my previous   video on macd trading strategy I told you how we use macd to determine the trend this is macd   it is bullish so it seems that short trade at this moment is not a good idea and this is the daily chart that was the H12 chart...on daily chart also macd is bullish so we want to know

whether we are we are going to have a bullish candle or a bearish candle since this H12 chart on this H12 chart macd is bullish on H8 it is bearish so should I open a long trade let's see how we solve this problem we solve this problem by developing a deep neural network I have coded this steep neural network it will predict the next candle   any currency pair any stock any time frame we will just use this model...this model works for all time frames all currency pairs all stocks so from datetime import datetime then we import metatrader5 as mt5 this is the api   that connects python with the mt5 and we can then seamlessly download data any time frame any currency pair or stock from mt5 before watching this video you should watch my video on how to connect python with mt5   in which i've explained about this metatrader5 api you should also watch my video on   pandas for algorithmic training talib  python library for technical analysis and   numpy for algorithmic training after you have watched these three videos then you should watch this   video and you'll easily understand most of the things in this video   so I hope you have watched the previous videos we import pandas as pd this is   pd is an alias...import numpy as np we import tensorflow as tf...tensorflow is a very powerful deep learning library open source  library developed by google google developed this tensorflow library in the beginning they used this tensorflow library for their   inner work for deep learning machine  learning and after a few years when this   tensorflow library was mature and very powerful they decided to open source it now we can use it   you can easily install it I've installed  it ...you just install it using pip install  

tensorflow my computer has got gpu nvidia card so tensorflow gpu is very fast I haven't installed that tensorflow gpu I'll be using just simple tensorflow from tensorflow.keras.model  import sequential keras is a layer built on tensorflow...it's on a higher abstraction level gives you a higher abstraction level   sequential means that we will have layers...once one layer calculates then it gives   that data to the second layer then once it calculates that it can give the data to the third layer and it will be like that so in a  sequence the data will flow first layer   will make the calculations then it will give the result to the second layer it   will make the calculation and give the result to the third layer will make that calculation then  give the result to fourth layer and we will decide in the model how many layers we are going to have each layer is known as dense...dense I'll explain again from keras.layers  import dense and lstm...lstm is a   long short-term memory it's a recurrent neural network...it's an advanced concept

in time series what we have is there's some memory in the past values so the researchers built   recurrent neural networks that also use that past values in determining the present value so long   short term memory is an architecture that we will be using and we'll be using dense also   and from tensorflow.keras. optimizers import adam...adam is our stochastic gradient descent type of thing stochastic most of these  deep learning models use gradient descent in order to reduce the time of calculation we use stochastic gradient descent stochastic   means we randomly choose values  and then calculate the gradient   if you have taken calculus then gradient is just the slope of the curve so we want to move in the downside and want to identify the valley because we'll be minimizing our loss I hope this explanation makes things  a little bit clearer to you somewhat  first we define the rates function  this is currency pair time frame bars   we'll be downloading the data from mt5 live as I've said in the beginning my broker provides   stocks also on mt5 cryptocurrencies also futures also commodities also so instead   of currency pair I should have used instrument but since I have now written this currency pair let's keep on using it but mt5 now is a very powerful platform and my broker provides stocks also listed   on new york stock exchange nasdaq it provides commodities also gold silver oil natural gas   provides futures also it provides cryptocurrencies also there are bitcoin ethereum litecoin ripple   we can use anything that mt5 symbol  window provides here so I should have   used instrument but since I've used currency pair   so we will use it but don't worry we can download any type of data from this...we open a connection   if there's error we close it if there is no  error we continue on copy rates from position   if the currency pair time frame...zero means the most recent bar which is being formed it's not closed   and bars yeah I wouldn't get hundred bars but in deep learning will need at least one thousand   bars hundred bars is too less after we have opened the connection we shut down that connection and then   we convert these rates into a pandas data frame and then return that rates now let's define make our   deep neural network we'll be taking currency pair time frame bars first we use the rates function and then we have a data frame we convert time to date time   we calculate the return we are using np.log...if you have watched my video on numpy algorithmic  

trading where I explained np.log we have got two types of returns one is percentage   return and one is log return the log returns have got better statistical properties so we try to use   this log returns but we can also use percentage returns in this case I have used log returns so see we have used and then we find the direction np.where if return is   greater than zero we give it one and if it is less than zero we give it zero means if it is   return is positive it is 1 it is negative  0 we use 5 lags we use 5 returns before that to make our calculation we   take a data price df.close return we add another column direction we shift it we shift it back instead of forward minus one   we should know the difference between plus one and minus one shift plus one is being shift the column   one row forward and in case of minus one shift column one row back we calculate for   lag and range one lags plus one we make this data this is known as data pre-processing we are going to build a data frame we will now add momentum...momentum is return rolling five mean   returns means we take the returns five   previous returns and take that average and we say   it is the momentum let's say we add the five return it is positive we say the momentum   is positive and if the five returns we add it's negative we will say that the momentum is negative   volatility of return rolling and we calculate the standard deviation 20 previous returns   we calculate distance price minus price into rolling 50 minus mean minus shift one means we   present price with price 50 back and  then rolling 50 means we've taken means   it's very important that we drop the na drop means once you'll calculate these things   initial rows will have na is not a number and they will drop it otherwise this model will just   complain that there are too many nas so we drop it we extend these columns then we add the   optimizer adam and model sequential as I've said sequentially one layer calculates then it gives   to second layer calculation with the third layer so these are the layers we add first we said sequential   first layer is dense 32 neurons activation is relu...relu is the activation function we use that relu now a days it's being used and then we add another layer then again it makes 32 neurons and then we have the last   layer it is just one neuron so we have added three layers two layers use relu one layer uses sigmoid   and then we compile the model and we use a binary cross entropy as loss...binary cross entropy means  

this model is going to calculate the probability of each return whether it is bullish or bearish   one or zero and then compare it with that actual whether it was one or zero and if it is as it predicts that is one and actually it is  zero then we penalize that model right so we then divide that data into  training data up to the last we remove last two bars and then we normalize that data it's very important that we normalize the data neural networks always love data that   is between zero and one so we normalize it using mean and standard deviation   and then we test data test data is last two bars and we again normalize it model that fit and we make the prediction test data columns if the probability is greater than   0.5 we'll say it's 1 and if it is less than 0.5 will say it's zero   means if that probability is let's say 0.6 will say it's bullish and if it is let's say like 0.4   and we say it's bearish and then we  return so this was our model now let's   use that model open the terminal run python interpreter okay we  have done it now this is the dnn1 file   I've defined these functions I'm to  import these functions through dnn1 see it's importing tensorflow saying that could find that cuda dll but since we have not installed gpu   so we'll just use ordinary tensorflow now we import this method we can use this on H1 H2   H4 H8 H12 let's run the model for daily timeframe and currency pair is EURJPY first read it and run this deep neural network time frame daily currency pair EURJPY and we download 1000 bars see rapid calculation epoch is one calculation zero what it means is that we'll have it's predicting a bearish bar  okay and let's see what it predicts on   H12 12 hour time frame on daily it's telling it's bearish on 12 hours it is telling it is bullish this is the prediction of our deep neural network how much accurate this prediction is   for that we will have to back test   once we back test with the last 10 years data only then we can say that this model   is 70 percent accurate 80 percent accurate 50 percent accurate 60 percent accurate so stay tuned in the next video I'm going to now show you how to backtest this...back testing is   very important because right now we don't have any idea how much accurate this model is so we   will backtest it first we'll train that model then make this prediction then make and compare   that prediction with the actual result and if we have got the same result with the prediction   so we have got a good prediction and if we have opposite prediction then we calculate for   let's say like 5 10 years and then we'll  know how much accurate this model is right now this model is saying that we will have a   bearish candle on D1 and we will have  a bullish candle or H12 in about an hour if you want to trade with this thing let's see and it seems to be correct because here we have got a bullish H12 bar and only we have got two hours left before this bar closes so model seems to be making the right prediction   that we'll have a bullish H12 bar and we will have a bearish bar on D1 so stay tuned in the next video  I am going to show you how to   backtest this model deep neutral  network model that uses tensorflow   and measure its accuracy over the period of last 10 years I hope you like my video

2022-02-21 01:07

Show Video

Other news