From Wiki:
    https://en.wikipedia.org/wiki/Kelly_criterion
Kelly rebalance will enhance the Sharpe ratio and reduce the risk.
def kelly_strategy():
        # Import history, we have to adjust for a bug that causes extra columns
        # to show up in history sometimes.
       # need to get price history
        prices = history(200, '1d', 'price')
      
        # remove empty or None fields, compute the percentage of change
        R = prices.pct_change().dropna()
      
        # Select securities by assuming all returns are statistically independent
        # and calculate their Kelly leverage.
        kelly = R.mean() / R.var()
      
        # Drop any Nan values and sort in ascending order
        kelly = kelly.dropna()
        kelly.sort()
     
        # just select port_size number of stocks
        picks = kelly.tail(context.port_size)
      
        # Limit short exposure if the Kelly score is negative
        kelly = picks.apply(lambda x: max(x, context.short_pct * x))
      
        # Adjust result to keep the account leverage constant
        kelly *= (context.leverage / kelly.abs().sum())
        # Place orders and sell off any securities that were dropped.
        for stock in data:
            if stock in kelly.index:               
                 # adjust the percentage of stocks, either buy or sell
                order_target_percent(stock, kelly[stock])
            else:
                # sell all of them
                order_target(stock, 0)
      
 
No comments:
Post a Comment