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 = [
opt.MaxGrossLeverage(MaxGrossLeverage),
opt.DollarNeutral(MaxNetExposure),
]

order_ids = algo.order_optimal_portfolio(
objective=objective,
constraints=constraints,
universe=targets.index
)

# Reporting

log.info("Today's positions: %s" % (" ".join(str(x) for x in [s.symbol for s in targets.index])))
record(Positions=len(targets))

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