Creating a Day Trading AI with a Deep Neural Network in Python

eminshall
4 Jun 202317:44

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

00:00

🤖 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.

05:00

📈 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.

10:05

🔍 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.

15:08

🚫 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

A deep neural network is a type of artificial neural network with multiple layers between the input and output layers, allowing it to model complex patterns in data. In the video, the presenter discusses implementing a deep neural network for algorithmic trading to predict market movements, which is central to the video's theme of financial forecasting.

💡Algorithmic Trading

Algorithmic trading refers to the use of computer programs to execute trades automatically based on predefined criteria. The video script describes the process of setting up a deep neural network to predict market direction for algorithmic trading, highlighting its importance in the context of automated financial decision-making.

💡Market Direction

Market direction refers to the predicted upward or downward movement of a financial market. The script outlines a classification problem where the market direction is categorized as 1 for upward movement and 0 or negative 1 for downward movement, which is essential for determining the trading strategy.

💡Classification Problem

A classification problem in machine learning involves predicting discrete labels for data points. The video's theme revolves around setting up a neural network to classify market movements as either up or down, which is a binary classification task.

💡Bitcoin to US Dollar

This term refers to the exchange rate between Bitcoin and the US Dollar. In the script, the presenter initially considers using Bitcoin to US Dollar data for the neural network model, indicating the currency pair's relevance to the trading strategy being discussed.

💡Adjusted Close

Adjusted close is the closing price of a financial instrument adjusted for factors such as dividends and stock splits. The script mentions using the adjusted close to calculate returns and direction, which is a common practice in financial analysis to account for corporate actions affecting the price.

💡Returns

Returns in finance refer to the profit or loss from an investment over a specific period. The video script discusses calculating returns by taking the log of the adjusted close, which is used to determine the market direction and to evaluate the trading strategy's performance.

💡Lags

In time series analysis, lags refer to the use of past observations as independent variables to predict future values. The script describes creating lagged terms to predict the next market value, illustrating the concept of using historical data for forecasting.

💡TensorFlow and Keras

TensorFlow and Keras are popular open-source software libraries used for machine learning and deep learning. The script mentions using these tools to implement the deep neural network, indicating their role in building and training the predictive model.

💡Normalization

Normalization is a technique used to scale data to a common scale without distorting differences in ranges. The video script describes normalizing features using the Gaussian method to prepare the data for the neural network, which is crucial for effective model training.

💡Momentum

Momentum in finance is a measure of the rate of change of an asset's price over time. The script discusses adding momentum as a feature to the model, which is intended to capture the trend direction and could influence the prediction of market movements.

💡Volatility

Volatility is a measure of the degree of variation of a trading price series over time. The script mentions calculating volatility as a feature, which is important for understanding the risk associated with market movements and can affect trading decisions.

💡Distance

In the context of the script, distance is defined as the adjusted close minus the momentum, which is a custom feature intended to capture the deviation of the current price from its moving average. This is used as an additional input for the neural network to improve predictions.

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

play00:00

Hey guys, we're going to be implementing another  deep neural network for our algorithmic trading.  

play00:08

We're going to essentially predict whether or  not a market is moving up or down, and based  

play00:16

on that market direction, we will place a long or  short position, and then we'll compare the returns  

play00:24

of that strategy against the benchmark strategy  of just buying and holding on to that financial  

play00:31

instrument. So it's going to be set up as a  classification problem. If the market's moving  

play00:38

upward, it'll be 1. If it's moving downward,  we're predicting 0 or negative 1, I should say.  

play00:46

So it'll make more sense when we jump into  it, but for right now, let's just wrangle  

play00:51

up some data. We're going to be doing the usual  suspects here. I'm going to try it with Bitcoin  

play00:58

to US dollar. When I wrote out this package, I  just did the Euro to US dollar, but hopefully  

play01:06

we can do Bitcoin here. Let's actually just take  a look to see if I can type here. There we go.  

play01:19

Okay, great. So we have it from 2014. I want  a full year's worth, so let's actually just do  

play01:36

Euro equals X. What do we got here? Okay, great.  So we're getting about 10 years worth of data  

play01:49

here. We're doing the Euro to US dollar. We're  grabbing the adjusted close and nothing else.  

play01:55

We do want to calculate some returns. We also, and  we do that just by taking the log of the adjusted  

play02:02

close divided by the log and the shifted over.  We're also going to get the direction. So if the  

play02:08

return is greater than 0, we'll classify that as  1. Less than 0, we're going to classify that as 0.  

play02:16

Right, so we have our directions here,  whether it's going up or if it's going down.  

play02:21

We also want to include lags because if you've  seen other videos I've made on forecasting, you  

play02:29

use the past five days or the lag terms to predict  the next value. So we'll go ahead and create  

play02:40

five additional columns and you'll see  we get something that looks like this.  

play02:47

So we have our dates, adjusted close, return  direction, lags 1 through 5. So now we're going  

play02:54

to implement our deep neural network. We're  going to use TensorFlow and Keras and we're  

play02:59

