Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how to replace this deprecated code

A lot of the code I use in numerous algorithms call a "has_open_orders" function as follows:

if has_open_orders(data,context) == True:  
            log.info('Has open orders, not rebalancing.')

def has_open_orders(data,context):  
    # Only rebalance when we have zero pending orders.  
    has_orders = False  
    for stk in data:  
        orders = get_open_orders(stk)  
        if orders:  
            for oo in orders:  
                message = 'Open order for {amount} shares in {stock}'  
                message = message.format(amount=oo.amount, stock=stk)  
                                print message  
            has_orders = True  
    return has_orders  

How can I rewrite this function to accomplish the same in the newest version of quantopian?

Thanks
Serge

2 responses

Okay, I've got this terrible habit of answering my own posts, but what else am I gonna do? This code, though clunky, works without any deprecation warnings, and partly does the trick

def has_open_orders_new(data,context,stklist):  
    has_orders = False  
    # Get all open orders.  
    for stk in stklist:  
        open_orders = get_open_orders(stk)  
        if open_orders:  
            for oo in open_orders:  
                message = 'Open order for {amount} shares in {stock}'  
                message = message.format(amount=oo.amount, stock=stk)  
            has_orders = True

    return has_orders  

Problem is, an unfilled transaction in any of my targeted "stklist " will trigger a "true" here, even if it might be completely filled for the stock you are immediately concerned with. In the algorithm I am currently working on this is cool but in many algos I write I need a security-specific condition for open orders.

I'm very confused about how "handle_data" streams its data. Is it one security at a time? If so how can I determine which security it is streaming at any given moment? data.current( somebasketlist, field) does not allow you to specify "symbol" or "sid" as a field. So how do I know which security is the current one for example for my log file troubleshooting?

I would appreciate clarifications as to this question.

Can't you just use the built in function get_open_orders()?

https://www.quantopian.com/help#api-get-open-orders

If you want to know if only a specific security has open orders (for example when selling) use get_open_orders(security), if you want to see if any security has open orders (ie when buying) use get_open_orders()

for your other question:

handle_data is just a function that runs once per minute, every minute. You can do whatever you want in there, like executing other functions or place orders. The data parameter just contains all of the information for all of your stocks. It requires nothing from you in order to use it, many people just ignore it by using pass

The data parameter needs to be included in a function when you are getting information from stocks
The context parameter needs to be included in a function whenever you are calling/storing global variables from context

You can see in this random backtest that handle_data just calls the rebalance function once per minute until the rebalance flag is satisfied and when there aren't any orders already open. before_trading_start will automatically get called 15 min prior to the start of the trading in the morning, I set my rebalance flag true and call ordering_logic to pick stocks in the morning prior to open so that when the rebalance function is called by handle data at 9:31 am it will buy the correct ratio of stocks. Once all the stocks are bought/sold the rebalance flag is turned off until the next day. This prevents the handle data from slowing down the algorithm backtester by attempting to by/sell stocks every minute of every day. The rebalance function itself is build such that it will initiate sells first, then only proceed to buy orders once there is no more open orders (all cash cleared and ready to buy). An even more intelligent way to do it would have been to check each sid in the selling part if it's got open orders and not check it in the handle data. That way it would sell stocks even if another stocks sale is pending.