Back to Community
Monte Carlo simulation of theoretical algos

So I've wanted to code this up for a while now: It's a simple Monte Carlo system which generates a bunch of track records given your algo's win rate and how much it gains on a win/loses on a loss.

Playing around with the values really gives a "hands on" feel for how even a perfectly good algo can do based on good/bad luck. It's always a surprise that even with 250 trades and a >50% chance of winning, there is a not-insignificant chance of ending up worse than where you started.

By using a fixed fraction of the portfolio in the simulation, its really very difficult to lose all your investment, because by definition you are only investing a fixed percentage of it every trade. In reality it is possible because companies go bankrupt, shares stop trading, etc.

If an algo is setup to use fixed dollar amounts to invest per trade, then your risk of ruin goes up simply based on the possibility of getting X losing trades in a row. e.g. investing $5000 on each trade of a $100,000 account leaves you open to a 20-run of losses for complete ruin, and this is nowhere near as crazy unlikely as it may seem.

Loading notebook preview...
Notebook previews are currently unavailable.
11 responses

Nassim... Taleb... uses monte carlo in trading derivatives.. in his book Dynamic Hedging...

Are you familiar with Taleb's Dynamic Hedging? I never got round to reading it but I'm familiar with his other works (Fooled by Randomness, etc)

I read today on Wikipedia that he wanted US congress to ban the use of Value At Risk on the basis that it gives traders a false sense of security [my words], which I found fascinating if it is true.

I'll just echo the other two... look into Nassim Taleb. He's got a lot of books, and talks on YouTube, which will fill you up with all the Monte Carlo that you probably want!

One of the things Taleb can teach you is that "its really very difficult to lose all your investment" is really not that hard, despite how you might feel about your odds. Say you have a 1% chance of losing it all. 1% chance to lose it all is a massive chance in the grand scheme of things, and you will almost certainly get tagged by a chance that big if you stick around.

So then, people start to think, "Hey, I will just split up my algos into a bunch of mini algos."

... the 1% statistical evaluation still remains, no matter how many sub parts you split the algo into.

Monte Carlo simulations can be great for testing risk, but, really, if any of your simulated algos goes down to blow up, that's not an algo you want to trade. Next, to really run a proper Monte Carlo simulation, you're really wanting to run at least millions of simulations. Doing this is very CPU expensive.

Taleb retired as a trader over a decade ago. Something out there convinced him he'd be better off leaving the game, and that he'd rather be on the outside of the trading. Being such a big risk analyst, I suspect he realized how lucky he probably was, and it was time to cash out.

Finally, I don't think Quantopian in its current state is anywhere near capable of running these MC simulations for you. The computing power just isn't there. You can back-test it just to see, but a proper one would probably takes weeks on this power.

Awesome. Also FWIW Dynamic Hedging was my favourite book of his.

I haven't double-checked your work, but I was planning on doing something like this also. I never really found a good answer for whether monte carlo sampling of algo returns was better done with or without replacement. Thomas Wiecki might have some good input on this.

@Harrison... what is the ... difference between Monte Carlo... simulating a thousand times... , ten thousand times... and 100 thousands times simulation do you get a more accurate prediction or you'll be just getting... alot of random noise...??

@John Chan Typically, the idea of a Monte Carlo, for my purposes, has been to check the "risk" of a strategy. Not so much in the sense that you have a "x" percent chance to profit or loss, instead, you're looking for fringe cases. MC is going to be used generally with HFT or even MFT trading cases, where if you go broke in 1/100,000 scenarios, you definitely want to not employ that strategy.

Every scenario in a MC has just as much of a chance to actually become reality as any of the others. Because of this, the 10,000 simulations might show no examples where you went bust. 100,000 might show 2. You might run another 100,000, and show none.

Ideally, you'd run at least millions. I believe I read a paper somewhere that showed why anything less than 1 million is pointless. For the types of strategies that you'd want to consult a MC, you're probably just using a type of random walk. This is cheap to process millions.

Nowadays, you can purchase the temporary computing power from AWS to run billions of scenarios in a matter of an hour for a hundredish dollars. I say hundredish because it depends how many steps you're taking in the random walk.

@Harrison... in statistics... there is hat we call... statistical... significance...relative to R-squared.... is there a way... that we can check if were getting an accurate results... at the the ten thousand level simulation... or 100 thousand level simulation... ? why go the distance.... how can we check...if we just getting some random noise... beyond.... 100 thousand simulation...??? Can you compare... baseline... apples to apples.... if MC that runs.. less than million times simulation.. is definitely... pointless.

It's not a question of accuracy, if you don't run enough simulations you are less likely to sample the tails you are interested in.

and what is the purpose.. of sampling the many tails.... whom will you trust.... the 95 percentile... the 10th percentile... or the median..? Let me put it differently does 100 thousand simulation will give you a more narrow range... against a thousand times simulation MC... is it a rule of thumb... that more simulation is always better? or will it reach a tipping point wherein more simulation beyond.. certain level... it will already be adverse or diminishing returns...

More is always better.

James Jack, great Job with this one. Did you had a chance to use it with a real algo to check the betting sizes and the bankroll?