going to be importing a sequential model and we're  going to optimize with Atom. So let's get that.  

play03:10

There we go. So let's just walk through  this a little bit. It's, I suppose,  

play03:18

three layers. We have two Reload layers and  then a Sigmoid layer. We're just going to do 64  

play03:26

and we're going to use accuracy for our metrics.  I'm not going to jump into this too deep because  

play03:33

this might turn into a long video, but just know  that this is our neural network here and we've  

play03:40

just set our model as the sequential model  and we're creating our dense layers here. A  

play03:47

good way to split up your time series data is  using the NumPy split function. Now just pass  

play03:54

through your data and call the number. So we're  going to do a 70-30 split. When I was writing  

play04:02

this out, I did an 80-20 split. I just want  to see if we split the data differently, if  

play04:08

it makes any effect on the outcome. Now we need to  normalize the features. So let's grab the average  

play04:17

and the standard deviation and we can normalize  it using the Gaussian. I'm not exactly sure how  

play04:25

you pronounce that, but we're just going to create  two additional variables where we take our train  

play04:31

and test that and we normalize the features. We're  going to then, you can go ahead and see that I've  

play04:39

ran that already before, but we will fit the  model on the columns in our train set here.  

play04:47

We're going to just do 50 epochs. We're not  going to shuffle it because that wouldn't  

play04:52

really make any sense, right? We want to make  sure that the data is in sequential order.  

play05:00

So great. Now that we have that, let's  throw it all into our data frame there and  

play05:07

let's get the accuracy and the validation  accuracy. Okay, this is pretty interesting.  

play05:20

So what are we looking at here?  Basically, the blue line is the accuracy  

play05:28

on our... Okay, so when we broke up our train  test data sets, we are basically also doing a  

play05:42

validation accuracy and an accuracy on that. So  you can think of the accuracy that it's trained on  

play05:50

and then the validation accuracy is sort  of like the out of sample accuracy based on  

play05:58

just that original test set. This is sort of a  weird graph. I didn't expect it to even be this  

play06:04

close. I expected the validation test set to be  farther down and then I'm not even sure what this  

play06:11

flat line is. It could be because of the split. It  could also be for any other reason. We're doing US  

play06:22

dollar to euro when I wrote this last time. It  was euro to US dollar, but let's just roll with  

play06:28

it. See what happens. So now let's evaluate the  model. You can see we have an accuracy of 0.51 or  

play06:39

0.52. So now let's create a prediction and we're  essentially saying, okay, if the prediction is  

play06:51

where it's greater than 0.5, let's say that's  an upward direction, otherwise it's a zero.  

play06:59

And what that looks like is  just an array of ones and zeros.  

play07:04

But this is where we'll flatten it so there's  no floats and we get something that looks  

play07:11

like that and then we're going to transform  the predictions into a long-short position.  

play07:16

So we're creating a variable using the  predictions that we used on our fitted model  

play07:26

on the normalized training data set. And  we're saying if the accuracy is greater than  

play07:37

the accuracy on what we just did here, where  we evaluated the columns in the direction,  

play07:47

then we're going to assign that as a one. If it's  lower than our accuracy prediction, we're going to  

play07:54

assign it as zero. So this is just saying if our  confidence in our prediction is greater than our  

play08:05

test or our training, let's be confident and say  that our prediction is that it's moving in the  

play08:12

direction that we've predicted it to. So then  we're going to transform the predictions that  

play08:21

we've made here into a column in our training  set as prediction, where if the prediction  

play08:27

is greater than zero, we're saying it's one, a  long position or a negative one short position.  

play08:35

So then we're going to calculate the  strategy, the returns given, the positions,  

play08:42

just by multiplying the prediction with the  return and that'll represent our strategy in  

play08:48

the data frame. And let's go ahead and get  the sum of it. So you can see here that our  

play08:56

strategy outperformed just the benchmark  prediction in our in sample test set.  

play09:04

So let's plot it just to see what it looks like.  And excellent. You can see the blue line is our  

play09:14

just benchmark returns of US dollar to euro. And  if we had implemented our deep neural network  

play09:23

to predict the price movements is the orange  line. You can see that we have a pretty good  

play09:31

spread. We're outperforming the return benchmark.  So this is just the in sample training set.  

play09:41

Now let's do it on our validation.  And it's essentially the same process.  

play09:47

So let me just roll on through that.  

play09:59

And you can see that we've done  significantly worse than our  

play10:05

benchmark. And when we graph  it, it looks like this.  

play10:10

So we've split it up a little bit differently. You  can kind of see that it's just following along,  

play10:15

but then pretty early, you know, in  before July and June or so, it looks like  

play10:24

things, you know, just started to get  away from it. Especially July of 2021,  

play10:29

just couldn't really keep up with the  predictions were underperforming the strategy,  

play10:37

underperforming the return. That's why it's  important to evaluate your model on an out of  

play10:44

sample test set to actually validate whether or  not your fitted model is working. One thing that  

play10:51

we can do to improve the performance of our model  is to include some additional features that might  

play10:58

