Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Is this a bug in get_order(order_ID)?

I use the following code to track if my order is filled and it returns that 0 shares is filled.

    if order_a_data is not None:  
        log.info('SPY order {amount} has {filled} shares filled'.format(amount=order_a_data.amount,filled=order_a_data.filled))  
    else:  
        log.info('SPY order is empty')  
    if order_b_data is not None:  
        log.info('Bull order {amount} has {filled} shares filled'.format(amount=order_b_data.amount,filled=order_b_data.filled))  
    else:  
        log.info('Bull order is empty')  
    if order_c_data is not None:  
        log.info('Bear order {amount} has {filled} shares filled'.format(amount=order_c_data.amount,filled=order_c_data.filled))  
    else:  
        log.info('Bear order is empty')  

2009-08-03 14:15 allocate:58 INFO SPY order -1 has 0 shares filled
2009-08-03 14:15 allocate:62 INFO Bull order 1 has 0 shares filled
2009-08-03 14:15 allocate:66 INFO Bear order -3 has 0 shares filled

However, when I print portfolio.cash, portfolio.long_exposure and portfolio.short_exposure the next day, I find that they are calculated based on the order amount that I put.

    log.info('Current portfolio cash:{cash}'.format(cash=context.portfolio.cash))  
    log.info('Current portfolio value:{value}'.format(value=context.portfolio.portfolio_value))  
    log.info('Capital used:{capital}'.format(capital=context.portfolio.capital_used))  
    context.long_exposure=context.portfolio.positions[context.shy].amount*data.current(context.shy,'price')  
    context.bull_exposure=context.portfolio.positions[context.bull].amount*data.current(context.bull,'price')  
    context.bear_exposure=context.portfolio.positions[context.bear].amount*data.current(context.bear,'price')  
    log.info('Current long exposure:{long_exposure}'.format(long_exposure=context.long_exposure))  
    log.info('Current short bull exposure:{bull_exposure}'.format(bull_exposure=context.bull_exposure))  
    log.info('Current short bear exposure:{bear_exposure}'.format(bear_exposure=context.bear_exposure))  

2009-08-03 14:15 allocate:70 INFO Current portfolio cash:11545.878
2009-08-03 14:15 allocate:71 INFO Current portfolio value:11787.068
2009-08-03 14:15 allocate:72 INFO Capital used:1545.878
2009-08-03 14:15 allocate:77 INFO Current long exposure:10133.33
2009-08-03 14:15 allocate:78 INFO Current short bull exposure:-3441.93
2009-08-03 14:15 allocate:79 INFO Current short bear exposure:-6450.21

I'm wondering if my order is not filled at all, how come I have the correct portfolio.long_exposure and portfolio.short_exposure number.

Does anyone know how the order is handled and how the portfolio related variables are calculated?

Thanks.

2 responses

Here is the full backtest code. Any feedback is appreciated.

Lu, the problem was that you were re-assigning your order variable before checking if the last order was filled.

I re-arranged your code so that it prints the fills of the previous orders, then makes new orders.

Disclaimer

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.