Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Strange transactions - please help

Hi,

In this back test there are some strange transactions at odd hours that result in leverage shooting above 3. I have taken every care to keep leverage under 3 but I am unable to explain these transactions on DISC_B. Is it a bug in Quantopian? Please help.

4 responses

Perhaps DISC_B is thinly traded. A problem can happen if entering buy orders repeatedly where not filled when expected, and then a few days later they could all kick in. That seems to be what happened on 2013-02-14, nine of them. Similar with sell and you wind up shorting. (For any market noobs that means selling shares not owned).

Thanks Gary. That explains.

mmm-k.

And I guess automatic cancels of unfilled orders don't occur in backtesting, just paper and live/real environments.
Edit: In backtesting for the unfilled part of a partial fill a new order is generated for the remainder.

Here are some modifications that may fix the problem:

for i, sid in enumerate(stocks):  
        if get_open_orders(sid):  
            pass  
        else:  
            order_target_value(sid, -W[i])  

I also added code that should cancel all orders at the close, which is what Quantopian does for real-money trading (and presumably paper trading, as well) with Interactive Brokers.

schedule_function(func=cancel_everything,date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_close())

def cancel_everything(context,data):  
    """  
    Cancels all open orders for all sids.  
    """  
    all_open_orders = get_open_orders()  
    if all_open_orders:  
        for security, oo_for_sid in all_open_orders.iteritems():  
            for order_obj in oo_for_sid:  
                log.info("%s: Cancelling order for %s of %s created on %s" %  
                         (get_datetime(), order_obj.amount,  
                          security.symbol, order_obj.created))  
                cancel_order(order_obj)  

I lifted the function to cancel all orders from https://www.quantopian.com/posts/how-to-cancel-all-open-orders (see John Ricklefs posted Feb 19, 2014).

One thing to keep in mind is that there is the slippage defined by the slippage model, but there is also an inherent slippage, in that orders are filled only when an actual historical trade occurs (i.e. when there was a market for the security). Reportedly, this assumption falls apart for certain thinly traded ETFs, through a creation/redemption mechanism, which I don't pretend to understand fully.

Grant