Monte Carlo simulations for trading in Python is easy now — Transcript
Full transcript
- 0:00Hey guys, it's Ole. It's been a while
- 0:01since I recorded any videos because I've
- 0:03been doing some serious development. I
- 0:05wanted to tackle some of the most
- 0:07important features that I've been
- 0:08wanting to work for years. And today,
- 0:11I'm happy to say that I implemented one
- 0:13of the most important ones, which is
- 0:15Monte Carlo simulations. They solve one
- 0:17of the biggest questions in ago trading,
- 0:19which is how to make sure your strategy
- 0:21isn't overfit. So, let's get right into
- 0:24it. Normally, when we do research, we
- 0:27use back test, right? So let's say this
- 0:29back testing result for one of my
- 0:31strategies. It looks really well. But
- 0:33before I go live with it, the serious
- 0:35question is how do I ensure it's not
- 0:37overfit? How do I ensure the risk that
- 0:40I'm seeing here? For example, the max
- 0:42throttle right now is minus almost 10%.
- 0:45How do I make sure this is going to be
- 0:47the result that I'm going to see in the
- 0:49live trading environment? Because in
- 0:51there it may be less. It might be minus
- 0:545%. It may also be minus 20%. And these
- 0:57three are actually very different. And
- 0:59depending on those, I'm going to have to
- 1:01adjust the position sizing of my
- 1:02strategy because for instance, the win
- 1:04rate in this back test result is 70%,
- 1:07which is amazing. But what if that
- 1:09wasn't the case? What if it was like
- 1:1150%. Would my equity still look like
- 1:14this or would it actually go down or
- 1:16what would be the max roto number? So
- 1:18these are some serious questions and to
- 1:20answer them before this release, we had
- 1:23no option. But starting today, we're
- 1:25going to have Monte Carlo simulations
- 1:27for that. But what is Monte Carlo
- 1:29really? Well, to put it simply, Monte
- 1:31Carlo basically means randomization. So,
- 1:34what if we weren't actually so lucky in
- 1:36the beginning of the back test like what
- 1:38happened in here and we were actually so
- 1:42unlucky like what happened in here. How
- 1:44would our equity c look like then? Or
- 1:47what if the price actually wasn't this?
- 1:49What if there was a little bit noise in
- 1:51it? So you see how market makers can
- 1:53manipulate the order book. Well, how can
- 1:56we ensure that our strategy is immune to
- 1:58that? Well, one way is to do a little
- 2:01bit of randomization. So for example,
- 2:03what if we don't take the trade when
- 2:05let's say the crossover happens now?
- 2:07What if we take it when it happens a few
- 2:10candles later? Now I know this sounds a
- 2:12bit crazy, but it is actually a great
- 2:14way for estimated stuff. And I emphasize
- 2:16on the word estimation because when we
- 2:18do Monte Carlo simulations, nothing is
- 2:20going to be accurate anymore like the
- 2:22actual back test itself. Everything is
- 2:23going to be in estimations. But anyways,
- 2:25don't let that scare you. It's actually
- 2:26very simple. Now, if you look up Monte
- 2:28Carlo simulations for trading on
- 2:30YouTube, you're going to find a couple
- 2:32of really good explanations of it and I
- 2:34highly suggest you guys go and watch it.
- 2:35Now, by the way, I am going to make my
- 2:37own explanation video and a deep dive
- 2:39into Monte Carlo in the future. But in
- 2:42this video, I will just show you the
- 2:43quickest way to run everything. So, in
- 2:45the meanwhile, make sure to give these
- 2:47videos a watch. But the point I want to
- 2:49make is this that if you look it up,
- 2:51you're going to see that they are using
- 2:53Monte Carlo in three ways. Some people
- 2:55like this guy are actually using it to
- 2:57validate their trading signal. Now, if
- 3:00you don't know what a trading signal or
- 3:01an entry signal is, don't worry about it
- 3:04because we're not going to work with it
- 3:05now. But the other two types of videos
- 3:07that I found are either shuffling the
- 3:10trades or the actual candles. And if you
- 3:12check out the documentation that I wrote
- 3:14for Monte Carlo, and you can find it
- 3:16under the research module and Monte
- 3:18Carlo page, you can see I started by
- 3:20explaining the difference between trade
- 3:22order shuffling Monte Carlo and
- 3:24candlesbased Monte Carlo. So, make sure
- 3:26to give this page a serious read because
- 3:28I worked really hard on it and it will
- 3:30make things very clear for you. I also
- 3:32included a lot of examples including
- 3:34this one which will give you a complete
- 3:37script to run just everything. Now if
- 3:39you want to do this you need to ensure
- 3:41the script is inside your Jessi project.
- 3:43Now my project is named but so the file
- 3:46that you run the script in it needs to
- 3:48be in the root of your Jessie project.
- 3:50Now assuming that's the case you only
- 3:52care about one section and that is
- 3:53configuration. We have trading routes.
- 3:56So in this case it is this for me and
- 3:59these are the data routes. So whatever
- 4:01you put in your back testing page on
- 4:03JS's dashboard make sure to put it
- 4:05inside this file. And then we have the
- 4:07simulation config. The number of
- 4:08scenarios is really important. So I set
- 4:11it to 200. You can set it to 100 or
- 4:15maybe even 50 sometimes works, but the
- 4:17higher it is, the more accurate numbers
- 4:18you're going to get. So even if you can
- 4:20set it to something like 1,000, it's
- 4:22going to be better. But of course, it's
- 4:24going to take longer for the simulation
- 4:25to finish. Then we have the starting and
- 4:27ending date of the simulation, the
- 4:29progress bar, the benchmark, and the
- 4:31fast mode. Now, I suggest you turn all
- 4:33of these on. And then we have the
- 4:35strategy config such as the starting
- 4:36balance, the trading fees and this is an
- 4:39important part which is the type of
- 4:40Monte Carlo simulations you want to do
- 4:42for candles. Now as I mentioned we have
- 4:44two types of Monte Carlo in Jesse and
- 4:46this is for the candle type. I'm going
- 4:47to show you what that is in a second but
- 4:49for now just so you know I suggest you
- 4:51guys begin with moving block
- 4:53bootstrapping method which is this one
- 4:55and it simply accepts the batch size.
- 4:57I've set it to one week like this. So
- 5:00maybe do the same. But if you want to go
- 5:02with the gajian noise option, which is
- 5:04this one, you also need to pass other
- 5:06values such as close sigma and other
- 5:08sigma values. And if you don't know what
- 5:10these are, again, I suggest you just
- 5:12stick with this one because it doesn't
- 5:14need as much configuration. It just
- 5:16works out of the box. But it's going to
- 5:17be different for every case. So if you
- 5:19have the knowledge and the time, I
- 5:20suggest playing around with both of
- 5:22them. And that's it really. Now you just
- 5:23need to go to your terminal and run the
- 5:26command python. And the name of that
- 5:28script file. I have named it test
- 5:29Monteolo. UI and I already ran it and
- 5:33here is the results. So let's begin with
- 5:35the first type and that is the Monteol
- 5:37candles and this is the one that I
- 5:38actually care the most. Now with this
- 5:40one what we actually do is that we add
- 5:42some noise to the candles or in the case
- 5:44of the moving block put strapping method
- 5:46which I selected here. We're not adding
- 5:48noise. We're just changing the order
- 5:50which those movements happen in the
- 5:52market. So for example, let's say the
- 5:54market actually goes up 1% today and
- 5:57tomorrow it goes down 2% and the next
- 5:59day it goes up 3%. But what if the order
- 6:02of it was different? What if it started
- 6:04by going up 2% and then going down 2%
- 6:06and then going up 1%. How would your
- 6:09strategy behave if this was the case? So
- 6:11you see I didn't add any noise and I
- 6:13didn't make up price changes. I just
- 6:16changed the order at which those price
- 6:18changes happened in the market. Now, if
- 6:21you're curious why this method actually
- 6:22works and why it's a standard, just look
- 6:24it up again. It is called moving block
- 6:27bootstrapping. But that's what I went
- 6:28with. And here's the thing. This is how
- 6:30much my original practice made 81%. And
- 6:34this is the max draw on. It was minus
- 6:3510%. The sharp was 2.28.
- 6:39Now, these numbers are really good, but
- 6:41if you actually ran the Monte Carlo on
- 6:43it and looked at the results here, this
- 6:46is how it would have looked like. So you
- 6:49see this green line here is the original
- 6:51back test and these blue lines here are
- 6:53the simulations. Now to put it very
- 6:56simply, the lower the original back test
- 6:59is among all of these simulations, the
- 7:02better it is for you. It means that you
- 7:04weren't as lucky in the original back
- 7:08test and that's if you go live with the
- 7:10same strategy, then the chances are that
- 7:13you are actually going to get lucky in
- 7:15the real trading. But if for example the
- 7:18original back test was in here, I was
- 7:21being super lucky in my back test and
- 7:23there is a very low chance that in the
- 7:26live trading I'm going to get the same
- 7:28amount of luck. But because simulation
- 7:31data isn't as accurate and as good as
- 7:33the actual original data, I don't expect
- 7:36this line to be really low, for example,
- 7:38in here. Maybe it does happen for you at
- 7:41some point and that may be a unicorn
- 7:44strategy. I don't know. But I've never
- 7:46found such a strategy. So how do we read
- 7:48this? We have a table for it. So the
- 7:51sharp ratio of the original back test
- 7:53was 3.28.
- 7:54But the median it was 2.33. Now what is
- 7:58median? It is basically what was
- 8:00happening in the middle of these
- 8:01simulations. So you see the original
- 8:03back test was better than the median and
- 8:05that's not a great sign. But on the
- 8:07other hand the best 5% the sharp was
- 8:11almost five. So these are the best 5%
- 8:14right? So yes, the strategy was a bit
- 8:16lucky in the back test, but it wasn't as
- 8:18lucky as the best 5%. Now there's also
- 8:20another way to read this whole thing and
- 8:22that is to say forget the original back
- 8:25test. Let's just read the results from
- 8:28the median and if that is good enough, I
- 8:31will go live with it. So the sharp ratio
- 8:33in the median is 2.33 and that is above
- 8:37two which I personally consider really
- 8:39good. So for that single reason I will
- 8:42go live with this strategy. And I also
- 8:44want to emphasize this. If you see some
- 8:47good results in your Monte Carlo
- 8:49simulations, yes, you can say there's a
- 8:51good chance that your strategy isn't
- 8:53overfit and that's great. But if you
- 8:55don't see good results here, it doesn't
- 8:57mean it is definitely overfit. So I also
- 9:00want to emphasize on this. All right,
- 9:02the Monte Carlo is not the law. It's not
- 9:05telling you what is definitely
- 9:07happening. It is just giving you some
- 9:09estimations. But at the end of the day,
- 9:11if you just had one single back test,
- 9:13you didn't have any estimations
- 9:15whatsoever. And having some estimations
- 9:17is definitely better than not having
- 9:20none at all. So from now on, when you do
- 9:22some research, you develop a strategy.
- 9:24If you like your back testing results
- 9:26and now you're wondering, should I take
- 9:28this live or not? Make sure to run a
- 9:30Monte Carlo simulation on those results.
- 9:33And that is going to tell you some
- 9:35further answers which is really really
- 9:37helpful. And yes, definitely this
- 9:39feature will come to the UI dashboard of
- 9:42Jessie which will make it super easy for
- 9:44literally everybody to use it. But
- 9:46that's going to take some more time. And
- 9:48as I said in the beginning of the video,
- 9:49I've been working on some serious
- 9:51features. I cannot spoil anything just
- 9:53yet, but just so you know, it is worth
- 9:56the wait. So make sure to use the script
- 9:58and give me some feedback. Thank you for
- 10:00watching the video. I'll see you in the
- 10:02next one.
- 10:04[Music]
- 10:07[Applause]
- 10:07[Music]
About this transcript
This page contains the full transcript of Monte Carlo simulations for trading in Python is easy now by Algo-trading with Saleh, generated from the public captions YouTube serves with the video. The transcript has 2,063 words across 280 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.