Click here to Skip to main content
15,888,527 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 
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 
I use dma on MICEX, so i can have trades, direction, order book, on full assets range from stocks, to futures, options, bonds etc. Btw i think you cant have 15-25% yields in market neutral strategies on LIFFE or NYSE. If you work more precisely with margin requirements you ll have more. So dont miss opportunities.
I pay for broker account not for infomation. How to get it was the main problem. I ve spent about 2 months linking Basic Micex trading terminal using c++/C# IPC logic. It was really difficult because it looks like real commerical decision and difficult to discuss and to ask help. For example i was trying to hear twice "proffesionals". Seems it was much faster to dive into c++. Now i am sure if i were paying for this job to somebody else he would resell these 3000 lines of C++ code to 100 banksSmile | :) . May be better not to trade and to sell?Smile | :) There is real demand on connect it to .Net environement. But much more profitable is to sell it in ready to use trading application. To be honest not sure if i could finish with it. Maintaining and developing is absolutely different business. But in what i m sure - it is effective enough and does what trader asks. Not stupied as Bloomberg and other kiss information terminals but real decision making knife. I had requirements 3d waves of bids and offers in main liquid instruments in real time.... Nobody could realise quckly. In reaility is is not needed. But how i can trust and check skills?Smile | :) I ve realised itSmile | :) May be later will find some cooperation.
Now i have real time/static data server getting data from Trading Terminal through Win Api IPC and distributing through much more comfortable WCF logic. Little bit overbet decision but if you want to finish better to do one but strong decision. At any time i can directly connect to Exchange but it will be more expensive. Now i have latency depending on market activity from 20 to 235 ms in general. There is an option to have garanteed latency not exceeding 20-25 ms. But i think it s enough to make most weighted decisions.
When i returned to C# i became happy again.
I dont trust Kelly. I think it is not optimal not convenient ratio risk/returns. But it is very easy formula.
That's why i was asking.
As for poker strategy......Not sure it will happen quicklySmile | :) Anyway Harrington and Sklanski are WSOP champions and came from maths. Sklansky has written such quantity of books...like D.Knut in programming ranging by limits and game types. But if i were programming poker bot - Sklanski is best adviser. There are some bots, one of them "Poker Academy" if i remember and based on Sklanski ideas. They even suggested to play with virtual mr.Sklansky from right and left positions in differents games style to even Heads Ups. Odds/betting, statistics on you it uses everything. Really interesting bot.
I respect your research/programming approach. To be honest Sklanski/Miller/Harrington are super addition to known authors like William Sharp, Black....But noone of last names cant tell anything about trading (i am sure they never pressed any buy/sell button on trading terminal)... I ve found Sklansky much more closer to Theory of speculations of 1900 Louis Bachelier (more known as Brownian motion and presented in 1906 by Einstein) than any other approaches Smile | :) .

As for meeting - as i told. Welcome to Moscow. Dont trust BBC and other trash channelsSmile | :)
Just send me email as you have sent it once and will be happy to meet you. Seems uncomfortable temperature finishes and i could show you something using James Bond style favorite car

modified 15-Aug-14 14:08pm.

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.