Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
question about order_target() function, why it's not ordering the specified shares of the stock?

As a newbie in quantopian, I wrote a simple test in handle_data() function,
def handle_data(context,data):
print("********the timestamp is",get_datetime(),"*******")
print("the cash before ordering is",context.portfolio.cash)
order_target(sid(270),100)
for item in context.portfolio.positions:
print("item is ", item,)
for position in context.portfolio.positions.itervalues():
print("position amount is",position.amount)
return

As a position.amount result for the stock sid(270), it's varying at each time instead of being a constant 100, can anyone help me to understand why is that? Thanks!

As a reference, below is the output:
2011-01-04 09:31 PRINT ('********the timestamp is', Timestamp('2011-01-04 14:31:00+0000', tz='UTC'), '****')
2011-01-04 09:31 PRINT ('the cash before ordering is', 100000.0)
2011-01-04 09:32 PRINT ('
*****the timestamp is', Timestamp('2011-01-04 14:32:00+0000', tz='UTC'), '****')
2011-01-04 09:32 PRINT ('the cash before ordering is', 100000.0)
2011-01-04 09:33 PRINT ('
*****the timestamp is', Timestamp('2011-01-04 14:33:00+0000', tz='UTC'), '*****')
2011-01-04 09:33 PRINT ('the cash before ordering is', 99988.4375376)
2011-01-04 09:33 PRINT ('item is ', Equity(270 [AKRX]))
2011-01-04 09:33 PRINT ('position amount is', **2
)
2011-01-04 09:34 PRINT ('********the timestamp is', Timestamp('2011-01-04 14:34:00+0000', tz='UTC'), '*****')
2011-01-04 09:34 PRINT ('the cash before ordering is', 99675.70408650559)
2011-01-04 09:34 PRINT ('item is ', Equity(270 [AKRX]))
2011-01-04 09:34 PRINT ('position amount is', **56
)
2011-01-04 09:35 PRINT ('********the timestamp is', Timestamp('2011-01-04 14:35:00+0000', tz='UTC'), '*****')
2011-01-04 09:35 PRINT ('the cash before ordering is', 99664.12162330559)
2011-01-04 09:35 PRINT ('item is ', Equity(270 [AKRX]))
2011-01-04 09:35 PRINT ('position amount is', **58
)
.............

2 responses

Hi Karl,

Thank you for the explanation. In order to understand how this order method works, I wrote a small piece of code (context.flag is initialized to be 0),

        print("---the timestamp is",get_datetime(),"---")  
        if context.flag==0:    # keep ordering if my share in hand is not reaching 5000  
            order_target(sid(24),10000)  
            print("bought!")  
        else:       #stop ordering if reaching 5000 shares  
            print("stop")

        print("position amount is",context.portfolio.positions[sid(24)].amount)  
        if context.portfolio.positions[sid(24)].amount>5000:  
            context.flag=1  
        elif context.portfolio.positions[sid(24)].amount<0:  
            context.flag=0  
    return  

I would image the the portfolio position amount would stop right after reaching 5000, however, I end up getting 27340 shares of the stock as below.
May I know why is this happening? :)

2011-01-04 09:31 PRINT ('---the timestamp is', Timestamp('2011-01-04 14:31:00+0000', tz='UTC'), '---')
2011-01-04 09:31 PRINT bought!
2011-01-04 09:31 PRINT ('position amount is', 0)
2011-01-04 09:32 PRINT ('---the timestamp is', Timestamp('2011-01-04 14:32:00+0000', tz='UTC'), '---')
2011-01-04 09:32 PRINT bought!
2011-01-04 09:32 PRINT ('position amount is', 2110)
2011-01-04 09:33 PRINT ('---the timestamp is', Timestamp('2011-01-04 14:33:00+0000', tz='UTC'), '---')
2011-01-04 09:33 PRINT bought!
2011-01-04 09:33 PRINT ('position amount is', 4307)
2011-01-04 09:34 PRINT ('---the timestamp is', Timestamp('2011-01-04 14:34:00+0000', tz='UTC'), '---')
2011-01-04 09:34 PRINT bought!
2011-01-04 09:34 PRINT ('position amount is', 6243)
2011-01-04 09:35 PRINT ('---the timestamp is', Timestamp('2011-01-04 14:35:00+0000', tz='UTC'), '---')
2011-01-04 09:35 PRINT stop
2011-01-04 09:35 PRINT ('position amount is', 7620)
2011-01-04 09:36 PRINT ('---the timestamp is', Timestamp('2011-01-04 14:36:00+0000', tz='UTC'), '---')
2011-01-04 09:36 PRINT stop
2011-01-04 09:36 PRINT ('position amount is', 9670)
... 2011-01-04 09:48 PRINT ('position amount is', 27340)
2011-01-04 09:49 PRINT ('---the timestamp is', Timestamp('2011-01-04 14:49:00+0000', tz='UTC'), '---')
2011-01-04 09:49 PRINT stop
2011-01-04 09:49 PRINT ('position amount is', 27340)
2011-01-04 09:50 PRINT ('---the timestamp is', Timestamp('2011-01-04 14:50:00+0000', tz='UTC'), '---')
2011-01-04 09:50 PRINT stop
2011-01-04 09:50 PRINT ('position amount is', 27340)

order_target doesn't evaluate open orders. So every time you print "bought!" you just placed an additional order. The traditional way to handle this is to check for open orders, and exit your ordering logic if you have open orders.

The better way to handle this is to use the Optimize API. The attached backtest uses the api, and it slims the ordering logic down to 3 lines. It's worth noting that the stock you're looking at there is a takeover target and will have some weird price and volume behavior - ordinarily we'd exclude stocks like that from our evaluations.

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.