Back to Community
Kelly Criterion Sizing

Has anyone successfully implemented the kelly criterion? I've been stuck on trying to implement this as a function that returns the risk/kelly% but I haven't had any luck. I've seen a few posts on this and the only functional example was one that takes in a universe and sorts them by the kelly %. I haven't seen a function call for one strategy and/or weigh it against your portfolio. Please help.

9 responses

Last time I looked into this (over 18 months ago), it looked pretty tedious to calculate since, at the time, one had to keep track of all the per-trade statistics oneself. That might have changed, perhaps Quantopian exposes more information about your strategy performance back into the algo? Max Dama's PDF had some excellent code (probably in Python even?) for calculating the "kelly criterion" using the empirical returns distribution of the strategy, rather than assuming normality. It's on here somewhere.

That's what I was afraid of and it also is where I was running into issues. Do you know of other types of dynamic sizing strategies that could be easily implemented on this platform?

I am not sure, sorry. In theory one should be able to write a module that did everything correctly. Perhaps one could appeal to the zipline contributors?

Bryant, have you seen David's algorithm of the Kelly formula here?

Is that something along the lines of what you're looking for?


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Alisa, what I believe he is looking for is being able to size the next trade based on the kelly criterion of the strategy itself over the last N trades. This requires keeping track of running statistics of the outcomes of previous trades of the strategy itself, and having access to those statistics from within the algorithm. From memory, one would need to know the % winning trades (of the last N), and the average win size, average loss size, and/or average trade profit/capital or some combination like that.

Alisa, I tried implementing this but Simon is right, since I couldn't count the last N trades I wasn't able to get the formula to work correctly. I couldn't find another example of this implemented anywhere. I'd like to implement Kelly sizing but if that isn't possible for what I'm doing is there other examples of dynamic sizing strategies? I've looked and haven't come up with many code examples on quantopian.

This is a tougher one, but it is doable. It's gonna require a method of tracking returns per strategy. The easiest way might be to use track the returns on each position and associate those returns with a particular strategy.

This is a simplistic attempt at tracking per position returns, it just uses the log return from open to close each day. It's a start, next you would have to aggregate the position returns into strategy buckets, and calculate the Kelly score of each bucket.

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 54614528d9305a34ca14fbcb
There was a runtime error.

Any update on this?

The Kelly Criterion was first implemented by Edward O. Thorp in his casino Blackjack card-counting system to optimize players' bet sizes. He derives the formula in his paper "The Kelly Criterion in Blackjack, Sports Betting, and the Stock Market" for a coin toss experiment using a biased coin and "even money" (i.e., the amount a player stands to win is equal to the amount the player stands to lose, and is also equal to the amount the player chooses to bet). He initially models the basic process as X(n) = X(0)(1+f)^S(1-f)^F where X(0) is the player's initial bankroll, X(n) is his bankroll after n trials, S is the number of Successful bets, F is the number of Failed bets such that S+F=n and S>F, and f is the fraction bet of the player's bankroll at each toss. (Since the probabilities of the bias of the coin don't change, neither does f throughout the experiment.) He then modifies this model to reflect just the average incremental gain to the player at each trial with the equation G=(1+f)^p*(1-f)^q, where p=S/n, q=F/n. and G=[X(n)/X(0)]^(1/n). To solve for "f", the logarithm of the equation is used since both p and q are fractions of 1 and a standard analytical solution for polynomial equations won't eliminate the exponents. This results in the equation g(f)=p*ln(1+f)+q*ln(1-f) where g(f)= ln[G(f)]. With the function of "f" in this form and the values of p and q are known, the equation can now be solved for "f*" as the maximum value of "f", which is a concave function over the domain of 0 to 1 with a maximum value somewhere in between. The solution for f* is f*=p-q. To solve for f* where the outcome is not even money the initial model can be expressed as X(n) = X(0)(1+b*f)^S(1-a*f)F, where "b" and "a" are the fraction (or multiple) of the amount bet (f) that the player stands to win or lose, respectively. Solving once again for "f", the result for this more general application is:
f* = p/a - q/b.

Outside of determining the optimal fraction of one's bankroll (or "available resources") to bet (or "invest") at each trial, where the amount the player stands to win or lose is not necessarily equal to the amount they have bet, something I find fascinating about the Kelly Criterion is how it relates to what could be the longest running piece of advice in trading; namely, "cut your losses quickly and let your winners run". Minimizing the term "a" can cause "f*" to go to values greater than 1, as can maximizing "b" to a lesser extent. Just about every trading guru quotes that rule, and it seems to me that even Jesse Livermore might have said something along that line. What the Kelly Criterion does, however, is quantify that advice in a working equation to give us an insight into just how powerful those words of advice are. Not only does the reward for reducing the variable "a" to its smallest practical value stand out all the more since "a" is in the denominator, but taking that measure also allows us to increase the fraction of our bankroll in a completely quantifiable way because "f*" just got bigger. I can see why Thorp dubbed the Kelly Criterion as "Fortune's Formula."

I'll continue this in another post. :)