1. ## Kelly Money Management

b = win/loss ratio,
p = % of winning trades,
q = % of losing trades (=1-p),
K = percentage of the balance to risk per trade.

Kelly formula : K = (b * p - q) / b

The kelly money management is supposed to help to recover from previous losses.

In the extern parameters, add :

Code:
```extern double initialrisk=0.1;

extern double stoploss=50;```
After the extern parameters add :

Code:
`int lotsize;`
Add in the init function :

Code:
```if(MarketInfo(Symbol(),MODE_MINLOT)>=1){lotsize=100000;}
if(MarketInfo(Symbol(),MODE_MINLOT)<1){lotsize=10000;}
if(MarketInfo(Symbol(),MODE_MINLOT)<0.1){lotsize=1000;}```
Kelly function :

Code:
```double kelly(){
int count=0;
double countprofit=0;
double countloss=0;
int countpve=0;
int countnve=0;

double b=0;
double p=0;
double q=0;

double k=0;

if(OrdersHistoryTotal()>0){
for(i=OrdersHistoryTotal()-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
count++;
if(OrderProfit()<0){countnve++;countloss+=OrderProfit();}
if(OrderProfit()>=0){countpve++;countprofit+=OrderProfit();}
}
}
if(countloss!=0 && count!=0 && countpve!=0 && countprofit!=0){
b=countprofit/countloss;
p=countpve/count;
q=1-p;
k=(b*p-q)/b;
//Print(" "+k);
}
}

return(initialrisk);
}```
Standard MM function :

Code:
```double mm(double risk,double stoploss){
double lot;
if(stoploss>0)lot=AccountBalance()*(risk/100)/(stoploss*pt/MarketInfo(Symbol(),MODE_TICKSIZE)*MarketInfo(Symbol(),MODE_TICKVALUE));
else lot=nd((AccountBalance()/lotsize)*0.01*risk,2);
if(lot<MarketInfo(Symbol(),MODE_MINLOT))lot=MarketInfo(Symbol(),MODE_MINLOT);
if(lot>MarketInfo(Symbol(),MODE_MAXLOT))lot=MarketInfo(Symbol(),MODE_MAXLOT);
return(lot);
}```
Replace lots in the Ordersend functions with :

Code:
`mm(kelly(),stoploss)`

2. this would be nice added to wallst forex robot or something similar? anyone fancy a programming task?

There might be an issue when you have a loss as your first trade. Cant divide by 0.

also the more trades the better it works. If you get a loss in the first few trades the lots can increase greatly.

Maybe just make the first few trades with a fixed lot size to overcome this?

4. nice job arbus... you're an asset to this forum pal!

5. ## Missing pt and function nd

Hi Funyoo

There is pt not defined and function nd is missing in this post. Could you post it?

Originally Posted by funyoo
b = win/loss ratio,
p = % of winning trades,
q = % of losing trades (=1-p),
K = percentage of the balance to risk per trade.

Kelly formula : K = (b * p - q) / b

The kelly money management is supposed to help to recover from previous losses.

In the extern parameters, add :

Code:
```extern double initialrisk=0.1;

extern double stoploss=50;```
After the extern parameters add :

Code:
`int lotsize;`
Add in the init function :

Code:
```if(MarketInfo(Symbol(),MODE_MINLOT)>=1){lotsize=100000;}
if(MarketInfo(Symbol(),MODE_MINLOT)<1){lotsize=10000;}
if(MarketInfo(Symbol(),MODE_MINLOT)<0.1){lotsize=1000;}```
Kelly function :

Code:
```double kelly(){
int count=0;
double countprofit=0;
double countloss=0;
int countpve=0;
int countnve=0;

double b=0;
double p=0;
double q=0;

double k=0;

if(OrdersHistoryTotal()>0){
for(i=OrdersHistoryTotal()-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
count++;
if(OrderProfit()<0){countnve++;countloss+=OrderProfit();}
if(OrderProfit()>=0){countpve++;countprofit+=OrderProfit();}
}
}
if(countloss!=0 && count!=0 && countpve!=0 && countprofit!=0){
b=countprofit/countloss;
p=countpve/count;
q=1-p;
k=(b*p-q)/b;
//Print(" "+k);
}
}

return(initialrisk);
}```
Standard MM function :

Code:
```double mm(double risk,double stoploss){
double lot;
if(stoploss>0)lot=AccountBalance()*(risk/100)/(stoploss*pt/MarketInfo(Symbol(),MODE_TICKSIZE)*MarketInfo(Symbol(),MODE_TICKVALUE));
else lot=nd((AccountBalance()/lotsize)*0.01*risk,2);
if(lot<MarketInfo(Symbol(),MODE_MINLOT))lot=MarketInfo(Symbol(),MODE_MINLOT);
if(lot>MarketInfo(Symbol(),MODE_MAXLOT))lot=MarketInfo(Symbol(),MODE_MAXLOT);
return(lot);
}```
Replace lots in the Ordersend functions with :

Code:
`mm(kelly(),stoploss)`

if(KellyMinusRisk)
{
k=(((100*(p-q))/count)+100)*0.5*0.01*Klots;
}
else
{
k=((((100*(q-p))/count)+100)*0.01*Klots)+Klots;
}
Klots = lots (weather using money management or not)

KellyMinusRisk - if true- lots are deducted when a loss occurs.
if false lots are added when a loss occurs.

The "kelly" lots are calculated between 0 and 100% of the lots being used.
So with (KellyMinusRisk=true)
100% wins the Kelly lots = Lots.
with every loss the lots drop a percentage of the lots.
100% losses the kelly lots=0.01 lots.

(KellyMinusRisk=false) the opposite is true. the more the losses the greater the lots (Lot size + up to 100% of the lot size)

I have been testing it against the regular MM and found with KellyMinusRisk = false it is giving better results.

eg
Regular MM
30.6%DD
PF1.54
Profit=69003

29.51DD
PF1.62
Profit=87150

7. someone knows what to put here ?

else lot=nd((AccountBalance()/lotsize)*0.01*risk,2);