Creating a Day Trading AI with a Deep Neural Network in Python
Summary
TLDRThis video script details the process of implementing a deep neural network for algorithmic trading to predict market movements. The aim is to classify market direction as up (1) or down (0/-1) and place corresponding long or short positions. Using historical currency data, the script explores data wrangling, model building with TensorFlow and Keras, and performance evaluation against a buy-and-hold benchmark. The initial model struggles, but after adding features like momentum and volatility, the strategy shows promise in-sample but underperforms out-of-sample, highlighting the importance of robust testing.
Takeaways
- 🤖 The video discusses implementing a deep neural network for algorithmic trading to predict market movements and place trades accordingly.
- 📈 The neural network aims to classify market direction as '1' for upward movement and '0' or '-1' for downward movement.
- 📊 Data wrangling involves using historical financial data, specifically looking at the Euro to US dollar exchange rate, and calculating returns and market direction.
- 🔢 The model includes lags to predict the next market value based on past five days of data, creating additional columns for this purpose.
- 🧠 The deep learning model is built using TensorFlow and Keras, with a sequential model consisting of two Dense layers and a Sigmoid output layer.
- ⚖️ The model's performance is evaluated using accuracy as the metric, with a focus on comparing it to a simple 'buy and hold' benchmark strategy.
- 📉 The initial model's accuracy is around 0.51 or 0.52, indicating a 50-50 chance of correctly predicting market movements, which is not ideal for trading.
- 📈 The video illustrates the process of transforming predictions into long or short positions and calculating the strategy's returns.
- 📉 The in-sample test set shows the strategy outperforming the benchmark, but the out-of-sample validation set shows underperformance, emphasizing the importance of out-of-sample testing.
- 🛠️ Additional features such as momentum, volatility, and distance are suggested to improve the model's predictive power.
- 📉 Despite improvements with additional features, the out-of-sample test still shows the strategy underperforming the benchmark, indicating the model's limitations.
Q & A
What is the main objective of implementing the deep neural network discussed in the script?
-The main objective is to predict market movements (upward as 1 or downward as 0/negative 1) and place long or short positions in financial instruments accordingly, then compare the returns of this strategy against a benchmark buy-and-hold strategy.
Which financial instruments are initially considered for the deep neural network implementation?
-Initially, the script mentions Bitcoin to US dollar and Euro to US dollar as the financial instruments for implementing the deep neural network.
What type of problem is this deep neural network setup as?
-The deep neural network is set up as a classification problem, where the market movement direction is classified as either upward (1) or downward (0 or negative 1).
How is the market direction classified based on returns?
-The market direction is classified as 1 if the return is greater than 0, indicating an upward movement, and as 0 if the return is less than 0, indicating a downward movement.
What additional columns are created to include lags in the data?
-Five additional columns are created for lags 1 through 5 to use past values for predicting the next market movement.
Which machine learning libraries are mentioned for building the neural network?
-TensorFlow and Keras are mentioned for building the neural network, with the model being a sequential model optimized with Adam.
What is the structure of the neural network described in the script?
-The neural network has three layers: two Dense layers with 64 neurons each and a Sigmoid output layer, using accuracy as the metric for evaluation.
How is the time series data split for training and testing?
-The data is split using a 70-30 ratio, with 70% for training and 30% for testing, ensuring the data remains in sequential order.
Why is feature normalization important in this context?
-Feature normalization is important to bring all the feature values to a similar scale, which helps in faster and more efficient training of the neural network.
What additional features are considered to improve the model's performance?
-Momentum, volatility, and distance are added as additional features to potentially improve the model's predictive performance.
How does the script suggest evaluating the robustness of the trading strategy?
-The script suggests evaluating the robustness of the trading strategy by testing it on an out-of-sample data set to ensure it performs well in real-world, unseen data.
What caution does the script provide regarding the backtesting of trading strategies?
-The script cautions that a strategy may appear to perform well on in-sample data but may underperform on out-of-sample data, emphasizing the importance of robust out-of-sample testing.
What is the final verdict on the performance of the deep neural network model based on the script?
-The final verdict is that while the model shows promise in-sample, it underperforms the benchmark on out-of-sample testing, indicating that further improvements are needed before considering it for production.
Outlines
🤖 Implementing a Deep Neural Network for Trading
The speaker introduces a project to develop a deep neural network for algorithmic trading, aiming to predict market movements up or down. The strategy involves placing long or short positions based on predictions and comparing the results with a buy-and-hold benchmark. The project is framed as a classification problem with '1' for upward movement and '0' or '-1' for downward movement. The data wrangling process for Bitcoin to USD and Euro to USD is discussed, including the collection of a decade's worth of data and the calculation of returns and market direction. Lags are incorporated to use past data for predictions, and the setup for the neural network using TensorFlow and Keras is outlined, including model architecture and optimization.
📈 Evaluating Model Performance and Strategy Returns
The speaker presents the initial results of the deep neural network's performance, noting the accuracy and validation accuracy on the training data. The unexpected closeness of the training and validation accuracy is highlighted, along with the model's evaluation yielding an accuracy of around 0.51 or 0.52. Predictions are made based on a threshold of 0.5, and these are translated into long-short positions. The strategy's returns are calculated and compared to the benchmark, showing an outperformance in the in-sample test set. However, when applied to the validation set, the strategy underperforms, indicating the importance of out-of-sample testing.
🔍 Refining the Model with Additional Features
To enhance the model's predictive capabilities, additional features such as momentum, volatility, and distance are introduced. The calculation methods for these features are explained, and the process of refitting and retraining the model with these new variables is described. The updated model shows improved performance in the in-sample training set, and the strategy returns are recalculated, demonstrating an outperformance of the benchmark. However, when tested on the out-of-sample data, the strategy again underperforms, suggesting that the model may not be robust enough for real-world application.
🚫 Limitations and Considerations for Model Deployment
The speaker clarifies that the deep neural network is a simple classification model and not a sophisticated artificial intelligence or reinforcement learning agent. The model's limitations are acknowledged, particularly its underperformance in out-of-sample testing. The video serves as a cautionary tale about the importance of robust testing, including the use of out-of-sample data, to ensure the validity of any trading strategy. The speaker suggests potential areas for improvement, such as testing different instruments or time frequencies, and concludes by encouraging viewers to subscribe and explore more content.
Mindmap
Keywords
💡Deep Neural Network
💡Algorithmic Trading
💡Market Direction
💡Classification Problem
💡Bitcoin to US Dollar
💡Adjusted Close
💡Returns
💡Lags
💡TensorFlow and Keras
💡Normalization
💡Momentum
💡Volatility
💡Distance
Highlights
Implementation of a deep neural network for algorithmic trading to predict market direction.
Market direction prediction to decide between long or short trading positions.
Comparison of strategy returns against a buy-and-hold benchmark strategy.
Setup as a classification problem with market movement classified as 1 (upward) or 0/-1 (downward).
Use of Bitcoin to US dollar data for testing the neural network.
Data wrangling includes 10 years of Euro to US dollar exchange rates with adjusted close values.
Calculation of returns and market direction based on log returns.
Inclusion of lags for past five days to predict the next market value.
Implementation of TensorFlow and Keras for the neural network model.
Model architecture consists of two Dense layers and a Sigmoid output layer.
Use of accuracy as the performance metric for the neural network.
Data split using NumPy for training and testing with a 70-30 ratio.
Normalization of features using Gaussian distribution for model training.
Model evaluation showing accuracy and validation accuracy.
Conversion of predictions into long-short positions for strategy implementation.
Strategy returns calculation and comparison with benchmark to assess performance.
Plotting of strategy and benchmark returns to visualize performance.
Inclusion of additional features like momentum, volatility, and distance to improve model performance.
Refitting and retraining the model with additional features for enhanced prediction capabilities.
Evaluation of the updated model showing improved performance in-sample.
Out-of-sample testing revealing underperformance of the strategy against the benchmark.
Discussion on the importance of out-of-sample testing for validating trading strategies.
Cautionary tale highlighting the risks of overfitting and the need for robust backtesting.
Suggestion to experiment with different instruments and time frequencies for model improvement.
Final thoughts on the limitations of the current model and the need for further refinement.
Transcripts
Hey guys, we're going to be implementing another deep neural network for our algorithmic trading.
We're going to essentially predict whether or not a market is moving up or down, and based
on that market direction, we will place a long or short position, and then we'll compare the returns
of that strategy against the benchmark strategy of just buying and holding on to that financial
instrument. So it's going to be set up as a classification problem. If the market's moving
upward, it'll be 1. If it's moving downward, we're predicting 0 or negative 1, I should say.
So it'll make more sense when we jump into it, but for right now, let's just wrangle
up some data. We're going to be doing the usual suspects here. I'm going to try it with Bitcoin
to US dollar. When I wrote out this package, I just did the Euro to US dollar, but hopefully
we can do Bitcoin here. Let's actually just take a look to see if I can type here. There we go.
Okay, great. So we have it from 2014. I want a full year's worth, so let's actually just do
Euro equals X. What do we got here? Okay, great. So we're getting about 10 years worth of data
here. We're doing the Euro to US dollar. We're grabbing the adjusted close and nothing else.
We do want to calculate some returns. We also, and we do that just by taking the log of the adjusted
close divided by the log and the shifted over. We're also going to get the direction. So if the
return is greater than 0, we'll classify that as 1. Less than 0, we're going to classify that as 0.
Right, so we have our directions here, whether it's going up or if it's going down.
We also want to include lags because if you've seen other videos I've made on forecasting, you
use the past five days or the lag terms to predict the next value. So we'll go ahead and create
five additional columns and you'll see we get something that looks like this.
So we have our dates, adjusted close, return direction, lags 1 through 5. So now we're going
to implement our deep neural network. We're going to use TensorFlow and Keras and we're
going to be importing a sequential model and we're going to optimize with Atom. So let's get that.
There we go. So let's just walk through this a little bit. It's, I suppose,
three layers. We have two Reload layers and then a Sigmoid layer. We're just going to do 64
and we're going to use accuracy for our metrics. I'm not going to jump into this too deep because
this might turn into a long video, but just know that this is our neural network here and we've
just set our model as the sequential model and we're creating our dense layers here. A
good way to split up your time series data is using the NumPy split function. Now just pass
through your data and call the number. So we're going to do a 70-30 split. When I was writing
this out, I did an 80-20 split. I just want to see if we split the data differently, if
it makes any effect on the outcome. Now we need to normalize the features. So let's grab the average
and the standard deviation and we can normalize it using the Gaussian. I'm not exactly sure how
you pronounce that, but we're just going to create two additional variables where we take our train
and test that and we normalize the features. We're going to then, you can go ahead and see that I've
ran that already before, but we will fit the model on the columns in our train set here.
We're going to just do 50 epochs. We're not going to shuffle it because that wouldn't
really make any sense, right? We want to make sure that the data is in sequential order.
So great. Now that we have that, let's throw it all into our data frame there and
let's get the accuracy and the validation accuracy. Okay, this is pretty interesting.
So what are we looking at here? Basically, the blue line is the accuracy
on our... Okay, so when we broke up our train test data sets, we are basically also doing a
validation accuracy and an accuracy on that. So you can think of the accuracy that it's trained on
and then the validation accuracy is sort of like the out of sample accuracy based on
just that original test set. This is sort of a weird graph. I didn't expect it to even be this
close. I expected the validation test set to be farther down and then I'm not even sure what this
flat line is. It could be because of the split. It could also be for any other reason. We're doing US
dollar to euro when I wrote this last time. It was euro to US dollar, but let's just roll with
it. See what happens. So now let's evaluate the model. You can see we have an accuracy of 0.51 or
0.52. So now let's create a prediction and we're essentially saying, okay, if the prediction is
where it's greater than 0.5, let's say that's an upward direction, otherwise it's a zero.
And what that looks like is just an array of ones and zeros.
But this is where we'll flatten it so there's no floats and we get something that looks
like that and then we're going to transform the predictions into a long-short position.
So we're creating a variable using the predictions that we used on our fitted model
on the normalized training data set. And we're saying if the accuracy is greater than
the accuracy on what we just did here, where we evaluated the columns in the direction,
then we're going to assign that as a one. If it's lower than our accuracy prediction, we're going to
assign it as zero. So this is just saying if our confidence in our prediction is greater than our
test or our training, let's be confident and say that our prediction is that it's moving in the
direction that we've predicted it to. So then we're going to transform the predictions that
we've made here into a column in our training set as prediction, where if the prediction
is greater than zero, we're saying it's one, a long position or a negative one short position.
So then we're going to calculate the strategy, the returns given, the positions,
just by multiplying the prediction with the return and that'll represent our strategy in
the data frame. And let's go ahead and get the sum of it. So you can see here that our
strategy outperformed just the benchmark prediction in our in sample test set.
So let's plot it just to see what it looks like. And excellent. You can see the blue line is our
just benchmark returns of US dollar to euro. And if we had implemented our deep neural network
to predict the price movements is the orange line. You can see that we have a pretty good
spread. We're outperforming the return benchmark. So this is just the in sample training set.
Now let's do it on our validation. And it's essentially the same process.
So let me just roll on through that.
And you can see that we've done significantly worse than our
benchmark. And when we graph it, it looks like this.
So we've split it up a little bit differently. You can kind of see that it's just following along,
but then pretty early, you know, in before July and June or so, it looks like
things, you know, just started to get away from it. Especially July of 2021,
just couldn't really keep up with the predictions were underperforming the strategy,
underperforming the return. That's why it's important to evaluate your model on an out of
sample test set to actually validate whether or not your fitted model is working. One thing that
we can do to improve the performance of our model is to include some additional features that might
help it along. So let's add those features. We're going to add momentum, volatility,
and distance to our data frame. And then we're basically going to go through the same process.
And now let's take a look at our new data frame. So we have what we had before, adjust to close,
return direction, legs one through five. But now we have the momentum, we have the volatility,
and we have the distance. And it might be worth sharing how you calculate that. So momentum is
just the rolling average of the previous return, right? And then volatility is the return,
the rolling of a certain number. In this case, we're using 20, the standard deviation of that,
of that shift. And then the distance we're just defining as the adjust to close minus
the momentum, essentially. But the momentum is 50 instead of five. All right. And we're moving
the shift on the outside of that calculation. So that's also important for calculating distance.
Okay, great. Let's refit and train test. So we're doing, again, a 70-30 split on the test set,
and then we're going to normalize it using the same methods that we did above.
And we're going to update our dense layers to 32 instead of 64 to represent our additional
features. But just about everything else is the same. We're going to refit it.
And let's evaluate. You can see that it's performing a little bit better than our last one.
And we'll do the same thing. We'll create a predictions variable and
then transform that into a long short position.
Calculate the strategy returns. And here we go. The strategy has added returns.
And here we go. The strategy has outperformed the benchmark. So that's
really exciting to see. And it looks like it's done pretty well for itself.
And let's plot it. Bam! Look at that spread there. That's looking pretty juicy if you are interested
in maybe implementing this. But again, this is on the in-sample training set. So for our in-sample
training set, it looks like the model performs very well. It's able to outperform the returns.
But that's largely because the data has seen it already or had all that data inside of it. But the
true test of whether or not our model, especially a deep neural network model, is performing
well is by putting it to the test and seeing what the results are for an out-of-sample data set.
So let's go ahead and do that now. Again, we're just going to go through the model like before.
Please ignore some things that we've seen here. And so, okay, so one thing I forgot to mention
in the last one, it is kind of nice just to see what the value counts of our positions are. So our
model is putting out way more long positions than short positions. So that might be an
indicator of whether or not things are going well or things are going bad. And you can see, again,
on the out-of-sample, we are getting a strategy that is underperforming our benchmark returns.
But we can go ahead and plot that anyway and view the cumulative returns of this. And you
can see the strategy again, once again, sort of understands it in the beginning. And then
simply around July, again, it just starts to get away. And the benchmark returns of the instrument
outperform the strategy and the predictions that are being made by the deep neural network.
So I want to be clear, in case you're not as familiar with how classification works,
the neural network that we're implementing here is essentially, it's not an artificial intelligence,
it's not some kind of, you know, in other videos I've made a reinforcement learning type agent.
This is simply a deep neural network that we're trying to train to predict the market movements
of the US dollar to euro index. So in this case, in the data that I've compiled or in some way of
the way that I've gone through it, it wouldn't be, I definitely wouldn't put this in production
at all. It's clearly in our back testing, it's underperforming the benchmark returns.
But this is a solid way of getting started and tinkering about. Maybe you
want to include a different instrument, maybe you want to include a different,
you know, a time frequency. I think in our data it was day to day. You could do intraday and see
if that works any better. But this is a simple classification. Is it going up or is it going or
is it not going up basically? And whether or not we can build a model to predict that. I hope you
guys found that enjoyable and insightful. I know we weren't getting the kind of strategic returns
that we were getting or that we were hoping for, but again, this is a good cautionary tale to make
sure that whatever strategy you're implementing and testing and back testing, that you also
have an out of sample set that you can test that strategy against to make sure that you're actually
robustly testing your strategy or not. Because, you know, depending on how you're plotting your
data and your performance, you might think that you have a really great strategy when you don't.
Well, that's all for now. I'll see you in the next video. Bye.
Hey everyone, thanks for watching. If you've made it this far,
please consider subscribing or watching another video. I really appreciate it. Bye.
Ver Más Videos Relacionados
Neural Network Python Project - Handwritten Digit Recognition
ICT Charter Price Action Model #4 Position Trading
Trading forex menggunakan Fundamental (News) di forex factory : winrate tinggi
Understanding Market Makers || Optiver Realized Volatility Kaggle Challenge
ICT Charter Price Action Model 13 - Charter Lecture On 2022 YouTube Model
For Scalpers Only! How to Scalp The Charts Profitably in 2024
5.0 / 5 (0 votes)