# Kelly bet sizing money management position sizing strategy 0

Posted on 17, June 2013

in Category SID

### Description for Kelly SID 259

Kelly bet sizing money management/position sizing strategy

Description

Kelly money management/position sizing strategy for growing your capital. For more information, type Kelly Formula into a search engine.

The Kelly formula is : Kelly % = W – (1-W)/R where:

W = Historical winning percentage of a trading system.
R = Historical Average Win/Loss ratio.
Kelly % = percentage of capital to be put into a single trade.

This is calculated before each enter order.

Orders available:

This MM will short and will pyramid if account options set.

There are several points you will need to understand before using this money management object.

Since this MM uses “historical trading” performance, you have to consider what should happen before there are enough trades to be statistically meaningful. As a default, this MM will invest 10% of cash per trade until there are at least 20 trades. From then on, the Kelly Formula is used.

The Kelly formula can produce negative values, which could be viewed as do not trade. If this were made into a rule, then the system would stop trading. Perhaps it should! In the cases where the Kelly formula produces a negative value, a warning will be given and 10% of trading cash will be invested for the next order.

A maximum of 50% of Cash will be invested – any Kelly values higher than this will be reduced to 50%.

All these values can be changed in the PositionSize function. The logic for this Position Sizing Strategy:

PositionSize:

```###########################
# Kelly Formula - read notes for a description
# Play around with the vars below to change the settings of this MM
#
my \$mintrades   = 20; #Minimum number of trades before kelly is used
my \$mintradeper = 10; #Percent to invest if we havn't got enough trades
my \$maxper     = 50; #Maximum percent that we will invest
my \$minper     = 10; #Minimum Percent that we will invest
#
#
############################

my \$kelly; #The kelly percent

#not enough trades to use the kelly, set default %
}
else {
#enough trades, work out the win/loss ratio
if (\$winlossratio) {
#we have a winlossratio, work out the win probablility
\$kelly=100*(\$winprob-(1-\$winprob) /\$winlossratio);
}
else {
#we dont have a winlossratio - no losses! Set the % to the max
\$kelly = \$maxper;
}
#check for min and max
\$kelly=\$maxper if \$kelly>\$maxper;
if (\$kelly<1) {
Warning('Negitive Kelly - should you stop trading?');
\$kelly = \$minper;
}
}

###############
#work out the number of units and return the number

my \$num=int(((\$kelly/100)* LiquidatingValue )/ClosePrice);
Output "Kelly \$kelly number of units \$num ".\$num*ClosePrice;
return \$num;
```

```if (ShortPosition) {
#If we have a short position, close it.
}
else {
#we have no position, or we are long.
my \$num=PositionSize();
}```

```my \$stop=shift;
#Stop trading if we do not have a stop price
die 'No stop price for BuyStop' unless \$stop;

if (ShortPosition) {
#If we have a short position, close it.
}
else {
#we have no position, or we are long.
my \$num=PositionSize();
}
```

```my \$limit=shift;
#Stop trading if we do not have a limit price
die 'No limit price for BuyLimit' unless \$limit;

if (ShortPosition) {
#If we have a short position, close it.
}
else {
#we have no position, or we are long.
my \$num=PositionSize();
}
```

```my (\$stop,\$limit)=@_;
#Stop trading if we do not have a stop and limit price
die 'No limit price for BuyStopLimit' unless \$limit;
die 'No stop price for BuyStopLimit' unless \$stop;

if (ShortPosition) {
#If we have a short position, close it.
}
else {
#we have no position, or we are long.
my \$num=PositionSize();
}```

SellMarket event logic:

```if (LongPosition) {
#If we have a long position, close it.
PlaceSellOpen('KellyClosingLong');
}
else {
#If we have no position or we are short, place the order
my \$num=PositionSize();
PlaceSellOpen('KellyGoingShort',\$num) if \$num;
}
```

SellStop event logic:

```my \$stop=shift;
#Stop trading if we do not have a stop price
die 'No stop price for SellStop' unless \$stop;

if (LongPosition) {
#If we have a long position, close it.
PlaceSellStop('KellyClosingLong',0,\$stop);
}
else {
#If we have no position or we are short, place the order
my \$num=PositionSize();
PlaceSellStop('KellyGoingShort',\$num,\$stop) if \$num;
}```

SellLimit event logic:

```my \$limit=shift;
#Stop trading if we do not have a limit price
die 'No limit price for SellLimit' unless \$limit;

if (LongPosition) {
#If we have a long position, close it.
PlaceSellLimit('KellyClosingLong',0,\$limit);
}
else {
#If we have no position or we are short, place the order
my \$num=PositionSize();
PlaceSellLimit('KellyGoingShort',\$num,\$limit) if \$num;
}```

SellStopLimit event logic:

```my (\$stop,\$limit)=@_;
#Stop trading if we do not have a stop and limit price
die 'No limit price for SellStopLimit' unless \$limit;
die 'No stop price for SellStopLimit' unless \$stop;

if (LongPosition) {
#If we have a long position, close it.
PlaceSellStopLimit('KellyClosingLong',0,\$stop,\$limit);
}
else {
#If we have no position or we are short, place the order
my \$num=PositionSize();
PlaceSellStopLimit('KellyGoingShort',\$num,\$stop,\$limit) if \$num;
}```