AI Trading Using Machine Learning (Step-by-Step) — Transcript
Full transcript
- 0:00Hey guys, so that. I'm going to show you
- 0:01how to use machine learning for trading
- 0:04in Python [music] and we're going to
- 0:05start from the very beginning. You don't
- 0:07have to be an expert and you wouldn't
- 0:08believe how easy it is. But I'm not
- 0:10going to lie to you. There are some
- 0:12caveats and problems when using machine
- 0:14learning in trading, but that brings me
- 0:16to the best part of the video, which is
- 0:18not only I'm going to show you the
- 0:19implementation, but I will show you a
- 0:21clear path for moving on so that you can
- 0:24keep going on till you get the results
- 0:25that you want. Now to get to this point,
- 0:27I had to do a lot of research and in try
- 0:29a lot of different implementations to
- 0:31get to this point. And one book which I
- 0:33found really helpful was called Advances
- 0:35in Financial Machine Learning by Marcos
- 0:37Lopez and I made it really easy to
- 0:39implement his concepts. I'm going to
- 0:40show you what I mean by that in a
- 0:41minute. All right, if all that sounds
- 0:43good, let's get right to it.
- 0:47All right, so let's spend a minute and
- 0:48talk about what is machine learning in
- 0:50the first place. Now machine learning is
- 0:52a way for the computer to learn from
- 0:54some patterns in order to predict
- 0:56something. Now if that sounds confusing,
- 0:58don't worry. But for now, just know that
- 1:00it is the opposite of setting some
- 1:02rules, which is what we usually do. So
- 1:03let me show you that with an example. So
- 1:05suppose we want to define a very simple
- 1:07function in Python which just adds two
- 1:09number, all right? So we would say
- 1:11something like this. So define some
- 1:14function and it's going to take two
- 1:17parameters and it's going to return A
- 1:20plus B. And if I were to use this
- 1:22function,
- 1:23I would simply do this. So 1 plus 2 and
- 1:27and let's, you know, print the values of
- 1:29it. So now if I go ahead and just run
- 1:32this file, I would get number three,
- 1:34right? So again, nothing fancy. But what
- 1:38if in a very special scenario, we did
- 1:41not know this logic. So we didn't know
- 1:44that to sum up two numbers, you're going
- 1:46to have to use this very special
- 1:48character to make it happen. So maybe it
- 1:51was something complex, right? If we were
- 1:53to use machine learning to solve this
- 1:55problem, to be able to add two numbers.
- 1:57Instead of this, we would have had to
- 1:59have some numbers. So, first we're going
- 2:01to have to prepare some values. So, for
- 2:04example, if the first two values, which
- 2:06is one and two, and the third one, which
- 2:09was the result, was three, well, we're
- 2:12telling the model, "Okay, so one and two
- 2:15equals three." Okay, but this one is not
- 2:17enough, right? So, we could give it
- 2:19another one. So, three and four, and
- 2:21then the third one is going to be what?
- 2:23Seven.
- 2:24And then we can again repeat. So, two
- 2:27and three, and the result is going to be
- 2:29five. And even my autocomplete, the one
- 2:31that I have on my editor, is learning
- 2:33this very fast, right? So, if I give it
- 2:35like Actually, no, it's a bit dumb. So,
- 2:370 + 9 equals again 9.
- 2:40And 0 by 0 equals 0. Okay, so this one's
- 2:44good. 1 + 6 = 7. So, you see, even the
- 2:47autocomplete on my machine is learning
- 2:50learning this very fast. So, now imagine
- 2:52if, instead of like seven or eight
- 2:54examples, I had like 1,000 examples.
- 2:57What machine learning does is that
- 2:59you're going to feed it these values,
- 3:01and it's going to give you this
- 3:04function. Although what gives you, it
- 3:06doesn't actually have these values, all
- 3:09right? So, it's just going to be the
- 3:11result of it. So, it's going to be a
- 3:12function called whatever, in this case
- 3:15like some funk.
- 3:16And this function is going to be able
- 3:19that when you give it a value such as
- 3:21like five and six or seven,
- 3:24it's going to give you
- 3:26the number 12. Without actually knowing
- 3:29the logic behind it, because it learned
- 3:32from the examples that you gave it. All
- 3:34right, so this makes sense. But what
- 3:37about in trading? Like, what are we
- 3:39going to try to learn and what? So,
- 3:41usually we we want to follow some exact
- 3:44and clear entry and exit rules for the
- 3:46strategy. So, for example, we want to
- 3:48say, "If the RSI is above this value,
- 3:50and the EMA crosses like the other EMA
- 3:52line, which is like faster or slower,
- 3:55and then we want to buy or open a long
- 3:56position. And then if the opposite of
- 3:58this happens, then the trend is against
- 4:00us and we want to look liquidate the
- 4:02position or exit it in other words. So,
- 4:04we have to have these indicator values
- 4:06in order to define these rules. And we
- 4:08also need to know these rules. But the
- 4:10problem is that so many people are
- 4:12following the exact same rule. So, for
- 4:13example, if you try to use something
- 4:15such as the RSI or the Bollinger Bands,
- 4:17let's say alone and that one single
- 4:19indicator, at this point the edge of it
- 4:22is mostly gone because so many other
- 4:25people know about this, right? So, like
- 4:27we know how many people just use
- 4:28TradingView. But what if you were to use
- 4:30some new data? So, for example, the
- 4:33funding fee of the exchange or the
- 4:34volume, things that not as many people
- 4:36use or any other kind of alternative
- 4:39data that you know has some kind of
- 4:41predictive power or at least you can
- 4:43guess that it does. But you cannot put
- 4:45it into words, like you cannot write an
- 4:47exact rule for it
- 4:50or cannot find a profitable one. Well,
- 4:52what if you could search through the
- 4:54data and just find something that
- 4:57actually works? Well, machine learning
- 4:59can do that for us. But what kind of
- 5:01features do you want to use? Well, in
- 5:03the case of trading, we could use
- 5:05anything. It could be some kind of
- 5:07indicator value such as the RSI or the
- 5:09EMA. It could be the volume, which again
- 5:12to me personally is never easy to use
- 5:14with an indicator, but with machine
- 5:16learning that is very different. It
- 5:18could be the time, like what time of the
- 5:20day it is, which day of the week we are
- 5:22in, which part of the month are we in,
- 5:24or the weekend or the beginning of every
- 5:26week. So, we know that these days are
- 5:29going to be different. But how are you
- 5:31going to use that with an indicator? But
- 5:33again, with machine learning you could
- 5:34just feed it into the model and maybe,
- 5:37just maybe, it can find a good pattern.
- 5:39All right, but the next question is what
- 5:42should the model try to predict? Like
- 5:44what exactly? Because the simplest one
- 5:46that comes to mind is the price, right?
- 5:48Well, actually, if you ask people who do
- 5:51this thing properly or successfully, if
- 5:55you look them up, they're going to tell
- 5:56you trying to predict the price is the
- 5:58hardest thing to do. So, there are much
- 6:00easier ways to use machine learning.
- 6:03Now, predicting the price, yes, it is
- 6:05one way to use it. Another way is to
- 6:08find a way to set the position sizing of
- 6:10your position. Another way is to simply
- 6:12use it as some sort of filter, like is
- 6:15this trade that I'm I'm actually about
- 6:17to take, is it profitable or not? If the
- 6:19model says it is, I'm going to take it.
- 6:21If it says it's not, I will not take it.
- 6:23And by doing this, we're going to
- 6:25improve our win rate. So, this is
- 6:26another type to do it. But, one which is
- 6:29very simple and handy, and what we're
- 6:32going to try in this video, is to simply
- 6:35try to predict the direction of the
- 6:37price. So, not the exact price, but the
- 6:39direction of it, like whether it's going
- 6:41up, whether it's going down, or whether
- 6:43it's in a sideways. And if I want to
- 6:46take, let's say, long positions, of
- 6:48course, I will only take it if the model
- 6:50says that the price is about to go up.
- 6:52So, that's it. I will only try to
- 6:53predict the direction of the price. So,
- 6:55for an uptrend, it will just give me
- 6:57one. For a downtrend, it will give me
- 6:59minus one. And for a sideways, it will
- 7:00give me zero. So, basically, like any
- 7:03kind of indicator that I would usually
- 7:05use in a strategy for the direction of
- 7:07the trend. Now, in machine learning,
- 7:09because we only have three types of
- 7:11outputs, this is called a classification
- 7:13problem. Now, another way you might have
- 7:15heard about a classification problem in
- 7:17machine learning is when you train a
- 7:18model to simply tell you if in this
- 7:21photo, is this a cat, is it a dog, or is
- 7:23it a human, for example. So, in this
- 7:26case, if you only expect three these
- 7:28outputs, it's going to be again a
- 7:30classification problem with three
- 7:32outputs only. Now, of course, this was a
- 7:34super simplification way of putting
- 7:36things. But, before I move on to the
- 7:38code, I want to talk about something
- 7:40really important. So, even if you are
- 7:42already with machine learning, I'm
- 7:44betting that this has been a really big
- 7:47problem for you. For me personally, this
- 7:49was the very reason why I tackled this
- 7:52thing way later than what I should have.
- 7:54All right. Now, this is the best part of
- 7:56the video because even if you have used
- 7:58machine learning in the past
- 7:59successfully in other fields, you're
- 8:01going to need it because I'm going to
- 8:02show you a solution that I found to a
- 8:05big problem. Now, what's the big
- 8:06problem? Well, you see, when you want to
- 8:08use machine learning for a simple
- 8:09problem such as detecting whether the
- 8:11photo is a dog or a cat. You simply feed
- 8:13it, let's say, thousands of photos, and
- 8:15that's it. The model can predict it
- 8:17pretty well with a very good accuracy.
- 8:18Or even when the model is trying to
- 8:20learn to play a game in the case of
- 8:22reinforcement learning, which you don't
- 8:24have to know what exactly it is, but my
- 8:26point is if you have a problem that is
- 8:29simple, it doesn't change, or in other
- 8:31words, in technical words, it is a
- 8:33stationary, then it is very easy to
- 8:35train a model. But in finance, we're
- 8:37dealing with random data, random noise,
- 8:40and markets which are changing all the
- 8:42time. And even different markets usually
- 8:45behave differently. So, often times you
- 8:47write a strategy, let's say, for BTC,
- 8:49but when you try to trade the same
- 8:50strategy on an altcoin such as, let's
- 8:52say, SOL USDT, you find out that it
- 8:55doesn't work at all. That's a really a
- 8:56scary problem, which means the accuracy
- 8:58of your model it's it's not going to be
- 9:00like 99%. It might be 60%. It might be
- 9:03significantly lower. Now, we all know
- 9:05this problem exist, but as I said, even
- 9:07if you get accuracy of, let's say, 60%,
- 9:10it's good, right? Like especially if
- 9:12strategy has a win rate of, let's say,
- 9:1340%, and then out of nowhere a model can
- 9:16have an accuracy of 60% just to know if
- 9:18the trades are profitable or not, that's
- 9:20going to be huge. But,
- 9:22here's the problem that I didn't have a
- 9:24solution for. You see, suppose I write
- 9:27my implementation, and I feed it some
- 9:29data. Now, it could be any data, let's
- 9:31say, just price data and volumes and
- 9:33some indicator values, the things that
- 9:35we easily have, not some alternative
- 9:37data such as the funding fees or things
- 9:39like that, okay? Now, suppose that I'm
- 9:41not getting good results. I'm getting
- 9:43negative results. Like my strategy isn't
- 9:45profitable. So, yes, we are getting the
- 9:48direction of the trend using this model
- 9:50which I'm about to train, but how should
- 9:52I know the problem is with my model or
- 9:55with the implementation that I've done?
- 9:57So, I thought about this question for a
- 9:59long time and like I said, I postponed
- 10:01machine learning altogether until I came
- 10:04up with a solution. In programming, we
- 10:06have this concept called TDD, which it
- 10:08stands for test-driven development. So,
- 10:11let me show you very quickly what that
- 10:12means in case you're not a super
- 10:14developer or you had to you just haven't
- 10:16heard about this. All right, so let's
- 10:18copy everything we had before. Okay, so
- 10:21you remember this example that I gave
- 10:23earlier? So, 1 + 2 = 3, 3 + 4 = 7, 2 + 3
- 10:28is supposed to be like 5. Now, if you
- 10:31want to implement this using TDD or
- 10:33again, test-driven development in
- 10:35Python, first we would have to write the
- 10:37test and then we would write the
- 10:39implementation. So, here's the example.
- 10:41So, I'm going to write a simple test
- 10:43function first, all right? So, I'm going
- 10:45to call it test some function and in it,
- 10:48I will say assert that some function
- 10:52when I give it 1 and 2, the result is
- 10:53going to be 3.
- 10:55We can also test for other scenarios
- 10:58because let's say the function is
- 11:00supposed to get one of them right, but
- 11:02just in case, I want to add some other
- 11:04scenarios, right? So, again, now, 3 and
- 11:074 = 7. All right, so it wrote these
- 11:10based on these examples that we had
- 11:11before. Very clever, actually. So, if I
- 11:14just do it one more time, yes, this is
- 11:16correct and so is this one. All right,
- 11:19so now we have all these examples here,
- 11:21right? You see we're getting an error
- 11:23here because we haven't defined the
- 11:25function yet. So, that was my point. So,
- 11:26first we define the test and then we
- 11:28define the function. Now, it doesn't
- 11:30really matter, but my point is that
- 11:32we're to have to have tests for it,
- 11:34right? So, now
- 11:36I can write the function itself. So, def
- 11:39and some function. When I give it A and
- 11:42B, it should return A plus B. And now
- 11:44we're not getting an error anymore,
- 11:46right? Now, just for this to make a
- 11:48little bit more sense, let's duplicate
- 11:49this line and comment these. And now,
- 11:53let's say I was going to say that 1 + 2
- 11:56does not equal 3. It equals, let's say,
- 11:585. Right? So, we know that this is
- 12:00incorrect, right? Now, we know this
- 12:02because we humans can easily sum up two
- 12:05integer numbers. That's very easy for
- 12:06us. But again, imagine that this is a
- 12:08super complicated function. So, it's not
- 12:11a simple one, and we don't know what
- 12:13it's supposed to return. But, now that
- 12:15we're trying to use TDD to write this,
- 12:17we know that the sum of 1 and 2 equals
- 12:213. So, we know that 5 is incorrect,
- 12:24right? So, I'm going to run this file by
- 12:26simply saying pytest tdd.py. Now, it
- 12:28doesn't matter what pytest is. Just
- 12:30forget about it. The point is it's how I
- 12:33am executing this, and I just want to
- 12:35show you the concept, okay? So, if I run
- 12:36this, it's going to tell me that it's a
- 12:38fail. And it is saying that 3 does not
- 12:42equal 5. So, this is what we were
- 12:43expecting, right? So, 3 should equal 5,
- 12:45but it doesn't. So, it is failing. So,
- 12:48if I comment this and bring these back,
- 12:51which are the correct values, and run
- 12:53this one more time, it's going to give
- 12:54me a pass. Now, forget these warnings,
- 12:56okay? So, it's giving me a pass. So, now
- 12:58that I'm getting a pass, I can be sure
- 13:01that my implementation of summing two
- 13:03integer values is correct. Why? Because
- 13:06all these tests are passing. Now, what
- 13:09if we did the same thing in machine
- 13:11learning? Well, if you run a test for
- 13:13it, and then it passes, then I can
- 13:17easily run a back test or deploy it for
- 13:19life. And if it doesn't work, I can be
- 13:22sure that my machine learning
- 13:24implementation is correct, and the
- 13:26problem is from somewhere else. Maybe
- 13:29it's in my position sizing, risk
- 13:31management, maybe it's in my exit rules
- 13:33of the strategy, or any other part of
- 13:35it. But, if it failed, if the unit test,
- 13:37or whatever kind of test that you want
- 13:39to call it, if that test failed, I can
- 13:41be sure right there that my
- 13:43implementation is incorrect. Then, I can
- 13:45dig further. Maybe the data that I'm
- 13:47feeding is incorrect, maybe the features
- 13:49are incorrect, maybe the labeling way
- 13:51that I'm doing is incorrect. Whatever,
- 13:53it doesn't matter. The point is I can
- 13:55know exactly where I should look for the
- 13:58problem to solve it. Now, I hope this
- 14:00all made sense to you, and I don't know
- 14:03about you, but as someone who has
- 14:04struggled with this concept, this simple
- 14:07solution that I just explained, it
- 14:09changed everything for me. So, in the
- 14:11rest of the video, yes, I'm going to
- 14:13implement things, but we're also going
- 14:15to test it, and that's the really
- 14:17important part. All right, so now that
- 14:19I've explained these concepts, I can
- 14:21move on to the actual strategy. Now, as
- 14:23always, I'm going to use the Jesse
- 14:24framework, which just recently added the
- 14:26machine learning and stuff, and you kind
- 14:28of see how easy it makes it for both
- 14:31training the model and deploying it for
- 14:33back test, live trading, or whatever
- 14:35that you want to do. All right, so,
- 14:37we're going to have one script to
- 14:39collect the data and train it, and then
- 14:42run the actual back test, and we're
- 14:44going to have the strategy file itself,
- 14:46which is basically the one you guys care
- 14:48the most. So, let's begin with that one.
- 14:50So, you see, here we have a simple
- 14:52strategy class, which is inheriting from
- 14:54the strategy class of Jesse. Now, if you
- 14:56have watched my previous videos or
- 14:58familiar with this framework even a
- 15:00little bit, you already know what these
- 15:02are and what type of properties it gives
- 15:04you in order to write a strategy, which
- 15:06makes it really easy. Now, we also have
- 15:08some comments here, which will describe
- 15:09the strategy's logic if you want to go
- 15:11through that. But, for now, let's just
- 15:13talk about what we are trying to predict
- 15:16here, okay? So, the method that we're
- 15:18trying to use here is called the triple
- 15:20barrier vertical method, okay? Now, I
- 15:23got this concept from the book that I
- 15:26mentioned in the beginning of the video.
- 15:27So, you could go and give it a watch,
- 15:29but it's actually super simple and the
- 15:32book didn't really add anything except
- 15:34just maybe one thing or maybe it made it
- 15:36a little bit clearer for me. Now, what
- 15:38is it? Well, we simply try to predict
- 15:41that n bars from now, if the price is
- 15:43going to be higher, lower, or if it's
- 15:47going to almost stay the same or in the
- 15:50concept of trading, are we going to be
- 15:52in a range market? Now, why is this
- 15:54important? Because usually when we open
- 15:56a position, like assuming that we only
- 15:58have one entry and one exit, the entry
- 16:01could, let's say, be done by a market
- 16:03order. Nothing fancy. But, the exit
- 16:06could happen with either a stop-loss
- 16:08order or a take profit, which is usually
- 16:10a limit order, right? And let's say it's
- 16:12a long position, all right? So, if the
- 16:14upper barrier or, you know, the higher
- 16:16line is touched first, we're going to
- 16:18say, "Okay, so this is a plus one." As
- 16:21if the direction of the trend is toward
- 16:22up or, in other words, we are in an
- 16:24uptrend. And if the stop-loss is going
- 16:27to be touched first, meaning that we're
- 16:28going to lose money, then this model is
- 16:30supposed to return minus one. In other
- 16:32words, it's saying that, "Hey, maybe
- 16:34we're in a downtrend, so don't take any
- 16:36long positions." And if it returns zero,
- 16:38it means that our vertical barrier is
- 16:41being touched first. Now, what is a
- 16:42vertical barrier? Well, you see, I said
- 16:44that after n bars, okay? So, we need to
- 16:46have some kind of box. So, suppose that
- 16:48we open the position
- 16:50right now. Now, n bars from now, now n
- 16:52could be any number, such as, let's say,
- 16:5410 bars. All right? So, 10 candles from
- 16:57now, what is going to be the price?
- 16:59Like, are we going to touch the upper
- 17:01barrier first or the lower barrier
- 17:03first? Or, if we're not going to touch
- 17:05either of them, we're going to consider
- 17:07it a range market and in that case we're
- 17:09going to return zero, because we have to
- 17:11have some kind of window, right? So, we
- 17:13cannot, like, open the position and wait
- 17:15like three months for a simple scalping
- 17:17strategy. It doesn't make sense. So,
- 17:19there has to be some kind of window. And
- 17:21we're going to have to define the
- 17:22window. And in this example, I defined
- 17:25it the number 50, which you can find
- 17:27here. So, feel free to change it however
- 17:29you like. But basically, so that's what
- 17:31we're trying to predict, right? So, it's
- 17:32a simple classification problem, and it
- 17:35either gives us minus one, plus one, or
- 17:38zero. That's it. Let's move on to the
- 17:40other parts of the strategy, starting
- 17:41with the before function, which is
- 17:44basically the one that you're going to
- 17:45use especially with the type of machine
- 17:48learning that we are using in this
- 17:50strategy, which is the triple barrier
- 17:52method. Okay. Now, we're simply saying
- 17:54that if you are in the gather mode,
- 17:55return because if we are in the deep
- 17:57play mode, we don't want to do the
- 17:59training and stuff that we're going to
- 18:00do right now. We just want to use the
- 18:02model, okay? Which we're going to cover
- 18:04later. All right. So, we're simply
- 18:06saying that if you haven't recorded
- 18:07anything yet, let's record the features,
- 18:10which I'm going to show you how that is.
- 18:12And then we are setting the upper
- 18:14barrier, the lower barrier, the index,
- 18:18the index that we started doing this,
- 18:20which is right now. And we simply give
- 18:22it the current index by simply saying
- 18:24self.index because that's a built-in
- 18:26property of Jesse. And then we're going
- 18:28to say, "Okay, so features have been
- 18:30recorded." So, this is this flag. And
- 18:31what it does is that on the next candle,
- 18:34we're not going to go through this.
- 18:35Okay? Not until at least like we have
- 18:38successfully recorded one whole you know
- 18:40record for machine learning. Now,
- 18:42starting the next candle, we're going to
- 18:44go here, right? So, we're saying, "Okay,
- 18:46has the upper barrier been touched?" And
- 18:49to do that, we're saying, "Okay, if the
- 18:51current price is above it, then it's
- 18:52been touched." We do the opposite for
- 18:54lower barrier. And then for the
- 18:56vertical, we're using the time. So,
- 18:58we're saying, "If the current index
- 18:59minus the recorded index, which we
- 19:01recorded here, is more than the vertical
- 19:03barrier, which we set it to number 50,
- 19:06if you remember." Okay, so it was here.
- 19:08Again, this could be any number that you
- 19:09want. And we're saying, "If the upper
- 19:11barrier have been touched or the lower
- 19:13one or the vertical, then the label is
- 19:16going to be one if it was the upper
- 19:18barrier. It's minus one if it was the
- 19:20lower barrier and it's zero if it was
- 19:23the vertical, okay? And then we are
- 19:25using self.record_label
- 19:27function of Jesse to record it. And
- 19:29we're giving it a name, which could be
- 19:30anything you want. And we're setting the
- 19:32value, which again is either 1, -1, or
- 19:350. And then we reset these flags, okay?
- 19:37So, nothing fancy. So, so far you have
- 19:41used two functions for machine learning.
- 19:43One is the record features, which is
- 19:45where we basically give the inputs of
- 19:47the model, and second is the record
- 19:50label, which is where we get the output
- 19:52of the model. Although, because we are
- 19:54in the training mode right now or gather
- 19:56mode as we are calling it here, we have
- 19:58to feed the output to the model. So,
- 20:00that's the thing. When we are in the
- 20:01gather mode or training mode, we have to
- 20:03feed the model both the input and the
- 20:05output. But, when we are in the deploy
- 20:07mode, that's when we're going to say,
- 20:09"Okay, here's the input. Now, give me
- 20:11the output. Predict the output." So,
- 20:13that's what machine learning does, all
- 20:14right? Now, what are the features that
- 20:16we are feeding it? So, the features are
- 20:18here. So, this is the function that you
- 20:20have to define in your machine learning
- 20:21in Jesse. And this has to be the exact
- 20:23name of it. And what it does is it
- 20:26simply returns a dictionary. Now, in
- 20:28here, these are the things that I have
- 20:29defined and, you know, I'm explaining it
- 20:32a little bit here in the comments. It
- 20:33doesn't matter. But, the point is we are
- 20:35defining the ATR, the current price, the
- 20:37EMA 9, 21, 50, the recent close numbers,
- 20:42the Keltner indicator, and some things
- 20:44like that. But, if you notice, I'm not
- 20:46just passing these values because they
- 20:47are not stationary, I'm turning them
- 20:50stationary first and then I'm using
- 20:52them. So, for example, here instead of
- 20:53simply the ATR, I'm saying ATR divided
- 20:56by the current price. Instead of simply
- 20:57the EMA 21, I'm saying 21 minus 50
- 21:00divided by EMA 50. So, I'm giving it a
- 21:02ratio. Because these values are
- 21:05stationary. But, if again, if I simply
- 21:07give it the price or the EMA, which also
- 21:10looks like the price, it's not going to
- 21:11be a stationary. Now, by the way, I made
- 21:12a whole page on our documentation about
- 21:15the stationary and why that is
- 21:16important. So, definitely make sure to
- 21:18check it out. Anyways, so we are
- 21:20returning a dictionary of values. So,
- 21:22those are my inputs and again, my
- 21:24output. So, that's it. That's all we had
- 21:26to do in order to train the model. Now,
- 21:29the next part is using it and that's
- 21:31also going to be really simple. So, for
- 21:33using the strategy, we're going to use
- 21:35the typical Jesse functions, the should
- 21:37long, should short, which are used to
- 21:39know whether or not we want to open a
- 21:41long or short position. So, basically,
- 21:42the entry rules of the strategy. And in
- 21:44it, we are simply saying that if you are
- 21:46in the gather mode, return false. So,
- 21:48because we don't want to be recording
- 21:49any trades in the gather mode or
- 21:52training phase. And again, this is just
- 21:54for this type of machine learning. For
- 21:55another one, for example, if you want to
- 21:58know if your trade is going to be
- 21:59profitable or not, you actually want to
- 22:00take trades and that's going to be the
- 22:02data that you're going to train the
- 22:03model on. But, in the triple barrier
- 22:06method, which we are simply trying to
- 22:08predict the direction of the trend, we
- 22:11don't want to be taking any trades. All
- 22:12right, so in it, we're simply saying the
- 22:14probs or probabilities is going to be
- 22:16self.ml.predict_probability.
- 22:19And this is a built-in function of
- 22:20Jesse. So, it makes it super simple. So,
- 22:23basically, assuming we already have the
- 22:24model, this is all I have to run in
- 22:27order to take it. And now, I have both
- 22:30the probability of the price going up
- 22:32and it going down. Now, I could also get
- 22:34it, you know, being in the vertical, but
- 22:36I don't need it here, so that's why I'm
- 22:37not using it. But, the entry rule of the
- 22:40strategy is going to be, so if the
- 22:42probability of the price going up is
- 22:43bigger than my threshold and if the
- 22:45probability of it going up is also more
- 22:48than 20% more than probability of it
- 22:51going down, then I want to take a long
- 22:53position. So, again, this is just
- 22:54something I defined. You could play
- 22:56around with the numbers. And also, if
- 22:57you want to see what's the threshold,
- 22:59well, we defined it here, so we set 45%.
- 23:01So, we want to know if the model has at
- 23:04least,
- 23:05you know, a little bit of confidence
- 23:07before actually taking a trade. Now,
- 23:09this is a test strategy that I'm running
- 23:11just to demonstrate how these things
- 23:13work, but in a production strategy, you
- 23:16probably want to have this condition as
- 23:19a secondary or just, you know, one other
- 23:22filter for your strategy. So, you
- 23:24probably don't want to use it as the
- 23:26single point of truth for the entry rule
- 23:29of the strategy. And that's it, guys.
- 23:31Now, for the short position, we're doing
- 23:32the opposite, and this is where we do
- 23:34the position sizing, the go long and go
- 23:35short method. So, assuming that the
- 23:37should long is returning true, we say,
- 23:39"Okay, so now let's go long." And if
- 23:42that's the case, my entry is going to be
- 23:43this price, which we passing the current
- 23:45price, in other words, the market order.
- 23:48And distance is something that I'm
- 23:50calculating here, simply using the ATR,
- 23:52and then I'm getting the quantity of it,
- 23:55and I'm submitting the buy order, which
- 23:56is my entry order, the stop loss, and
- 23:58the take profit. Now, usually I submit
- 24:01these two in the on open position method
- 24:04of Jesse, which is this function where
- 24:06it says, "Okay, so now that we have an
- 24:08open position, let's submit the exit
- 24:10orders," which would be these two, but
- 24:12we can also set it here, so this is
- 24:14perfectly fine. And that's it. That's
- 24:16all that we had to do. So, let's go up,
- 24:20and yep, so this is the distance that I
- 24:21talked about, which I'm using the ATR,
- 24:24and that's it. Like, this was an entire
- 24:27strategy for using machine learning in
- 24:29Jesse. So, that's how simple it just
- 24:32got. But now, let's move on to the
- 24:33script, which you're going to have to
- 24:35run. Now, in the documentation, I have
- 24:38mentioned a couple of scripts and how to
- 24:39run them, but here I'm going to leave
- 24:41you with one, at least for the triple
- 24:43barrier method, and I'm using the TDD.
- 24:45Now, what is TDD? Well, I I explain. So,
- 24:47basically, we're creating some synthetic
- 24:49data, right? Some fake data. So, first
- 24:52we have to create that fake data, and
- 24:55I'm importing some libraries and also
- 24:57the strategy I just showed you guys and
- 24:59I'm defining the number of regimes, you
- 25:01know, the price regimes that are going
- 25:03to change and the total number of
- 25:05candles I'm giving it 6 months. The
- 25:07exchange I named it test exchange. The
- 25:09symbol is test USD and the time frame is
- 25:1115 minutes. We are calculating some
- 25:13stuff for the one hour candles and so on
- 25:15and this is just for the printing output
- 25:18and this is where we define the price
- 25:20regime. So first we're going to have an
- 25:22uptrend, sideway, a downtrend and again
- 25:24a sideway and again we're going to
- 25:26repeat this whole thing four times which
- 25:27will give us four in three
- 25:30in total. Now you can also change this
- 25:32and 6 months may not be enough and again
- 25:34this is just a test one. Feel free to
- 25:36change it however you like and then
- 25:38here's where we actually generate the
- 25:39close data, right? So we're saying that
- 25:42okay,
- 25:42this is a empty list.
- 25:44The starting price is 1,000 and if the
- 25:47direction is plus one, we're going to
- 25:50add to the price by 2.5. If it's in a
- 25:52downtrend, we want to subtract it and if
- 25:55it's in a sideway, we are just using a
- 25:57sinus method to add to it. And that's
- 26:00it. It returns the prices. Now these are
- 26:02just for printing stuff, not important.
- 26:05And then the first phase which is the
- 26:06gather mode, again printing itself
- 26:08doesn't matter. We're getting all the
- 26:10candles. We are defining the routes
- 26:12according to what Jesse expects. So
- 26:14we're simply giving it the exchange
- 26:16symbol and the candles and we're
- 26:18returning the config. The fees are set
- 26:20to zero. I'm passing the routes, the one
- 26:22hour candles and everything and that's
- 26:24it. This is returning the data points.
- 26:26In the second phase which is where we do
- 26:27the retraining, we're using the train
- 26:29model method of Jesse. Now this method
- 26:32has been added to the research module of
- 26:34Jesse and you have to import it and it
- 26:36takes the data and some parameters of
- 26:38the estimator or the classifier of the
- 26:40machine learning. Now here I'm using the
- 26:42random forest classifier and this could
- 26:45be anything that you like. It could be a
- 26:47support vector machine which actually
- 26:48works pretty well especially when the
- 26:50number of data that you have is limited,
- 26:52or it could be any other type of
- 26:54classifier. And this is really
- 26:55important. So, not only I made it
- 26:57simple, I also made it flexible. So, I
- 26:59didn't want to limit you to just like
- 27:02one type of machine learning. So, that's
- 27:05why you can just pass any kind of
- 27:06classifier and it will just work. And
- 27:08these are some values that I have
- 27:10defined, which help with the model. Now,
- 27:12for these values, if you're not sure
- 27:13what to set, just ask AI. They are
- 27:15really good with these things. But
- 27:17especially if you want to prevent the
- 27:19overfitting, or if the number of classes
- 27:21that you have are not balanced, like
- 27:23these values are going to be important.
- 27:25And then I'm passing the task, which is
- 27:27multi-class. Now, if we only had two
- 27:29types, like a true or false kind of
- 27:32output, we could have set this to
- 27:33binary. And we also have the regression
- 27:35type, and we also have the multi-class,
- 27:37which could be more than two, like in
- 27:40this case, just three classes. And then
- 27:42we have the face deploy, which is
- 27:43actually where we actually run a
- 27:45backtest and see some results to see if
- 27:47the model is actually working correctly
- 27:49or not. And in it, we're using the
- 27:50backtest function of the research module
- 27:52of Jesse, which isn't really something
- 27:54new. We always had this. Now, this one
- 27:56is again for printing stuff, and this
- 27:58one is for validating the model, because
- 28:00we want to see some metrics in order to
- 28:02be sure the model is actually working
- 28:04properly, so we don't just run a
- 28:06backtest. First, we validate the model,
- 28:08and then we run the backtest. And this
- 28:09is the main entry of the file, where we
- 28:12actually run those face functions that I
- 28:14just showed you. So, the data point, the
- 28:16training point, then we are validating
- 28:19the model. After we get the results,
- 28:21then we deploy it to get some backtest
- 28:24results, and we print some final stuff.
- 28:26So, that's it. And again, I'm going to
- 28:29open source this file, so you guys can
- 28:30see it and run it for yourself. And now
- 28:32we can actually run this by saying
- 28:35Python triple barrier, and that's it.
- 28:40And as you can see, it's actually pretty
- 28:41fast, so it's not going to take a lot of
- 28:44time. All right, so a lot of stuff are
- 28:45not absolutely necessary, they're just
- 28:47some helpful output that we are
- 28:49printing. So, for example, the total
- 28:50number of bars, the bars per regime, the
- 28:53price change, so we started from here
- 28:55and went up to this, so it was clear
- 28:57uptrend overall. And these are the
- 28:58regimes, so the first the uptrend, the
- 29:00sideways, downtrend, and you can see
- 29:02what the price it started and where did
- 29:04it end. Again, nothing that important,
- 29:07although it does help you to ensure that
- 29:08the data that you generated is correct.
- 29:11And here we can see the number of
- 29:12features, the strategy, the regimes, and
- 29:14things like that. Now, this is the
- 29:16backtest results initially, and it says
- 29:18no trade were opened, which is what we
- 29:21wanted because, like I said, if you are
- 29:23in the garden, what we don't want to
- 29:24execute any trade, so this is correct.
- 29:26And we can also see that what was in
- 29:28total number of data points which we
- 29:30used for training. It was 1,584,
- 29:33and 46% of it was just the output being
- 29:37minus one, 42% was one, and 10% of it
- 29:41was a clear sideways. All right, so this
- 29:43is talking about the data collected, and
- 29:45this is talking about data that was
- 29:46trained, which is exactly the same
- 29:49thing, basically, so
- 29:51you don't have to read this again. And
- 29:53then we have the feature importance,
- 29:55which will tell you which one of these
- 29:58features that we used were actually
- 29:59helpful. It gives them some kind of
- 30:01score, and some F value, so these are
- 30:03some standard metrics which are really
- 30:05helpful, and here you can also read
- 30:07about every single one of them, like
- 30:08which one does what. Now, if this isn't
- 30:11super clear to you, it doesn't matter
- 30:12cuz we have another one which is
- 30:14clearer, so let's just skip it for now.
- 30:16And here it says the type of the
- 30:18classifier we used, and here we can see
- 30:20the accuracy of the model and some other
- 30:22metrics. And as you can see, it got a
- 30:24accuracy of 99.7%,
- 30:27which is almost 100%. And this is how I
- 30:29know that the model is working. Again,
- 30:32the reason we are seeing such a huge
- 30:34number of accuracy is because we're
- 30:37using synthetic data, fake data in other
- 30:39words. But in a real-world scenario,
- 30:42there's no way you're going to get this
- 30:44good of a result. Okay? And that's
- 30:46perfectly normal. But, because we were
- 30:48doing TDD, we're creating the tests
- 30:50ourselves with fake data just to see if
- 30:53the model works or not, we actually
- 30:54wanted to have such a high accuracy. So,
- 30:57this is expected and this shows that my
- 30:59implementation is correct and I'm ready
- 31:01to move on to the real prices of the
- 31:04market and try to beat the market. And
- 31:06if it doesn't work, I can just play
- 31:07around with my inputs and things like
- 31:08that. But, the implementation itself is
- 31:11correct. Now, this also tells me like
- 31:13how many times the model predicted that
- 31:15it's going to be minus one and how many
- 31:16times it was actually, which you can see
- 31:18it was this. There was just one case
- 31:20where it predicted that it's going to be
- 31:22zero, but it was actually minus one. And
- 31:24that's why we did not get an accuracy of
- 31:26100%. And for the other stuff, it's
- 31:30100%. So, it predicted zero and one
- 31:32perfectly fine, but for minus one, it
- 31:34just got one thing wrong. Now, in a real
- 31:36world scenario, you really going to love
- 31:38this table. So, it's my favorite,
- 31:41actually, cuz it tells you how many
- 31:42false and true negatives or false and
- 31:45true positives exist with the model.
- 31:48Now, if you don't know what that is,
- 31:49don't worry about it. Once you get your
- 31:51hands dirty with machine learning,
- 31:52you're going to hear it all the time.
- 31:53Now, we also have some other metrics of
- 31:55the model, so we can know for every
- 31:57single output, for minus one, zero, or
- 31:59one, what was precision, what was the
- 32:01recall, and what was the F1. Now, this
- 32:03is also really, really important because
- 32:05you don't want just the precision of the
- 32:08model to be high, like the recall of it
- 32:11and the F1 are also important. Now, if
- 32:13you don't know these metrics, again,
- 32:14look them up because they can be pretty
- 32:16helpful. Now, this is that table that I
- 32:18said, you know, I'm going to show you
- 32:19later and it's super helpful. You know,
- 32:21instead of this one, which is might be a
- 32:23little hard for some of you guys to
- 32:24read, you could just use this. And
- 32:26basically, it gives us uh the fact that
- 32:29what if we did not use this input? Like,
- 32:32what would happen to the accuracy then?
- 32:33And based on that, it's going to say if
- 32:36this input is actually helping or
- 32:38hurting the predictability of your
- 32:41model. Now, in this case, we're getting
- 32:42the exact same number. It's saying that
- 32:44all of them are neutral, so it doesn't
- 32:45matter if I drop them or not, and the
- 32:47accuracy is not changing, and that's
- 32:49because I'm using fake data. But, in a
- 32:51real-world scenario, you're not going to
- 32:53see this, and for some of them it's
- 32:54going to say, "Okay, so these are
- 32:55helpful, keep them." And for some of
- 32:57them, it's going to say, "Okay, drop
- 32:59them because they're actually hurting
- 33:01the performance of the model." Because
- 33:03you see, in a real-world scenario, you
- 33:05shouldn't just give the model as many
- 33:07inputs as you can. Because, sure, in
- 33:09that case, you could just give it 100
- 33:10inputs, right? But, the reality is that
- 33:12some of the inputs are not going to be
- 33:14helpful. They will actually hurt the
- 33:16performance of the model. So, that's why
- 33:18you want to run this test, look at this
- 33:20table, and if some of them are hurting
- 33:22it, you could just remove them, and by
- 33:23doing so, you're going to actually get a
- 33:25better performance out of your model and
- 33:28data. But, because this is going to be
- 33:30different per each test, per each market
- 33:33or or things, so you don't want to just
- 33:36always use one certain input or always
- 33:39not use it. Okay, so this is why you
- 33:41need to actually test things for
- 33:42yourself to see if it helps or it hurts.
- 33:46And that's it pretty much. So, we got
- 33:48the accuracy again, the MCC, and it's
- 33:51saying the model quality is well above
- 33:53the random chance on synthetic data. The
- 33:55triple barrier labeling and training
- 33:57pipeline are correct. So, again, I know
- 33:59everything is fine. And if you were to
- 34:01run a backtest with this, we would have
- 34:02executed a total number of 1,816
- 34:06trades, and the win rate would have been
- 34:0899.83%,
- 34:10which is absolutely insane. Of course,
- 34:12you're not going to see this in a
- 34:13real-world scenario. The net profit or
- 34:15P&L is 251%,
- 34:18and that's because I did not use a
- 34:19compound position sizing. If I did, this
- 34:22number would have been crazy high, and
- 34:24the Sharpe ratio is 10.80, which again
- 34:27is totally unrealistic. And that's it.
- 34:29Now, guys, we are not done here. You
- 34:32should definitely check out the other
- 34:34types of machine learning stuff that
- 34:36just supports now. So, this was the
- 34:38multi-class that I just showed you.
- 34:40We also have the binary, which can be
- 34:42very helpful. So, for example, if you
- 34:44want to know if the trade you're about
- 34:47to take is going to be profitable or
- 34:49not. And maybe based on that, you want
- 34:51to set your position sizing. Or maybe if
- 34:53it's it tells you it's not going to be
- 34:55profitable, you just want to drop that
- 34:57entire trade. And by doing so, you're
- 34:59going to increase the win rate of your
- 35:01strategy. So, this could be super
- 35:03helpful. Check out this page. We also
- 35:05have another one for regression. Now, if
- 35:08you guys want me to make videos about it
- 35:10and explain it, well, drop a comment and
- 35:12let me know. I might do it. But, we also
- 35:14have another one called meta labeling,
- 35:17which is for setting the position sizing
- 35:20of the trade. And pretty much, that's
- 35:22it. Again, I also explain the stationary
- 35:24data and the importance of using it. And
- 35:26I'm giving some examples. So, instead
- 35:28of, you know, the price, you should be
- 35:30using something like this. Instead of
- 35:32the ATR, you should be using something
- 35:33like this. So, this is also a really
- 35:35helpful page. And this was my first
- 35:38attempt at adding machine learning to
- 35:41the framework. So, I cannot wait to hear
- 35:43your feedback, especially if you guys
- 35:44are an expert in machine learning. Let
- 35:46me know what you think about the
- 35:47implementation that I just did. And
- 35:49whether or not you want it to be more
- 35:50flexible or if there's something that we
- 35:52are missing here. Or if there are more
- 35:54ways that you want to use for testing
- 35:56the model and performance of the
- 35:58strategy, which is using the machine
- 36:00learning. Now, my goal was to make it so
- 36:02easy that you guys don't have to
- 36:05actually read about this concept as much
- 36:07as I did just in order to be able to,
- 36:09you know, try a couple of things.
- 36:10Because I found the implementation of
- 36:12machine learning very much scary, no
- 36:15matter where I read about it. And I
- 36:17think this just made it so much easier,
- 36:20at least for me. And I hope it does the
- 36:22same thing for you. Then we're going to
- 36:23have a giveaway. A random subscriber who
- 36:26likes and comments is going to win 1
- 36:28million Punk Token. Now, let's pick the
- 36:30winner from the previous video.
- 36:34And the winner is, "Would be cool to see
- 36:36how things work out if you give each
- 36:38model personality trait like
- 36:39ex-legendary trait." Yeah, that sounds
- 36:42really great. Okay, thank you so much
- 36:43for your comment. Please do reach out to
- 36:45me so I can send you your tokens. Thank
- 36:46you so much for watching. I'll see you
- 36:48in the next one.
About this transcript
This page contains the full transcript of AI Trading Using Machine Learning (Step-by-Step) by Algo-trading with Saleh, generated from the public captions YouTube serves with the video. The transcript has 7,654 words across 1,046 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.