Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Bug in order_target_percent

I have recently been tearing my hair out trying to figure why I get different results for the same algorithm in Quantopian and Zipline. After many hours, I discovered this bug in Quantopian: if you order a target 100% of a stock and some days later again order a target of 100% (which shouldn't change anything, as the stock is already at 100%), the portfolio CASH changes.

If you try the same in Zipline, you get a warning that you are trying to place an order for amount = 0, which is fine, and the portfolio CASH doesn't change, as expected.

Obviously, over an extended backtest period, the difference mounts up - in my case the difference ended up being > 100%!

3 responses

Hello Dave,

I've attached a backtest with these settings to rule out commission and slippage effects:

set_commission(commission.PerShare(cost=0))  
set_slippage(slippage.FixedSlippage(spread=0.00))  

However, keep in mind that effectively there is slippage built into the backtester, since the price at order submission differs from the fill/execution price by one bar period (a whole day when running on daily bars).

It bears more investigation, but I figure that SPY must be paying out a dividend, and then order_target_percent periodically re-invests the dividend, driving cash back to zero.

Does this shed any light on what you are trying to sort out?

Grant

Thanks for the quick response Grant. I had assumed that 'price' was adjusted for splits and dividends.

No, unfortunately haven't yet solved the Quantopian/Zipline puzzle. But I'll keep you posted if I discover anything...

How are you handling dividends in zipline? Per the Quantopian help, when running the online backtester:

Dividend events modify the security price and the portfolio's cash balance

Are you doing the same in zipline?

Another comment is that unless you get cash = 0 exactly in the online backtester, I'd expect some level of adjustment by order_target_percent due to the inherent slippage mentioned above (and perhaps rounding to whole numbers of shares).