help it along. So let's add those features.  We're going to add momentum, volatility,  

play11:04

and distance to our data frame. And then we're  basically going to go through the same process.  

play11:14

And now let's take a look at our new data frame.  So we have what we had before, adjust to close,  

play11:19

return direction, legs one through five. But now  we have the momentum, we have the volatility,  

play11:24

and we have the distance. And it might be worth  sharing how you calculate that. So momentum is  

play11:29

just the rolling average of the previous return,  right? And then volatility is the return,  

play11:35

the rolling of a certain number. In this case,  we're using 20, the standard deviation of that,  

play11:43

of that shift. And then the distance we're  just defining as the adjust to close minus  

play11:48

the momentum, essentially. But the momentum is  50 instead of five. All right. And we're moving  

play11:59

the shift on the outside of that calculation. So  that's also important for calculating distance.  

play12:07

Okay, great. Let's refit and train test. So we're  doing, again, a 70-30 split on the test set,  

play12:17

and then we're going to normalize it  using the same methods that we did above.  

play12:24

And we're going to update our dense layers to  32 instead of 64 to represent our additional  

play12:31

features. But just about everything else  is the same. We're going to refit it.  

play12:43

And let's evaluate. You can see that it's  performing a little bit better than our last one.  

play12:52

And we'll do the same thing. We'll  create a predictions variable and  

play12:57

then transform that into a long short position.  

play13:01

Calculate the strategy returns. And here  we go. The strategy has added returns.  

play13:04

And here we go. The strategy has  outperformed the benchmark. So that's  

play13:10

really exciting to see. And it looks  like it's done pretty well for itself.  

play13:16

And let's plot it. Bam! Look at that spread there.  That's looking pretty juicy if you are interested  

play13:26

in maybe implementing this. But again, this is on  the in-sample training set. So for our in-sample  

play13:36

training set, it looks like the model performs  very well. It's able to outperform the returns.  

play13:43

But that's largely because the data has seen it  already or had all that data inside of it. But the  

play13:49

true test of whether or not our model, especially  a deep neural network model, is performing  

play13:55

well is by putting it to the test and seeing what  the results are for an out-of-sample data set.  

play14:05

So let's go ahead and do that now. Again, we're  just going to go through the model like before.  

play14:13

Please ignore some things that we've seen here.  And so, okay, so one thing I forgot to mention  

play14:19

in the last one, it is kind of nice just to see  what the value counts of our positions are. So our  

play14:26

model is putting out way more long positions  than short positions. So that might be an  

play14:32

indicator of whether or not things are going well  or things are going bad. And you can see, again,  

play14:37

on the out-of-sample, we are getting a strategy  that is underperforming our benchmark returns.  

play14:49

But we can go ahead and plot that anyway and  view the cumulative returns of this. And you  

play14:55

can see the strategy again, once again, sort  of understands it in the beginning. And then  

play15:00

simply around July, again, it just starts to get  away. And the benchmark returns of the instrument  

play15:07

outperform the strategy and the predictions  that are being made by the deep neural network.  

play15:14

So I want to be clear, in case you're not  as familiar with how classification works,  

play15:21

the neural network that we're implementing here is  essentially, it's not an artificial intelligence,  

play15:29

it's not some kind of, you know, in other videos  I've made a reinforcement learning type agent.  

play15:36

This is simply a deep neural network that we're  trying to train to predict the market movements  

play15:47

of the US dollar to euro index. So in this case,  in the data that I've compiled or in some way of  

play15:55

the way that I've gone through it, it wouldn't  be, I definitely wouldn't put this in production  

play16:02

at all. It's clearly in our back testing,  it's underperforming the benchmark returns.  

play16:09

But this is a solid way of getting  started and tinkering about. Maybe you  

play16:17

want to include a different instrument,  maybe you want to include a different,  

play16:22

you know, a time frequency. I think in our data  it was day to day. You could do intraday and see  

play16:31

if that works any better. But this is a simple  classification. Is it going up or is it going or  

play16:39

is it not going up basically? And whether or not  we can build a model to predict that. I hope you  

play16:46

guys found that enjoyable and insightful. I know  we weren't getting the kind of strategic returns  

play16:52

that we were getting or that we were hoping for,  but again, this is a good cautionary tale to make  

play16:59

sure that whatever strategy you're implementing  and testing and back testing, that you also  

play17:05

have an out of sample set that you can test that  strategy against to make sure that you're actually  

play17:15

robustly testing your strategy or not. Because,  you know, depending on how you're plotting your  

play17:22

data and your performance, you might think that  you have a really great strategy when you don't.  

play17:28

Well, that's all for now. I'll  see you in the next video. Bye.  

play17:34

Hey everyone, thanks for watching.  If you've made it this far,  

play17:37

please consider subscribing or watching  another video. I really appreciate it. Bye.

Rate This

5.0 / 5 (0 votes)

Related Tags
Algorithmic TradingDeep LearningMarket PredictionBitcoinEuro to USDNeural NetworkData WranglingFinancial AnalysisMachine LearningTrading StrategyPerformance Evaluation