Click here to Skip to main content
15,888,579 members
Articles / Programming Languages / C#

The Kelly Criterion in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
14 Aug 2014CPOL 10.1K   4   5
The Kelly Criterion in C#

Given a bank roll, a stake you have already bet and a potential pot of winnings, the Kelly Criterion should calculate for you the optimal amount you should bet to maximize your winnings in the long run.

So, here’s what looks like a trivial piece of code, but is actually quite powerful:

C#
public class KellyEvaluator
{
private decimal proportion;
public KellyEvaluator(decimal proportion = 1.0M)
{
this.proportion = proportion;
}

public decimal MaxBankRoll(decimal bankRoll, decimal pot, decimal stake, decimal ourWinPercentage)
{
return bankRoll * MaxFraction(bankRoll, pot, stake, ourWinPercentage);
}

//given our current bankroll, a pot, an amount we’ve already staked, 
//our win percentage and the mean for the # of players,
//what is the maximum value we should allow ourselves to bet?
public decimal MaxNormalisedBankRoll(decimal bankRoll, decimal pot, decimal stake,
decimal ourWinPercentage, decimal meanWinPercentage)
{
return bankRoll * 
MaxNormalisedFraction(bankRoll, pot, stake, ourWinPercentage, meanWinPercentage);
}

//given a win% and an amount we’ve already staked, whatis the max we can bet
public decimal MaxNormalisedFraction(decimal bankRoll, decimal pot, decimal totalStaked,
decimal ourWinPercentage, decimal meanWinPercentage)
{
decimal f = 0.0M;
decimal proposedBet = bankRoll;
decimal max = 0.0M;
while (proposedBet > 0.0M)
{
f = NormalisedFraction
(bankRoll, pot – totalStaked, proposedBet, ourWinPercentage, meanWinPercentage);
if (f >= 0.0M)
max = f;
proposedBet -= 1.0M;
}

return max;
}

//given a win% whatis the max we can bet
public decimal MaxFraction
(decimal bankRoll, decimal pot, decimal totalStaked, decimal ourWinPercentage)
{
decimal f = 0.0M;
decimal proposedBet = bankRoll;
decimal max = 0.0M;
while(proposedBet > 0.0M)
{
f = Fraction(bankRoll, pot – totalStaked, proposedBet, ourWinPercentage);
if (f >= 0.0M)
max = f;
proposedBet -= 1.0M;
}

return max;
}

private decimal NormalisedFraction(decimal bankRoll, decimal potentialWinnings, decimal ourStake,
decimal ourWinPercentage, decimal meanWinPercentage)
{
decimal delta = ourWinPercentage – meanWinPercentage;
decimal edgePercentage = delta / meanWinPercentage;

return Fraction(bankRoll, potentialWinnings, ourStake, edgePercentage);
}

private decimal Fraction(decimal bankRoll, decimal potentialWinnings, 
                         decimal ourStake, decimal ourWinPercentage)
{

//b:1 i.e., if pot is 100 and call value is 20 and we’ve staked nothing else so far, then 5:1
decimal b = potentialWinnings / (ourStake);
decimal f = ((b * ourWinPercentage) – (1 – ourWinPercentage)) / b;

return f * proportion;
}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Alpha Integralis Limited
United Kingdom United Kingdom
CatchExAs aka Nick Wilton runs Alpha Integralis, a software development consultancy to companies in the City of London.

Main interests include exploring Algo-Trading, Quant Development and Physics using C# and Java.

www.nickwilton.info/Blog

Comments and Discussions

 
QuestionSmall question Pin
Alexey KK14-Aug-14 22:40
professionalAlexey KK14-Aug-14 22:40 
AnswerRe: Small question Pin
CatchExAs15-Aug-14 1:53
professionalCatchExAs15-Aug-14 1:53 
GeneralRe: Small question Pin
Alexey KK15-Aug-14 2:31
professionalAlexey KK15-Aug-14 2:31 
I live in Moscow. So if any ... Smile | :) Welcome.
I dont respect Ep Chan too much and many other authors. Many liquid words without practical meaning. Enough to write book.

There is nice book dated on 1990-1992's. Author Ralph Wince. Maths of capital management where only one thing is discussed - 300-450 pages of optimal bet sizing.Smile | :)

Previuosly (15 years ago) i worked as trader mostly with external debts and fixed income but certainly i was working with full set of money instruments of Bank's Treasury. Had some training courses with european banks epsecially i remember Lehmans.
All other bank experience was managing bank as bad asset in 2009-2010 etc. No mathmatics only other kinds of managements. Concerning fixed income or clients services you never think of sizingSmile | :) Limits yes.
But if you work with full set of available instruments and tactics available through electronic market place - you have another possibilities of analyzing dataseries and tools.
Trust me when i ve broken leg some years agoSmile | :) all i could do - only try to test neuro nets and different bet sizing strategies. Application i was using - WealthLab (i think best general purpose application at the moment). This app has more than 20 versions in setups of bet sizing.
My basic math knowledge didnt let me trust american broker Fidelity applicationSmile | :) . Another reason - when i started to test real time series and rewritten series methods i ve got up to 240 times improvement on 1.5 mio data series (dont you think is good enough only for one run of StdDev?!). So i have precisely studied this Ralph Wince book on bet sizing. He's key idea in one word - optimization of geometrical mean of different sizes of profit/losses series. In theory he proves well that it is best tactic. That's why if i dont see any word about it i close any "pro" book when author starts to write tails.
Another suspect - i ve tested so many approaches through WealthLab and last time with my own algo's - i cant understand Smile | :) how usefull these theoretical approaches in reality?
I have found if you are optimizing portfolio of strategies and thinking about not correlataed profit/losses series - the last option you have thing is optimal bet size (the main idea of sizing is to make equity curve smoother - means hide extremums through it sizing weights - this is look from another side). In real time trading you have another problem - execution. You may dream about sizing - but real word ll never let you do it.
Only weights of strategies in full portfolio could be optmimized through bet sizing on each strategy. But as i told there are some ways to get overall result idea faster.
And at the same time odds/bet are much more important. Another question how you ll get odds...I use some ideas. And certainly through overall optimization again.
I prefere Dan Harrington or David Sklanski logic odds/bet involved. These authors made me "break brain" after have done all exercises again like i was doing it in Aerospatial Academy when i was student.
So what i meant - do you know anybody really using Kelly formula in real time trading? Algo word is very closed.

modified 15-Aug-14 9:09am.

GeneralRe: Small question Pin
CatchExAs15-Aug-14 3:05
professionalCatchExAs15-Aug-14 3:05 
GeneralRe: Small question Pin
Alexey KK15-Aug-14 3:18
professionalAlexey KK15-Aug-14 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.