Results 1 to 7 of 7
 1 Attachment(s)    

Thread: Kelly Money Management

  1. #1
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default 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.

    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 :

    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);
             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)

  2. #2

    Default

    this would be nice added to wallst forex robot or something similar? anyone fancy a programming task?
    Last edited by neilrickaby; 01-24-2012 at 14:33.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    259

    Default

    Already done.

    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?
    Attached Images Attached Images Kelly Money Management-ws-scalper-kelly-gif 

  4. #4

    Default

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

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    1

    Default Missing pt and function nd

    Hi Funyoo

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



    Quote Originally Posted by funyoo View Post
    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 :

    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);
             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)

  6. #6
    Member
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    259

    Default

    Here's a little upgrade I made to the formula

    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

    KellyMinusRisk = false : maxtradeskelly=15
    29.51DD
    PF1.62
    Profit=87150

  7. #7
    Junior Member
    Join Date
    Jan 2009
    Posts
    18

    Default

    someone knows what to put here ?

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

Similar Threads

  1. Five Top Money Management Tips
    By painofhell in forum Trading discussion
    Replies: 4
    Last Post: 10-09-2015, 02:27
  2. Money management techniques
    By painofhell in forum Trading discussion
    Replies: 18
    Last Post: 08-22-2015, 14:56
  3. New money management technique-kelly criterion
    By pakallis in forum Ideas for expert advisors
    Replies: 3
    Last Post: 12-23-2009, 16:35

Tags for this Thread

100, add, forex, forex robot, information, money, profit, programming, robot, stoploss

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •