YouTube2Text

Monte Carlo simulations for trading in Python is easy now — Transcript

by Algo-trading with Saleh · 2,063 words · 280 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hey guys, it's Ole. It's been a while
  2. 0:01since I recorded any videos because I've
  3. 0:03been doing some serious development. I
  4. 0:05wanted to tackle some of the most
  5. 0:07important features that I've been
  6. 0:08wanting to work for years. And today,
  7. 0:11I'm happy to say that I implemented one
  8. 0:13of the most important ones, which is
  9. 0:15Monte Carlo simulations. They solve one
  10. 0:17of the biggest questions in ago trading,
  11. 0:19which is how to make sure your strategy
  12. 0:21isn't overfit. So, let's get right into
  13. 0:24it. Normally, when we do research, we
  14. 0:27use back test, right? So let's say this
  15. 0:29back testing result for one of my
  16. 0:31strategies. It looks really well. But
  17. 0:33before I go live with it, the serious
  18. 0:35question is how do I ensure it's not
  19. 0:37overfit? How do I ensure the risk that
  20. 0:40I'm seeing here? For example, the max
  21. 0:42throttle right now is minus almost 10%.
  22. 0:45How do I make sure this is going to be
  23. 0:47the result that I'm going to see in the
  24. 0:49live trading environment? Because in
  25. 0:51there it may be less. It might be minus
  26. 0:545%. It may also be minus 20%. And these
  27. 0:57three are actually very different. And
  28. 0:59depending on those, I'm going to have to
  29. 1:01adjust the position sizing of my
  30. 1:02strategy because for instance, the win
  31. 1:04rate in this back test result is 70%,
  32. 1:07which is amazing. But what if that
  33. 1:09wasn't the case? What if it was like
  34. 1:1150%. Would my equity still look like
  35. 1:14this or would it actually go down or
  36. 1:16what would be the max roto number? So
  37. 1:18these are some serious questions and to
  38. 1:20answer them before this release, we had
  39. 1:23no option. But starting today, we're
  40. 1:25going to have Monte Carlo simulations
  41. 1:27for that. But what is Monte Carlo
  42. 1:29really? Well, to put it simply, Monte
  43. 1:31Carlo basically means randomization. So,
  44. 1:34what if we weren't actually so lucky in
  45. 1:36the beginning of the back test like what
  46. 1:38happened in here and we were actually so
  47. 1:42unlucky like what happened in here. How
  48. 1:44would our equity c look like then? Or
  49. 1:47what if the price actually wasn't this?
  50. 1:49What if there was a little bit noise in
  51. 1:51it? So you see how market makers can
  52. 1:53manipulate the order book. Well, how can
  53. 1:56we ensure that our strategy is immune to
  54. 1:58that? Well, one way is to do a little
  55. 2:01bit of randomization. So for example,
  56. 2:03what if we don't take the trade when
  57. 2:05let's say the crossover happens now?
  58. 2:07What if we take it when it happens a few
  59. 2:10candles later? Now I know this sounds a
  60. 2:12bit crazy, but it is actually a great
  61. 2:14way for estimated stuff. And I emphasize
  62. 2:16on the word estimation because when we
  63. 2:18do Monte Carlo simulations, nothing is
  64. 2:20going to be accurate anymore like the
  65. 2:22actual back test itself. Everything is
  66. 2:23going to be in estimations. But anyways,
  67. 2:25don't let that scare you. It's actually
  68. 2:26very simple. Now, if you look up Monte
  69. 2:28Carlo simulations for trading on
  70. 2:30YouTube, you're going to find a couple
  71. 2:32of really good explanations of it and I
  72. 2:34highly suggest you guys go and watch it.
  73. 2:35Now, by the way, I am going to make my
  74. 2:37own explanation video and a deep dive
  75. 2:39into Monte Carlo in the future. But in
  76. 2:42this video, I will just show you the
  77. 2:43quickest way to run everything. So, in
  78. 2:45the meanwhile, make sure to give these
  79. 2:47videos a watch. But the point I want to
  80. 2:49make is this that if you look it up,
  81. 2:51you're going to see that they are using
  82. 2:53Monte Carlo in three ways. Some people
  83. 2:55like this guy are actually using it to
  84. 2:57validate their trading signal. Now, if
  85. 3:00you don't know what a trading signal or
  86. 3:01an entry signal is, don't worry about it
  87. 3:04because we're not going to work with it
  88. 3:05now. But the other two types of videos
  89. 3:07that I found are either shuffling the
  90. 3:10trades or the actual candles. And if you
  91. 3:12check out the documentation that I wrote
  92. 3:14for Monte Carlo, and you can find it
  93. 3:16under the research module and Monte
  94. 3:18Carlo page, you can see I started by
  95. 3:20explaining the difference between trade
  96. 3:22order shuffling Monte Carlo and
  97. 3:24candlesbased Monte Carlo. So, make sure
  98. 3:26to give this page a serious read because
  99. 3:28I worked really hard on it and it will
  100. 3:30make things very clear for you. I also
  101. 3:32included a lot of examples including
  102. 3:34this one which will give you a complete
  103. 3:37script to run just everything. Now if
  104. 3:39you want to do this you need to ensure
  105. 3:41the script is inside your Jessi project.
  106. 3:43Now my project is named but so the file
  107. 3:46that you run the script in it needs to
  108. 3:48be in the root of your Jessie project.
  109. 3:50Now assuming that's the case you only
  110. 3:52care about one section and that is
  111. 3:53configuration. We have trading routes.
  112. 3:56So in this case it is this for me and
  113. 3:59these are the data routes. So whatever
  114. 4:01you put in your back testing page on
  115. 4:03JS's dashboard make sure to put it
  116. 4:05inside this file. And then we have the
  117. 4:07simulation config. The number of
  118. 4:08scenarios is really important. So I set
  119. 4:11it to 200. You can set it to 100 or
  120. 4:15maybe even 50 sometimes works, but the
  121. 4:17higher it is, the more accurate numbers
  122. 4:18you're going to get. So even if you can
  123. 4:20set it to something like 1,000, it's
  124. 4:22going to be better. But of course, it's
  125. 4:24going to take longer for the simulation
  126. 4:25to finish. Then we have the starting and
  127. 4:27ending date of the simulation, the
  128. 4:29progress bar, the benchmark, and the
  129. 4:31fast mode. Now, I suggest you turn all
  130. 4:33of these on. And then we have the
  131. 4:35strategy config such as the starting
  132. 4:36balance, the trading fees and this is an
  133. 4:39important part which is the type of
  134. 4:40Monte Carlo simulations you want to do
  135. 4:42for candles. Now as I mentioned we have
  136. 4:44two types of Monte Carlo in Jesse and
  137. 4:46this is for the candle type. I'm going
  138. 4:47to show you what that is in a second but
  139. 4:49for now just so you know I suggest you
  140. 4:51guys begin with moving block
  141. 4:53bootstrapping method which is this one
  142. 4:55and it simply accepts the batch size.
  143. 4:57I've set it to one week like this. So
  144. 5:00maybe do the same. But if you want to go
  145. 5:02with the gajian noise option, which is
  146. 5:04this one, you also need to pass other
  147. 5:06values such as close sigma and other
  148. 5:08sigma values. And if you don't know what
  149. 5:10these are, again, I suggest you just
  150. 5:12stick with this one because it doesn't
  151. 5:14need as much configuration. It just
  152. 5:16works out of the box. But it's going to
  153. 5:17be different for every case. So if you
  154. 5:19have the knowledge and the time, I
  155. 5:20suggest playing around with both of
  156. 5:22them. And that's it really. Now you just
  157. 5:23need to go to your terminal and run the
  158. 5:26command python. And the name of that
  159. 5:28script file. I have named it test
  160. 5:29Monteolo. UI and I already ran it and
  161. 5:33here is the results. So let's begin with
  162. 5:35the first type and that is the Monteol
  163. 5:37candles and this is the one that I
  164. 5:38actually care the most. Now with this
  165. 5:40one what we actually do is that we add
  166. 5:42some noise to the candles or in the case
  167. 5:44of the moving block put strapping method
  168. 5:46which I selected here. We're not adding
  169. 5:48noise. We're just changing the order
  170. 5:50which those movements happen in the
  171. 5:52market. So for example, let's say the
  172. 5:54market actually goes up 1% today and
  173. 5:57tomorrow it goes down 2% and the next
  174. 5:59day it goes up 3%. But what if the order
  175. 6:02of it was different? What if it started
  176. 6:04by going up 2% and then going down 2%
  177. 6:06and then going up 1%. How would your
  178. 6:09strategy behave if this was the case? So
  179. 6:11you see I didn't add any noise and I
  180. 6:13didn't make up price changes. I just
  181. 6:16changed the order at which those price
  182. 6:18changes happened in the market. Now, if
  183. 6:21you're curious why this method actually
  184. 6:22works and why it's a standard, just look
  185. 6:24it up again. It is called moving block
  186. 6:27bootstrapping. But that's what I went
  187. 6:28with. And here's the thing. This is how
  188. 6:30much my original practice made 81%. And
  189. 6:34this is the max draw on. It was minus
  190. 6:3510%. The sharp was 2.28.
  191. 6:39Now, these numbers are really good, but
  192. 6:41if you actually ran the Monte Carlo on
  193. 6:43it and looked at the results here, this
  194. 6:46is how it would have looked like. So you
  195. 6:49see this green line here is the original
  196. 6:51back test and these blue lines here are
  197. 6:53the simulations. Now to put it very
  198. 6:56simply, the lower the original back test
  199. 6:59is among all of these simulations, the
  200. 7:02better it is for you. It means that you
  201. 7:04weren't as lucky in the original back
  202. 7:08test and that's if you go live with the
  203. 7:10same strategy, then the chances are that
  204. 7:13you are actually going to get lucky in
  205. 7:15the real trading. But if for example the
  206. 7:18original back test was in here, I was
  207. 7:21being super lucky in my back test and
  208. 7:23there is a very low chance that in the
  209. 7:26live trading I'm going to get the same
  210. 7:28amount of luck. But because simulation
  211. 7:31data isn't as accurate and as good as
  212. 7:33the actual original data, I don't expect
  213. 7:36this line to be really low, for example,
  214. 7:38in here. Maybe it does happen for you at
  215. 7:41some point and that may be a unicorn
  216. 7:44strategy. I don't know. But I've never
  217. 7:46found such a strategy. So how do we read
  218. 7:48this? We have a table for it. So the
  219. 7:51sharp ratio of the original back test
  220. 7:53was 3.28.
  221. 7:54But the median it was 2.33. Now what is
  222. 7:58median? It is basically what was
  223. 8:00happening in the middle of these
  224. 8:01simulations. So you see the original
  225. 8:03back test was better than the median and
  226. 8:05that's not a great sign. But on the
  227. 8:07other hand the best 5% the sharp was
  228. 8:11almost five. So these are the best 5%
  229. 8:14right? So yes, the strategy was a bit
  230. 8:16lucky in the back test, but it wasn't as
  231. 8:18lucky as the best 5%. Now there's also
  232. 8:20another way to read this whole thing and
  233. 8:22that is to say forget the original back
  234. 8:25test. Let's just read the results from
  235. 8:28the median and if that is good enough, I
  236. 8:31will go live with it. So the sharp ratio
  237. 8:33in the median is 2.33 and that is above
  238. 8:37two which I personally consider really
  239. 8:39good. So for that single reason I will
  240. 8:42go live with this strategy. And I also
  241. 8:44want to emphasize this. If you see some
  242. 8:47good results in your Monte Carlo
  243. 8:49simulations, yes, you can say there's a
  244. 8:51good chance that your strategy isn't
  245. 8:53overfit and that's great. But if you
  246. 8:55don't see good results here, it doesn't
  247. 8:57mean it is definitely overfit. So I also
  248. 9:00want to emphasize on this. All right,
  249. 9:02the Monte Carlo is not the law. It's not
  250. 9:05telling you what is definitely
  251. 9:07happening. It is just giving you some
  252. 9:09estimations. But at the end of the day,
  253. 9:11if you just had one single back test,
  254. 9:13you didn't have any estimations
  255. 9:15whatsoever. And having some estimations
  256. 9:17is definitely better than not having
  257. 9:20none at all. So from now on, when you do
  258. 9:22some research, you develop a strategy.
  259. 9:24If you like your back testing results
  260. 9:26and now you're wondering, should I take
  261. 9:28this live or not? Make sure to run a
  262. 9:30Monte Carlo simulation on those results.
  263. 9:33And that is going to tell you some
  264. 9:35further answers which is really really
  265. 9:37helpful. And yes, definitely this
  266. 9:39feature will come to the UI dashboard of
  267. 9:42Jessie which will make it super easy for
  268. 9:44literally everybody to use it. But
  269. 9:46that's going to take some more time. And
  270. 9:48as I said in the beginning of the video,
  271. 9:49I've been working on some serious
  272. 9:51features. I cannot spoil anything just
  273. 9:53yet, but just so you know, it is worth
  274. 9:56the wait. So make sure to use the script
  275. 9:58and give me some feedback. Thank you for
  276. 10:00watching the video. I'll see you in the
  277. 10:02next one.
  278. 10:04[Music]
  279. 10:07[Applause]
  280. 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.