Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Confused over order_target_percent.
API

Hey all,

Hope someone can help me with this. From my understanding, order_target_percent adjust a position to a target percent of the current portfolio value. If such position does not exists, we enter the position instead. Easy!

So what is the different between the followings:

def handle_data(context, data):
if ( context.n == 0 ):
context.n = 1
order_target_percent( context.asset, 1.0 )

and:

def handle_data(context, data):
order_target_percent( context.asset, 1.0 )

Both yield slightly different result during backtest, like return of -78.7% for the first one vs -78.29%. I would have thought that both version should do the same thing.

What get worst is comparing the short version :

def handle_data(context, data):
if ( context.n == 0 ):
context.n = 1
order_target_percent( context.asset, -1.0 )

to this:

def handle_data(context, data):
order_target_percent( context.asset, -1.0 )

From backtest, the first one yield a return of 78.02% but the second one yield as much as 215.85%!! See backtest!

5 responses

When does n get incremented? Could they be getting incremented at different times in the backtest between the two variations? The most likely culprit is you're running into a long queue of open orders. The functions order_target_* don't check for open orders and that management needs to be done with a guard in your code: https://www.quantopian.com/posts/order-target-percent-ordering-too-much

I suspect if you turn off the slippage model and force all the shares to fill instantly, it will confirm the theory and returns will be identical.

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.

Hi Alisa,

n is just there to make sure that order_target_percent is called once.

I have remove the slippage and commision using the following code:
set_slippage(slippage.FixedSlippage(spread = 0.00))
set_commission(commission.PerTrade(cost=0.00))

I have make sure to check for any open orders before I proceed in entering more orders. Looking at the logs, there are no open orders.

Please have a look at my back test attached here. This one make a call to order_target_percentage daily.

This one just enter the order once at the first time. Please check out the back test. Noticed the different in returns. -78.36 vs -78.22.

The difference are small, but it get worst when I shorted them instead of long (pls ffind the backtest in my next post)

*Note: I realized you cannot see the returns different here in my post due to rounding up, you will need to clone and rerun the algo.

This is short only once, with a returns of 78.36 (opposite of the long version as expected)

This is the one we short daily. 235.74%!!

If I understand correctly both version should behave more or less the same. Is there something that I'm missing here?

I expect both version, the one that enter an order once during the first call back as 100% and readjusting the order daily as 100%, yield the same amount of returns.