Back to Community
Zero "0" share trades impacting backtest results????

When building trading models, before taking the model into production because of substantial back testing confirmed by several months of forward testing, the final pre-production validation is done by converting the algorithm to native python, so the two are identical, and then running a closed backtest using something other than Zipline. Plus, it's a bit easier to do diagnostics outside of Zipline.

Recently, a model ready for production went through this final check and I noticed a handful of orders for zero shares were being generated. I thought this was due to something in the native Py version of the code, so I suppressed 0 share orders. Just to be safe, I added the same code to the algo on Quantopian and ran another test. I was shocked to see performance adversely affected. Not significantly over a short time period of 3 months, but enough so that over time, it affects compounded RoR.

I record gross and net exposure because this is a market/dollar neutral strategy designed to handle substantial capacity, with or without leverage (12-20x) with vola still within bounds. When I suppressed 0 share trades with the following code, it caused gross exposure to oscillate in large ranges:

def registerOrder(context, longPos, sym, shares):  
    if sym.symbol in context.orderSet:  
        #'Duplicate order for %s - ignored' % sym.symbol)  
        return False  
    elif shares == 0 and prevent_zero_trades:  
        #'Zero shares trade for %s - ignored' % sym.symbol)  
        return False  
        context.orderList.append(OrderEntry(longPos, sym, shares))  
        return True

def executeOrders(context):  
    for orderObj in context.orderList:  
        order(orderObj.symbol, orderObj.shares)

    context.orderList = []

By changing the 2nd 'return' value to True, it suppresses. False lets the trade go.

Any idea what I'm doing wrong? Are zero trades a bug in Quantopian? What is their purpose?

1 response

Hi Pej, I haven't been able to produce a backtest that exhibits this behaviour. Any chance you can share a backtest that I can dig into (either here or via a private support ticket)?


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.