Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Does order_value() include commission? Possible crash if wrongly used.

TLDR:

Don't do the below as it doesn't include the commission and the account will go short

order_value(sid(x), context.portfolio.cash)  

Complete story:

In the attached algo, I get a crash in Q when I increase the commission from

    set_commission(commission.PerShare(cost=0.005))  

to

    set_commission(commission.PerShare(cost=5))  

Since it crashes with cost = 5, I cannot share the algo, and so in the source code shared, the cost is 0.005

The error is
*

Something went wrong on our end and we've been notified about it.
Sorry for the inconvenience. OverflowError: Can't order more than
100000000000 shares File test_algorithm_sycheck.py:29, in handle_data
File algoproxy.py:1258, in order File /zipline/algorithm.py:455, in
order File /zipline/finance/blotter.py:126, in order File
test_algorithm_sycheck.py:27, in handle_data File algoproxy.py:1258,
in order File /zipline/algorithm.py:455, in order File
/zipline/finance/blotter.py:126, in order File
test_algorithm_sycheck.py:13, in handle_data File algoproxy.py:1258,
in order File /zipline/algorithm.py:455, in order File
/zipline/finance/blotter.py:126, in order

*

HOWEVER, I suspect that the issue happens when the returns go negative below a certain percentage (100%?) and then the algo goes haywire and tries to buy a huge amount of stocks.

This happens only when I increase the commission to a very high value and play with smaller priced stocks (ARNA in this case).

I SUSPECT that the order or order_value does not include the commission before calculating how many stocks can be bought, and because the commission is included after the calculation of number of stocks to trade, the account gets short and then the algo misbehaves. Looking at zipline, that seems to be the case.

So what is the recommended method for order_value?
Is it to calculate the commission before using order_value especially if we try to use the entire cash we have?

Ajay

1 response

Hi Ajay,

This error is linked to the problem you were receiving here: https://www.quantopian.com/posts/logs-not-getting-generated-or-getting-lost

In line 25 you write:

context.order_id = order_value(context.stock, context.portfolio.cash)  

I would recommend to use the order_target_percent() method because it scales with the cash available in your portfolio. When you place an order_value, it gets filled and then the commissions are applied. For more information you can see the Zipline code (line 584): https://github.com/quantopian/zipline/blob/4860a966b3a3102fa80d43f393155e53015cc349/zipline/algorithm.py

The error message you posted happens when you write something like:
order(context.stock, context.portfolio.cash)

You try to place an order for too many shares (1 billion in your case) and are prevented by the backtester. Take a look at the order_target_percent method, it should give you the behavior you're expecting.

Alisa

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.