Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to limit orders to available cash?

I wanted to investigate how order_target_percent works when I try to order more than the size of my portfolio. I found out it just orders more than the size of my portfolio! This is not what I expected. Is there a way to prevent that from happening? I am wondering how my algorithm will behave in live trading, because my broker will not allow me to order more than I have in cash!

I tried modifying the context.account object in initialize, hoping if I set the buying_power variable from infinity to my available_funds it will prevent this excessive ordering. But it seems to have no effect, I am thinking the account object is not for setting only getting. Can anyone comment on that?

Thanks for any help!!

5 responses

I am new to Q but you can check your current amount of cash using this:
https://www.quantopian.com/help#api-portfolio

Hope that's what you are looking for.

Kevin - Did you find a way to figure out how much cash is available (cash minus total cost of open buy orders)?

This might help?

Thank you very much for that Lucas. Looked it over quickly and I''m assuming this should help my situation. I'm still very new to Quantopian and python in general so that method is a little overwhelming but I guess it'll give me something to play with over the weekend ;)

In your use case you can simply do this:

#  
# create an instance at initialization time  
#  
def initialize(context):  
      context.exposure = ExposureMngr(target_leverage = 1.0,   # change here if you like to increase leverage  
                                      target_long_exposure_perc = 1.00,      # long only, 100% to long positions  
                                      target_short_exposure_perc = 0.00)    # 0% to short positions




#  
#  then use the class  
#  
def handle_data(context, data):  
     # update internal state (open orders and positions)  
    context.exposure.update(context, data)  
    # get availble cash  
    available_cash = context.exposure.get_available_cash()

Try it and see if it does what you need, enjoy.