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.
More information : http://www.racing.saratoga.ny.us/kelly.pdf
In the extern parameters, add :
Code:
extern double initialrisk=0.1;
extern int mintradeskelly=10;
extern int maxtradeskelly=50;
extern double stoploss=50;
After the extern parameters add :
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);
if(count==maxtradeskelly)break;
if(OrderMagicNumber()==magic && count<maxtradeskelly){
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);
if(count>=mintradeskelly)return(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)