Back to Community
Chart of daily portfolio turnover

Sometimes you can improve a strategy's performance by simply reducing the portfolio turnover. For example, by entering the top 10% of stocks by alpha signal, but then only exiting when they fall out of the top 20%. You need to be able to monitor your turnover, as the trading costs will be directly related to that. You can do this in Pyfolio, but I like to see the feedback within the backtester.

Here's some code that takes the output of Optimizer, and charts the portfolio's daily turnover. For my shorter term strategies, I have daily turnover in the 20-40% range.

    objective = opt.TargetPortfolioWeights(targets.weight)

    constraints = [  

    order_ids = algo.order_optimal_portfolio(  

    # Reporting"Today's positions: %s" % (" ".join(str(x) for x in [s.symbol for s in targets.index])))  

    orders  = pd.Series({get_order(i).sid:get_order(i).amount for i in order_ids.values})  
    prices = data.current(orders.index,'price')  
    buy_order_value  = sum([abs(amount*prices[s]) for s,amount in orders.iteritems() if amount>0])  
    sell_order_value = sum([abs(amount*prices[s]) for s,amount in orders.iteritems() if amount<0])  
    order_value = min(buy_order_value, sell_order_value)  
    turnover = order_value / context.portfolio.portfolio_value  * 100